我是一个蟒蛇人,我在使用for循环时遇到了麻烦。 我有一个列表,表示包含colums(Sample_Name_Column,ComparisonColumn,MeasureA,MeasureB,MeasureC,MeasureD)的数据框中特定列的名称,我想将其用于线性混合效果模型(使用nlme库)。所以我写了一个简单的循环来尝试这样做:
list <- c("MeasureA","MeasureB","MeasureC","MeasureD")
for (i in list){
model = lme(i ~ ComparisonColumn, random=~1|Sample_Name_Column,
data=sampleDataSheet, method="REML")
}
但当然这失败了。
Error in model.frame.default(formula = ~i + ComparisonColumn + Sample_Name_Column, : variable lengths differ (found for 'ComparisonColumn')
函数lme不扩展变量;正在寻找列i作为输入。还有其他功能,如print()或length()。奇。 无论如何,我发现了一些使用.asformula和重新制定here的帖子,但是我在使用它时遇到了很多麻烦。
for (i in groupList) {
model = lme(as.formula(paste0(i, " ~ ComparisonColumn, random=~1|Sample_Name_Column")), data=sampleDataSheet, method="REML")
}
我进一步(因为已经正确插入了iterable):
Error in parse(text = x, keep.source = FALSE) :
<text>:1:26: unexpected ','
1: MeasureA ~ ComparisonColumn,
^
但这里也出现了问题。
我应该补充一点,直接运行模型:
model = lme(MeasureA ~ ComparisonColumn, random=~1|Sample_Name_Column,
data=sampleDataSheet, method="REML")
Linear mixed-effects model fit by REML
Data:
sampleDataSheet
Log-restricted-likelihood: -462.6646
Fixed: MeasureA ~ ComparisonColumn
(Intercept) ComparisonColumnTreatmentA
0.81377249 -0.08312908
Random effects:
Formula: ~1 | Sample_Name_Column
(Intercept) Residual
StdDev: 0.1800545 0.5348801
Number of Observations: 564
Number of Groups: 16
我已经取得了一些进展,但有些灵魂可以帮助我完成它吗?
感谢, ķ
答案 0 :(得分:1)
看看这个例子,并比较fm1和fm2的结果,它可能对你有帮助:
fm1 <- lme(distance ~ age + Sex, data = Orthodont, random = ~ 1)
params <- list(fixed = reformulate(c("age", "Sex"), response = "distance"),
random = reformulate("1"),
data = Orthodont)
fm2 <- do.call(lme,params)
答案 1 :(得分:0)
我认为Mr.SmithGoesToWashington几乎让我在那里。我现在使用的代码是:
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)
}
但是对于如何解释sampleDataSheet中的数据存在一些奇怪。
如果没有循环,模型变量将打印为:
> 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
但是在循环中,params由do.call函数处理,数据框整体打印为&#34;结构&#34 ;;它打印为(一旦它将循环迭代到&#34; MeasureD&#34;):
> 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, 2L,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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
这会对均值的多重比较的输出产生影响,这些比较输出整个数据帧而不是dataFrame名称。这不是一个大问题,但它很混乱。
> 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, 11L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 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)
任何想法如何解决?否则它正在工作。感谢。