Is there a way to specify a two-way ANOVA, with one within-subjects predictor and one between-subjects predictor using lme (from nlme) or lmer (from lme4)? Perhaps this is a CrossValidated question, but I don't think I am interested in random-effects, because I don't care about the variation within subjects nor about making predictions about individual subjects. I'm interested in the average subject, so this should be a within-subjects fixed-effects predictor (ie a fixed number of levels) and a between-subjects fixed-effects predictor. I don't want a repeated-measures ANOVA, which has many assumptions including sphericity and has difficult to do post-hocs. Here are some examples of the kinds of models I need:
- volume predicted by age + sex * region, where region is within-subjects
- score predicted by psychiatricGroup * sex * scoreType, where scoreType is within-subjects
- volume predicted by sex * scoreType * score (this one may have to be random-effects for score, but within-subjects at fixed levels for scoreType).
(In JMP, the first two can be specified using the "Mixed Model" personality, "Exchangeable" repeated covariance structure, specifying the repeated within-subjects predictor and specifying the subject.)
Using the "Repeated-measures / within-subjects ANOVA in R", "repeated measure anova using regression models (LM, LMER)", and "How to convert Afex or car ANOVA models to lmer? Observed variables" questions and Chapter 4 of the lme4 book, I created the following code that uses the CO2 sample data. It's not the best sample dataset, but assume that: "Plant" is like subject ID, "Type" and "Treatment are between-subjects effects, and "conc" is within-subjects, and its levels are NOT ORDERED (ie it's of categorical and character type, in the same way that multiple psychological score types from a single person would be, or multiple anatomical regions from a single person would be). If you know of a better sample dataset, use that instead!
- What is the correct code to model uptake predicted by conc using random-effects?
- How would I model uptake predicted by conc * Treatment?
- Can this analysis be done in R with fixed-effects only?
- Is there a way to run post-hocs for all levels of an interaction (within-factor*between-factor) and apply multiple comparison correction (e.g. Tukey HSD)? For this example, that would be 7 concentrations * 2 Treatments = 14 comparisons.
The code below produces the following error: "Error: number of observations (=84) <= number of random effects (=84) for term (conc | Plant); the random-effects parameters and the residual variance (or scale parameter) are probably unidentifiable"
library(data.table)
library(lme4)
CO2 <- data.table(CO2)
CO2[, (1:4) := lapply(.SD, as.factor), .SDcols=(1:4)]
# uptake predicted by concentration
mylm1 <- lmer(uptake ~ (conc|Plant), data = CO2)
# uptake predicted by concentration*Treatement
mylm2 <- lmer(uptake ~ Treatment*(conc|Plant), data = CO2)
UPDATE The parentheses for lmer() specifies which measures are random-effects, but you still need to specify the predictor.
mylm1 <- lmer(uptake ~ conc + (1|Plant), data = CO2)
mylm2 <- lmer(uptake ~ Treatment*conc + (1|Plant), data = CO2)