【问题标题】:Shapiro.test in R giving "all x values are identical"?R中的Shapiro.test给出“所有x值都是相同的”?
【发布时间】:2023-03-18 02:55:01
【问题描述】:

我一直致力于在我的数据框上获取 shapiro-wilkes 正态性假设检验 p 值表。这是comma-delimeted CSV的数据框(名为“mdf1”)。

R 中的 Shapiro-Wilkes 测试需要大于 3 的样本量。为了对我的数据框(其中包含两个相关因素,“变量”和“站点”)进行子集化,我使用了以下代码:

    Z <- as.data.frame(data.table(mdf1)[, list(freq=.N, value=value), by=list(Site,variable)][freq > 3])

这导致数据框“Z”包含属于 n 大于 3 的“站点”*“变量”组合的所有值。然后,我尝试将 Z 传递给ddply 函数以获得一个夏皮罗-威尔克斯 p 值表:

    norm2 <- ddply(Z, .(Site, variable), summarize, n=length(value), sw=shapiro.test(value)[2])

这个命令的结果是:

Error in shapiro.test(val) : all 'x' values are identical

怎么可能?有什么想法吗?

【问题讨论】:

  • 我不确定,但我想您是按变量拆分数据帧,然后对拆分后的变量应用测试。例如。对所有这些 c(1,1,....,1)c(2,2,....2) 等的测试。
  • 我知道这会是个问题,但是在ddply 包中的shapiro.test 命令中,我清楚地输入了“值”,而不是变量或因子。每个值都是不同的数值。
  • 这最后一条评论让我走错了路:all 'x' values are identical 消息清楚地表明提供给 shapiro.test 的所有值都是相同的。这可能不是本意,但它确实发生了。

标签: r plyr


【解决方案1】:

您的value 变量在这里是字符串。但是??shapiro.test(x)x 是数据值的数值向量...允许缺失值,但非缺失值的数量必须在3 到5000 之间。代码的前两行与我对您的earlier 问题的回答。

所以你可以使用下面的代码(经过测试):

mydata$inter<-with(mydata,interaction(Site,variable))
mydata1<-mydata[mydata$inter %in% names(which(table(mydata$inter) > 3)), ]  
library(plyr) 
ddply(mydata1, .(inter), summarize, n=length(value),sw=shapiro.test(as.numeric(value))[2])

                inter  n        sw
1  41332.Effluent (N) 18 0.6294289
2  41369.Effluent (N) 18 0.6294289
3  41385.Effluent (N) 10  0.969692
4  41394.Effluent (N) 12 0.5272433
5  41402.Effluent (N) 12 0.4404443
6  41436.Effluent (N) 14 0.6283259
7  41439.Effluent (N)  6  0.484449
8  41450.Effluent (N)  5 0.5012284
9  41452.Effluent (N) 14 0.5331113
10 41457.Effluent (N) 12 0.5272433
11 41458.Effluent (N) 12 0.5272433
12 43635.Effluent (N)  7 0.7437188
13 41332.Effluent (S) 13 0.5331956
14 41369.Effluent (S)  7 0.4869206
15 41379.Effluent (S)  6  0.484449
16 41385.Effluent (S)  7 0.4869206
17 41394.Effluent (S) 12 0.5272433
18 41436.Effluent (S) 14 0.6283259
19 41332.Influent (N) 18 0.6294289
20 41369.Influent (N) 18 0.6294289
21 41385.Influent (N) 10  0.969692
22 41394.Influent (N) 12 0.5272433
23 41402.Influent (N) 12 0.4404443
24 41436.Influent (N) 14 0.6283259
25 41439.Influent (N)  6  0.484449
26 41450.Influent (N)  5 0.5012284
27 41452.Influent (N) 14 0.5331113
28 41457.Influent (N) 12 0.5272433
29 41458.Influent (N) 12 0.5272433
30 43635.Influent (N)  7 0.7437188
31 41332.Influent (S) 13 0.5331956
32 41369.Influent (S)  7 0.4869206
33 41379.Influent (S)  6  0.484449
34 41385.Influent (S)  7 0.4869206
35 41394.Influent (S) 12 0.5272433
36 41402.Influent (S) 12 0.4404443
37 41436.Influent (S) 14 0.6283259
38 41452.Influent (S)  7 0.6578695
39 41457.Influent (S)  7 0.6578695
40 41458.Influent (S)  8 0.7159932
41         41332.PLot  6  0.484449
42         41369.PLot  6  0.484449
43         41379.PLot  6  0.484449
44         41385.PLot  7 0.4869206
45         41394.PLot 12 0.5272433
46         41402.PLot 12 0.4404443
47         41452.PLot  7 0.6578695
48         41457.PLot  7 0.6578695
49         41458.PLot  8 0.7159932

【讨论】:

  • 假设我已经在Z$value 上运行了as.numeric 函数。 shapiro.test 怎么还能输出那个错误?此外,shapiro-wilkes 测试不应该在样本的“n”值上运行,而是在样本值本身上运行,所以我对你的 shapiro.test(n)[2] 语法有点困惑。
  • 是的,你是对的,但是你需要在我现在更新的代码中实现你的假设。它有效。
  • 感谢您的建议。我只是再次运行它并收到相同的错误消息。这是我剪切和粘贴的代码:norm2 &lt;- ddply(Z, .(Site, variable), summarize, n=length(value), sw=shapiro.test(as.numeric(value))[2])
  • 请检查我更新的完整代码。现在应该没问题了。如果没有,请尝试重新启动 R
  • 哇,逐行将其应用到 R-studio,即使重新启动,我仍然收到相同的错误消息。有趣的是,当我使用 ddply 和 shapiro-wilkes、andersen-darling 和 lilliefors 测试(在原始、对数和平方根转换数据上)时,它可以完美运行:codeprinted dataframe。因此,您的示例应该对我有用,因为您已将因式分解减少为一个变量(“inter”)。好奇怪……
【解决方案2】:

错误消息听起来很简单。通常,如果列/变量中的所有值都相同,则意味着您肯定会得到错误。我建议您首先检查并删除方差为零的变量。这对我有用:

# Load dplyr
library(dplyr) # Data manipulation
library(caret) # nearZeroVar function

# Load your data and assign it the name, df
# *code to load data goes here*

# Etract names for numeric columns
numeric_vars<-df%>%select_if(is.numeric)%>%names()

# Extract zero variance variables first
zero_var_columns<-nearZeroVar(df, saveMetrics = TRUE)%>%
filter(zeroVar==TRUE)%>%
row.names()

# Show
zero_var_columns

# Drop zero variance columns
df<-df%>%
select(-zero_var_columns)

# Now test for normality
df%>%
select_if(is.numeric)%>%
sapply(shapiro.test)%>%
t()%>%
data.frame()%>%
select(p.value)%>%
mutate(Is_normally_distributed=p.value>=.05)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 1970-01-01
    • 2022-06-15
    • 2018-06-06
    • 1970-01-01
    • 2019-12-17
    • 1970-01-01
    相关资源
    最近更新 更多