1
require(MTS)
rt=rnorm(200)
b<-archTest(rt, lag = 10)

此代码导致值

Q(m) of squared series(LM test):  
Test statistic:  7.694531  p-value:  0.6586466 
Rank-based Test:  
Test statistic:  20.80503  p-value:  0.02249487

typeof(b)但是当使用and检查 b 的结构时str(b),它给出NULL.

这怎么可能?如何知道b从该变量中提取值所需的结构。

4

1 回答 1

3

如果您查看 的源代码archTest,您的问题或多或少归结为:我可以停止cat返回NULL吗?答案是否定的。

但是,您可以进行修改archTest,使其返回测试统计数据和 p 值(见下文)。b是一个命名向量。

b <- my_archTest(rt, lag = 10)
str(b)
# Named num [1:2] 7.565 0.671
# - attr(*, "names")= chr [1:2] "Test statistic" "p-value"
b
#Test statistic        p-value 
#     7.5653283      0.6712114
b["Test statistic"]
#Test statistic 
#      7.565328

我在末尾添加了三行MTS::archTest

my_archTest <- function (rt, lag = 10) {
  at = rt
  if (is.matrix(at)) 
    at = at[, 1]
  m1 = acf(at^2, lag.max = lag, plot = F)
  acf = m1$acf[2:(lag + 1)]
  nT = length(at)
  c1 = c(1:lag)
  deno = rep(nT, lag) - c1
  Q = sum(acf^2/deno) * nT * (nT + 2)
  pv1 = 1 - pchisq(Q, lag)
  cat("Q(m) of squared series(LM test): ", "\n")
  cat("Test statistic: ", Q, " p-value: ", pv1, "\n")
  rk = rank(at^2)
  m2 = acf(rk, lag.max = lag, plot = F)
  acf = m2$acf[2:(lag + 1)]
  mu = -(rep(nT, lag) - c(1:lag))/(nT * (nT - 1))
  v1 = rep(5 * nT^4, lag) - (5 * c(1:lag) + 9) * nT^3 + 9 * 
    (c(1:lag) - 2) * nT^2 + 2 * c(1:lag) * (5 * c(1:lag) + 
                                              8) * nT + 16 * c(1:lag)^2
  v1 = v1/(5 * (nT - 1)^2 * nT^2 * (nT + 1))
  QR = sum((acf - mu)^2/v1)
  pv2 = 1 - pchisq(QR, lag)
  cat("Rank-based Test: ", "\n")
  cat("Test statistic: ", QR, " p-value: ", pv2, "\n")

  out <- c("Test statistic" = QR, # new lines added here
           "p-value" = pv2)
  return(out)

# or instead simply
# c("Test statistic" = QR,
#   "p-value" = pv2)
}

正如@42- 在评论中指出的那样,return调用是不必要的。

于 2018-09-16T06:33:05.140 回答