With time series, a subset and some other functions cause conversion to a matrix or vector. You do not need to restore time series, you can just transfer the attributes of the source ts
to the result.
hpfilter <- function(x,lambda=1600){ eye <- diag(length(x)) result <- solve(eye+lambda*crossprod(diff(eye,lag=1,d=2)),x) attributes(result) <- attributes(x) return(result) }
You can also use a subset to change (but not add) values ββin a time series:
hpfilter <- function(x,lambda=1600){ eye <- diag(length(x)) x[] <- solve(eye+lambda*crossprod(diff(eye,lag=1,d=2)),x) return(x) }
source share