0

I am trying to run the following code:

##Chen et al (2017) TVC-HAR model 
library(tvReg)
TVCHAR <- tvAR (MyData$Pct_change_plus_250, p = 1, exogen = cbind (MyData$Pct_change_250), bw = 20)
print(TVCHAR)

but due to the fact that there are some NA values in MyData$Pct_change_250, I get the following error:

Error in tvAR(MyData$Pct_change_plus_250, p = 1, exogen = cbind(MyData$Pct_change_250),  : 

NAs in y.

How can I fix it?

adrCoder
  • 3,145
  • 4
  • 31
  • 56
  • Impute the missing values? – Samuel Dec 16 '19 at 13:34
  • How do I do that? – adrCoder Dec 16 '19 at 13:45
  • 1
    This is a question in the subject of missing data. Its more about statistics than R, and depends whether you assume it has missing at random or not. The MICE package contains different methods for imputation. More commonly you would look at the complete cases of your data. Eg. `dat <- complete.cases(dat)` – Oliver Dec 16 '19 at 13:46
  • @Oliver I get this error: `Error in dat$Pct_change_plus_250 : $ operator is invalid for atomic vectors` – adrCoder Dec 16 '19 at 13:52
  • 1
    @adrCoder, being on my pc i see a few more things. 1) Your exogen variable and explanatory variable is the same. More likely you are looking for an ar(1) model only on your Y variable (pct_change_plus_250), and you should simply exclude `exogen` as explained in `help(tvAR)`. 2) i did a mistake, as `complete.cases(...)` returns an index. It would be `MyData <- MyData[complete.cases(MyData)]`. 3) Combining these 2, you are likely looking for `TVCHAR <- with(MyData[complete.cases(MyData), ], tvAR(Pct_change_plus_250, p = 1, bw = 20))`. – Oliver Dec 16 '19 at 18:56
  • @Oliver, the exogen and explanatory variables are not the same. `Pct_change_plus_250` is the return for days t to t+250, while `Pct_change_250` is the return t-250 to t. – adrCoder Dec 17 '19 at 07:39
  • And also, when I use the `dat <- MyData[complete.cases(MyData)]` command, the new dataset has 0 columns, while my initial dataset has 83 columns. How can I deal with that? – adrCoder Dec 17 '19 at 07:46
  • Remember the comma `dat <- MyData[complete.cases(MyData),]`. In the case that it has no **rows** whatsoever, this would be due to missing values across all rows. – Oliver Dec 17 '19 at 08:19
  • When I write `dat <- MyData[complete.cases(MyData),]` I get 0 obs and 83 variables... – adrCoder Dec 17 '19 at 08:36

0 Answers0