在 R 函数中使用 aov 执行多个方差分析

Perform multiple anova's with aov in an R function

我正在为多个变量的 运行 方差分析编写一个函数。我用一个预测器尝试了该函数并且它工作正常,现在我正在尝试使用两个变量但函数正在崩溃。

下面是带有一个预测变量“Condition”的代码

lapply(names(data10)[6: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


使用两个预测变量——“条件”和“测试”——代码崩溃了,我不知道如何编辑上面的代码

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

data10 <- structure(list(Subject = structure(c(12L, 24L, 12L, 24L, 12L, 
24L, 12L, 24L, 12L, 24L, 12L, 24L), .Label = 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"), class = "factor"), Condition = structure(c(1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("CEN", 
"IPS", "CTL"), class = "factor"), Wave = c(1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1), test = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 
2L, 2L, 1L, 1L, 2L, 2L), .Label = c("Block 1", "Block 10"), class = "factor"), 
    ...5 = c("P25-CondA1", "P38-CondA1", "P25-CondA1", "P38-CondA1", 
    "P25-CondB", "P38-CondB", "P25-CondB", "P38-CondB", "P25-CondC", 
    "P38-CondC", "P25-CondC", "P38-CondC"), Fp1 = c(-5.840139, 
    -24.64444, -5.91461, -10.269491, -1.063752, -10.417562, -3.057545, 
    -22.833533, -0.528596, -6.226959, -5.206908, -16.431027), 
    Fz = c(-0.160478, -4.796688, -0.572049, -2.42414, -0.081133, 
    -2.212509, -0.507796, -5.125658, -0.247309, -1.391691, -0.340251, 
    -3.275482), F3 = c(-3.237156, -7.13964, -2.356562, -4.498527, 
    -1.912161, -3.369108, -0.764519, -7.338588, -0.967395, -2.61488, 
    -2.006442, -6.064484), F7 = c(-12.291867, -23.007904, -13.424168, 
    -51.746685, -4.683314, -16.146104, 3.129978, -16.762208, 
    -4.072039, -3.997164, -9.893088, -30.463026), FT9 = c(-5.145666, 
    -19.855898, -7.159363, -79.547642, -2.737132, -24.988066, 
    -0.764389, -8.678315, -2.424962, -3.655473, -16.684891, -40.817396
    ), FC5 = c(-1.912046, -7.299207, -2.782865, -13.433165, -2.717561, 
    -6.404056, -1.020601, -5.731675, -1.449527, -2.455464, -4.816248, 
    -8.451322), FC1 = c(-0.014723, -3.250151, -0.025628, -2.072377, 
    -0.025264, -0.816653, -0.008836, -1.645703, -0.025248, -0.886561, 
    -0.019677, -1.440108), C3 = c(-1.673774, -3.907241, -1.517202, 
    -3.189047, -1.721737, -1.953359, -0.130617, -1.255402, -1.000825, 
    -2.154744, -4.642536, -2.5244), T7 = c(-6.692289, -11.995039, 
    -4.388139, -26.845076, -2.150749, -19.960795, 0.957167, -9.285198, 
    -2.610707, -3.387525, -16.078687, -13.23254), TP9 = c(-6.577069, 
    -24.083291, -5.319204, -17.99928, -2.009148, -22.327639, 
    -0.39423, -11.561658, -11.369999, -1.670421, -27.666286, 
    -8.352513), CP5 = c(-4.256723, -7.168578, -3.69391, -8.595433, 
    -3.172519, -5.686751, -1.439715, -5.684416, -2.23341, -3.819792, 
    -12.817709, -4.332906), CP1 = c(-2.424485, -2.105764, -2.437918, 
    -1.684633, -1.896294, -0.603067, -1.310922, -1.246486, -1.058182, 
    -1.723797, -4.112311, -0.39083), Pz = c(-5.230209, -6.931432, 
    -5.447097, -4.420237, -4.298466, -0.572419, -3.76145, -4.866791, 
    -1.882695, -2.773345, -7.048977, -1.297095), P3 = c(-5.717413, 
    -8.568655, -5.872956, -6.676226, -3.610336, -3.78263, -2.678968, 
    -5.256058, -1.84788, -4.615055, -10.663448, -3.350067), P7 = c(-8.747675, 
    -14.647256, -5.446711, -13.149602, -3.162763, -11.810952, 
    -0.844879, -10.721099, -3.343471, -1.414516, -21.813454, 
    -5.784583), O1 = c(-6.725148, -23.142534, -12.192043, -14.436759, 
    -5.107042, -8.746607, -4.771125, -13.834345, -3.327836, -0.988959, 
    -23.619593, -8.835368), Oz = c(-6.360107, -19.795088, -11.102469, 
    -13.143209, -4.797536, -6.422619, -4.972995, -13.851071, 
    -3.273859, -0.095299, -23.344231, -10.013479), O2 = c(-8.604531, 
    -16.010789, -9.163555, -14.997259, -5.795494, -7.69402, -4.861372, 
    -15.428395, -3.201026, 0.689055, -25.061542, -14.588914), 
    P4 = c(-5.786419, -8.570093, -6.01655, -7.250971, -4.000653, 
    -5.302266, -3.95116, -11.026836, -1.672558, 0.563733, -9.640803, 
    -5.720633), P8 = c(-10.232556, -11.447704, -6.468332, -13.059249, 
    -3.728454, -8.160377, -1.216276, -15.638084, -3.104394, 1.522973, 
    -17.690755, -17.926968), TP10 = c(-8.034667, -11.84704, -6.469727, 
    -17.795867, -3.392685, -14.240206, -0.750774, -14.076242, 
    -2.212408, -0.838559, -24.863607, -13.99878), CP6 = c(-4.075277, 
    -6.763672, -4.167653, -13.016007, -3.034482, -6.815944, -1.626736, 
    -10.962036, -2.496595, -0.236931, -7.798438, -6.962835), 
    CP2 = c(-2.405001, -2.252853, -2.180014, -2.301028, -2.242261, 
    -1.182271, -2.213638, -3.496284, -1.008576, -0.61297, -3.492367, 
    -0.51545), Cz = c(-0.468023, -0.004084, -0.638258, -0.00788, 
    -0.851801, -0.007214, -0.706636, -0.010855, -0.536705, -0.013132, 
    -1.039244, -0.007484), C4 = c(-1.666512, -2.444287, -1.888915, 
    -5.533711, -1.222455, -1.965361, -0.873634, -4.527433, -1.818675, 
    -0.937223, -2.389627, -2.275628), T8 = c(-6.815842, -8.228049, 
    -5.290419, -15.105313, -1.736178, -13.356379, -1.95265, -19.905574, 
    -3.711849, -9.398742, -10.633943, -12.196464), EOG = c(-20.232283, 
    -99.109516, -42.37872, -41.447092, 1.38567, -23.524751, 3.979485, 
    -17.365329, -6.501096, -8.57049, -27.861358, -35.333672), 
    FC6 = c(-3.332464, -3.964186, -3.596093, -15.233898, -0.609353, 
    -6.322399, -0.27383, -7.378963, -2.265338, -1.842532, -3.983366, 
    -3.932509), FC2 = c(-0.026439, -1.089279, -0.01404, -1.967481, 
    -0.021138, -0.912876, -0.016665, -1.514663, -0.016964, -0.101446, 
    -0.017254, -0.920491), F4 = c(-1.289919, -3.210157, -1.225391, 
    -8.599483, -0.13505, -4.758277, -0.16653, -6.277869, -1.030201, 
    -0.780301, -1.717511, -3.839278), F8 = c(-4.269784, -14.505706, 
    -9.1726, -44.947816, 0.269252, -16.018926, -2.184163, -13.581011, 
    -2.464344, -5.565065, -6.70289, -12.713321), Fp2 = c(-10.068992, 
    -12.382243, 0.860865, -23.860863, 0.389788, -13.042772, 0.540714, 
    -21.70098, -2.526254, 2.619264, -3.399223, -59.010281)), row.names = c(NA, 
-12L), class = c("tbl_df", "tbl", "data.frame"))

这是错误信息

Error in na.fail.default(list(Fp1 = c(-4.50016, -1.485162, -2.205266, : missing values in object
library(tidyverse)
library(afex)
library (lsmeans)


lapply(names(data10)[6:ncol(data10)], function(x) {
  a1 <- aov_ez("Subject", x, data10, within = c("Condition",   "test"))
  (ls1 <- lsmeans(a1, c("Condition", "test")))
  update(pairs(ls1), by=NULL, adjust = "none")

}) -> result

result