[英]repeated two-way ANOVA on multiple columns sorted per factor level in r
我希望看到period
和site
之间的 10 个环境变量( height
、 iwdo
、 rdos
等。直到no2
)中的每一个的双向方差分析。 这在stream
中分组的三个不同的独立分水岭中。
对于每个stream
我需要检查 shapiro.test 的正态性和shapiro.test
的同方差leveneTest
。 在我运行 model aov(nest_database[nest_database=="stream name (ie smeltaite)",]environmental variable (ieiwdo)~period*site)
。
那么,是否有一个公式可以使三个stream
的此类过程自动化,同时在环境变量的每一列上重现,分别为我提供shapiro.test
、 leveneTest
和aov
结果的摘要?
在我的数据集head
下方
nest_data<-structure(list(stream = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label =
c("blendziava",
"smeltaite", "sventoji"), class = "factor"), period = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("February", "March", "April",
"May"), class = c("ordered", "factor")), site = structure(c(1L,
2L, 1L, 2L, 1L, 2L), .Label = c("N", "NN"), class = "factor"),
stake = c("A", "A", "B", "B", "C", "C"), class = c("low",
"medium", "low", "low", "low", "high"), height = c(0, 10,
0, 3.5, 0, 15), iwdo = c(13, 8.37, 10.8, 3.3, 11, 5.3), rdos = c(89.041095890411,
57.3287671232877, 73.972602739726, 22.6027397260274, 75.3424657534247,
36.3013698630137), iwc = c(359, 375, 357, 340, 360, 357),
dwc = c(2, 14, 4, 21, 1, 4), iwt = c(2.2, 2.1, 2.3, 2.3,
2.6, 2.3), dt = c(0, 0.1, 0.0999999999999996, 0.0999999999999996,
0.4, 0.0999999999999996), no3 = c(0.8104551, 0.6300294, 1.1296698,
1.2962166, 0.963123, 1.240701), nh4 = c(0.2187052, 0.1457344,
0.186718, 0.2177056, 0.2297008, 0.2187052), no2 = c(0.0133336,
0.0100408, 0.0116872, 0.0083944, 0.0127848, 0.009492)), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
到目前为止,我正在使用代码:
nest_data %>%
split(.$stream) %>%
purrr::map(.,function(x){
aov(iwdo ~ period*site, data = x) %>%
tidy(.)
}) -> results
df <- as.data.frame(do.call(rbind,results))
这使我可以对三个stream
进行测试,但只能在一列上进行。 我认为我应该使用for
循环,但不确定在 function 内放置的位置
在此先感谢,希望我很清楚,因为这是我在这里的第一个问题!
`
考虑在定义的方法中概括所有步骤。 然后迭代地调用方法,其中by
和sapply
的基础 R 方法可以提供帮助。 使用reformulate
调整公式。 请填写每个省略号 ( ...
)。
env_vars <- c("height", "iwdo", "rdos", ..., "no2")
proc_model <- function(sub_df) {
# NAMED LIST OF ENVIRONMENT VARS MODEL AND TESTS
sapply(env_vars, function(env) {
model <- aov(reformulate("period*site", env), data = sub_df)
sp <- shapiro.test(...)
lv <- leveneTest(...)
# NAMED LIST OF MODEL AND TESTS
list(
aov_result = model, shapiro_test = sp, levene_test = lv
)
})
}
# NESTED NAMED LIST BY STREAM FOR EACH ENV VAR
results_list <- by(nest_data, nest_data$stream, proc_model)
要访问结果:
results_list$smeltaite$height$aov_result
results_list$smeltaite$height$shapiro_test
results_list$smeltaite$height$levene_test
对于您的原始实现:
results <- nest_data %>%
split(.$stream) %>%
purrr::map(proc_model)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.