# How logistic regression accounts non-linear relationship of independent variable with dependent class?

I have binary response variable and one independent variable.Wanted to fit logistic regression model. I have plotted response variable against independent variable which clearly shows non-linear relationship .Then how logistic regression account such non-linear relationship? What we need to do take this effect into account? I am not able to understand how such kind of non-relationship of independent variable with response variable will logistic regression handle?

• It would help if you included the plot. // It is routine to use nonlinear features in a logistic regression, such as splines.
– Dave
Aug 13, 2021 at 18:00
• If those are your raw data, there is essentially perfect separation of cases having predictor values in [30,60] from all the others. Does that make sense, based on your understanding of the subject matter?
– EdM
Aug 13, 2021 at 18:04
• @EdM these are perfectly simulated data. Actually i just wanted to understand for such kind of relationship how logistic regression account non-linear relationship. Aug 14, 2021 at 2:02

To illustrate the point that Dave made in a comment, here's how you can use restricted cubic splines to handle an extreme situation like this. Remember that linearity of a generalized linear model is in the coefficients; you can use any appropriate transformations/combinations of predictor values to handle non-linearities with respect to predictors themselves. Restricted cubic splines let the data tell you the shape of the non-linearity in the predictors, so you don't have to guess for yourself.

Generate some random binomial data with event probability related quadratically to values of x from 1 to 100, centered at x = 50.5 where the probability is exactly 0. Load the rms package to make things easier.

> library(rms)
> set.seed(101)

Here are the values, similar to yours but with some randomness:

Set up a data frame with those values, let rms evaluate the data distribution and fit to the predictor X with a 5-knot restricted cubic spline (rcs).

The overall fit has a concordance of 0.88; the model isn't terribly overfit, with an optimism-corrected concordance of 0.87 and slope optimism about 10%. (Done with validate() in rms; not shown.) Get a data frame with predicted log-odds values and 95% confidence intervals, then add known log odds based on the data model.

> predLO5[,"actualLO"] <- with(predLO5,log((((X-50.5)^2)/2500)/(1-(((X-50.5)^2)/2500))))

Plot* the actual and predicted (with 95% CI) log odds versus X:

Despite the singularity in log odds at X = 50.5 and the awkward shape of the actual log-odds plot, the fit is pretty good. Even with the minimum of 3 knots you get a fit with the same discrimination, but the sharp dip near X = 50 isn't captured so well in this type of plot.

Restricted cubic splines can be your friends. Get to know them well.

*Code for the plot; ggplot2 is loaded by rms:

> ggplot(predLO5,aes(x=X)) +
geom_line(aes(y=yhat,color="Predicted")) +
geom_ribbon(aes(ymin=lower,ymax=upper),alpha=0.3,color="gray") +
geom_line(aes(y=actualLO,color="actual")) +
ylab("Log Odds") +
theme(legend.position=c(0.75,0.25))