newquant84
newquant84

Reputation: 63

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

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))

Upvotes: 0

Views: 362

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 270348

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

Upvotes: 1

Related Questions