Reputation: 63
I want to perform a Jarque-Bera Test with the tseries
package on a data.frame
with about 200 columns but it doesn't work with NA values.
My data.frame
looks like this:
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")
Output:
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
I tried:
library(tseries)
JB <- lapply(2:6, function(i) jarque.bera.test(d1[,i]))
but this gives me following error message:
Error in jarque.bera.test(d1[, i]) : NAs in x
Also JB <- lapply(2:6, function(i) jarque.bera.test(d1[,i], na.rm=TRUE))
did not work.
The NA's are only at the beginning of the time series. I'm therefore looking for way to ignore the NA's at the beginning of the time series.
Thank you!
Upvotes: 3
Views: 1815
Reputation: 9656
Here matrixTests
implementation of Jarque-Berra would also be a good fit:
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
Upvotes: 0
Reputation: 1833
Here is one possible solution using the R tseries library:
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,]))
Check the JB results are reasonable.
Upvotes: 0
Reputation: 736
You could try using this version of the Jarque Bera test from the DescTools, which allow for removal of NA (it is a merge of the jarque.bera.test from the tseries packages).
JarqueBeraTest(x, robust = TRUE, method = c("chisq", "mc"), N = 0, na.rm = FALSE)
In your case:
lapply(2:6, function(i) DescTools::JarqueBeraTest(x = d1[,i], method="chisq", na.rm=TRUE))
Upvotes: 2