[Rd] Proposed modification to decompose() and plot.decomposed.ts()

2011-04-07 Thread Rob J Hyndman
The decompose() function truncates the seasonal component
unnecessarily. I've modified the function to fix this problem, and
also added the original data to the object returned (to enable better
plotting).

I've also modified the plot.decomposed.ts() function so that it plots
the original data in the top panel rather than the reconstructed data.
The difference between the two is that the reconstructed data has
missing values at the two ends whereas the original data does not.

Please find the revised code below. I hope this can find its way into
the stats package in a future release.

Regards,
Rob Hyndman


# Replacement for decompose function in the stats package
# Only changes are (1) the seasonal component is complete instead of truncated
# and (2) the original data is included in the returned object.

decompose <- function (x, type = c("additive", "multiplicative"),
filter = NULL)
{
   type <- match.arg(type)
   l <- length(x)
   f <- frequency(x)
   if (f <= 1 || length(na.omit(x)) < 2 * f)
       stop("time series has no or less than 2 periods")
   if (is.null(filter))
       filter <- if (!f%%2)
           c(0.5, rep(1, f - 1), 0.5)/f
       else rep(1, f)/f
   trend <- filter(x, filter)
   season <- if (type == "additive")
       x - trend
   else x/trend
   season <- na.omit(c(as.numeric(window(season, start(x) +
       c(1, 0), end(x))), as.numeric(window(season, start(x),
       start(x) + c(0, f)
   periods <- l%/%f
   index <- c(0, cumsum(rep(f, periods - 2)))
   figure <- numeric(f)
   for (i in 1L:f) figure[i] <- mean(season[index + i])
   figure <- if (type == "additive")
       figure - mean(figure)
   else figure/mean(figure)
   # Following lines are only changes
   seasonal <- ts(rep(figure, periods+1)[1:l], start = start(x), frequency = f)
   structure(list(x=x, seasonal = seasonal, trend = trend, random =
if (type ==
       "additive") x - seasonal - trend else x/seasonal/trend,
       figure = figure, type = type), class = "decomposed.ts")
}

# Changed definition of observed to use passed data rather than construct on fly

plot.decomposed.ts <- function (x, ...)
{
   plot(cbind(observed = x$x, trend = x$trend, seasonal = x$seasonal,
random = x$random),
       main = paste("Decomposition of", x$type, "time series"), ...)
}



-- 
-
Rob J Hyndman
Professor of Statistics, Monash University
www.robjhyndman.com

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] stats::median

2017-02-15 Thread Rob J Hyndman
The generic stats::median method is defined as
   median <- function (x, na.rm = FALSE)  {UseMethod("median")}

I suggest that this should become
   median <- function (x, na.rm = FALSE, ...)  {UseMethod("median")}

This would allow additional S3 methods to be developed with additional
arguments.

Currently I have to over-ride this generic definition in the
demography package because median.demogdata has several other
arguments.

This shouldn't break any code, and will make it easier for new S3
methods to be developed. It is also consistent with almost all other
S3 methods which do include an ellipsis.

-------------
Rob J Hyndman
Professor of Statistics, Monash University
www.robjhyndman.com

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel