对R中的多个变量进行Shapiro-Wilk正态性检验的循环 [英] Loop for Shapiro-Wilk normality test for multiple variables in R

查看:116
本文介绍了对R中的多个变量进行Shapiro-Wilk正态性检验的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为"My_data"的数据集,以及三个名为a,b,c的变量.我的数据头是这样的:

I have a dataset called "My_data", and three variables called a, b, c. The head of my data is like this:

> head(My_data)
  variable_A variable_B     value
1  Jul       W1 18.780294
2  Jul       W2 13.932397
3  Aug       W2 20.877093
4  Sep       W3  9.291295
5  May       W1 10.939570
6  Oct       W1 12.23671

我想对具有两个变量的每个子集进行Shapiro正态性检验.

I want to do Shapiro normality test for each subset with two variables.

> Subset1=subset(My_data, variable_A== "Jan" & variable == "W1")
> Subset2=subset(My_data, variable_A== "Feb" & variable == "W1")
> Subset3=subset(My_data, variable_A== "Mar" & variable == "W1")
.
.
> Subset_n=subset(My_data, variable_A== "Jan" & variable == "W2")
> 

Subset_n2 = subset(My_data,variable_A =="Jan"& variable =="W3")

Subset_n2=subset(My_data, variable_A== "Jan" & variable == "W3")

您看到我需要制作很多子集,并对每个子集进行Shapiro.

You see that I need to make a lot of subsets and do Shapiro for each one.

但是,如果我可以循环播放,它将使我的工作更加轻松.

But if I can loop it, it makes my job easier.

我有此代码可用于

> loop_Shapiro = list()
> for (ids in unique(My_data$variable_A)){
+   My_sub = subset(x=My_data, subset=variable_A==ids)
+   
+   loop_Shapiro[[ids]] = shapiro.test(My_sub$value)
+ }

此循环有效,但问题是它仅基于带有一个变量的分租,而我想要两个.

This loop works, but the problem is that it is only based subletting with one variable, but I want for two.

推荐答案

首先,让我们创建一个示例数据框.

First, let's create an example data frame.

# Create example data frame
set.seed(18800)

My_data <- data.frame(
  variable_A = rep(month.abb, each = 30),
  variable_B = rep(paste0("W", 1:3), times = 120),
  value = rnorm(360)
)

我们可以使用 split 拆分数据帧,而无需使用 subset .结果是一个列表.

We can split the data frame using split without the use of subset. The result is a list.

# Split the data frame
My_list <- split(My_data, f = list(My_data$variable_A, My_data$variable_B))

此后,我们可以按如下方式使用 for-loop .

After that, we can use for-loop as follows.

loop_Shapiro <- list()

for (name in names(My_list)){
  My_sub <- My_list[[name]]
  loop_Shapiro[[name]] <- shapiro.test(My_sub$value)
}

# Check the results of the first shapiro test
loop_Shapiro[1]
# $Apr.W1
# 
# Shapiro-Wilk normality test
# 
# data:  My_sub$value
# W = 0.89219, p-value = 0.1794

我们还可以考虑在拆分后使用 lapply 函数.结果是一个列表.

We can also consider using the lapply function after the split. The result is a list.

# Use lapply
loop_Shapiro2 <- lapply(My_list, function(x) shapiro.test(x$value))

loop_Shapiro2[1]
# $Apr.W1
# 
# Shapiro-Wilk normality test
# 
# data:  x$value
# W = 0.89219, p-value = 0.1794

这篇关于对R中的多个变量进行Shapiro-Wilk正态性检验的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