0

I have prepared a csv file with some data of interest and selected 2 of the 8 columns in order to run some statistical tests.

`

library("readr")
#read csv to dataframe
dfdata = read.csv("TRSI_USDZAR_Data.csv",na.strings=c('#N/A'))
str(dfdata) 

#Selecting specific columns from the dataframe and converting text to date format, leave out NA rows

USDZAR_TS <- data.frame(Date = dfdata[[1]], USDZAR = dfdata[[3]])
USDZAR_TS$Date <- as.POSIXct( USDZAR_TS$Date, format="%Y/%m/%d" )
USDZAR_TS <- na.omit(USDZAR_TS)

#convert the data frame to a 'zoo' time series object
library("xts")
t_series <- xts(USDZAR_TS[[2]], USDZAR_TS[[1]])
class(t_series)

#load packages - WORKS WELL
library(forecast)
library(tseries)

# fit ARIMA model - WORKS WELL
arima.model <- auto.arima(USDZAR_TS[[2]], allowdrift = T)
arima.model

# fit ARIMA model - THIS IS WHERE THE BREAK OCCURS
library(lmtest)
coeftest(arima.model)

When I run the ARIMA model fit, I do get some of the result:

Series: USDZAR_TS[[2]] ARIMA(0,1,0)

sigma^2 = 0.01143: log likelihood = 4166.44 AIC=-8330.87 AICc=-8330.87 BIC=-8324.34

However, something breaks as I also then get the following error:

Error in dimnames(x) <- dn: length of 'dimnames' [2] not equal to array extent Traceback:

coeftest(arima.model) coeftest.default(arima.model) colnames<-(*tmp*, value = cnames)

I have used the same code before (and adapted here) with no issues. Why is this happening?

Herewith a MRE:

structure(list(Date = c("2013/08/03", "2013/08/04", "2013/08/05", "2013/08/06", "2013/08/07", "2013/08/08", "2013/08/09", "2013/08/10", "2013/08/11", "2013/08/12", "2013/08/13", "2013/08/14", "2013/08/15", "2013/08/16", "2013/08/17", "2013/08/18", "2013/08/19", "2013/08/20", "2013/08/21", "2013/08/22", "", "", ""), Score = c(-0.068489547, -0.068489547, -0.068489547, -0.068489547, -0.06956316, -0.06956316, -0.069382006, -0.069382006, -0.069201793, -0.069382006, -0.069382006, -0.067785649, -0.06761184, -0.06743892, -0.06743892, -0.06743892, -0.068312444, -0.068312444, -0.06859699, -0.068310516, NA, NA, NA), USDZAR = c(9.836, 9.836, 9.8437, 9.934, 9.9617, 9.8679, 9.8221, 9.8221, 9.8221, 9.8899, 9.9956, 9.9787, 9.9908, 10.0922, 10.0922, 10.0922, 10.2064, 10.1598, 10.3897, 10.2803, NA, NA, NA)), class = "data.frame", row.names = c(NA, -23L))

newquant84
  • 13
  • 3
  • Not reproducible due to `.csv` reading, please provide a complete minimal example, e.g. by using `dput()`. – Jan Jul 12 '23 at 06:45
  • I am not sure how to use dput() so I have pasted the data in text from on pastebin. I hope that suffices – newquant84 Jul 12 '23 at 14:54
  • Actually not really. Load your data in your workspace (I assume `dfdata` and call `dput()` on it. You can then paste the output here. See here [how-to-make-a-great-r-reproducible-example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Jan Jul 12 '23 at 14:57
  • Hi - thanks for the link. I have followed the instructions and pasted the dput() output above, adjusting the code to pick the correct columns – newquant84 Jul 12 '23 at 18:48
  • The error appears in the coeftesst line but there are lots of code lines in the question that that line does not depend on whichi should be removed so that it represents a *Minimal* Working Example. – G. Grothendieck Jul 16 '23 at 16:48
  • I have tried to clean the code up a bit to remove the superfluous line items. – newquant84 Jul 18 '23 at 10:40

1 Answers1

0

The problem is that arima.model is a c(0, 1, 0) arima model. You will need p or q to be non-zero to use coeftest.

arima.model
## Series: USDZAR_TS[[2]] 
## ARIMA(0,1,0) 
##
## sigma^2 = 0.006657:  log likelihood = 20.66
## AIC=-39.32   AICc=-39.09   BIC=-38.38

For example,

coeftest(arima(USDZAR_TS[[2]], c(0, 1, 0)))
## Error in dimnames(x) <- dn : 
##   length of 'dimnames' [2] not equal to array extent

coeftest(arima(USDZAR_TS[[2]], c(1, 1, 0))) # p nonzero
## z test of coefficients:
##
##     Estimate Std. Error z value Pr(>|z|)
## ar1 -0.24686    0.22636 -1.0906   0.2755

coeftest(arima(USDZAR_TS[[2]], c(0, 1, 1))) # q nonzero
## z test of coefficients:
## 
##     Estimate Std. Error z value Pr(>|z|)
## ma1 -0.17958    0.18824  -0.954   0.3401

Update

Have completely revised.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341