Skip to content

Commit

Permalink
Fixed errors and warnings triggered by unit tests in R 4.1 (current R…
Browse files Browse the repository at this point in the history
…-devel):

* Explicitly ignored environment when testing equality of functions; this was the default behaviour until R 4.0 but will change in R 4.1 [1] and caused check errors on CRAN.
* Suppressed expected warnings due to unsufficient MCMC samples (done intentionally in tests to increase speed).
* Updated outdated URLs.

[1] https://stat.ethz.ch/pipermail/r-devel/2020-December/080172.html
  • Loading branch information
Alain Hauser committed Dec 20, 2020
1 parent 07b60e1 commit 3179767
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 48 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: CausalImpact
Title: Inferring Causal Effects using Bayesian Structural Time-Series Models
Date: 2019-12-26
Date: 2020-12-21
Author: Kay H. Brodersen <kbrodersen@google.com>,
Alain Hauser <alhauser@google.com>
Maintainer: Alain Hauser <alhauser@google.com>
Expand All @@ -9,8 +9,8 @@ Description: Implements a Bayesian approach to causal impact estimation in time
series, as described in Brodersen et al. (2015) <DOI:10.1214/14-AOAS788>.
See the package documentation on GitHub
<https://google.github.io/CausalImpact/> to get started.
Copyright: Copyright (C) 2014-2019 Google, Inc.
Version: 1.2.4
Copyright: Copyright (C) 2014-2020 Google, Inc.
Version: 1.2.5
VignetteBuilder: knitr
License: Apache License 2.0 | file LICENSE
Imports: assertthat (>= 0.2.0), Boom, dplyr, ggplot2, zoo
Expand Down
2 changes: 1 addition & 1 deletion R/impact_analysis.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2014-2017 Google Inc. All rights reserved.
# Copyright 2014-2020 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion R/impact_model.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2014-2017 Google Inc. All rights reserved.
# Copyright 2014-2020 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ library(CausalImpact)
## Further resources

