簡體   English   中英

使用 NA 的 Jarque Bera 測試

[英]Jarque Bera Test with NA's

我希望與執行Jarque-Bera檢驗tseries包上data.frame約200列,但它不與NA值的工作。

我的data.frame看起來像這樣:

d1 <- structure(list(Time=structure(17942:17947, class="Date"),
                      x1=c(NA, NA, 17L, 29L, 27L, 10L), 
                      x2=c(30L, 19L, 22L, 20L, 11L, 24L), 
                      x3=c(NA, 23L, 22L, 27L, 21L, 26L), 
                      x4=c(30L, 28L, 23L, 24L, 10L, 17L), 
                      x5=c(12L, 18L, 17L, 16L, 30L, 26L)),
                      row.names=c(NA, 6L), class="data.frame")

輸出:

Time x1 x2 x3 x4 x5
1 2019-02-15 NA 30 NA 30 12
2 2019-02-16 NA 19 23 28 18
3 2019-02-17 17 22 22 23 17
4 2019-02-18 29 20 27 24 16
5 2019-02-19 27 11 21 10 30
6 2019-02-20 10 24 26 17 26

我試過:

library(tseries)
JB <- lapply(2:6, function(i) jarque.bera.test(d1[,i]))

但這給了我以下錯誤消息:

jarque.bera.test(d1[, i]) 中的錯誤:x 中的 NA

JB <- lapply(2:6, function(i) jarque.bera.test(d1[,i], na.rm=TRUE))也不起作用。

NA 僅位於時間序列的開頭。 因此,我正在尋找在時間序列開始時忽略 NA 的方法。

謝謝!

您可以嘗試使用 DescTools 中的 Jarque Bera 測試版本,它允許刪除 NA(它是 tseries 包中 jarque.bera.test 的合並)。

JarqueBeraTest(x, robust = TRUE, method = c("chisq", "mc"), N = 0, na.rm = FALSE)

在你的情況下:

lapply(2:6, function(i) DescTools::JarqueBeraTest(x = d1[,i], method="chisq", na.rm=TRUE))

這是使用 R tseries 庫的一種可能的解決方案:

library(tseries)

# remove NAs
d1.cc <- d1[complete.cases(d1),]

# form time series
d1.cc.ts <- ts(d1.cc)

# run jarque.bera.test
JB <- lapply(1:nrow(d1.cc), function(i) jarque.bera.test(d1.cc.ts[i,]))

檢查JB結果是否合理。

Jarque-Berra 的matrixTests實現也很適合:

col_jarquebera(d1[,-1])

   obs   skewness kurtosis df  statistic    pvalue
x1   4 -0.2686809 1.406646  2 0.47125566 0.7900747
x2   6 -0.2249531 2.602041  2 0.09019679 0.9559034
x3   5  0.2436883 1.396859  2 0.58491604 0.7464266
x4   6 -0.6027177 2.139432  2 0.54841298 0.7601751
x5   6  0.5090893 1.854664  2 0.58712051 0.7456043

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM