2

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:

  1. volume predicted by age + sex * region, where region is within-subjects
  2. score predicted by psychiatricGroup * sex * scoreType, where scoreType is within-subjects
  3. 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!

  1. What is the correct code to model uptake predicted by conc using random-effects?
  2. How would I model uptake predicted by conc * Treatment?
  3. Can this analysis be done in R with fixed-effects only?
  4. 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)
Kayle Sawyer
  • 549
  • 7
  • 22
  • I think you should ask about your study design on CrossValidated - you may not be particularly interested in the random variation across subjects, but you will probably need to account for this variation in your model. With a within-subjects design, observations on the same subject will be correlated. Therefore you can't use a pure fixed-effects model as you can't assume the obs are independent. – Marius Aug 04 '17 at 04:34
  • Thanks for the suggestion. I'll do that. Assuming I'll go with random effects after all, about just items 1 (working code for this example), 2 (add between-subjects interaction) and 4 (all levels post-hocs), then? – Kayle Sawyer Aug 04 '17 at 12:21

0 Answers0