Mr.Rlover
Mr.Rlover

Reputation: 2613

Extracting statiscal values from a list with multiple lists of results of statistical test

I did a Ljung-Box Test for independence in r with 36 lags and stored the results in a list.

for (lag in c(1:36)){
box.test.list[[lag]] <- (Box.test(btcr, type = "Ljung", lag))
}

I want to extract the p-values as well as the test statistic (X-squared) and print them out to look something like:

X-squared = 100, p-value = 0.0001

I also want to pull it out p-value indivually but rather than just spit out numbers, I want something like:

[1] p-value = 0.001

[2] p-value = 0.0001

and so on. Can this be done?

Upvotes: 2

Views: 439

Answers (1)

MrFlick
MrFlick

Reputation: 206197

With the test data

set.seed(7)
btcr <- rnorm(100)

you can perform all your tests with

box.test.list <- lapply(1:36, function(i) Box.test(btcr, type = "Ljung", i))

and then put all the results in a data.frame with

results <- data.frame(
  lag = 1:36, 
  xsquared = sapply(box.test.list, "[[", "statistic"), 
  pvalue = sapply(box.test.list, "[[", "p.value")
)

Then you can do what you like with the results

head(results)
#   lag  xsquared     pvalue
# 1   1  3.659102 0.05576369
# 2   2  7.868083 0.01956444
# 3   3  8.822760 0.03174261
# 4   4  9.654935 0.04665920
# 5   5 11.190969 0.04772238
# 6   6 12.607454 0.04971085

Upvotes: 2

Related Questions