Hello there,
Hope everyone is having a wonderful holiday! In the photo below, I am trying to fit the following logistic regression model with rms’ lrm function in R, but I seem to be running into the shown errors/warnings and I don’t know why. Does the lrm function not support survey weights yet? I am currently using the svyglm function from survey instead and that is working fine for now.
Additionally, I plan on approximating the full model with a reduced model using the stepdown approach described by Harrell and Ambler, in which predictors and interactions that contribute the least to each model’s R2 will be dropped sequentially from each respective model until removing the next predictor or interaction would decrease R2 to less than 95% of the initial model’s R2. I didn’t find anything in the course notes about this approach or code for it, so below is the R function I made for it and I am wondering if it is correct.
# Function for stepdown procedure by Harrell and Ambler
stepdown <- function(full_model, data, threshold = 0.95) {
initial_r2 <- calculate_nagelkerke_r2(full_model, data)
current_model <- full_model
current_r2 <- initial_r2
removed_terms <- c()
# Get the terms in the model
terms_in_model <- attr(terms(full_model), "term.labels")
# Iteratively remove terms and check R²
while (length(terms_in_model) > 0) {
r2_drop <- sapply(terms_in_model, function(term) {
reduced_model <- try(update(current_model, as.formula(paste(". ~ . -", term))), silent = TRUE)
if (inherits(reduced_model, "try-error")) return(Inf)
calculate_nagelkerke_r2(reduced_model, data)
})
# Identify the term with the least impact on R²
term_to_remove <- terms_in_model[which.max(r2_drop)]
max_r2_drop <- r2_drop[which.max(r2_drop)]
# Check if the current R² after removing the term is still above the threshold
if (max_r2_drop < threshold * initial_r2) {
break
} else {
current_model <- update(current_model, as.formula(paste(". ~ . -", term_to_remove)))
terms_in_model <- setdiff(terms_in_model, term_to_remove)
removed_terms <- c(removed_terms, term_to_remove)
current_r2 <- max_r2_drop
}
}
return(current_model)
}
I would appreciate any feedback!
Thanks,
Raf