R:一个缺失值会破坏我的Jarque-Bera测试

fnatzsnv  于 2022-12-30  发布在  其他
关注(0)|答案(2)|浏览(140)

这段代码没有省略N/As并正确执行Jarque-Bera测试。什么代码可以呢?谢谢你的建议。

library(moments)
jarque.test(mydata$item1, na.rm = TRUE)

我的数据

structure(list(item1 = c(6, 7, NA, 7, 6, 2, 7, 3, 4, 7, 4, 5, 
4, 7, 7, 6, 5, 7, 6, 7, 4, 7, 5, 6, 5, 4, 7, 5, 4, 6, 7, 5, 5, 
7, 7, 5, 7, 7, 7, 4, 5, 7, 7, 7, 5, 7, 6, 7, 7, 5), item2 = c(6, 
7, 6, 6, 6, 3, 7, 3, 3, 4, 5, 6, 4, 7, 6, 6, 4, 6, 6, 7, 6, 3, 
5, 5, 3, 2, 7, 5, 6, 6, 7, 3, 5, 7, 6, 5, 6, 5, 6, 4, 6, 7, 7, 
7, 7, 7, 6, 7, 4, 7)), row.names = c(NA, -50L), class = c("tbl_df", 
"tbl", "data.frame"))
2w3kk1z5

2w3kk1z51#

正如注解所述,由于moments::jarque.test中没有na.rm参数,并且它只接受一个参数,因此一种解决方法是只将您的数据子集化,排除NA

moments::jarque.test(mydata$item1[!is.na(mydata$item1)])

#  Jarque-Bera Normality Test
# 
# data:  mydata$item1[!is.na(mydata$item1)]
# JB = 4.9442, p-value = 0.08441
# alternative hypothesis: greater
axr492tv

axr492tv2#

我们可以使用JarqueBeraTest的形式DescTools,与其他不同的是,它有一个na.rm参数:

library(DescTools)
JarqueBeraTest(df$item1, robust = TRUE, na.rm = TRUE)
data:  structure(c(6, 7, 7, 6, 2, 7, 3, 4, 7, 4, 5, 4, 7, 7, 6, 5, 7, 6, 7, 4, 7, 5, 6, 5, 4, 7, 5, 4, 6, 7, 5, 5, 7, 7, 5, 7, 7, 7, 4, 5, 7, 7, 7, 5, 7, 6, 7, 7, 5), na.action = structure(3L, class = "omit"))
X-squared = 3.9623, df = 2, p-value = 0.1379

相关问题