循环遍历 Dataframe 中的列以在 R 中构建重复测量线性模型 lme

Looping through columns in a Dataframe for to build a repeated measure linear model lme in R

我正在尝试遍历该列以获得类似这样的内容“aov.GROUPconint <- lme(F3 ~ Condition, random = ~1|Subject, data=data10)”但我不断收到类似的错误下面那个

(错误 model.frame.default(公式 = ~i + 条件 + 主题,数据 = 列表( : 可变长度不同(发现 'Condition'))

library(lme4)
library(nlme)
library(lmerTest)

for(i in names(data10)) {
aov.GROUPconint <- lme(get(i) ~ Condition, random = ~1|Subject, data=data10)
GROUPconint.posthoc <- summary(glht(aov.GROUPconint, linfct = mcp(Condition = "Tukey")))
GROUPconint.posthoc}
data10 <- structure(list(Subject = c("P14", "P15", "P16", "P17", "P18", 
"P19", "P20", "P21", "P22", "P23", "P24", "P25", "P26", "P27", 
"P28", "P29", "P30", "P31", "P32", "P33", "P34", "P35", "P37", 
"P38", "P14", "P15", "P16", "P17", "P18", "P19", "P20", "P21", 
"P22", "P23", "P24", "P25", "P26", "P27", "P28", "P29", "P30", 
"P31", "P32", "P33", "P34", "P35", "P37", "P38", "P14", "P15", 
"P16", "P17", "P18", "P19", "P20", "P21", "P22", "P23", "P24", 
"P25", "P26", "P27", "P28", "P29", "P30", "P31", "P32", "P33", 
"P34", "P35", "P37", "P38"), Condition = structure(c(1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Midline", "No Midline crossing", 
"Midline crossing"), class = "factor"), Wave = c(2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2), Cond = c("P14-CondA1", "P15-CondA1", "P16-CondA1", 
"P17-CondA1", "P18-CondA1", "P19-CondA1", "P20-CondA1", "P21-CondA1", 
"P22-CondA1", "P23-CondA1", "P24-CondA1", "P25-CondA1", "P26-CondA1", 
"P27-CondA1", "P28-CondA1", "P29-CondA1", "P30-CondA1", "P31-CondA1", 
"P32-CondA1", "P33-CondA1", "P34-CondA1", "P35-CondA1", "P37-CondA1", 
"P38-CondA1", "P14-CondB", "P15-CondB", "P16-CondB", "P17-CondB", 
"P18-CondB", "P19-CondB", "P20-CondB", "P21-CondB", "P22-CondB", 
"P23-CondB", "P24-CondB", "P25-CondB", "P26-CondB", "P27-CondB", 
"P28-CondB", "P29-CondB", "P30-CondB", "P31-CondB", "P32-CondB", 
"P33-CondB", "P34-CondB", "P35-CondB", "P37-CondB", "P38-CondB", 
"P14-CondC", "P15-CondC", "P16-CondC", "P17-CondC", "P18-CondC", 
"P19-CondC", "P20-CondC", "P21-CondC", "P22-CondC", "P23-CondC", 
"P24-CondC", "P25-CondC", "P26-CondC", "P27-CondC", "P28-CondC", 
"P29-CondC", "P30-CondC", "P31-CondC", "P32-CondC", "P33-CondC", 
"P34-CondC", "P35-CondC", "P37-CondC", "P38-CondC"), Fp1 = c(-0.496612, 
0.175496, -0.902775, 0.409193, -0.1442, -0.221652, -0.096781, 
-0.913399, 2.40579, -1.118419, -1.022421, -0.53183, -0.13084, 
-0.252216, -0.994141, -0.130316, -0.698332, -0.235089, 1.125141, 
0.183875, -0.22187, 0.540777, -0.746555, -0.400875, -0.443366, 
-0.031881, -0.1578, 0.119318, -0.241995, -0.136576, -0.304703, 
-1.351032, 0.842869, -1.082589, -2.197998, -0.243583, -0.221359, 
-1.027024, -1.389851, -0.435382, -0.722848, -0.428661, 0.787846, 
-0.013075, -1.119274, -0.118651, -0.329103, -0.693915, -0.53459, 
0.273439, -0.475136, -0.296188, -0.415787, -0.148723, 0.096408, 
-0.96811, 2.154568, -0.436949, -1.074031, -0.26988, 0.043973, 
-0.399649, -0.758342, 0.189896, -1.143895, -0.144397, 0.244302, 
0.193854, -0.577475, -0.285723, -1.074546, -0.432559), Fz = c(-0.060342, 
-0.036696, -0.000543, -0.018201, -0.052527, 0.004731, -0.008874, 
-0.159202, -0.036133, -0.000619, -0.177204, -0.025317, -0.013332, 
-0.163497, -0.526575, -0.005551, -0.072309, -0.01921, 0.05475, 
-0.059213, -0.163045, -0.041531, -0.066216, 0.020983, -0.017219, 
0.00765, -0.00089, -0.020147, -0.109683, 0.015722, 0.015082, 
-0.168747, -0.080931, -0.03073, -0.273976, -0.038441, -0.022641, 
-0.233099, -0.400717, -0.002534, -0.086837, -0.042448, 0.055243, 
-0.064166, -0.183284, -0.034489, -0.052705, -0.030963, -0.008431, 
0.027167, -0.001037, -0.057967, -0.100134, -0.003177, 0.005983, 
-0.164096, -0.017424, 0.006722, -0.221726, -0.034103, -0.021846, 
-0.180729, -0.478264, 0.019417, -0.14423, -0.066716, 0.024858, 
-0.060647, -0.221947, -0.043251, -0.135783, -0.071788), F3 = c(-0.174065, 
-0.065702, -0.159353, -0.032133, -0.150052, -0.067101, -0.07448, 
-0.237671, 0.055216, -0.105337, -0.222753, -0.104898, -0.147431, 
-0.287062, -0.573383, 0.239084, -0.165919, -0.00523, 0.032584, 
-0.108239, -0.076064, 0.001332, -0.039636, -0.027232, -0.108062, 
-0.037226, -0.165048, -0.053539, -0.279105, -0.052014, -0.083507, 
-0.216209, -0.081164, -0.14322, -0.555109, -0.027574, -0.100662, 
-0.480267, -0.614076, 0.072236, -0.154807, -0.062123, -0.020433, 
-0.1123, -0.129918, -0.095419, -0.092241, -0.109496, -0.094647, 
-0.036632, -0.175535, -0.163347, -0.320636, -0.089897, -0.030159, 
-0.213181, 0.011029, 0.04508, -0.277485, -0.004721, -0.127541, 
-0.245669, -0.497781, 0.37073, -0.244013, -0.031662, -0.097902, 
-0.081723, -0.148037, -0.12719, -0.153081, -0.079912), F7 = c(-0.635082, 
0.2404, -0.307119, -0.185126, -0.369607, -0.491062, -0.401024, 
0.442896, 0.718414, -1.463474, -0.693627, -1.116109, -0.15568, 
-0.052029, 0.357872, 1.226794, -1.625299, 0.118726, 0.20047, 
-0.004455, 0.134617, -0.131425, -0.154691, -1.174462, -0.415042, 
0.197994, -0.06514, -0.367751, -0.735034, -0.339338, -0.458973, 
-0.060714, -0.489308, -1.380347, -1.166543, -0.088834, -0.301531, 
-0.712918, -0.598602, 0.779757, -1.562481, -0.157596, 0.028825, 
0.03752, 0.015231, -0.424226, -0.418021, -1.283733, -0.29097, 
-0.062861, -0.426187, -0.621661, -0.556111, -0.6845, -0.238905, 
0.148606, -0.097206, -1.257836, -1.122073, 0.043903, -0.263615, 
0.921246, 0.557765, 1.521124, -1.592826, 0.008504, 0.016988, 
-0.104755, -0.090443, -0.684738, -0.157054, -0.385854), FT9 = c(-0.406084, 
1.381676, -0.045519, -0.297303, -0.312741, -0.616132, 0.063265, 
0.057329, 1.527738, -2.148968, -1.102047, -1.088216, -0.175023, 
-0.325755, -0.137663, 0.75953, -0.987475, 0.252517, -0.173661, 
0.00846, -0.327396, 0.122252, -0.00034, -1.563515, -0.273845, 
1.602534, -0.001253, -0.303526, -0.689548, -0.415145, 0.050247, 
-0.201039, -0.781193, -1.962264, -1.33081, -0.123767, -0.178854, 
-1.643061, -0.753764, 0.131857, -0.743438, -0.143156, -0.263083, 
0.1625, -0.213537, -0.32504, -0.199028, -1.612283, -0.362902, 
1.214414, -0.218702, -0.582437, -0.617929, -0.974579, 0.158473, 
0.071369, -0.495419, -1.91758, -1.23533, 0.206926, -0.265826, 
-1.040215, 0.551552, 0.744699, -1.089204, 0.083859, -0.256619, 
-0.211586, -0.462199, -0.297986, -0.246886, -0.482127), FC5 = c(-0.598897, 
-0.053646, -0.251429, -0.157872, -0.237714, -0.19274, -0.25328, 
-0.027087, 0.366831, -0.892455, -0.391851, -0.40384, -0.215869, 
-0.393929, -0.604355, 0.681853, -0.3318, 0.055977, -0.138587, 
-0.127397, -0.060716, 0.069392, -0.003673, -0.334343, -0.293524, 
-0.009061, -0.149024, -0.22737, -0.434437, -0.17316, -0.223214, 
-0.085838, -0.286085, -0.800674, -0.580662, -0.040196, -0.12631, 
-0.793884, -0.777343, 0.366846, -0.278754, -0.076514, -0.19062, 
-0.063746, -0.060636, -0.053689, -0.163588, -0.429794, -0.34853, 
-0.148778, -0.126716, -0.299513, -0.401491, -0.313193, -0.163203, 
0.022764, -0.171705, -0.737934, -0.561581, 0.048091, -0.182426, 
-0.392664, -0.360901, 0.759159, -0.427882, -0.001378, -0.221162, 
-0.224786, -0.129561, -0.080656, -0.129178, -0.15966)), row.names = c(NA, 
-72L), class = c("tbl_df", "tbl", "data.frame"))

您可以使用reformulate动态构造公式。

我正在使用 lapply 而不是 for 循环,也 运行 从第 5 列开始。

library(nlme)
library(multcomp)

lapply(names(data10)[5:ncol(data10)], function(x) {
  aov.GROUPconint <- lme(reformulate('Condition', x), random = ~1|Subject, data=data10)
  GROUPconint.posthoc <- summary(glht(aov.GROUPconint, linfct = mcp(Condition = "Tukey")))
  GROUPconint.posthoc
}) -> result

result