I have a zoo object in R that has daily data and is missing the weekends. When I try to run some functions (for example ar()
) on the object i get the error:
mkt.ar <- ar(zoo_object)
Error in na.fail.default(as.ts(x)) : missing values in object
If I do:
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 since when zoo tries to convert things to ts, the weekends are inherently missing. Other than converting things to a vector using coredata(zoo_object)
and running ar()
on that, is there a way to tell R to skip the missing data?
Thanks
I gather that every day is represented in your data including weekdays and weekends but the days for which no data is present are NA
(as opposed to not being present at all). In the future please provide some test data for better clarity.
Aside from your solution, if you have enough data you could perform an ar
on weekly data only by extracting the last non-missing value on or before 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 Fridays it can be shortened by replacing na.locf(z)
with z
.)
Another possibility is to use 1, 2, ... for the times but give them names in which case you could always find out what date a point belongs to by checking the name of its time.
z1 <- na.omit(z)
time(z1) <- setNames(seq_along(z1), time(z1))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With