R中的双向方差分析。如何自动检查所有数据帧变量?

Two-way ANOVA in R. How to check all the dataframe variables automatically?

我有一个包含 33 个变量和 1 个因变量的数据框。我需要执行双向方差分析测试以查看它们的影响。 现在我必须手动输入 vars:

two.way <- aov(`Yield t/ha` ~ 
TypeP*PreviousCulture *               
T1may*T2may*T3may*T1june*         
T2june*T3june*T1july*T2july*           
T3july*T1aug*T2aug*T3aug*        
T1sept*T2sept*T3sept*
P1may*P2may*P3may*P1june*         
P2june*P3june*P1july*P2july*           
P3july*P1aug*P2aug*P3aug*        
P1sept*P2sept*P3sept,
data = KemData)

summary(two.way)

也许还有另一种方法可以将这些变量放入 aov() 函数中?

数据样本:

> dput(head(KemData, 6))
structure(list(TypeP = structure(c(2L, 2L, 2L, 1L, 1L, 1L), .Label = c("Combined deep", 
"Deep moldboard"), class = "factor"), PreviousCulture = structure(c(1L, 
3L, 2L, 1L, 3L, 2L), .Label = c("Pure steam", "Sideral steam (melilot)", 
"Sideral steam (rapeseed)"), class = "factor"), `Yield t/ha` = c(1.53, 
1.33, 1.46, 0.5, 0.66, 0.58), T1may = c(9.55, 9.55, 9.55, 11.04, 
11.04, 11.04), T2may = c(5.92, 5.92, 5.92, 6.89, 6.89, 6.89), 
    T3may = c(9.26, 9.26, 9.26, 7.61, 7.61, 7.61), T1june = c(11.43, 
    11.43, 11.43, 8.02, 8.02, 8.02), T2june = c(16.37, 16.37, 
    16.37, 18.28, 18.28, 18.28), T3june = c(15.89, 15.89, 15.89, 
    22.34, 22.34, 22.34), T1july = c(16.01, 16.01, 16.01, 21.1, 
    21.1, 21.1), T2july = c(20.02, 20.02, 20.02, 20.85, 20.85, 
    20.85), T3july = c(19.02, 19.02, 19.02, 18, 18, 18), T1aug = c(18.57, 
    18.57, 18.57, 17.32, 17.32, 17.32), T2aug = c(16.53, 16.53, 
    16.53, 20.82, 20.82, 20.82), T3aug = c(15.36, 15.36, 15.36, 
    13.64, 13.64, 13.64), T1sept = c(12.46, 12.46, 12.46, 10.45, 
    10.45, 10.45), T2sept = c(6.89, 6.89, 6.89, 7.33, 7.33, 7.33
    ), T3sept = c(6.64, 6.64, 6.64, 5.98, 5.98, 5.98), P1may = c(1.69, 
    1.69, 1.69, 0.06, 0.06, 0.06), P2may = c(2.44, 2.44, 2.44, 
    2.8, 2.8, 2.8), P3may = c(2.04, 2.04, 2.04, 3.94, 3.94, 3.94
    ), P1june = c(1, 1, 1, 2.23, 2.23, 2.23), P2june = c(1.73, 
    1.73, 1.73, 0.87, 0.87, 0.87), P3june = c(1.34, 1.34, 1.34, 
    0.31, 0.31, 0.31), P1july = c(5.65, 5.65, 5.65, 0.44, 0.44, 
    0.44), P2july = c(0.18, 0.18, 0.18, 2.18, 2.18, 2.18), P3july = c(6.7, 
    6.7, 6.7, 3.57, 3.57, 3.57), P1aug = c(3.38, 3.38, 3.38, 
    0.62, 0.62, 0.62), P2aug = c(7.65, 7.65, 7.65, 1.26, 1.26, 
    1.26), P3aug = c(2.73, 2.73, 2.73, 4.5, 4.5, 4.5), P1sept = c(0.31, 
    0.31, 0.31, 1.44, 1.44, 1.44), P2sept = c(2.94, 2.94, 2.94, 
    3.13, 3.13, 3.13), P3sept = c(1.65, 1.65, 1.65, 0.64, 0.64, 
    0.64)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", 
"data.frame"))

TypeP    PreviousCulture    `Yield t/ha` T1may T2may T3may T1june T2june T3june T1july T2july T3july T1aug T2aug T3aug T1sept T2sept T3sept P1may P2may P3may P1june P2june P3june
  <fct>    <fct>                     <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>  <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>  <dbl>
1 Deep mo~ Pure steam                 1.53  9.55  5.92  9.26  11.4    16.4   15.9   16.0   20.0   19.0  18.6  16.5  15.4   12.5   6.89   6.64  1.69  2.44  2.04   1      1.73   1.34
2 Deep mo~ Sideral steam (ra~         1.33  9.55  5.92  9.26  11.4    16.4   15.9   16.0   20.0   19.0  18.6  16.5  15.4   12.5   6.89   6.64  1.69  2.44  2.04   1      1.73   1.34
3 Deep mo~ Sideral steam (me~         1.46  9.55  5.92  9.26  11.4    16.4   15.9   16.0   20.0   19.0  18.6  16.5  15.4   12.5   6.89   6.64  1.69  2.44  2.04   1      1.73   1.34
4 Combine~ Pure steam                 0.5  11.0   6.89  7.61   8.02   18.3   22.3   21.1   20.8   18    17.3  20.8  13.6   10.4   7.33   5.98  0.06  2.8   3.94   2.23   0.87   0.31
5 Combine~ Sideral steam (ra~         0.66 11.0   6.89  7.61   8.02   18.3   22.3   21.1   20.8   18    17.3  20.8  13.6   10.4   7.33   5.98  0.06  2.8   3.94   2.23   0.87   0.31
6 Combine~ Sideral steam (me~         0.58 11.0   6.89  7.61   8.02   18.3   22.3   21.1   20.8   18    17.3  20.8  13.6   10.4   7.33   5.98  0.06  2.8   3.94   2.23   0.87   0.31

在循环中使用 paste() 构建公式:

获取变量名,排除依赖变量​​:

var.names = colnames(KemData)
var.names = var.names[-which(var.names="Урожайность т/га")]

现在循环:

formula = "Урожайность т/га ~ "
for(i in var.names){
  formula = paste0(formula, "`", i, "`", " * ")}
formula = substr(formula, 1, nchar(formula)-3)