R: Special contrasts within an interaction effect

给你一囗甜甜゛ 提交于 2021-02-07 20:49:14

问题


Within the context of a 2-way ANOVA, I would like to enter contrast levels for one factor (10, 20, 30 degrees) within levels of another factor (SpeciesA, SpeciesB). This assumes the interaction effect is significant so the contrasts cannot simply be done on the temperature main effect.

I have attempted to do this within EZanova, multcomp and phia, but have been unable to figure out a syntax that works. Have I missed a package that allows this?

Contrast and coefficients

Contrast1: Does 10 degrees differ from 20 and 30 for Species A? Species:Temp (2,-1,-1,0,0,0) Contrast2: Does 10 degrees differ from 20 and 30 for Species B? Species:Temp (0,0,0,2,-1,-1) Contrast3: Does 10 degrees for Species A differ from 20 and 30 degrees for Species B? Species:Temp (2,0,0,0,-1,-1)

species<-rep(c("speciesA","speciesB"),each=12)
temp<-rep(rep(c("10","20","30"),each=4),2)
y<-rnorm(24,5,3)
(result<-anova(lm(y~species*temp)))

回答1:


It's part of base-R. See the ?contrasts page. I will illustrate for the first requested contrast; the other two should be very straightforward. I'm assuming you want these contrast p-values calculated separately, so I use the how.many argument to limit the contrast construction to a single comparison. Otherwise you would have gotten 5 contrasts, since by default the contrasts function tries to build a spanning set of orthogonal contrasts:

set.seed(123)
species <- rep(c("speciesA","speciesB"),each=12)
temp <- rep(rep(c("10","20","30"),each=4),2)
y<-rnorm(24,5,3)

intvar <- interaction(species, temp)  # create an interaction variable
?contrasts

Need to know which levels are which to properly order the contrast values:

> levels(intvar)
[1] "speciesA.10" "speciesB.10" "speciesA.20" "speciesB.20" "speciesA.30"
[6] "speciesB.30"

So this should build a contrast for 10 versus 20 or 30 angles among the A species:

 contrasts(intvar, how.many=1) <- c(2,0,-1,0,-1,0)
 anova(lm(y~intvar) )
#------------
Analysis of Variance Table

Response: y
          Df  Sum Sq Mean Sq F value Pr(>F)
intvar     1   0.013  0.0129  0.0015 0.9695
Residuals 22 190.306  8.6503      

You seem to be at a fairly early stage in learning R so I would recommend that you learn to build dataframes to pass as data=-arguments to regression functions, rather than working on "loose" objects in your work-space and please do not pick up the nasty habit of using attach.



来源:https://stackoverflow.com/questions/32533236/r-special-contrasts-within-an-interaction-effect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!