Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8878143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:38:37+00:00 2026-06-14T19:38:37+00:00

I’m having trouble with setting a priori contrasts and would like to ask for

  • 0

I’m having trouble with setting a priori contrasts and would like to ask for some help. The following code should give two orthogonal contrasts to the factor level “d”.

Response <- c(1,3,2,2,2,2,2,2,4,6,5,5,5,5,5,5,4,6,5,5,5,5,5,5)
A <- factor(c(rep("c",8),rep("d",8),rep("h",8)))
contrasts(A) <- cbind("d vs h"=c(0,1,-1),"d vs c"=c(-1,1,0))
summary.lm(aov(Response~A))

What I get is:

Call:
aov(formula = Response ~ A)

Residuals:
   Min         1Q     Median         3Q        Max 
-1.000e+00 -3.136e-16 -8.281e-18 -8.281e-18  1.000e+00 

Coefficients:
        Estimate Std. Error t value Pr(>|t|)    
(Intercept)   4.0000     0.1091  36.661  < 2e-16 ***
Ad vs h      -1.0000     0.1543  -6.481 2.02e-06 ***
Ad vs c       2.0000     0.1543  12.961 1.74e-11 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 0.5345 on 21 degrees of freedom
Multiple R-squared: 0.8889,     Adjusted R-squared: 0.8783 
F-statistic:    84 on 2 and 21 DF,  p-value: 9.56e-11

But I expect the Estimate of (Intercept) to be 5.00, as it should be equal to the level d, right? Also the other estimates look strange…

I know I can get the correct values easier using relevel(A, ref=”d”) (where they are displayed correctly), but I am interested in learning the correct formulation to test own hypotheses. If I run a similar example with the folowing code (from a website), it works as expected:

irrigation<-factor(c(rep("Control",10),rep("Irrigated 10mm",10),rep("Irrigated20mm",10))) 
biomass<-1:30 
contrastmatrix<-cbind("10 vs 20"=c(0,1,-1),"c vs 10"=c(-1,1,0))
contrasts(irrigation)<-contrastmatrix 
summary.lm(aov(biomass~irrigation))


Call:
aov(formula = biomass ~ irrigation)

Residuals:
       Min         1Q     Median         3Q        Max 
-4.500e+00 -2.500e+00  3.608e-16  2.500e+00  4.500e+00 

Coefficients:
                   Estimate Std. Error t value Pr(>|t|)    
(Intercept)         15.5000     0.5528   28.04  < 2e-16 ***
irrigation10 vs 20 -10.0000     0.7817  -12.79 5.67e-13 ***
irrigationc vs 10   10.0000     0.7817   12.79 5.67e-13 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 3.028 on 27 degrees of freedom
Multiple R-squared: 0.8899,     Adjusted R-squared: 0.8817 
F-statistic: 109.1 on 2 and 27 DF,  p-value: 1.162e-13

I would really appreciate some explanation for this.

Thanks, Jeremias

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:38:38+00:00Added an answer on June 14, 2026 at 7:38 pm

    I think the problem is in the understanding of contrasts (You may ?contrasts for detail). Let me explain in detail:

    If you use the default way for factor A,

    A <- factor(c(rep("c",8),rep("d",8),rep("h",8)))
    > contrasts(A)
      d h
    c 0 0
    d 1 0
    h 0 1
    

    thus the model lm gives you are

    Mean(Response) = Intercept + beta_1 * I(d = 1) + beta_2 * I(h = 1)
    
    summary.lm(aov(Response~A))
    Coefficients:
        Estimate Std. Error t value Pr(>|t|)    
    (Intercept)    2.000      0.189    10.6  7.1e-10 ***
    Ad             3.000      0.267    11.2  2.5e-10 ***
    Ah             3.000      0.267    11.2  2.5e-10 ***
    

    So for group c, the mean is just intercept 2, for group d , the mean is 2 + 3 = 5, same for group h.

    What if you use your own contrast:

    contrasts(A) <- cbind("d vs h"=c(0,1,-1),"d vs c"=c(-1,1,0))
    
    A
    [1] c c c c c c c c d d d d d d d d h h h h h h h h
    attr(,"contrasts")
        d vs h d vs c
    c      0     -1
    d      1      1
    h     -1      0
    

    The model you fit turns out to be

    Mean(Response) = Intercept + beta_1 * (I(d = 1) - I(h = 1)) + beta_2 * (I(d = 1) - I(c = 1))
         = Intercept + (beta_1 + beta_2) * I(d = 1) - beta_2 * I(c = 1) - beta_1 * I(h = 1)
    
    Coefficients:
    Estimate Std. Error t value Pr(>|t|)    
    (Intercept)    4.000      0.109   36.66  < 2e-16 ***
    Ad vs h       -1.000      0.154   -6.48  2.0e-06 ***
    Ad vs c        2.000      0.154   12.96  1.7e-11 ***
    

    So for group c, the mean is 4 - 2 = 2, for group d, the mean is 4 - 1 + 2 = 5, for group h, the mean is 4 - (-1) = 5.

    ==========================

    Update:

    The easiest way to do your contrast is to set the base (reference) level to be d.

    contrasts(A) <- contr.treatment(3, base = 2)
    Coefficients:
                 Estimate Std. Error t value Pr(>|t|)    
    (Intercept)  5.00e+00   1.89e-01    26.5  < 2e-16 ***
    A1          -3.00e+00   2.67e-01   -11.2  2.5e-10 ***
    A3          -4.86e-17   2.67e-01     0.0        1    
    

    If you want to use your contrast:

    Response <- c(1,3,2,2,2,2,2,2,4,6,5,5,5,5,5,5,4,6,5,5,5,5,5,5)
    A <- factor(c(rep("c",8),rep("d",8),rep("h",8)))
    mat<- cbind(rep(1/3, 3), "d vs h"=c(0,1,-1),"d vs c"=c(-1,1,0))
    mymat <- solve(t(mat))
    my.contrast <- mymat[,2:3]
    contrasts(A) <- my.contrast
    summary.lm(aov(Response~A))
    
    Coefficients:
    Estimate Std. Error t value Pr(>|t|)    
    (Intercept) 4.00e+00   1.09e-01    36.7  < 2e-16 ***
    Ad vs h     7.69e-16   2.67e-01     0.0        1    
    Ad vs c     3.00e+00   2.67e-01    11.2  2.5e-10 ***
    

    Reference: http://www.ats.ucla.edu/stat/r/library/contrast_coding.htm

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to run a str_replace or preg_replace which looks for certain words
I would like to count the length of a string with PHP. The string
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.