1
$\begingroup$

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?

$\endgroup$
4
  • 3
    $\begingroup$ Deciding to include a predictor based on its individual association with outcome can lead to omitted-variable bias, not taking into account the contributions of other predictors to outcome. It's a particularly bad problem with logistic regression. Forward model selection with stepAIC() is highly problematic. Look at Chapter 4 of Frank Harrell's Regression Modeling Strategies for principled ways to build multiple regression models. $\endgroup$
    – EdM
    Commented Nov 12 at 14:58
  • 1
    $\begingroup$ Along the lines of @EdM's valuable comments, never look at individual p-values for spline components and never allow the components to be selected individually. Make heavy use of "chunk tests". E.g. rms::anova.rms automatically pools effects of spline components. $\endgroup$ Commented Nov 12 at 15:37
  • $\begingroup$ Thank you both for the feedback, now from what i read i understand that using for instance a LASSO penalization is a more stable and reliable method in order to have a better generalization of the model, rather than using model selections algorithms. My question now is this: is using random forest to select n variables for the model (without overfitting it) a good idea? $\endgroup$
    – Leo_Miche
    Commented Nov 12 at 16:24
  • 1
    $\begingroup$ It would help other visitors to the site if you would edit the question to include why you now want to use random forest. Comments are very easy to overlook and can even be deleted. When you do that, please also explain why you need to select variables at all. There are penalization methods besides LASSO that can minimize overfitting while using information from all available predictors. Also, recognize that while predictions from LASSO can be reasonably stable and reliable, the sets of particular variables it selects from multiple correlated predictors typically aren't stable. $\endgroup$
    – EdM
    Commented Nov 12 at 17:12

1 Answer 1

1
$\begingroup$

Here are some options for automatically determining linear or non-linear effect and doing variable selection, while appropriately accounting for uncertainty in the entire process when doing statistical inference:

  1. Bayesian logistic generalized additive model (GAM) with variable selection

  2. The frequentist counterpart to (1) is here with instructions for variable selection here. For numeric variables, you can add them as + s(x) in the model formula, and this will automatically estimate the effect shape (linear/non-linear).

  • I'm not 100% sure if the approach in (2) will result in valid pvalues/confidence intervals, but the approach in (1) will yield valid inferences via Bayes Theorem.
  • An alternative frequentist approach for GAM with variable selection is here. I don't think they provide pvalues/CIs though.

EDIT: I should add, if you don't have that many predictors, why not just fit the full model (using your current approach with rcs() for numeric features OR fit a GAM with mgcv R pacakge), and do inference from that model? The full model will have pvalues/CIs that appropriately account for uncertainty in all effects. Getting correct inference after variable selection is hard, but the above Bayesian method is promising.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.