[R] Contrasts

Spencer Graves spencer.graves at pdf.com
Mon May 31 19:49:13 CEST 2004


      I hope you get a reply from someone who knows more about this than 
I.  However, in the spirit that a quick hack is sometimes better than an 
elegant answer later, consider the following: 

     DF <- data.frame(a=rep(letters[1:4], 2), y=1:8)
 >       DF$b <- ((DF$a %in% letters[1:2])-(DF$a%in% letters[3:4]))
 >       fit <- lm(y~b+a, DF, singular.ok=T)
 >       summary(fit)

Call:
lm(formula = y ~ b + a, data = DF, singular.ok = T)

Residuals:
 1  2  3  4  5  6  7  8
-2 -2 -2 -2  2  2  2  2

Coefficients: (1 not defined because of singularities)
            Estimate Std. Error t value Pr(>|t|) 
(Intercept)    4.500      1.414   3.182   0.0335 *
b             -1.500      1.414  -1.061   0.3486 
ab             1.000      2.828   0.354   0.7415 
ac            -1.000      2.828  -0.354   0.7415 
ad                NA         NA      NA       NA 
---
Signif. codes:  0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1

Residual standard error: 2.828 on 4 degrees of freedom
Multiple R-Squared: 0.2381,     Adjusted R-squared: -0.3333
F-statistic: 0.4167 on 3 and 4 DF,  p-value: 0.751

      This will give you what you want ONLY if you have the same number 
of observations in the two subsets to be compared.  If not, then you can 
redefine DF$b to produce what you want. 

     hope this helps.  spencer graves

Kimberly Ann Fernandes wrote:

>Hello,
>
>I am trying to figure out how to conduct a t-test on a specific contrast
>for my data.  I have four factors in my data and would like to conduct a
>t-test on the average of the data from the first two factors against the
>average of the data on the second two factor (i.e. is the average of the
>first two different from the average of the second two).  Is there a
>quick way to do this?  I found the contrast function, but wasn't sure
>how to apply it.
>
>Thank you,
>Kim
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>  
>




More information about the R-help mailing list