从具有多个统计测试结果列表的列表中提取统计值

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

我在 r 中进行了 Ljung-Box 独立性测试,有 36 个滞后并将结果存储在列表中。

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

我想提取 p 值以及检验统计量(X 平方)并将它们打印出来,如下所示:

X 平方 = 100,p 值 = 0.0001

我也想单独提取 p 值,但不只是吐出数字,我想要这样的东西:

[1] p 值 = 0.001

[2] p 值 = 0.0001

等等。这能做到吗?

有测试数据

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

您可以使用

执行所有测试
box.test.list <- lapply(1:36, function(i) Box.test(btcr, type = "Ljung", i))

然后把所有的结果放在一个data.frame和

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

然后你可以对结果做你想做的事

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