Time Series in R

Discussion in 'CS2' started by GeorgeFS, Feb 9, 2022.

  1. GeorgeFS

    GeorgeFS Keen member

    I am trying Q13-14.10 from the PBOR, and I'm getting an error when trying to run the arima() function. The error is as follows:

    Error in arima(Xt, order = c(p, d, q)) :
    only implemented for univariate time series

    I think this is R potentially saying it doesn't recognise Xt as a time series, I have tried using as.ts(Xt) but I'm still getting the error when trying to run.

     
  2. Andrew Martin

    Andrew Martin ActEd Tutor Staff Member

    Hello

    Could you please copy over all of your code? I'll then take a look.

    Thanks

    Andy
     
  3. GeorgeFS

    GeorgeFS Keen member

    #Q13-14.10#

    Xt <- read.table("Xt.csv", sep = ",", header = TRUE)
    Xt <- ts(Xt, 1990, frequency = 12)


    #(i)

    answer <- numeric(4)
    for (p in 0:2) for (d in 0:2) for (q in 0:2){
    aic <- arima(Xt, order = c(p,d,q))$aic
    row <- c(p,d,q,aic)
    answer <- rbind(answer, row)

    }

    The text in red is copied from the question and it uses Xt.csv from the PBOR.
     
  4. Andrew Martin

    Andrew Martin ActEd Tutor Staff Member

    Your code runs fine for me. Could you try running it in a fresh R session and see if you get the same error?

    What version of R are you using?

    Andy
     
  5. GeorgeFS

    GeorgeFS Keen member

    Hmm, that's strange.

    I am using R version 4.0.3

    George
     
  6. Andrew Martin

    Andrew Martin ActEd Tutor Staff Member

    What happens if you run:

    set.seed(4)
    Xt = arima.sim(100, model = list(ar = 0.2, ma = 0.1))
    arima(Xt)
     
  7. GeorgeFS

    GeorgeFS Keen member

    That works and gives me results - they aren't the same as the results in the solutions - but for the purpose of the exercise and setting up the loop I think I'm ok with that.

    Thanks for your help!!
     
  8. Andrew Martin

    Andrew Martin ActEd Tutor Staff Member

    No problem. Yes you're right, I should have been clearer about that. The code wasn't to recreate the same time series, just to check the arima() function is working for you with another time series.

    Strange that this works but you can't get it to work with the data in the file. What output do you get if you run:

    Xt <- read.table("Xt.csv", sep = ",", header = TRUE)
    Xt <- ts(Xt, 1990, frequency = 12)
    str(Xt)
    head(Xt)
     

Share This Page