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?enter image description here I am not able to understand how such kind of non-relationship of independent variable with response variable will logistic regression handle?

  • $\begingroup$ It would help if you included the plot. // It is routine to use nonlinear features in a logistic regression, such as splines. $\endgroup$
    – Dave
    Aug 13, 2021 at 18:00
  • 1
    $\begingroup$ 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? $\endgroup$
    – EdM
    Aug 13, 2021 at 18:04
  • $\begingroup$ @EdM these are perfectly simulated data. Actually i just wanted to understand for such kind of relationship how logistic regression account non-linear relationship. $\endgroup$
    – sandip
    Aug 14, 2021 at 2:02

1 Answer 1


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)
> quadTest <- NULL
> for (x in 1:100){quadTest <- c(quadTest,rbinom(1,1,((x-50.5)^2)/2500))}
> plot(quadTest, xlab="X",ylab="event")

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

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).

> quadDF <- data.frame(X=1:100,event=quadTest)
> ddQuad <- datadist(quadDF)
> options(datadist="ddQuad")
> quadSpline5 <- lrm(event~rcs(X,5),data=quadDF, x=TRUE, y=TRUE)

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 <- data.frame(Predict(quadSpline5))
> 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:

predicted versus actual log odds

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") +

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.