我想绘制我的数据的逻辑回归曲线,但每次尝试时,我的图形都会产生多条曲线。这是我最后一次尝试的图片:
last attempt
这是我正在使用的相关代码:
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)
newdata=hr
?你有newdata=dat
。 - Marc in the box