0

I am trying to perform an adfTest in order to evaluate the of my daily returns. Using the function adfTest, I get the following error:

Error in r[i1] - r[-length(r):-(length(r) - lag + 1L)] : 
non-numeric argument to binary operator
In addition: Warning message:
In if (class(x) == "timeSeries") x = series(x) :
  the condition has length > 1 and only the first element will be used

I think it might comes from the fact that daily returns are not an univariate but I don't understand then how I could test the stationarity of my daily returns?

I computed the following example for a better understanding of my problem:

library(fUnitRoots)
library(zoo)
test.date <- as.Date(c("2009-02-01", "2009-02-02", "2009-02-05", "2009-02-06"))
test.return <- c("0.01", "0.02", "-0.05", "0.008")
test.data <- data.frame(test.date, test.return)
test.ts <- read.zoo(test.data, format = "%Y-%m-%d")
adfTest(test.ts)

Thanks for your help

Mr.Rlover
  • 2,523
  • 3
  • 14
  • 32
B-Z
  • 83
  • 7

1 Answers1

0

The error tells you what's wrong. You are trying to perform mathematical operations on non-numeric data. You need to make your data numeric before creating the zoo series.

library(fUnitRoots)
library(zoo)

test.date <- as.Date(c("2009-02-01", "2009-02-02", "2009-02-05", "2009-02-06", "2009-02-09",
                       "2009-02-10", "2009-02-11", "2009-02-12", "2009-02-13", "2009-02-16"))
test.return <- c("0.01", "0.02", "-0.05", "0.008", "-0.001", "0.009", "0.0112", "0.005",
                 "-0.003", "0.014")
test.return <- sapply(test.return, as.numeric)
test.data <- data.frame(test.date, test.return)
test.ts <- read.zoo(test.data, format = "%Y-%m-%d")


adfTest(test.ts)

Title:
 Augmented Dickey-Fuller Test

Test Results:
  PARAMETER:
    Lag Order: 1
  STATISTIC:
    Dickey-Fuller: -2.8031
  P VALUE:
    0.01 

Description:
 Fri Mar 20 19:00:50 2020 by user: MrRLover

I've added extra data points as your original series wasn't long enough to estimate points.

Mr.Rlover
  • 2,523
  • 3
  • 14
  • 32
  • Thank you @MrRlover. In my own sample, the columns of returns are in class factor. Applying `sapply(test.return, as.numeric)` transform my values into other values. Looking on the forum, I found this discussion [https://stackoverflow.com/questions/6328771/changing-values-when-converting-column-type-to-numeric], where they use `as.numeric(as.character(data[,2]))`. Trying that, I get the following error `Warning message: NAs introduced by coercion`, would you have an alternative to solve this issue? – B-Z Mar 21 '20 at 17:30
  • That's not an error, it's a warning message. The operation worked, but some of your rows have NAs in them. NAs can't be of type numeric, so ````as.numeric```` coerces a character string of "NA" to NA. Essentially anything that's not a number will be given an NA. So even "a" will be coerced to NA since as.numeric treats anything not in a numeric format as missing data. I'd suggest looking into your original data to see if those NAs are indeed NAs and not any random non-numeric string – Mr.Rlover Mar 21 '20 at 22:12