Skip to content

Commit

Permalink
version 0.9.5
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-the-bayesian authored and cran-robot committed May 2, 2020
1 parent 2f7002b commit b159c94
Show file tree
Hide file tree
Showing 51 changed files with 2,253 additions and 408 deletions.
14 changes: 7 additions & 7 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
Package: bsts
Date: 2019-09-01
Version: 0.9.5
Date: 2020-04-29
Title: Bayesian Structural Time Series
Author: Steven L. Scott <[email protected]>
Maintainer: Steven L. Scott <[email protected]>
Description: Time series regression using dynamic linear models fit using
MCMC. See Scott and Varian (2014) <DOI:10.1504/IJMMNO.2014.059942>, among many
other sources.
Depends: BoomSpikeSlab (>= 1.2.1), zoo, xts, Boom (>= 0.9.2), R(>=
3.4.0)
Depends: BoomSpikeSlab (>= 1.2.3), zoo (>= 1.8), xts, Boom (>= 0.9.6),
R(>= 3.4.0)
Suggests: testthat
LinkingTo: Boom (>= 0.9.2)
Version: 0.9.2
LinkingTo: Boom (>= 0.9.6)
License: LGPL-2.1 | file LICENSE
Encoding: UTF-8
NeedsCompilation: yes
Packaged: 2019-09-04 15:04:45 UTC; stevescott
Packaged: 2020-05-01 14:24:35 UTC; steve
Repository: CRAN
Date/Publication: 2019-09-22 17:40:02 UTC
Date/Publication: 2020-05-02 15:00:02 UTC
66 changes: 50 additions & 16 deletions MD5
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
c011856d0cfeaf5ea54a3ed79c41dc2a *DESCRIPTION
758a97a1f5e02321348bc9386de9d546 *DESCRIPTION
7eb09e6fd83eb49ed22911c3b2f06744 *LICENSE
f37f71bbbb4895f02ea12ce0d71b23cf *NAMESPACE
ceb07fe9db975f5c42496dfaa33a5d14 *R/add.ar.R
67f4924e8412933001ff4b890dd18252 *R/add.dynamic.regression.R
f4f62d5be00d37a5ef3af326bc790b15 *R/add.dynamic.regression.R
a2207f7a3e1753f1f3add83cc6b9dd7f *R/add.generalized.local.linear.trend.R
14576ffbd475455ed2de73f12a331a67 *R/add.local.level.R
f9d3fe80f95e42e2cc4c2444c22bfbfd *R/add.local.linear.trend.R
Expand All @@ -13,8 +13,8 @@ be7dbb9f6b44ced309469e03439952ea *R/add.seasonal.R
d4f5df46c6a15e0d4e47ede074775446 *R/add.semilocal.linear.trend.R
f965dd362d2321286bea83acd0b40c2d *R/add.static.intercept.R
c69a20b8a59408f1c7e47c51c180b36b *R/add.student.local.linear.trend.R
f39fba9e5105da6b1189fe08005204fe *R/add.trig.R
dd0291e65ee3c1c9bceac80241bffec1 *R/bsts.R
585ef9759883b1349a00d140b91faacd *R/add.trig.R
a98db043d454fecd68c910546bdecf64 *R/bsts.R
642f82708acd5ce362948999df6aaf2d *R/compare.bsts.models.R
994c84adfa23228d724050d98c6a560e *R/date.functions.R
6fe231fe1d4debe9e9edeb92eb269869 *R/diagnostics.R
Expand All @@ -26,7 +26,7 @@ cd4f60ef6f034426065ac031ee9bd537 *R/format.timestamps.R
14b5d35d71c65d0c33ffa731332c9201 *R/mbsts.plots.R
c2a346a0d66201f93f85d29b01575365 *R/mixed.frequency.R
63bc374c03e9d515b6aca12da8729631 *R/plot_seasonal_effect.R
7818d79bd2fd7af3a2d38580b03fd63e *R/plots.R
9855bc34d9b463799387b55ac2184112 *R/plots.R
408c9fa167d8a809727fb221448cc028 *R/predict.bsts.R
55b01ea970a54ee73360d9bcd3c1930a *R/predict.mbsts.R
09187c9baabe7cdb4a0301f38b5648f1 *R/summary.bsts.R
Expand All @@ -38,19 +38,36 @@ e96e68697531b9dbeffbded63b4ee8a2 *data/iclaims.RData
371264a117afb763845fd25ac0492c60 *data/rsxfs.RData
38a19670e134d95d2cc0ed134abe3539 *data/shark.RData
761daf2618eaf1014ccc6500f3c82e61 *data/turkish.RData
0465bac211a2e128119a6fbd17f19c56 *inst/tests/tests/testthat.R
630452660972de113c484c1dd4d52269 *inst/tests/tests/testthat/test-autoar.R
172df9e68c0f7876dcaade3768c2d5f9 *inst/tests/tests/testthat/test-date-range.R
1caabb1def19e5a88b94e2d6ae0cb760 *inst/tests/tests/testthat/test-dirm.R
3de38eb5bcb72ddf38c3b818d9518c0d *inst/tests/tests/testthat/test-dynamic-regression.R
236c11dceb0ea37b1966a706f7ac3a62 *inst/tests/tests/testthat/test-goog.R
8e8e34dbc5a1d1735ec663091bcd03a9 *inst/tests/tests/testthat/test-holidays.R
8ab76fcd982a9ba00fd8b18dc9c0ae1b *inst/tests/tests/testthat/test-multivariate.R
b8cf1c7b59eede2825f50fea5feac4c1 *inst/tests/tests/testthat/test-plot-components.R
abadc45049de045297dc88b776546d08 *inst/tests/tests/testthat/test-poisson.R
673847af82904f53ddaaaabf8daa45a0 *inst/tests/tests/testthat/test-prediction-errors.R
e91ff5f823c6acbff0b7b7d6810a82a8 *inst/tests/tests/testthat/test-prediction.R
6d0e435f602c43c1c921f580edef8b39 *inst/tests/tests/testthat/test-regressionholiday.R
ebe710afd159f82025b7c8b9d6e6d237 *inst/tests/tests/testthat/test-seasonal.R
9f6f10bf2045923b08f20734cef76b22 *inst/tests/tests/testthat/test-trig.R
0465bac211a2e128119a6fbd17f19c56 *inst/tests/testthat.R
630452660972de113c484c1dd4d52269 *inst/tests/testthat/test-autoar.R
172df9e68c0f7876dcaade3768c2d5f9 *inst/tests/testthat/test-date-range.R
1caabb1def19e5a88b94e2d6ae0cb760 *inst/tests/testthat/test-dirm.R
231e979353b04ea2e3a0c36a5e16bbfa *inst/tests/testthat/test-dynamic-regression.R
3de38eb5bcb72ddf38c3b818d9518c0d *inst/tests/testthat/test-dynamic-regression.R
236c11dceb0ea37b1966a706f7ac3a62 *inst/tests/testthat/test-goog.R
8e8e34dbc5a1d1735ec663091bcd03a9 *inst/tests/testthat/test-holidays.R
8ab76fcd982a9ba00fd8b18dc9c0ae1b *inst/tests/testthat/test-multivariate.R
b8cf1c7b59eede2825f50fea5feac4c1 *inst/tests/testthat/test-plot-components.R
9e42d2a2253da1a900ed4d1946e2115b *inst/tests/testthat/test-poisson.R
905121aa9af3a048070013df7197dff6 *inst/tests/testthat/test-prediction-errors.R
c9cdb7f00e18460278079a78b6becc39 *inst/tests/testthat/test-prediction.R
abadc45049de045297dc88b776546d08 *inst/tests/testthat/test-poisson.R
673847af82904f53ddaaaabf8daa45a0 *inst/tests/testthat/test-prediction-errors.R
e91ff5f823c6acbff0b7b7d6810a82a8 *inst/tests/testthat/test-prediction.R
6d0e435f602c43c1c921f580edef8b39 *inst/tests/testthat/test-regressionholiday.R
ebe710afd159f82025b7c8b9d6e6d237 *inst/tests/testthat/test-seasonal.R
9f6f10bf2045923b08f20734cef76b22 *inst/tests/testthat/test-trig.R
65ad45f30d10d63352989bcab06c428f *man/HarveyCumulator.Rd
7d1eef9eb650d72d09f64620466950eb *man/MATCH.NumericTimestamps.Rd
a96603175b3b0a7b3dca2bfbe5ec5e50 *man/StateSpecification.Rd
Expand Down Expand Up @@ -91,7 +108,7 @@ cf7b7d18e7252f5eccd0341eb26a6d82 *man/last.day.in.month.Rd
d001026a7f503a1d7e4c3139b193bfe5 *man/match.week.to.month.Rd
5c321a7f4189941f9d151f71627da8d2 *man/max.window.width.Rd
9ddd53fb98bff70c7eb8dbeb5452e2d1 *man/mbsts.Rd
88248ff37e7a3fa6f5ba97fb96e691a2 *man/mixed.frequency.Rd
43b82cce35af62b441d08cda809329a1 *man/mixed.frequency.Rd
4c9639c7bfb85396081677060adc4541 *man/month.distance.Rd
129cf408079a8ce4c6fea7e5e1c54515 *man/named.holidays.Rd
16d4b2b4b066dab48738e29d93bc48c7 *man/new.home.sales.Rd
Expand Down Expand Up @@ -122,22 +139,22 @@ d96e18ecf4ab02527bf1b60ce08f106e *man/weekday.names.Rd
cd4f3fcff595de3ac83d8e85ce89e627 *man/wide.to.long.Rd
ad3e4c15cd397b85b1c6a45f0803500a *src/Makevars
d99ce4ef93988dc2e1b7279ab92be31e *src/aggregate_time_series.cc
14a5789b717b958f3e2bc746f491b57d *src/bsts.cc
84059b88fee889c7194f6d541ce3ed54 *src/bsts.cc
8c5caf631398cd06050d059691d443e3 *src/bsts_init.cc
604673026495a954489f5e86602bc48f *src/create_dynamic_intercept_state_model.cpp
2d60cd8ba33ef34e0238517e07bf7c9c *src/create_dynamic_intercept_state_model.h
3c36c0dc370bec5d41d595f71403f718 *src/create_shared_state_model.cpp
e348872535b6a2fda297739ea91d7b6c *src/create_shared_state_model.h
5770639938d765cb2ad35bde622d299f *src/create_state_model.cpp
09f4b4e16bba358e1b4c462ae2b5e7a6 *src/create_state_model.cpp
f8f89c660dc8cf6920e8694d25b99671 *src/create_state_model.h
4a0ab82799effea8a0f2998cfb9302f7 *src/dirm.cc
3b6fc2c846615a9e79fdd8f49539da26 *src/dynamic_intercept_model_manager.cc
f4b47e0c03ea817b60f652813b16fb06 *src/dynamic_intercept_model_manager.h
e6f88ebc8a3c65c29fd14268e57fe4d9 *src/get_date_ranges.cc
b95582788ae274e41af2e10f568209aa *src/mbsts.cc
a739fc5cf1bbccb1ae83860c48992b06 *src/mixed_frequency.cc
18fb2e5c4580c02e351c167c8156e789 *src/model_manager.cc
4e3a8e09f1b1944b9909f07716cafa26 *src/model_manager.h
4bc3088809b3d5525ee65288bf8aa959 *src/mixed_frequency.cc
5719cbcd19da21c199d82c1f163b2abd *src/model_manager.cc
8992bcf3e4c473fc26d7d5fb1a744b55 *src/model_manager.h
7ce877770f10b81a6a6cf2f0d47a055d *src/multivariate_gaussian_model_manager.cc
93eae1ab5fe99f38d0c643687ce02683 *src/multivariate_gaussian_model_manager.h
82d4ded6a46e394a9e9bfabeb59d9621 *src/state_space_gaussian_model_manager.cc
Expand All @@ -148,7 +165,24 @@ a432aebec2404eb926fe13e9bcb5fb25 *src/state_space_logit_model_manager.cc
e216be891919e3263441168f35507631 *src/state_space_poisson_model_manager.h
d12a59708f82f866ed02944f5431bb59 *src/state_space_regression_model_manager.cc
7ac07cf5d57c124a845c31baeb64f782 *src/state_space_regression_model_manager.h
ca0a7d4075cfcfaa3c975feae0d96249 *src/state_space_student_model_manager.cc
06440dfd21a4660c76b6e0bb313dc944 *src/state_space_student_model_manager.cc
70d7c3d65ab85979568d692b4a217f9f *src/state_space_student_model_manager.h
b8f60bed801663863a327aa8bd4d367e *src/timestamp_info.cc
1c682544f82b2cf9331c827ec6c3f0cf *src/timestamp_info.h
758355f181bebfed943c020342250e5e *src/utils.cc
9b299499f0d977e24d5e96e61872bf62 *src/utils.h
0465bac211a2e128119a6fbd17f19c56 *tests/testthat.R
630452660972de113c484c1dd4d52269 *tests/testthat/test-autoar.R
172df9e68c0f7876dcaade3768c2d5f9 *tests/testthat/test-date-range.R
1caabb1def19e5a88b94e2d6ae0cb760 *tests/testthat/test-dirm.R
3de38eb5bcb72ddf38c3b818d9518c0d *tests/testthat/test-dynamic-regression.R
236c11dceb0ea37b1966a706f7ac3a62 *tests/testthat/test-goog.R
8e8e34dbc5a1d1735ec663091bcd03a9 *tests/testthat/test-holidays.R
8ab76fcd982a9ba00fd8b18dc9c0ae1b *tests/testthat/test-multivariate.R
b8cf1c7b59eede2825f50fea5feac4c1 *tests/testthat/test-plot-components.R
abadc45049de045297dc88b776546d08 *tests/testthat/test-poisson.R
673847af82904f53ddaaaabf8daa45a0 *tests/testthat/test-prediction-errors.R
e91ff5f823c6acbff0b7b7d6810a82a8 *tests/testthat/test-prediction.R
6d0e435f602c43c1c921f580edef8b39 *tests/testthat/test-regressionholiday.R
ebe710afd159f82025b7c8b9d6e6d237 *tests/testthat/test-seasonal.R
9f6f10bf2045923b08f20734cef76b22 *tests/testthat/test-trig.R
6 changes: 3 additions & 3 deletions R/add.dynamic.regression.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ DynamicRegressionRandomWalkOptions <- function(
## used to scale the default prior if sigma.prior is NULL. It is unused
## otherwise.
## Returns:
## An object that can be passed to AddDynamicRegression as the model.options argument.
## An object that can be passed to AddDynamicRegression as the model.options argument.
if (!is.null(sigma.prior)) {
# sigma.prior must either be an SdPrior or a list of SdPrior objects.
stopifnot(inherits(sigma.prior, "SdPrior")
Expand All @@ -44,7 +44,7 @@ DynamicRegressionRandomWalkOptions <- function(
if (any(sdx <= 0)) {
stop("All elements of sdx must be positive.")
}

number.of.coefficients <- length(sdx)
sigma.prior <- list()
for (i in 1:number.of.coefficients) {
Expand Down Expand Up @@ -272,7 +272,7 @@ AddDynamicRegression <- function(
if (any(is.na(predictors))) {
stop("NA's are not allowed in the predictor matrix.")
}

stopifnot(ncol(predictors) >= 1)
stopifnot(nrow(predictors) >= 1)

Expand Down
4 changes: 2 additions & 2 deletions R/add.trig.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ AddTrig <- function(state.specification = NULL,
length(frequencies) > 0,
all(frequencies > 0))
method <- match.arg(method)

## Check the prior on the sinusoid coefficient increments.
if (is.null(sigma.prior)) {
sigma.prior <- SdPrior(0.01 * sdy, upper.limit = sdy)
Expand All @@ -83,7 +83,7 @@ AddTrig <- function(state.specification = NULL,

## All data has been checked and gathered at this point. Return the
## object.
trig <- list(name = "trig",
trig <- list(name = paste0("trig.", period),
frequencies = frequencies,
period = period,
sigma.prior = sigma.prior,
Expand Down
24 changes: 24 additions & 0 deletions R/bsts.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,12 @@ bsts <- function(formula,
prior$max.flips <- -1
}
}

## Check state compatibility. There are some combinations of options and
## state models that just don't work together.
.CheckCompatibility(data.list, state.specification, model.options,
family)

##----------------------------------------------------------------------
ans <- .Call("analysis_common_r_fit_bsts_model_",
data.list,
Expand Down Expand Up @@ -611,3 +617,21 @@ BstsOptions <- function(save.state.contributions = TRUE,
object$ngood <- NULL
return(object)
}

.CheckCompatibility <- function(data.list, state.specification,
model.options, family) {
## Ensure that any incompatible combinations of state models, model options,
## data formats, etc. raise an error in R before they get to crash C++.
##
## Args:
## data.list: A list containing the packaged data to be sent to C++.
## state.specification: A list of state component objects.
## model.options: A list of model options.
## family: string denoting the model family.
dynamic.regression <- any(sapply(state.specification,
inherits, "DynamicRegression"))

if (!data.list$timestamp.info$timestamps.are.trivial && dynamic.regression) {
stop("Dynamic regression models are only supported with trivial time stamps.")
}
}
28 changes: 13 additions & 15 deletions R/plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ YearPlot <- function(y, colors = NULL, ylab = NULL, ylim = NULL, legend = TRUE,
if (inherits(index(y), "Date")) {
index(y) <- DateToPOSIX(index(y))
}

years <- sort(unique(sapply(strsplit(as.character(index(y)), "-"),
function(x) as.numeric(x[1]))))

ts.list <- list()
for (i in 1:length(years)) {
year <- years[i]
Expand Down Expand Up @@ -199,7 +199,7 @@ MonthPlot <- function(y, seasonal.identifier = months, colors = NULL, ylab = NUL
colors <- 1:length(season.names)
}
stopifnot(length(colors) >= length(season.names))

for (i in 1:length(season.names)) {
season <- season.names[i]
index <- as.character(seasonal.identifier(index(y))) == season
Expand Down Expand Up @@ -455,7 +455,7 @@ PlotBstsComponents <- function(bsts.object,
## components: A numeric index vector indicating which state components to
## plot.
## ...: Extra arguments passed to PlotDynamicDistribution.
##
##
## Returns:
## Produces a plot on the current graphics device. Returns invisible NULL.
stopifnot(is.numeric(components),
Expand All @@ -464,13 +464,13 @@ PlotBstsComponents <- function(bsts.object,
min(components) >= 1,
max(components) <= length(bsts.object$state.specification))
components <- unique(components)

state.specification <- bsts.object$state.specification[components]
number.of.components <- length(state.specification)
if (bsts.object$has.regression) {
number.of.components <- number.of.components + 1
}

if (is.null(time)) {
time <- bsts.object$timestamp.info$regular.timestamps
}
Expand All @@ -493,7 +493,7 @@ PlotBstsComponents <- function(bsts.object,
par(mfrow = c(num.rows, num.cols))
cex <- par("cex")
par(mfrow = c(1,1), cex= cex)

screen.numbers <- split.screen(screen.matrix)
on.exit({close.screen(screen.numbers); par(original.par)})

Expand Down Expand Up @@ -633,7 +633,7 @@ plot.StateModel <- function(x,
title(main = state.specification$name)
return(invisible(NULL))
}

###----------------------------------------------------------------------
PlotBstsState <- function(bsts.object, burn = SuggestBurn(.1, bsts.object),
time, show.actuals = TRUE,
Expand Down Expand Up @@ -691,7 +691,7 @@ PlotBstsState <- function(bsts.object, burn = SuggestBurn(.1, bsts.object),
ylim <- range(state, na.rm = TRUE)
}
}

if (style == "boxplot") {
TimeSeriesBoxplot(state, time = time, ylim = ylim, ...)
} else {
Expand Down Expand Up @@ -891,7 +891,7 @@ PlotDynamicRegression <- function(
if (is.null(ylim) && same.scale == TRUE) {
ylim <- range(beta, na.rm = TRUE)
}

layout <- match.arg(layout)
if (layout == "square") {
num.rows <- floor(sqrt(number.of.variables))
Expand All @@ -910,7 +910,7 @@ PlotDynamicRegression <- function(
for (variable in 1:number.of.variables) {
if (need.ylim) {
ylim <- range(beta[, variable, ], na.rm = TRUE)
}
}
if (style == "boxplot") {
TimeSeriesBoxplot(beta[, variable, ],
time = time, ylim = ylim, ...)
Expand Down Expand Up @@ -948,7 +948,7 @@ PlotHoliday <- function(holiday, model, show.raw.data = TRUE,
## Effects:
## A set of boxplots are drawn on the current graphics device showing the
## posterior distribution of the impact of the holiday during each day of
## its influence window.
## its influence window.
date.ranges <- DateRange(holiday, model$timestamp.info$timestamps)
raw.data<- list()
for (i in 1:nrow(date.ranges)) {
Expand All @@ -964,7 +964,7 @@ PlotHoliday <- function(holiday, model, show.raw.data = TRUE,
ylim <- range(holiday.effects, na.rm = TRUE)
if (show.raw.data) {
ylim <- range(ylim, raw.data, na.rm = TRUE)
}
}
}

boxplot(holiday.effects, ylim = ylim, ...)
Expand Down Expand Up @@ -1007,5 +1007,3 @@ PlotHoliday <- function(holiday, model, show.raw.data = TRUE,
stop(paste0("Could not find a holiday named ", holiday.name, "."))
return(NULL)
}


5 changes: 5 additions & 0 deletions inst/tests/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
library(testthat)
library(bsts)
library(Boom)

test_check("bsts")
12 changes: 12 additions & 0 deletions inst/tests/tests/testthat/test-autoar.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
library(bsts)
library(testthat)

test_that("AutoAr does not crash on minimal data.", {
y <- rnorm(4)
ss <- AddAutoAr(list(), y=y, lags = 4)

for (i in 1:20) {
model <- bsts(y, ss, niter = 100, ping = -1)
}
expect_true(inherits(model, "bsts"))
})
13 changes: 13 additions & 0 deletions inst/tests/tests/testthat/test-date-range.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
holiday <- NamedHoliday("MemorialDay", days.before = 2, days.after = 2)
timestamps <- seq.Date(from = as.Date("2001-01-01"), by = "day",
length.out = 365 * 10)

influence <- DateRange(holiday, timestamps)

test_that("DateRange returns a two-column data frame of dates", {
expect_that(influence, is_a("data.frame"))
expect_that(ncol(influence), equals(2))
expect_that(influence[, 1], is_a("Date"))
expect_that(influence[, 2], is_a("Date"))
expect_true(all(influence[, 1] <= influence[, 2]))
})
Loading

0 comments on commit b159c94

Please sign in to comment.