我在一栏中有两个不同的 类。如何测试他们每个人的正常性?
I have two different classes in one column. How to test normality of each of them?
R 新手。
考虑到我的情况是这样的:(其实我的真实情况要复杂得多)
set.seed(100)
df = data.frame(SEX=sample(c("M","F"),100,replace=TRUE),BW = rnorm(100,80,2))
一栏是SEX(男女),另一栏是BW(体重)。
我想测试男性的体重正常值和女性的体重正常值。然后我可以分别测试方差相等性。最后对这种情况进行T检验或其他检验。
但是 shapiro.test
不能在这种情况下使用。 (喜欢shapiro.test(BW~SEX,data=df)
)
我该怎么办?我不想分离数据框或创建新的子集。
提前致谢~!
此处详细描述了此问题的 "tidyverse" 解决方案:Running a model on separate groups。
简单地说,使用您的数据:
library(dplyr) # for mutate
library(tidyr) # for nest/unnest
library(purrr) # for map
library(broom) # for glance
df %>%
nest(data = c(BW)) %>%
mutate(model = map(data, ~ shapiro.test(.x$BW)),
g = map(model, glance)) %>%
unnest(g)
结果:
# A tibble: 2 x 6
SEX data model statistic p.value method
<fct> <list<df[,1]>> <list> <dbl> <dbl> <chr>
1 F [50 x 1] <htest> 0.982 0.639 Shapiro-Wilk normality test
2 M [50 x 1] <htest> 0.980 0.535 Shapiro-Wilk normality test
哦,我自己想出来的...
使用此代码
with(df, shapiro.test(BW[SEX == "M"]))
with(df, shapiro.test(BW[SEX == "F"]))
很高兴我能学到更多!
R 新手。 考虑到我的情况是这样的:(其实我的真实情况要复杂得多)
set.seed(100)
df = data.frame(SEX=sample(c("M","F"),100,replace=TRUE),BW = rnorm(100,80,2))
一栏是SEX(男女),另一栏是BW(体重)。
我想测试男性的体重正常值和女性的体重正常值。然后我可以分别测试方差相等性。最后对这种情况进行T检验或其他检验。
但是 shapiro.test
不能在这种情况下使用。 (喜欢shapiro.test(BW~SEX,data=df)
)
我该怎么办?我不想分离数据框或创建新的子集。
提前致谢~!
此处详细描述了此问题的 "tidyverse" 解决方案:Running a model on separate groups。
简单地说,使用您的数据:
library(dplyr) # for mutate
library(tidyr) # for nest/unnest
library(purrr) # for map
library(broom) # for glance
df %>%
nest(data = c(BW)) %>%
mutate(model = map(data, ~ shapiro.test(.x$BW)),
g = map(model, glance)) %>%
unnest(g)
结果:
# A tibble: 2 x 6
SEX data model statistic p.value method
<fct> <list<df[,1]>> <list> <dbl> <dbl> <chr>
1 F [50 x 1] <htest> 0.982 0.639 Shapiro-Wilk normality test
2 M [50 x 1] <htest> 0.980 0.535 Shapiro-Wilk normality test
哦,我自己想出来的... 使用此代码
with(df, shapiro.test(BW[SEX == "M"]))
with(df, shapiro.test(BW[SEX == "F"]))
很高兴我能学到更多!