* Manuscript:
[Brodersen et al., Annals of Applied Statistics (2015)](http://research.google.com/pubs/pub41854.html)
[Brodersen et al., Annals of Applied Statistics (2015)](https://research.google/pubs/pub41854/)

* For questions on the statistics behind CausalImpact:
[Cross Validated](http://stats.stackexchange.com/questions/tagged/causalimpact)
[Cross Validated](https://stats.stackexchange.com/questions/tagged/causalimpact)

* For questions on how to use the CausalImpact R package:
[Stack Overflow](http://stackoverflow.com/questions/tagged/causalimpact)
[Stack Overflow](https://stackoverflow.com/questions/tagged/causalimpact)

* [Bug reports](https://github.com/google/CausalImpact/issues)
4 changes: 2 additions & 2 deletions inst/CITATION
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ bibentry(
textVersion =
paste0("Brodersen et al., 2015, Annals of Applied Statistics. ",
"Inferring causal impact using Bayesian structural time-series ",
"models. http://research.google.com/pubs/pub41854.html"),
"models. https://research.google/pubs/pub41854/"),
header = "To cite CausalImpact in publications use:",

title = paste0("Inferring causal impact using {B}ayesian structural ",
Expand All @@ -18,5 +18,5 @@ bibentry(
year = "2014",
volume = "9",
pages = "247--274",
url = "http://research.google.com/pubs/pub41854.html"
url = "https://research.google/pubs/pub41854/"
)
33 changes: 19 additions & 14 deletions tests/testthat/test-impact-analysis.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2014-2017 Google Inc. All rights reserved.
# Copyright 2014-2020 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -408,8 +408,9 @@ test_that("CausalImpact.RunWithData.MissingValues", {
pre.period <- c(1, 20)
post.period <- c(21, 30)
model.args <- list(niter = 100)
expect_error(impact <- CausalImpact(data, pre.period, post.period,
model.args),
expect_error(suppressWarnings(impact <- CausalImpact(data, pre.period,
post.period,
model.args)),
NA)
# Test that all columns in the result series except those associated with
# point predictions have missing values at the time points the result time
Expand Down Expand Up @@ -469,8 +470,9 @@ test_that("CausalImpact.RunWithData.StandardizeData", {
tolerance = 0.001)
expect_equal(impact1$series, impact2$series, tolerance = 0.1)

# Recover ground truth (generative model with 1 covariate)
# with/without <standardize.data>
# Recover ground truth (generative model with 1 covariate) with/without
# <standardize.data>. Suppressing the warning that there might not be enough
# MCMC samples in CausalImpact calls.
set.seed(1)
n <- 500
x1 <- sin(1:n / 100) + rnorm(n, 0, 0.1)
Expand All @@ -480,14 +482,14 @@ test_that("CausalImpact.RunWithData.StandardizeData", {
pre.period <- c(1, 250)
post.period <- c(251, 500)
data <- cbind(y, x1)
impact1 <- CausalImpact(data, pre.period, post.period,
model.args = list(niter = 500,
standardize.data = FALSE))
suppressWarnings(impact1 <- CausalImpact(
data, pre.period, post.period,
model.args = list(niter = 500, standardize.data = FALSE)))
estimates1 <- colMeans(impact1$model$bsts.model$coefficients)
expect_equal(as.vector(estimates1)[2], beta[2], tolerance = 0.05)
impact2 <- CausalImpact(data, pre.period, post.period,
model.args = list(niter = 500,
standardize.data = TRUE))
suppressWarnings(impact2 <- CausalImpact(
data, pre.period, post.period,
model.args = list(niter = 500, standardize.data = TRUE)))
estimates2 <- colMeans(impact2$model$bsts.model$coefficients)
expect_equal(as.vector(estimates2)[2], 1, tolerance = 0.05)
})
Expand Down Expand Up @@ -577,13 +579,16 @@ test_that("CausalImpact.RunWithData.MissingTimePoint", {
post.period <- as.Date(c("2017-01-16", "2017-01-20"))
model.args <- list(niter = 100)

# Missing in pre-period
impact <- CausalImpact(series[-10, ], pre.period, post.period, model.args)
# Missing in pre-period. Suppressing the warning that there might not be
# enough MCMC samples.
suppressWarnings(impact <- CausalImpact(series[-10, ], pre.period,
post.period, model.args))
indices <- time(impact$series)
expect_equal(indices, time(series)[-10])

# Missing in post-period
impact <- CausalImpact(series[-17, ], pre.period, post.period, model.args)
suppressWarnings(impact <- CausalImpact(series[-17, ], pre.period,
post.period, model.args))
indices <- time(impact$series)
expect_equal(indices, time(series)[-17])
})
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-impact-inference.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2014 Google Inc. All rights reserved.
# Copyright 2014-2020 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,7 +28,7 @@ test_that("GetPosteriorStateSamples", {
model.args <- list(niter = 100)

# Create a healthy bsts.model and test it
bsts.model <- ConstructModel(data, model.args)
suppressWarnings(bsts.model <- ConstructModel(data, model.args))
state.samples <- GetPosteriorStateSamples(bsts.model)
expect_equal(ncol(state.samples), 365)
})
Expand All @@ -43,7 +43,7 @@ test_that("ComputeResponseTrajectories", {
# Test healthy input
data <- zoo(cbind(rnorm(365), rnorm(365), rnorm(365)))
model.args <- list(niter = 100)
bsts.model <- ConstructModel(data, model.args)
suppressWarnings(bsts.model <- ConstructModel(data, model.args))
y.samples <- ComputeResponseTrajectories(bsts.model)
expect_equal(ncol(y.samples), 365)
})
Expand Down
14 changes: 9 additions & 5 deletions tests/testthat/test-impact-misc.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2014-2017 Google Inc. All rights reserved.
# Copyright 2014-2020 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -187,7 +187,8 @@ test_that("StandardizeAllVariables", {
expect_equal(mean(result$data[, column]), 0, tolerance = 0.0001);
expect_equal(sd(result$data[, column]), 1, tolerance = 0.0001)
})
expect_equal(result$UnStandardize, Standardize(data[, 1])$UnStandardize)
expect_equal(result$UnStandardize, Standardize(data[, 1])$UnStandardize,
check.environment = FALSE)

# Test that several columns are standardized correctly when fitting mean and
# SD only over part of the rows.
Expand All @@ -196,7 +197,8 @@ test_that("StandardizeAllVariables", {
expect_equal(mean(result$data[11 : 90, column]), 0, tolerance = 0.0001);
expect_equal(sd(result$data[11 : 90, column]), 1, tolerance = 0.0001)
})
expect_equal(result$UnStandardize, Standardize(data[, 1])$UnStandardize)
expect_equal(result$UnStandardize, Standardize(data[, 1])$UnStandardize,
check.environment = FALSE)

# Test healthy input: single series only
set.seed(1)
Expand All @@ -206,14 +208,16 @@ test_that("StandardizeAllVariables", {
expect_equal(names(result), c("data", "UnStandardize"))
expect_equal(mean(result$data), 0, tolerance = 0.0001)
expect_equal(sd(result$data), 1, tolerance = 0.0001)
expect_equal(result$UnStandardize, Standardize(data)$UnStandardize)
expect_equal(result$UnStandardize, Standardize(data)$UnStandardize,
check.environment = FALSE)

# Test that a single series is standardized correctly when fitting mean and SD
# only over part of the data range.
result <- StandardizeAllVariables(data, c(11, 90))
expect_equal(mean(result$data[11: 90]), 0, tolerance = 0.0001)
expect_equal(sd(result$data[11 : 90]), 1, tolerance = 0.0001)
expect_equal(result$UnStandardize, Standardize(data)$UnStandardize)
expect_equal(result$UnStandardize, Standardize(data)$UnStandardize,
check.environment = FALSE)
})

test_that("GetPeriodIndices.InvalidInput", {
Expand Down
27 changes: 20 additions & 7 deletions tests/testthat/test-impact-model.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2014-2017 Google Inc. All rights reserved.
# Copyright 2014-2020 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,16 +28,29 @@ test_that("ObservationsAreIllConditioned", {
expect_false(ObservationsAreIllConditioned(c(NA, NA, 1, NA, 2, 3, NA, NA)))

# Test all NA
expect_true(ObservationsAreIllConditioned(c(NA, NA, NA, NA, NA)))
expect_warning(ObservationsAreIllConditioned(c(NA, NA, NA, NA, NA)), "all NA")
expect_true(suppressWarnings(
ObservationsAreIllConditioned(c(NA, NA, NA, NA, NA))))

# Test fewer than 3 non-NA values
expect_error(ObservationsAreIllConditioned(NULL))
expect_error(ObservationsAreIllConditioned(c()))
expect_true(ObservationsAreIllConditioned(c(1)))
expect_true(ObservationsAreIllConditioned(c(1, 2)))
expect_true(ObservationsAreIllConditioned(c(1, 2, NA)))
expect_true(ObservationsAreIllConditioned(c(NA, 1, 2, NA)))
expect_true(ObservationsAreIllConditioned(c(NA, 1, 2, NA, NA)))
expect_warning(ObservationsAreIllConditioned(c(1)),
"fewer than 3 non-NA values")
expect_true(suppressWarnings(ObservationsAreIllConditioned(c(1))))
expect_warning(ObservationsAreIllConditioned(c(1, 2)),
"fewer than 3 non-NA values")
expect_true(suppressWarnings(ObservationsAreIllConditioned(c(1, 2))))
expect_warning(ObservationsAreIllConditioned(c(1, 2, NA)),
"fewer than 3 non-NA values")
expect_true(suppressWarnings(ObservationsAreIllConditioned(c(1, 2, NA))))
expect_warning(ObservationsAreIllConditioned(c(NA, 1, 2, NA)),
"fewer than 3 non-NA values")
expect_true(suppressWarnings(ObservationsAreIllConditioned(c(NA, 1, 2, NA))))
expect_warning(ObservationsAreIllConditioned(c(NA, 1, 2, NA, NA)),
"fewer than 3 non-NA values")
expect_true(suppressWarnings(
ObservationsAreIllConditioned(c(NA, 1, 2, NA, NA))))
})

test_that("FormatInputForConstructModel", {
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-impact-plot.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2014 Google Inc. All rights reserved.
# Copyright 2014-2020 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -121,13 +121,13 @@ test_that("CreateImpactPlot", {
# Test plot.CausalImpact() generic
q1 <- CreateImpactPlot(impact)
q2 <- plot(impact) # dispatched to plot.CausalImpact()
expect_equal(q1, q2)
expect_equal(q1, q2, check.environment = FALSE)

# Test plotting different metrics
q1 <- plot(impact)
q2 <- plot(impact, c("original", "pointwise", "cumulative"))
q3 <- plot(impact, c("o", "point", "c"))
expect_equal(q1, q2)
expect_equal(q1, q2, check.environment = FALSE)
# As of ggplot 2.0.0, `q1` and `q2` are still the same but `q3` is different.
# This is because `q1` and `q2` contains:
# > q1$plot_env$metrics
Expand All @@ -139,10 +139,10 @@ test_that("CreateImpactPlot", {
# "original" "pointwise" "cumulative"
# So we test whether `q1` equals `q3` except for `$plot_env$metrics`.
q3$plot_env$metrics <- q2$plot_env$metrics
expect_equal(q1, q3)
expect_equal(q1, q3, check.environment = FALSE)

# Test different order
q1 <- plot(impact, c("p", "c"))
q2 <- plot(impact, c("c", "p"))
expect_true(!isTRUE(all.equal(q1, q2)))
expect_true(!isTRUE(all.equal(q1, q2, check.environment = FALSE)))
})
8 changes: 4 additions & 4 deletions vignettes/CausalImpact.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ evolved after the intervention if the intervention had never occurred. For a
quick overview, watch the
[tutorial video](https://www.youtube.com/watch?v=GTgZfCltMm8).
For details, see:
[Brodersen et al., Annals of Applied Statistics (2015)](http://research.google.com/pubs/pub41854.html).
[Brodersen et al., Annals of Applied Statistics (2015)](https://research.google/pubs/pub41854/).

#### What assumptions does the model make?

Expand Down Expand Up @@ -332,7 +332,7 @@ We recommend referencing the use of the CausalImpact R package as shown in the
example below:

"CausalImpact `r packageVersion("CausalImpact")`, Brodersen et al., Annals of
Applied Statistics (2015). http://google.github.io/CausalImpact/"
Applied Statistics (2015). https://google.github.io/CausalImpact/"

To find out which package version you are using, type
`packageVersion("CausalImpact")`. See the bottom of this page for full
Expand Down Expand Up @@ -422,11 +422,11 @@ the plot, see
Brodersen KH, Gallusser F, Koehler J, Remy N, Scott SL.
Inferring causal impact using Bayesian structural time-series models.
*Annals of Applied Statistics*, 2015, Vol. 9, No. 1, 247-274.
http://research.google.com/pubs/pub41854.html
https://research.google/pubs/pub41854/

### Discussion forum

http://stats.stackexchange.com/
https://stats.stackexchange.com/


- - -
Expand Down

0 comments on commit 3179767

Please sign in to comment.