R 中的逻辑回归图给出一条直线而不是 S 形曲线

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

我正在绘制逻辑回归的结果,但我得到的不是预期的 S 曲线,而是一条这样的直线:

这是我使用的代码:

我从原始的 x 轴创建了一系列数据,将其转换为数据框,然后进行预测并绘制线条。

 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)

旁注:我正在学习本教程:http://www.theanalysisfactor.com/r-glm-plotting/

如有任何见解,我们将不胜感激!谢谢! :)

哈哈,我明白了。这是因为您绘制的范围。我从你的评论里看到了曲线的函数形式,我把它定义为一个函数:

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

现在,如果我们绘制

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

Logistic 曲线在中间呈近乎线性的形状。这就是您的目的!