Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce construct.model parameter #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

luciferous
Copy link

@luciferous luciferous commented Jul 25, 2021

construct.model is a function that takes modeling data (NA in
post.period) and returns a bsts model.

This invocation:

go <- function(data) {
  y <- data[, 1]
  sdy <- sd(y, na.rm = TRUE)
  sd.prior <- SdPrior(sigma.guess = 0.01 * sdy,
                      upper.limit = sdy,
                      sample.size = 32)
  ss <- AddLocalLevel(list(), y, sigma.prior = sd.prior)
  bsts.model <- bsts(y, state.specification = ss, niter = 100,
                     seed = 1, ping = 0)
}

impact <- CausalImpact(data, pre.period, post.period,
                       construct.model = go)

is equivalent to:

model.args <- list(niter = 100)
impact <- CausalImpact(data, pre.period, post.period, model.args)

. This change provides an interface that has RunWithData's flexibility
with pre- and post-period, and RunWithBstsModel's full configurability
of the model. For example, the caller can now specify a custom model
where the post-period is before the end of the data, or with a gap
between the pre-period and post-period, either of which aren't possible
with the previous interface.

@luciferous
Copy link
Author

plot(impact) from the added test case.

Rplot construct-model

@luciferous
Copy link
Author

Hi @alhauser,

This proposal changes the API but in a backwards-compatible way. Let me know if you'd be open to it, I'm happy to chat about other ways to get the same outcome.

@luciferous luciferous marked this pull request as ready for review July 25, 2021 17:31
construct.model is a function that takes modeling data (NA in
post.period) and returns a bsts model.

This invocation:

    go <- function(data) {
      y <- data[, 1]
      sdy <- sd(y, na.rm = TRUE)
      sd.prior <- SdPrior(sigma.guess = 0.01 * sdy,
                          upper.limit = sdy,
                          sample.size = 32)
      ss <- AddLocalLevel(list(), y, sigma.prior = sd.prior)
      bsts.model <- bsts(y, state.specification = ss, niter = 100,
                         seed = 1, ping = 0)
    }

    impact <- CausalImpact(data, pre.period, post.period,
                           construct.model = go)

is equivalent to:

    model.args <- list(niter = 100)
    impact <- CausalImpact(data, pre.period, post.period, model.args)

. This change provides an interface that has RunWithData's flexibility
with pre- and post-period, and RunWithBstsModel's full configurability
of the model. For example, the caller can now specify a custom model
where the post-period is before the end of the data, or with a gap
between the pre-period and post-period, either of which aren't possible
with the previous interface.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant