Skip to content

Commit

Permalink
more theta tests and start on theta plot method
Browse files Browse the repository at this point in the history
  • Loading branch information
ellisp committed Nov 1, 2016
1 parent 8bf6979 commit 971f539
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: forecastHybrid
Title: Convenient Functions for Ensemble Time Series Forecasts
Version: 0.3.0
Version: 0.3.1.9000
Date: 2016-09-23
Authors@R: c(
person("David", "Shaub", email = "davidshaub@gmx.com", role = c("aut", "cre")),
Expand Down
22 changes: 21 additions & 1 deletion pkg/R/theta.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#' used in \code{hybridModel()}.
#' @return An object of class \code{thetam}
#' @author Peter Ellis
#' @examples
#' mod1 <- thetam(Nile)
#' plot(mod1)
#' @seealso \code{\link{forecast.thetam}}
thetam <- function(y){
n <- length(y)
Expand Down Expand Up @@ -41,7 +44,7 @@ thetam <- function(y){
object$x <- origy
object$drift <- stats::lsfit(0:(n - 1), y)$coef[2] / 2
object$method <- "Theta"
class(object) <- "thetam"
class(object) <- c("thetam")

return(object)
}
Expand All @@ -56,6 +59,10 @@ thetam <- function(y){
#' @param level Confidence level for prediction intervals
#' @param fan If TRUE, level is set to \code{seq(51, 99, by = 3)}. This is suitable for fan plots.
#' @return An object of class \code{forecast}
#' @examples
#' mod1 <- thetam(Nile)
#' fc1 <- forecast(mod1)
#' plot(fc1)
#' @author Peter Ellis
#' @seealso \code{\link{thetam}}
forecast.thetam <- function(object, h = ifelse(object$m > 1, 2 * object$m, 10),
Expand Down Expand Up @@ -85,4 +92,17 @@ forecast.thetam <- function(object, h = ifelse(object$m > 1, 2 * object$m, 10),
return(fcast)
}

#' Plot components from Theta model
#'
#' Produces a plot of the level components from the ETS model underlying a Theta model
#' @export
#' @param x Object of class "thetam".
#' @author Peter Ellis
plot.thetam <- function(x, ...){
y <- x$x
plot(cbind(observed = y, level = x$states[, 1]),
main = paste("Decomposition by", x$method, "method"), ...)
#TODO - add the slope
}

# TODO - autoplot.thetam
2 changes: 2 additions & 0 deletions pkg/inst/NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Version 0.3.0 [Unreleased]
* Prediction intervals are now created for `nnetar` objects in the ensemble. This should address one aspect of incorrect prediction intervals (e.g. issue #37).
* theta models can be added (by including "`f`" in the `models=` argument for `hybridModel()`)
* `accuracy.cvts` is exported

# Version 0.2.0 [2016-09-23]
* Add timeseries cross validation with `cvts()`
Expand Down
4 changes: 4 additions & 0 deletions pkg/man/thetam.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions pkg/tests/testthat/test-theta.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,14 @@ fan2 <- thetaf(AirPassengers, level = c(75, 90))
expect_equal(sum(fan1$upper - fan2$upper), 0)
expect_equal(sum(fan1$lower - fan2$lower), 0)

# another example
fc1 <- thetaf(Nile)
fc2 <- forecast(thetam(Nile))
expect_equal(sum(fc2$mean - fc1$mean), 0)
expect_equal(sum(fc2$upper - fc1$upper), 0)
expect_equal(sum(fc2$lower - fc1$lower), 0)

fc3 <- forecast(thetam(Nile), level = c(0.7, 0.9))
expect_equal(sum(fc3$mean - fc1$mean), 0)
expect_lt(sum(fc3$upper - fc1$upper), 0)
expect_gt(sum(fc3$lower - fc1$lower), 0)

0 comments on commit 971f539

Please sign in to comment.