用R中类似的对象元素制作一张漂亮的桌子?

时间:2019-05-14 03:53:34

标签: r

不确定之前是否确实有人问过这个问题,但是我对许多不同的模型进行了一系列系数测试:

coeftest(arima(detrend, order = c(0,0,2))) #MA2
coeftest(arima(detrend, order = c(0,0,3))) #MA3
coeftest(arima(detrend, order = c(0,0,4))) #MA4
coeftest(arima(detrend, order = c(1,0,0))) #AR1
coeftest(arima(detrend, order = c(2,0,0))) #AR2
coeftest(arima(detrend, order = c(1,0,1))) #ARMA(1,1)
coeftest(arima(detrend, order = c(2,0,1))) #ARMA(2,1) 
coeftest(arima(detrend, order = c(2,0,1))) #ARMA(1,2) 

如果我将这些测试存储在对象中,并将所得的时间序列估计值与p值一起存储,该如何创建一个可以在其中具有模型/对象名称的表格?像这样:

Model  AR Terms MA Terms P-value
MA1      NA       1       0.006
AR2      1        NA      0.0042
         2        NA      0.0020
ARMA11   1        0       0.0031
         0        1       0.0005

任何arima模型都可以作为样本,因此我将提供以下ar2流程:


phi.1 <- 1.2
phi.2 <- -0.6
sigma.e <- 9
Y <- rnorm(2, 0, sigma.e)
n <- 200

for(t in 3:(n+2)){
    new <- phi.1*(Y[t-1]) + phi.2*(Y[t-2]) + rnorm(1, 0, sigma.e)
    Y <- c(Y, new)
}

ar2 <- as.ts(Y)

1 个答案:

答案 0 :(得分:0)

您能否进一步说明所需的输出?这些模型具有多个AR项和多个MA项,从而产生许多P值。

更新:像这样吗?

library(tidyverse)

test <- list(
  "MA2" = coeftest(arima(ar2 , order = c(0,0,2))),
  "MA3" = coeftest(arima(ar2 , order = c(0,0,3))), 
  "MA4" = coeftest(arima(ar2 , order = c(0,0,4))), 
  "AR1" = coeftest(arima(ar2 , order = c(1,0,0))), 
  "AR2" = coeftest(arima(ar2 , order = c(2,0,0))), 
  "ARMA(1,1)" = coeftest(arima(ar2 , order = c(1,0,1))),
  "ARMA(2,1)" = coeftest(arima(ar2 , order = c(2,0,1))), 
  "ARMA(1,2)" = coeftest(arima(ar2 , order = c(2,0,1))))

map_df(test, tidy, .id = "model") %>%
  select(-std.error, -statistic) %>%
  complete(model, term, fill = list(NA)) %>%
  nest(estimate, p.value, .key = 'stats') %>%
  spread(key = term, value = stats) %>%
  unnest(.sep = '_') -> res


# A tibble: 8 x 15
  model ar1_estimate ar1_p.value ar2_estimate ar2_p.value intercept_estim~
  <chr>        <dbl>       <dbl>        <dbl>       <dbl>            <dbl>
1 AR1          0.750   8.92e- 60       NA       NA                   1.04 
2 AR2          1.29    1.54e-152       -0.714    2.28e-48            1.00 
3 ARMA~        0.625   1.03e- 26       NA       NA                   1.05 
4 ARMA~        1.30    5.92e- 77       -0.722    3.33e-32            1.00 
5 ARMA~        1.30    5.92e- 77       -0.722    3.33e-32            1.00 
6 MA2         NA      NA               NA       NA                   0.982
7 MA3         NA      NA               NA       NA                   1.00 
8 MA4         NA      NA               NA       NA                   1.07 
# ... with 9 more variables: intercept_p.value <dbl>, ma1_estimate <dbl>,
#   ma1_p.value <dbl>, ma2_estimate <dbl>, ma2_p.value <dbl>, ma3_estimate <dbl>,
#   ma3_p.value <dbl>, ma4_estimate <dbl>, ma4_p.value <dbl>

您想从这里去哪里?