rquantitative-financehypothesis-testcomputational-finance

R: Durbin Watson test with NA result


I am trying to gauge the correlation between the historic of a stock price and an index using the Durbin Watson test in R.

This is what I have done so far:

data <- read.xlsx("data.xlsx", colNames = TRUE, detectDates = TRUE)
data
head(data)
data$X1 <- as.Date(data$X1)

bbva <- xts(data$BBVA, data$X1)
ibex <- xts(data$IBEX, data$X1)

ldbbva <- diff(log(bbva))
ldibex <- diff(log(ibex))

Here I fill some NA values.

mean <- mean(ldbbva, na.rm = TRUE) 
ldbbva[is.na(ldbbva)] <- mean

mean <- mean(ldibex, na.rm = TRUE) 
ldibex[is.na(ldibex)] <- mean

And I make the regression

regression <- lm(ldibex ~ ldbbva)

If we take a look at the ldibex (for example), we can see something like this:

                    [,1]
2010-01-04 -0.0001060206
2010-01-05  0.0048708104
2010-01-06  0.0014819410
2010-01-07 -0.0046086970
2010-01-08 -0.0002712618
2010-01-11 -0.0073027658

But when I try to run the test dwtest(regression), this is the output:

    Durbin-Watson test

data:  regression
DW = NA, p-value = NA
alternative hypothesis: true autocorrelation is greater than 0

I already had filled all NA values, so I don't get why is this NA.


Solution

  • There is a problem with using xts object with Durbin-Watson test. Try convert your data to numeric vector:

    ldbbva <- as.numeric(diff(log(bbva)))
    ldibex <- as.numeric(diff(log(ibex)))
    

    I hope it helps!