关于CausalImpact/bsts纳入先验概率及M参数设置的技术问询
Answer to your CausalImpact Model Size M Question
Great question! Let's unpack how this works in the CausalImpact package:
Where is the M/J prior selection logic implemented?
The core logic for covariate selection with the M/J prior lives in two key parts of the package:
- Internal model-building functions: When you call
CausalImpact(), it uses helper functions (likebuildModel()—you can inspect this in R withgetAnywhere(buildModel)) to calculate the prior probability for covariate inclusion. This probability is exactly theM/Jvalue referenced in the paper, whereJis your total number of covariates. - Underlying Stan model code: The actual prior distribution is defined in the package's Stan model file. You can locate this file on your system with this R command:
Inside the file, you’ll find code for the inclusion indicator variables (typically namedsystem.file("stan", "causalimpact.stan", package = "CausalImpact")gamma) that uses a Bernoulli prior tied to theprior_inclusion_probparameter. This parameter is the computedM/Jvalue.
If you don’t specify M, the package uses a default (often a 50% inclusion rate, i.e., M = J/2) when no explicit guidance is given.
Can you set a custom M?
There’s no direct M parameter exposed in the CausalImpact() function, but you can achieve this indirectly by calculating the corresponding inclusion probability and passing it via the model.args parameter:
- Calculate the target probability:
prob = M / J(replaceMwith your expected model size andJwith your total number of covariates) - Pass this probability when initializing your
CausalImpactobject:ci <- CausalImpact(data, pre.period, post.period, model.args = list(prior.inclusion.prob = prob))
This lets you explicitly control the expected model size M by aligning the prior inclusion probability with your desired M/J ratio.
内容的提问来源于stack exchange,提问作者Booley
相关产品推荐
相关产品推荐

