predict_counterfactuals {beeca}R Documentation

Predict counterfactual outcomes in GLM models

Description

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.

Usage

predict_counterfactuals(object, trt)

Arguments

object

a fitted glm object for which counterfactual predictions are desired.

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 object.

Details

The function works by creating two new datasets from the original data used to fit the GLM model. In these datasets, the treatment variable is set to each of its levels across all records (e.g., patients).

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().

Value

an updated glm object appended with an additional component counterfactual.predictions.

This component contains a tibble with two columns: cf_pred_0 and cf_pred_1, representing counterfactual predictions for each level of the treatment variable. A descriptive label attribute explains the counterfactual scenario associated with each column.

See Also

average_predictions() for averaging counterfactual predictions.

get_marginal_effect() for estimating marginal effects directly from an original glm object

Examples

# 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)


[Package beeca version 0.1.3 Index]