假设我想用Shapiro检验从给定的数据集评估几个变量的正态性。
在示例数据中,我还想按物种分组。
data(iris)
library(dplyr)
library(purrr)
iris %>%
select(Sepal.Length, Sepal.Width)%>%
group_by(iris$Species)%>%
lapply(. , shapiro.test)这给了我一个错误:
Error in FUN(X[[i]], ...) : is.numeric(x) is not TRUE我猜这个错误的发生是因为lapply将三个变量计算为对象来应用测试,而不是按物种分组,而且由于物种不是数字的,所以无法计算它。
任何帮助都会很感激的。
发布于 2020-11-14 01:22:45
您可以在这里使用dplyr函数进行计算。
将shapiro.test应用于Sepal.Length和Sepal.Width中的每个
library(dplyr)
iris %>%
select(Sepal.Length, Sepal.Width, Species) %>%
group_by(Species) %>%
summarise(across(.fns = ~list(shapiro.test(.)))) -> result
result
# Species Sepal.Length Sepal.Width
# <fct> <list> <list>
#1 setosa <htest> <htest>
#2 versicolor <htest> <htest>
#3 virginica <htest> <htest> 要获得p值,您可以这样做:
iris %>%
select(Sepal.Length, Sepal.Width, Species) %>%
group_by(Species) %>%
summarise(across(.fns = ~shapiro.test(.)$p.value)) -> result
result
# Species Sepal.Length Sepal.Width
# <fct> <dbl> <dbl>
#1 setosa 0.460 0.272
#2 versicolor 0.465 0.338
#3 virginica 0.258 0.181https://stackoverflow.com/questions/64829961
复制相似问题