Reputation: 543
I want to plot a logistic regression curve of my data, but whenever I try to my plot produces multiple curves. Here's a picture of my last attempt:
Here's the relevant code I am using:
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)
My professor uses the following code, but when I try to run it I get an error on the last line saying that the x and y lengths do not match:
# 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)
Any help would be appreciated.
Edit:
As requested, reproduceable code using the mtcars dataset:
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)
Upvotes: 18
Views: 64930
Reputation: 51
Here's a function (based on Marc in the box's answer) that will take any logistic model fit using glm and create a plot of the logistic regression curve:
plot_logistic_curve = function(log_mod){
mod_frame = model.frame(log_mod)
var_names = names(mod_frame)
newdat = setNames(data.frame(seq(min(mod_frame[[2]]), max(mod_frame[[2]]), len=100)), var_names[2])
newdat[var_names[1]] = predict(log_mod, newdata = newdat, type="response")
plot(mod_frame[[1]] ~ mod_frame[[2]], col = "red4", xlab = var_names[[2]], ylab = var_names[[1]])
lines(newdat[[var_names[2]]], newdat[[var_names[1]]], col = "green4", lwd = 2)
}
Upvotes: 3
Reputation: 12005
fit = glm(vs ~ hp, data=mtcars, family=binomial)
newdat <- data.frame(hp=seq(min(mtcars$hp), max(mtcars$hp),len=100))
newdat$vs = predict(fit, newdata=newdat, type="response")
plot(vs~hp, data=mtcars, col="red4")
lines(vs ~ hp, newdat, col="green4", lwd=2)
Upvotes: 35