View source: R/predict_counterfactuals.R
predict_counterfactuals | R Documentation |
This function calculates counterfactual predictions for each level of a specified treatment variable in a generalized linear model (GLM). It is designed to aid in the assessment of treatment effects by predicting outcomes under different treatments under causal inference framework.
predict_counterfactuals(object, trt)
object |
a fitted |
trt |
a string specifying the name of the treatment variable
in the model formula. It must be one of the linear predictor variables used
in fitting the |
The function works by creating new datasets from the original data used to fit the GLM model. In these datasets, the treatment variable for all records (e.g., patients) is set to each possible treatment level.
Predictions are then made for each dataset based on the fitted GLM model, simulating the response variable under each treatment condition.
The results are stored in a tidy format and appended to the original model object for further analysis or inspection.
For averaging counterfactual outcomes, apply average_predictions()
.
an updated glm
object appended with an
additional component counterfactual.predictions
.
This component contains a tibble with columns representing counterfactual
predictions for each level of the treatment variable. A descriptive label
attribute explains the counterfactual scenario associated with each column.
average_predictions()
for averaging counterfactual
predictions.
get_marginal_effect()
for estimating marginal effects directly
from an original glm
object
# Preparing data and fitting a GLM model
trial01$trtp <- factor(trial01$trtp)
fit1 <- glm(aval ~ trtp + bl_cov, family = "binomial", data = trial01)
# Generating counterfactual predictions
fit2 <- predict_counterfactuals(fit1, "trtp")
# Accessing the counterfactual predictions
fit2$counterfactual.predictions
attributes(fit2$counterfactual.predictions)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.