On 02/11/11 12:20 PM, Prof Brian Ripley wrote: > On Wed, 2 Nov 2011, Pierre Chausse wrote: > >> I have a suggestion for kernapply for ts objects. When we choose the >> option circular=F, the returned series don't have the correct dates. The > > That's a matter of opinion. A kernel is applied in the same way as an > MA filter, to historical data.
I understand for MA which is the weighted sum of past data but kernapply does not average present and past values of the series X(t) but values around it ex. with length(k[[1]])=2 Smoothed(X_t) = k[[1]][2] X_{t-1} + k[[1]][1] X_{t} + k[[1]][2] X_{t+1} which makes it natural to have the same date as X_t. Furthermore in the kernapply.vector which is used for time series, the function returns the following for circular=F return (y[(1L+m):(n-m)]) In other words it removes the first ans last observations. > >> removed dates are all at the beginning instead of half at the beginning >> and half at the end. It is particularly useful when we need to smooth >> the series (or remove a trend using a filter) before estimating a model >> (like in macroeconomics) or simply to plot the original series with the >> smoothed one. Of course, there is always the option of doing it by hand >> of the use circular=T and trim the series but I thought it would be >> nicer that way. >> >> Here is my suggestion (maybe not the nicest way to do it but it works) >> >> >> kernapply.ts <- function (x, k, circular = FALSE, ...) >> { >> if (!is.matrix(x)) >> { >> y <- kernapply.vector(as.vector(x), k, circular=circular) >> ts (y, end=end(x), frequency=frequency(x)) >> } >> else >> y <- apply(x, MARGIN=2L, FUN=kernapply, k, circular=circular) >> >> if(circular) >> ts (y, end=end(x), frequency=frequency(x)) >> else >> { >> y <- as.ts(y) >> t1 <- tsp(x)[1]+(length(k[[1]])-1)/tsp(x)[3] >> t2 <- tsp(x)[2]-(length(k[[1]])-1)/tsp(x)[3] >> tsp(y) <- c(t1,t2,tsp(x)[3]) >> return(y) >> } >> } >> >> -- >> *Pierre Chauss?* >> Assistant Professor >> Department of Economics >> University of Waterloo >> >> [[alternative HTML version deleted]] >> >> > -- *Pierre Chaussé* Assistant Professor Department of Economics University of Waterloo [[alternative HTML version deleted]]
______________________________________________ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel