I am trying to build a logistic regression model in R and I'm examining whether some covariates may follow a non linear distribution by using the splines function. The dataset I am using is the Wisconsin Breast Cancer: For example, I checked the linearity for the column fractal_dimension_mean by doing
fit.splines.fractal <- lrm(diagnosis ~
rcs(fractal_dimension_mean, 4), data=data)
print(fit.splines.fractal) # non linear
and I obtained this:
Coef S.E. Wald Z Pr(>|Z|)
Intercept -2.4383 0.4609 -5.29 <0.0001
fractal_dimension_mean -2.1338 0.4876 -4.38 <0.0001
fractal_dimension_mean' 9.3886 2.3722 3.96 <0.0001
fractal_dimension_mean'' -23.0268 6.2039 -3.71 0.0002
Now from what i can understand it seems that the distribution of this variable is non linear and therefore I should consider adding the complex form of this column into the model. So what I did was creating a function to add the complex forms of those variable and try using the forward selection method for the model variables:
spline_vars <- c("texture_se", "fractal_dimension_mean")
# formula:
formula <- as.formula(paste(
"diagnosis ~",
paste(
lapply(names(data), function(var) {
if (var %in% spline_vars) {
paste0("rcs(", var, ", 4)") # rcs() for splines
} else if (var != "diagnosis") {
var # keep the variables without transformation
}
}),
collapse = " + "
)
))
model_null <- glm(diagnosis ~ 1, data = data, family = binomial) # null model
model_full <- glm(formula, data = data, family = binomial) # full model
# Forward selection
model_forward <- stepAIC(model_null, scope = list(lower = model_null,
upper = model_full), direction = "forward")
summary(model_forward)
Now because of the significance of the variables with the transformation I thought that the model would have had those as covariates, but instead I obtained a bigger AIC and log-likelihood than using a forward selection method without transformation. Am I doing something wrong?
stepAIC()
is highly problematic. Look at Chapter 4 of Frank Harrell's Regression Modeling Strategies for principled ways to build multiple regression models. $\endgroup$rms::anova.rms
automatically pools effects of spline components. $\endgroup$