0

Hello and thanks in advance. I'm trying to run a unit root test on a subset of series X after it has been differenced, X_diff

X_diff <- diff(X, differences = 1)

urdfTest( subset(X_diff, dat[["Year"]] > 2001 ), lags = 4, type = c("c"), doplot = TRUE)

I get the following error: 'Error in na.fail.default(as.ts(x)) : missing values in object'

Now I noticed that if I view the entire X_diff variable, I get no NA values. However, if I view the subset of X_diff variable (code below) I get an NA value all the way at the end, and this is true for any year I place into the condition.

TBG_diff[which(dat[["Year"]] > 2001)]

Why does the NA appear and how can I run the subset of the differenced series without getting the NA error?

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
gtnbz2nyt
  • 1,465
  • 3
  • 17
  • 33
  • What is the output of `traceback()`? – AdamO Feb 12 '14 at 21:53
  • Do you still have the problem after trying: `X_diff <- X_diff[!which(is.na(X_diff))]` ? – Jota Feb 12 '14 at 21:56
  • 1
    @Frank did you mean x_diff[!is.na(x_diff)] ? You don't need the `which` here. `-which(..)` would also have worked but `!which(...)` doesn't. – Vincent Feb 12 '14 at 22:33
  • Thanks for the input guys. @ashkan, when i try to run the unit-root test and then use traceback() I get this: 3: stop("\nNAs in y.\n") 2: ur.df(x, type = Type, lags = lags) 1: urdfTest(subset(TBG_diff, dat[["Year"]] > 2001), lags = 4, type = c("ct"), doplot = TRUE) – gtnbz2nyt Feb 13 '14 at 20:13
  • @Frank yes I do still unfortunately – gtnbz2nyt Feb 13 '14 at 20:14
  • Mentioning `urdfTest` is confusing here, as that comes from some package that you haven't mentioned. Can you construct a reproducible example? http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example (different Frank here, by the way.) – Frank Feb 13 '14 at 21:57
  • @Vincent yes, you're right, the `which` is unnecessary. – Jota Feb 15 '14 at 17:16

1 Answers1

1

I bet the problem is that you have differenced, so, one end of the differences is looking for a year that doesn't exist (either the year before the first year you have data for, or the year after the last year you have data for).

The solution would be to difference for a set that is one year shorter than your actual set of years

EDIT

Thinking about it some more, I think you should subset before you difference rather than the other way around. Something like

X_2001 <- subset(X, dat[["Year"]] > 2001)
urdfTest(X_2001, MORE CODE HERE)
Peter Flom
  • 2,008
  • 4
  • 22
  • 35
  • This answer clearly is about the logic of differencing time series, and would apply to many implementations, rather than the particulars of implementation in R. It's not clear to me why this was considered off topic on CV. – Glen_b Feb 12 '14 at 21:40
  • I agree that the question was asked in that way but the actual issue seems to be more one of statistical analysis (in the sense of thinking about what effect the difference transformation will have). Perhaps I am mistaken; it wouldn't be the first time. – Glen_b Feb 12 '14 at 22:04
  • Or it could be me that's mistaken. Also not a first time. :-) – Peter Flom Feb 12 '14 at 22:12
  • @PeterFlom, thanks for the advice. I'm trying to attempt it but I can't seem to get it right. Do you have the code? I'm attempting: X_diff <- diff( subset( date[["X"]], dat[["Year"]] < 2013), differences = 1) – gtnbz2nyt Feb 13 '14 at 20:19
  • @PeterFlom that worked! thank you very much. Do you happen to know the technical reason why that occurs? I'm new to R but not to stat programs like SPSS and STATA. – gtnbz2nyt Feb 14 '14 at 15:49
  • I don't really know. I would probably have tried what you did first – Peter Flom Feb 14 '14 at 22:31