Stackoverflowuser
Stackoverflowuser

Reputation: 97

Extracting coefficients and intercepts of ARIMA model using R

I want to collect the coefficient of the following ARIMA modelling and save in external folder, any assistance would be appreciated.

    set.seed(123)
## ARMA(2,2) description for arim.sim()
ARMA22 <- list(order = c(2, 0, 2), ar = c(-0.7, 0.2), ma = c(0.7,
                                                             0.2))
## mean of process
mu <- 5
## simulated process (+ mean)
ARMA.sim <- arima.sim(n = 10000, model = ARMA22) + mu
## estimate parameters
arima(x = ARMA.sim, order = c(2, 0, 2))

## empty list to store model fits
ARMA.res <- list()
## set counter
cc <- 1
## loop over AR
for (p in 0:3) {
  ## loop over MA
  for (q in 0:3) {
    ARMA.res[[cc]] <- arima(x = ARMA.sim, order = c(p, 0, q))
    cc <- cc + 1
  }
}
ARMA.res

output:

     [[1]]
    
    Call:
    arima(x = ARMA.sim, order = c(p, 0, q))
    
    Coefficients:
          intercept
             4.9975
    s.e.     0.0132
    
    sigma^2 estimated as 1.739:  log likelihood = -16955.58,  aic = 33915.15
    
    [[2]]
    
    Call:
    arima(x = ARMA.sim, order = c(p, 0, q))
    
    Coefficients:
              ma1  intercept
          -0.2106     4.9975
    s.e.   0.0073     0.0100
    
    sigma^2 estimated as 1.602:  log likelihood = -16546.2,  aic = 33098.4
    
    [[3]]
    
    Call:
    arima(x = ARMA.sim, order = c(p, 0, q))
    
    Coefficients:
             ma1     ma2  intercept
          -0.101  0.4002     4.9975
    s.e.   0.011  0.0084     0.0149
    
    sigma^2 estimated as 1.321:  log likelihood = -15581.31,  aic = 31170.62

''''
  
    [[9]]
    
    '''
    [[16]]

I want to collect the coefficients and the intercept in the following format and save in the external folder:

   intercept ar1  ar2  ma1    ma2 
    4.995   NA   NA  NA      NA
    4.997   NA   NA  -0.2106 NA
    .............................
    ..............................

When I use the do.call, it won't give me. Any assistance, please?

Upvotes: 1

Views: 1379

Answers (1)

duckmayr
duckmayr

Reputation: 16940

You can use a combination of dplyr::bind_rows() and coef():

dplyr::bind_rows(lapply(ARMA.res, coef))
# A tibble: 16 x 7
   intercept    ma1    ma2     ma3     ar1    ar2     ar3
       <dbl>  <dbl>  <dbl>   <dbl>   <dbl>  <dbl>   <dbl>
 1      5.00 NA     NA     NA      NA      NA     NA     
 2      5.00 -0.211 NA     NA      NA      NA     NA     
 3      5.00 -0.101  0.400 NA      NA      NA     NA     
 4      5.00 -0.104  0.369 -0.262  NA      NA     NA     
 5      5.00 NA     NA     NA      -0.388  NA     NA     
 6      5.00  0.726 NA     NA      -0.953  NA     NA     
 7      5.00  0.867  0.332 NA      -0.903  NA     NA     
 8      5.00  0.904  0.386  0.0723 -0.921  NA     NA     
 9      5.00 NA     NA     NA      -0.191   0.507 NA     
10      5.00  0.482 NA     NA      -0.514   0.392 NA     
11      5.00  0.691  0.200 NA      -0.708   0.192 NA     
12      5.00  0.766  0.266  0.0265 -0.783   0.125 NA     
13      5.00 NA     NA     NA      -0.0477  0.453 -0.283 
14      5.00  0.315 NA     NA      -0.333   0.398 -0.140 
15      5.00  0.660  0.186 NA      -0.677   0.206 -0.0136
16      5.00  0.182 -0.152 -0.102  -0.199   0.553 -0.0972

Upvotes: 1

Related Questions