I think MrSmithGoesToWashington has got me almost there. The code I'm now using is:
library(lsmeans)
library(multcomp)
library(nlme)
groupList = c("MeasureA","MeasureB","MeasureC","MeasureD")
for (i in groupList){
print(i)
# following MrSmithGoesToWashington's example
params = list(fixed = reformulate("ComparisonColumn", response = i), random = reformulate("1|Sample_Name_Column"), data = sampleDataSheet, method="REML")
model = do.call(lme,params)
anova.lme(model, type="sequential", adjustSigma = FALSE)
posthoc = glht(model, linfct=mcp(ComparisonColumn="Tukey"))
Multiple_Comparisons_of_Means = summary(posthoc, sampleDataSheet=adjusted("single-step"))
print(Multiple_Comparisons_of_Means)
}
But there's some weirdness about how the data from sampleDataSheet is being interpreted.
Without looping, the model variable prints as:
> model
Linear mixed-effects model fit by REML
Data: test
Log-restricted-likelihood: -2961.527
Fixed: MeasureD ~ ComparisonColumn
(Intercept) ComparisonColumnNP
1.924292e+02 7.532103e-03
Random effects:
Formula: ~1 | Sample_Name_Column
(Intercept) Residual
StdDev: 21.12601 45.3235
Number of Observations: 564
Number of Groups: 16
but in the loop, the way params is handled by the do.call function, the data frame prints in its entirety as a "structure"; it prints as (once it has iterated the loop to "MeasureD"):
> model
Linear mixed-effects model fit by REML
Data: structure(list(Sample_Name_Column = structure(c(10L, 10L, 11L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, .....), .Label = c("A.2", "A.3", "A.4", "A.5", "A.7", "A.4", "A.6", "A.8", "B.10", "B.8", "B.9", "B.3", "B.4", "B.5", "B.6", "B.7"), class = "factor"), ComparisonColumn = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,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3L, 3L, 3L, 3L, 3L, 2L, 2L, 4L, 0L, 3L, 3L, 4L, 1L, 2L, 0L, 3L, 1L, 5L, 3L, 2L, 5L, 2L, 0L, 1L, 2L, 3L, 1L, 1L, 1L, 3L, 1L, 1L, 0L, 0L, 3L, 0L, 2L, 2L, 0L, 3L, 0L, 0L, 1L, 2L, 0L, 2L, 0L, 1L, 2L, 1L, 1L, 0L, 5L, 4L, 2L, 3L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 2L, 2L, 2L, 0L, 1L, 1L, 3L, 0L, 1L, 2L, 0L, 1L, 0L, 0L, 2L, 1L, 0L, 1L, 2L, 1L, 1L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 2L, 1L, 3L, 2L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 3L, 0L, 2L, 0L, 1L, 1L, 0L, 2L, 0L, 1L, 2L, 2L, 0L, 1L, ..... 2L, 1L, 0L, 1L, 0L, 0L, 1L, 2L, 3L, 1L, 0L, 1L, 0L, 1L, 0L, 3L, 1L, 0L, 0L, 3L, 2L, 0L, 1L, 1L, 1L, 0L, 0L), MeasureD = c(157L, 150L, 120L, 159L, 193L, 96L, 225L, 197L, 278L, 252L, 191L, 165L, 240L, 202L, 221L, 225L, 167L, 235L, 249L, 231L, 219L, 273L, 185L, 221L, 150L, 180L, 282L, 216L, 128L, 255L, 161L, 152L, 90L, 154L, 153L, 135L, 130L, 145L, 131L, 175L, 99L, 148L, 173L, 115L, 196L, 227L, 208L, 139L, 278L, 234L, 148L, 109L, 233L, 167L, 151L, 141L, 122L, 106L, 120L, 140L, 266L, 226L, 277L, 198L, 237L, 162L, 203L, 201L, 192L, 237L, 230L, 221L, 182L, 184L, 298L, 191L, 240L, 210L, 250L, 186L, 187L, 229L, 230L, 206L, 293L, 182L, 218L, 209L, 171L, 152L, 279L, 324L, 122L, 132L, 223L, 250L, 155L, 189L, 206L, 213L, 233L, 215L, 95L, 164L, 213L, 188L, 273L, 284L, 206L, 185L, 209L, 176L, 136L, 190L, 214L, 240L, 231L, 190L, 211L, 165L, 246L, 236L, 244L, 265L, 160L, 220L, 203L, 186L, 110L, 181L, 180L, 264L, 159L, 151L, 179L, 144L, 187L, 144L, 280L, 280L, 295L, 214L, 217L, 246L, 184L, 204L, 200L, 223L, 192L, 226L, 209L, 146L, 209L, 181L, 223L, 196L, 226L, 147L, 191L, 180L, 154L, 162L, 170L, 174L, 144L, 230L, 155L, 197L, 228L, 196L, 166L, 182L, 169L, 192L, 206L, 117L, 133L, 127L, 193L, 156L, 140L, 267L, 234L, 280L, 181L, 230L, 169L, 192L, 166L, 182L, 140L, 244L, 201L, 230L, 168L, 159L, 152L, 211L, 195L, 125L, ..... 202L, 295L, 188L, 103L, 104L, 168L, 229L, 210L, 163L, 228L, 231L, 143L, 164L)), .Names = c("Sample_Name_Column", "ComparisonColumn", "MeasureA", "MeasureB", "MeasureC", "MeasureD" ), class = "data.frame", row.names = c(NA, -564L))
Log-restricted-likelihood: -2961.527
Fixed: MeasureD ~ ComparisonColumn
(Intercept) ComparisonColumnNP
1.924292e+02 7.532103e-03
Random effects:
Formula: ~1 | Sample_Name_Column
(Intercept) Residual
StdDev: 21.12601 45.3235
Number of Observations: 564
Number of Groups: 16
This has repercussions on the output of the multiple comparisons of means, which outputs the whole data frame rather than the dataFrame name. It's not a huge issue, but it's messy.
> summary(Multiple_Comparisons_of_Means)
Simultaneous Tests for General Linear Hypotheses
Multiple Comparisons of Means: Tukey Contrasts
Fit: lme.formula(fixed = MeasureD ~ ComparisonColumn, data = list(
Sample_Name_Column = c(10L, 10L, 11L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L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
1L, 0L, 2L, 3L, 5L, 2L, 3L, 2L, 2L, 2L, 1L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 2L, 2L, 4L, 0L, 3L, 3L, 4L, 1L, 2L, 0L, 3L,
1L, 5L, 3L, 2L, 5L, 2L, 0L, 1L, 2L, 3L, 1L, 1L, 1L, 3L, 1L,
1L, 0L, 0L, 3L, 0L, 2L, 2L, 0L, 3L, 0L, 0L, 1L, 2L, 0L, 2L,
0L, 1L, 2L, 1L, 1L, 0L, 5L, 4L, 2L, 3L, 0L, 1L, 1L, 1L, 0L,
1L, 1L, 2L, 2L, 2L, 0L, 1L, 1L, 3L, 0L, 1L, 2L, 0L, 1L, 0L,
0L, 2L, 1L, 0L, 1L, 2L, 1L, 1L, 0L, 0L, 0L, 0L, 2L, 0L, 1L,
0L, 1L, 1L, 1L, 1L, 1L, 0L, 2L, 1L, 3L, 2L, 1L, 1L, 1L, 0L,
1L, 1L, 0L, 3L, 0L, 2L, 0L, 1L, 1L, 0L, 2L, 0L, 1L, 2L, 2L,
0L, 1L, 1L, 1L, 1L, 1L, 0L, 4L, 4L, 2L, 1L, 1L, 1L, 2L, 2L,
MeasureD = c(157L, 150L, 120L, 159L, 193L, 96L, 225L, 197L,
278L, 252L, 191L, 165L, 240L, 202L, 221L, 225L, 167L, 235L,
249L, 231L, 219L, 273L, 185L, 221L, 150L, 180L, 282L, 216L,
128L, 255L, 161L, 152L, 90L, 154L, 153L, 135L, 130L, 145L,
131L, 175L, 99L, 148L, 173L, 115L, 196L, 227L, 208L, 139L,
278L, 234L, 148L, 109L, 233L, 167L, 151L, 141L, 122L, 106L)), random = ~1 | Sample_Name_Column,
method = "REML")
Linear Hypotheses:
Estimate Std. Error z value Pr(>|z|)
NP - LP == 0 0.007532 11.238300 0.001 0.999
(Adjusted p values reported -- single-step method)
Any ideas how to fix that? Otherwise it's working. Thanks.