I would like to program a time series class. The idea is that I instantiate an object with an expression and some other time series objects, for instance
(two time series)
x <- ts(rnorm(10), frequency = 4, start = c(1959, 2))
y <- ts(rnorm(10), frequency = 4, start = c(1959, 2))
(a time series, defined to be the sum of x and y)
z <- exprTs("x+y", parents=list(x=x, y=y))
(get some part of the series)
window(z, start=1960, end=1960.75)
The problem is, how can I evaluate the expression? I tried the following:
#(constructor for class)
exprTs <- function(expr, parents) {
res = list(expr=expr, parents=parents)
class(res) <- "exprTs"
res
}
#(window method)
window.exprTs <- function(z, ...) {
eval(substitute(z$expr, lapply(z$parents, window, ...)))
#do.call(z$expr, lapply(z$parents, window, ...))
}
I can not get the window method to work.
If you could guide me to how to use substitute, eval, do.call appropriately, that would be very helpful.
One problem is that you are specifying the expression as a string so it will be evaluated into a string. If you want to parse a string into an expression, you need to use the
parsecommand:But yeah, why not use
applyand functions instead?