Wboy - 1 year ago 68
R Question

# Logistic regression plot in R gives a straight line instead of an S-shape curve

I was plotting the results of a logistic regression, but instead of the expected S curve, I got a straight line like this:

This was the code that I was using:

I created a range of data from the original x-axis, converted it to data frame, and then predicted and drew the lines.

`````` model = glm(SHOT_RESULT~SHOT_DISTANCE,family='binomial',data = df_2shot)
summary(model)
#Eqn : P(SHOT_RESULT = True) = 1 / (1 + e^-(0.306 - 0.0586(SHOT_DISTANCE)))

r = range(df_2shot\$SHOT_DISTANCE) # draws a curve based on prediction
x_range = seq(r[1],r[2],1)
x_range = as.integer(x_range)
y = predict(model,data.frame(SHOT_DISTANCE = x_range),type="response")
plot(df_2shot\$SHOT_DISTANCE, df_2shot\$SHOT_RESULT, pch = 16,
xlab = "SHOT DISTANCE", ylab = "SHOT RESULT")
lines(x_range,y)
``````

Side note: I was following this tutorial: http://www.theanalysisfactor.com/r-glm-plotting/

Any insights would be appreciated! Thank you! :)

Haha, I see what happened. It is because of the range you plot. I saw the functional form of the curve from your comment line, and I define it as a function:

``````f <- function (x) 1 / (1 + exp(-0.306 + 0.0586 * x))
``````

Now, if we plot

``````x <- -100 : 100
plot(x, f(x), type = "l")
``````

Logistic curve has a near linear shape in the middle. That is what you arrived at!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download