Skip to content
Prev 2499 / 20628 Next

testing simple main effects by contrasts

Thank you so much. I highly appreciate your help.

However, when I extend my example to the case of a design with 3 drug
levels, the results that I get with the contrast-package and the results
that I get with your approach seem to differ. I don't know why this is the
case. Could you please have a look at the following example and tell me what
I'm doing wrong.

#prepare an example data.frame
set.seed(5)
x <- expand.grid(Block= paste('B', 1:3, sep=''),
Drug = c('Pla', 'Drug1','Drug2'), Subj=1:24)
df <- data.frame(x, Group = rep(c('A','B'), each=108),
value=rnorm(216, sd=10))
m <- model.matrix(lm(value~Group*Drug+Block,df))
df$value <- df$value+m[,2]*7+ m[,3]*2 + m[,4]*5+
 m[,5]*8 + m[,6]*-2+m[,7]*3+m[,8]*-5 
df <- df[-as.numeric(sample(rownames(df),20)),]

mod <- lme(value~Group*Drug+Block, random=~1|Subj,data=df)

library(contrast)
#Effect of Drug1 | Group A
contrast(mod,
list(Block=c('B1','B2','B3'), Group = 'A', Drug = 'Pla'),
list(Block=c('B1','B2','B3'), Group = 'A', Drug = 'Drug1'),
type='average')

#Effect of Drug2 | Group A
contrast(mod,
list(Block=c('B1','B2','B3'), Group = 'A', Drug = 'Pla'),
list(Block=c('B1','B2','B3'), Group = 'A', Drug = 'Drug2'),
type='average')

#Effect of Drug1 | Group B
contrast(mod,
list(Block=c('B1','B2','B3'), Group = 'B', Drug = 'Pla'),
list(Block=c('B1','B2','B3'), Group = 'B', Drug = 'Drug1'),
type='average')

#Effect of Drug2 | Group B
contrast(mod,
list(Block=c('B1','B2','B3'), Group = 'B', Drug = 'Pla'),
list(Block=c('B1','B2','B3'), Group = 'B', Drug = 'Drug2'),
type='average')


df$Cond <- with(df, interaction(Drug, Group))
#Main effect of Group
group <- c(-1,-1,-1,1,1,1)
#Effect of Drug1 | Group A
Drug1.A <- c(-1,1,0,0,0,0)
#Effect of Drug2 | Group A
Drug2.A <- c(-1,0,1,0,0,0)
#Effect of Drug1 | Group B
Drug1.B <- c(0,0,0,-1,1,0)
#Effect of Drug2 | Group B
Drug2.B <- c(0,0,0,-1,0,1)
contrasts(df$Cond) <- cbind(group, Drug1.A, Drug2.A,
Drug1.B, Drug2.B)/2

summary(lme(value ~ Cond + Block, random=~1|Subj,data=df))



-----Urspr?ngliche Nachricht-----
Von: Reinhold Kliegl [mailto:reinhold.kliegl at gmail.com] 
Gesendet: Samstag, 13. Juni 2009 20:20
An: Erich Studerus
Cc: r-sig-mixed-models at r-project.org
Betreff: Re: [R-sig-ME] testing simple main effects by contrasts
of the two Groups. Here is a simple way to do this:

# Convert 2 Drug x 2 Group design to 1 x 4 Cond design
df$Cond <- factor(paste(df$Group, df$Drug, sep="_"))

# ANOVA: one main, two nested fixed effects
cmat <- matrix(c( -1, -1, +1, +1,             # Main effect of Group
	                     -1, +1,  0,  0,             # Effect of Drug |
Group==A
	                       0,  0, -1, +1),  4,  3)    # Effect of Drug |
Group==B
colnames(cmat) <- c(".group", ".drug_A", ".drug_B")

contrasts(df$Cond) <- cmat/2
print(mod <- lmer(value ~ Cond + (1 |Subj), data=df), cor=FALSE)
print(mod <- lmer(value ~ Cond + Block + (1 |Subj), data=df),
cor=FALSE)  # Block as fixed effect
print(mod <- lmer(value ~ Cond + (1 |Subj) + (1|Block), data=df),
cor=FALSE) # Block as random effect

The fixed-effect coefficients reported correspond to
(Intercept)  = mean of four conditions
Group  = difference between the two groups
.drug_A = difference between Drug levels in Group A
.drug_B = difference between Drug levels in Group B

and associated standard errors and "t-values".  Estimates line up with
table means for balanced designs.

You mention 2 Days of testing but this factor did not appear in the example.

The models you tested with lme and lmer where structurally different
with respect to Block. Given that you have only 3  Blocks, I would
include Block as a fixed effect, not as a random effect, but opinions
differ on this. Anyway, this has not much of a bearing on the
estimates of the Group difference and the estimates of Drug effects
within Groups.

Reinhold Kliegl

On Sat, Jun 13, 2009 at 4:57 PM, Erich
Studerus<erich.studerus at bli.uzh.ch> wrote:
vs
a
model?