简体   繁体   English

在循环中运行双向混合模型方差分析时出错,但在明确识别数据列时却没有

[英]Error running two way mixed model ANOVA in loop but not when column of data specifically identified

This is the loop that is generating the error.这是产生错误的循环。

Error: !错误: ! Can't subset columns with df_numeric[[column]] .不能使用df_numeric[[column]]对列进行子集化。 ✖ Can't convert from df_numeric[[column]] to due to loss of precision. ✖ 由于精度损失,无法从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)
}  

But when I pull out the code for the anova and specifically input the column from my dataframe it generates the proper anova table results.但是,当我提取 anova 的代码并专门从我的数据框中输入列时,它会生成正确的 anova 表结果。

res.aov <- anova_test(data = df, dv=  `Tregs CD127lo CD25+`, wid = `Subject`, within = `Timepoint`, between = `Genotype`)
get_anova_table(res.aov)

I have tried using df_numeric$column .我试过使用df_numeric$column

Dataframe数据框

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"))


> 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))

Thank you for providing the dput and code - you are using the df dataset, so you don't really need the df_numeric dataset.感谢您提供dput和代码——您使用的是df数据集,因此您并不真正需要df_numeric数据集。 To get the names of all your numeric columns, you can use the following code: names(df)[unlist(lapply(df, is.numeric))] .要获取所有数字列的名称,您可以使用以下代码: names(df)[unlist(lapply(df, is.numeric))] Also you assigned a vector of values to the command dv - it should only be the name of the column.您还为命令dv分配了一个值向量 - 它应该只是列的名称。

The below should work for you, it did for me:以下应该对你有用,它对我有用:

for (column in names(df)[unlist(lapply(df, is.numeric))]) {
  res.aov <- rstatix::anova_test(data = df, dv = column, 
                                 wid = `Subject`, within = `Timepoint`, between = `Genotype`)
  rstatix::get_anova_table(res.aov)
}  

Note that in your loop, you are overwriting res.aov with each iteration and you are not storing the results from get_anova_table(res.aov) - I would suggest storing these data in a list:请注意,在您的循环中,每次迭代都会覆盖res.aov并且您不会存储get_anova_table(res.aov)的结果 - 我建议将这些数据存储在列表中:

nnames <- names(df)[unlist(lapply(df, is.numeric))]
res.aov <- list()
aov_tab <- list()
for (column in nnames) {
  res.aov[[column]] <- rstatix::anova_test(data = df, dv = column, 
                                 wid = `Subject`, within = `Timepoint`, between = `Genotype`)
  aov_tab[[column]] <- rstatix::get_anova_table(res.aov[[column]])
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM