【发布时间】:2016-08-09 17:10:05
【问题描述】:
我想绘制我的数据的逻辑回归曲线,但每当我尝试绘制时,都会产生多条曲线。这是我最后一次尝试的照片:
这是我正在使用的相关代码:
fit = glm(output ~ maxhr, data=heart, family=binomial)
predicted = predict(fit, newdata=heart, type="response")
plot(output~maxhr, data=heart, col="red4")
lines(heart$maxhr, predicted, col="green4", lwd=2)
我的教授使用以下代码,但是当我尝试运行它时,我在最后一行收到错误,提示 x 和 y 长度不匹配:
# fit logistic regression model
fit = glm(output ~ maxhr, data=heart, family=binomial)
# plot the result
hr = data.frame(maxhr=seq(80,200,10))
probs = predict(fit, newdata=dat, type="response")
plot(output ~ maxhr, data=heart, col="red4", xlab ="max HR", ylab="P(heart disease)")
lines(hr$maxhr, probs, col="green4", lwd=2)
任何帮助将不胜感激。
编辑:
根据要求,使用 mtcars 数据集的可重现代码:
fit = glm(vs ~ hp, data=mtcars, family=binomial)
predicted= predict(fit, newdata=mtcars, type="response")
plot(vs~hp, data=mtcars, col="red4")
lines(mtcars$hp, predicted, col="green4", lwd=2)
【问题讨论】:
-
所以有lots of questions on plotting logistic regression curves。他们中的任何一个有帮助吗?
-
请参阅提供的链接 eipi,或使您的示例可重现。模拟一些适合您已经提供的代码的数据。
-
我确实尝试过先搜索 SO,但大多数问题涉及的内容超出了我的想象,或者没有解决我遇到的问题。我发布了一些使用内置 mtcars 数据集的代码,以便可以重现问题。
-
newdata=hr?你有newdata=dat
标签: r plot statistics regression