Predict the standard error from a prediction formula using the delta method
Source:R/deltamethod_pred_function.R
deltamethod_pred_function.RdConstructs a prediction function that computes model predictions, their standard errors and Wald confidence interval using the delta method for a user-specified prediction formula.
Arguments
- prediction_str
Character. A string specifying the prediction formula, which must contain one occurrence of
"LP"to be replaced by the linear predictor constructed from the coefficients and covariate values.- coefs
Named numeric vector. The estimated coefficients from the fitted model. The names must correspond to the covariate names used in the model.
- coef_cov
Square numeric matrix. The covariance matrix of the estimated coefficients in
coefs. The row and column names must match the names ofcoefs.- additional_coefs
Character vector. Names of coefficients in
coefsthat are not part of the linear predictor but are required in the prediction formula (default is an empty character vector). An example is the estimated scale from a parametric survival model.- fixed_vars
Named numeric vector. Fixed values for variables used in the prediction formula but not present in the new data (default is an empty numeric vector).
- logit
Logical.
TRUE, the delta method is used on the logit scale. This ensures CIs are between 0 and 1 (default isTRUE).
Value
A function(df, z = 1.96) that takes a data frame of covariates
df and returns a data frame with columns for the
prediction prediction, lower lower and upper upper Wald
confidence intervals, and standard error se. The function can also take
an optional argument z for the z-score used in confidence
interval calculation (default is 1.96 for 95% confidence intervals).
Details
This function is intended for use with fitted regression models, including those for survival analysis, and allows flexible specification of the linear predictor, additional coefficients, and fixed covariate values. The resulting function can be applied to new data to obtain predictions, delta method-based standard errors and confidence intervals.
This function is used internally in the package by
deltamethod_from_model().
For a prediction function \(g(\theta)\), the standard error is approximated as \[\sqrt{\nabla g(\theta)^\top \Sigma \nabla g(\theta)}\] where \(\Sigma\) is the covariance matrix of the estimated coefficients.
The input prediction_str must contain one occurrence of
"LP", which will be replaced by the linear predictor constructed
from the provided coefficients and covariate values.
With logit=TRUE the confidence intervals can be calculated on a logit
scale, see IPCWJK for more information.
Examples
coefs <- c("(Intercept)" = 0.5, "age" = 0.1, "sex" = -0.2)
coef_cov <- diag(c(0.01, 0.0025, 0.0025))
rownames(coef_cov) <- colnames(coef_cov) <- names(coefs)
pred_fun <- deltamethod_pred_function(
prediction_str = "1 / (1 + exp(-(LP)))",
coefs = coefs,
coef_cov = coef_cov
)
newdata <- data.frame(age = c(50, 60), sex = c(1, 0))
pred_fun(newdata)
#> prediction lower upper se
#> 1 0.9950332 0.9707925 1.019274 0.012367680
#> 2 0.9984988 0.9896802 1.007317 0.004499284