Internal time series NA, Zoo, R

I have a zoo facility in R that has daily data and no weekend. When I try to run some functions (e.g. ar() ) on an object, I get an error:

 mkt.ar <- ar(zoo_object) Error in na.fail.default(as.ts(x)) : missing values in object 

If I do this:

 mkt.ar <- ar(zoo_object, na.action=na.omit) Error in na.omit.ts(as.ts(x)) : time series contains internal NAs 

This makes sense because when the zoo tries to convert things to ts, there are essentially no days off. Besides converting objects to a vector using coredata(zoo_object) and running ar() on this, is there any way to tell R to skip the missing data?

thanks

+4
source share
2 answers

I understand that every day is represented in your data, including days of the week and weekends, but days for which there is no data are NA (as opposed to the fact that they are not present at all). In the future, please provide some test data for better clarity.

Besides your decision, if you have enough data, you can execute ar in weekly data only by retrieving the last missing value on Friday or until Friday:

 library(zoo) # test data library(chron) # is.weekend z <- zoo(100:130, as.Date("2000-01-01") + 0:30) z[is.weekend(time(z))] <- NA # extract Fridays zfri <- na.locf(z)[format(time(z), "%w") == 5] 

(If there are no missing spots, it can be reduced by replacing na.locf(z) with z .)

Another possibility is to use 1, 2, ... for time, but give them names, in this case you can always find out what date the point belongs to, checking the name of your time.

 z1 <- na.omit(z) time(z1) <- setNames(seq_along(z1), time(z1)) 
+6
source

The simplest method will convert a ZOO object to a data.frame object using for example ( z1 is a zoo object):

 dz1<-data.frame(na.omit(z1)) 

then convert it to a time series object.

 ts(dz1, frequency=5) 
-1
source

Source: https://habr.com/ru/post/1401297/


All Articles