update_model {CausalQueries} | R Documentation |
Takes a model and data and returns a model object with data attached and a posterior model
update_model(model, data = NULL, data_type = "long", keep_fit = FALSE, ...)
model |
A |
data |
A |
data_type |
Either 'long' (as made by |
keep_fit |
Logical. Whether to append the |
... |
Options passed onto |
An object of class causal_model
. It essentially returns a list containing the elements comprising
a model (e.g. 'statement', 'nodal_types' and 'DAG') with the 'posterior_distribution' returned by stan
attached to it.
model <- make_model('X->Y') data_long <- simulate_data(model, n = 4) data_short <- collapse_data(data_long, model) model_1 <- update_model(model, data_long) ## Not run: # Throws error unless compact data indicated: model_3 <- update_model(model, data_short) ## End(Not run) model_4 <- update_model(model, data_short, data_type = 'compact') # It is possible to implement updating without data, in which case the posterior # is a stan object that reflects the prior model5 <- update_model(model) # Advanced: Example of a model with tailored parameters. # We take a model and add a tailored P matrix (which maps from parameters # to causal types) and a tailored parameters_df which reports that # all parameters are in one family. # Parameters in this example are not connected with nodal types in any way. model <- make_model('X->Y') P <- diag(8) colnames(P) <- rownames(model$causal_types) model <- set_parameter_matrix(model, P = P) model$parameters_df <- data.frame( param_names = paste0('x',1:8), param_set = 1, priors = 1, parameters = 1/8) # Update fully confounded model on strongly correlated data data <- make_data(make_model('X->Y'), n = 100, parameters = c(.5, .5, .1,.1,.7,.1)) fully_confounded <- update_model(model, data, keep_fit = TRUE) fully_confounded$stan_fit query_model(fully_confounded, 'Y[X = 1] > Y[X=0]', using = 'posteriors') # To see the confounding: with(fully_confounded$posterior_distribution %>% data.frame(), {par(mfrow = c(1,2)) plot(x1, x5, main = 'joint distribution of X0.Y00, X0.Y01') plot(x1, x6, main = 'joint distribution of X0.Y00, X1.Y01')})