Hi everyone: I'm hoping for some conceptual and practical advice when it comes to statistical analysis using linear models.
The overview is this: I have values measured from 4 trials, each of which included two treatments. In two of the trials, the treatment had no effect, in the two others, it did have an effect. I'd like to use an LM to show this.
I've simulated some data that approximate my real data:
testdf <- data.frame(trial =c(rep ("1", 20), rep("2", 20), rep("3", 20),
rep("4", 20)), treatment = c(rep(c(rep("A", 10), rep("B", 10)),4)), value =
NA) #create a data frame of 80 observations
trueval= 3.7 #the mean value overall
treatmentval = 1.2 #the effect of treatment
set.seed(6) #so we get the same answer each time
testdf$value <- rnorm(80, mean=trueval) #values drawn from a normal dist
testdf$value[testdf$trial=="3"&testdf$treatment=="B"] <-rnorm(10,
mean=trueval+treatmentval) # values for trial 3 treatment B are different
testdf$value[testdf$trial=="4"&testdf$treatment=="B"] <-rnorm(10,
mean=trueval+treatmentval) # values for trial 4 treatment B are different
head(testdf)
So in my made-up data I know that there's a treatment x trial interaction, but how to test this? An ANOVA gives me the expected result:
> summary(aov(value~trial*treatment, data = testdf))
Df Sum Sq Mean Sq F value Pr(>F)
trial 3 4.12 1.375 1.127 0.34386
treatment 1 8.69 8.687 7.124 0.00939 **
trial:treatment 3 11.20 3.733 3.061 0.03351 *
But I don't want to use an ANOVA (in my real data I have some random effects to deal with).
My problem with using an LM is that with a factor variable (trial) the model output compares each level of the factor to each other, instead of giving me a P-value for the entire variable.
> summary(lm(value~trial*treatment, data = testdf))
Call:
lm(formula = value ~ trial * treatment, data = testdf)
Residuals:
Min 1Q Median 3Q Max
-1.7489 -0.7443 -0.0995 0.6794 2.4066
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 3.8054 0.3492 10.897 <2e-16 ***
trial2 0.1057 0.4939 0.214 0.8312
trial3 -0.1710 0.4939 -0.346 0.7301
trial4 -0.4477 0.4939 -0.907 0.3676
treatmentB 0.1767 0.4939 0.358 0.7216
trial2:treatmentB -0.4871 0.6984 -0.697 0.4878
trial3:treatmentB 1.2905 0.6984 1.848 0.0687 .
trial4:treatmentB 1.1261 0.6984 1.612 0.1113
This is annoying because 1. I have to decide which trial (if not 1) should be the "reference" 2. All subsequent trials are compared to that particular reference 3. Doing all these trial by trial comparisons is making the interaction hard to pick up.
My goal is to come up with some statistical justification for the statement "The treatment had a significant effect in Trials C and D, but not A and B."
I've decided I could run an lm for each trial separately and do some sort of Bonferroni-Holm p-val correction but I'm trying to limit the number of models I run (to avoid complaints of data dredging). Does anyone have another way to approach dealing with factor variables in linear models?
Thanks a million
aov()
is a wrapper forlm()
. In your example the two models are exactly equivalent: if you use theanova()
function on the model fit withlm()
you will get the same analysis of variance table. It is more complicated if you want to add random effects, because for example if you fit the model withlmer()
the functionanova()
won't give you p values. There are however ways to get p-values also for mixed-effect models, see for example this question $\endgroup$lme4
package. But you can fit the model with either thelmer
function in thelme4
package orlme
innlme
, and get the p-values, respectively, with thelmerTest
package, or theanova
function. The following link gives some pretty clear examples, I think, tho thelsmeans
package should be updated to theemmeans
package. SAEPER: One-way ANOVA with Random Blocks $\endgroup$