循环运行双向混合模型ANOVA报错,指定列执行却正常
循环调用anova_test报错的解决方法
错误信息
错误:
! 无法用df_numeric[[column]]提取列。
✖ 由于精度损失,无法将df_numeric[[column]]转换为目标类型。
触发错误的循环代码
df_numeric <- df[, sapply(df, is.numeric)] for (column in names(df_numeric)) { res.aov <- anova_test(data = df, dv= df_numeric[[column]], wid = `Subject`, within = `Timepoint`, between = `Genotype`) get_anova_table(res.aov) }
可正常运行的单例代码
直接指定列名时,代码能正常生成ANOVA表:
res.aov <- anova_test(data = df, dv= `Tregs CD127lo CD25+`, wid = `Subject`, within = `Timepoint`, between = `Genotype`) get_anova_table(res.aov)
曾尝试使用df_numeric$column,但问题未解决。
数据结构
df_numeric的结构
library(rstatix) dput(df_numeric) structure(list(`Tregs CD127lo CD25+` = c(2702, 2175, 2651, 1672.8, 3762, 4264, 1975, 3208, 3285, 3457, 3383, 2619.9, 11872, 16101, 13443, 3935, 1894, 2297, 7385, 8901, 9522, 7100, 8789, 9309, 371, 379, 514), `Monocytes % of Live by Size` = c(1.38, 2.66, 4.74, 5.83, 3.9, 5.06, 6.36, 3.45, 2.64, 6.33, 10.7, 9.41, 3.42, 3.46, 2.73, 2.38, 3.12, 4.44, 5.31, 3.59, 4.91, 1.53, 6.54, 4.85, 6.87, 3.66, 5.07), `NK cells` = c(90.62, 153.6, 159.8, 88, 118, 159, 74, 82, 64, 30, 344, 73, 29, 198, 79, 145, 258, 307, 30, 74.4, 0, 47.3, 32, 0, 52.6, 95.3, 51.7)), row.names = c(NA, -27L ), class = c("tbl_df", "tbl", "data.frame"))
df的结构
> dput(df) structure(list(Subject = c("ASCVD002", "ASCVD002", "ASCVD002", "ASCVD003", "ASCVD003", "ASCVD003", "ASCVD004", "ASCVD004", "ASCVD004", "ASCVD005", "ASCVD005", "ASCVD005", "ASCVD006", "ASCVD006", "ASCVD006", "ASCVD008", "ASCVD008", "ASCVD008", "ASCVD009", "ASCVD009", "ASCVD009", "ASCVD010", "ASCVD010", "ASCVD010", "ASCVD011", "ASCVD011", "ASCVD011" ), Timepoint = c("0", "0.25", "0.5", "0", "0.25", "0.5", "0", "0.25", "0.5", "0", "0.25", "0.5", "0", "0.25", "0.5", "0", "0.25", "0.5", "0", "0.25", "0.5", "0", "0.25", "0.5", "0", "0.25", "0.5" ), Genotype = c("Heterozygote", "Heterozygote", "Heterozygote", "Heterozygote", "Heterozygote", "Heterozygote", "Heterozygote", "Heterozygote", "Heterozygote", "GG", "GG", "GG", "AA", "AA", "AA", "GG", "GG", "GG", "AA", "AA", "AA", "AA", "AA", "AA", "GG", "GG", "GG"), `Tregs CD127lo CD25+` = c(2702, 2175, 2651, 1672.8, 3762, 4264, 1975, 3208, 3285, 3457, 3383, 2619.9, 11872, 16101, 13443, 3935, 1894, 2297, 7385, 8901, 9522, 7100, 8789, 9309, 371, 379, 514), `Monocytes % of Live by Size` = c(1.38, 2.66, 4.74, 5.83, 3.9, 5.06, 6.36, 3.45, 2.64, 6.33, 10.7, 9.41, 3.42, 3.46, 2.73, 2.38, 3.12, 4.44, 5.31, 3.59, 4.91, 1.53, 6.54, 4.85, 6.87, 3.66, 5.07), `NK cells` = c(90.62, 153.6, 159.8, 88, 118, 159, 74, 82, 64, 30, 344, 73, 29, 198, 79, 145, 258, 307, 30, 74.4, 0, 47.3, 32, 0, 52.6, 95.3, 51.7)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -27L))
问题原因与解决办法
问题根源
anova_test的dv参数要求传入列名的符号或字符串,用来指定数据框中的因变量列。而原循环中传递的df_numeric[[column]]是列的数值向量,不符合函数参数要求,因此触发精度转换错误。
解决方法1:使用符号注入
利用sym()将列名字符串转为符号,再用!!注入到函数参数中:
df_numeric <- df[, sapply(df, is.numeric)] for (column in names(df_numeric)) { res.aov <- anova_test(data = df, dv= !!sym(column), wid = `Subject`, within = `Timepoint`, between = `Genotype`) print(get_anova_table(res.aov)) }
解决方法2:构造公式传递
通过as.formula动态构建ANOVA公式,直接指定因变量列:
df_numeric <- df[, sapply(df, is.numeric)] for (column in names(df_numeric)) { aov_formula <- as.formula(paste(column, "~ Genotype * Timepoint + Error(Subject/Timepoint)")) res.aov <- anova_test(data = df, formula = aov_formula) print(get_anova_table(res.aov)) }
注意:循环中需要用print()主动输出结果,否则默认不会显示每一次循环的ANOVA表。
内容的提问来源于stack exchange,提问作者swats15
相关产品推荐
相关产品推荐

