Search code examples
rfunctionpanelautoregressive-models

Extracting ARIMA coefficients for use in custom function


I want to run a customized function based on ARIMA model. The function calls the ma3 coefficient from ARIMA (2, 0, 3) model ran on daily data of a year and subtracts ma3 coefficient from 2, for every firm. I have 5 years daily data for five firms, so each firm should have 5 year-wise values. My code:

>Stressy =function(x) 2-summary(arima(x, order=c(2,0,3)))$coefficients[1, "ma3"] 
>Funny = aggregate(cbind(QQ) ~  Year + Firm , df, FUN = Stressy)

Running my code gives the following error:

Error in summary(arima(x, order = c(2, 0, 3)))$coefficients : $ operator is invalid for atomic vectors 

I know the result can be estimated manually but my data set is large enough to be confusing when handled manually. Please suggest an edit to fix this.


Solution

  • There are two ways you could get the ma3 coefficient:

    Stressy <- function(x) 2-coef(arima(x, order=c(2,0,3)))["ma3"] 
    

    or

    Stressy <- function(x) 2-arima(x, order=c(2,0,3))$coef["ma3"] 
    

    Your original custom function didn't work because summary(arima_object) gives you a table, to which you cannot apply the $ operator:

    x <- arima(df, c(2,0,3))
    class(summary(x))
    [1] "summaryDefault" "table"