0
votes

I would like to have "Last Observation Carried Forward (LOCF)" from one xts time series to another xts series containing the time stamps that I would like to use. I have tried with na.locf but xout does not seem to work as I know from na.approx.

Any suggestions would be appreciated!

Here is an example:

I have two xts time series data sets. One with my data and another with time stamps that I would like to use.

xts_with_data <- as.xts(read.zoo(text='
2016-07-01 00:00:10,   1.0
2016-07-01 00:00:14,   2.0
2016-07-01 00:00:20,   3.0
2016-07-01 00:00:29,   4.0
2016-07-01 00:00:34,   5.0
2016-07-01 00:00:39,   6.0
', sep=',', index=1, tz='', format="%Y-%m-%d %H:%M:%S"))
names(xts_with_data) <- c('x')

xts_with_timestamps <- as.xts(read.zoo(text='
2016-07-01 00:00:15,   0.0
2016-07-01 00:00:20,   0.0
2016-07-01 00:00:30,   0.0
2016-07-01 00:00:35,   0.0
2016-07-01 00:00:38,   0.0
', sep=',', index=1, tz='', format="%Y-%m-%d %H:%M:%S"))

What I would like, is this:

                    [,1]
2016-07-01 00:00:15    2
2016-07-01 00:00:20    3
2016-07-01 00:00:30    4
2016-07-01 00:00:35    5
2016-07-01 00:00:38    5

(i.e. the time stamp from xts_with_timestamps and corresponding locf from xts_with_data).

I thought I could do it with xout like this (which works fine for na.approx):

na.locf(xts_with_data, xout = index(xts_with_timestamps))

but that just returns my original xts_with_data.

Any suggestions?

Thanks in advance.

1
It seems like the source of your troubles is a missing feature in xts:::na.locf.xts. It might be worth letting Joshua (the developer) know, if he doesn't already.AkselA

1 Answers

1
votes

Using merge() to join the two series before applying na.locf() can be one approach.

xts_all <- merge(xts_with_data, xts_with_timestamps)[,-2]
na.locf(xts_all)[index(xts_with_timestamps)]

#                     x
# 2016-07-01 00:00:15 2
# 2016-07-01 00:00:20 3
# 2016-07-01 00:00:30 4
# 2016-07-01 00:00:35 5
# 2016-07-01 00:00:38 5

It seems like the reason why xout works for na.approx() but not for na.locf() is that there is an xts method for the latter which doesn't seem to pass on extra arguments correctly. You can however force the use of the default method, and thus restore the support for xout

na.locf.default(xts_with_data, xout=index(xts_with_timestamps))
# or
na.locf(zoo(xts_with_data), xout=index(xts_with_timestamps))

#                     x
# 2016-07-01 00:00:15 2
# 2016-07-01 00:00:20 3
# 2016-07-01 00:00:30 4
# 2016-07-01 00:00:35 5
# 2016-07-01 00:00:38 5