Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot logistic regression curve in R

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:

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)
like image 351
cafemolecular Avatar asked Apr 18 '16 05:04

cafemolecular


People also ask

How do you plot a logistic regression graph in R?

To plot the logistic regression curve in base R, we first fit the variables in a logistic regression model by using the glm() function. The glm() function is used to fit generalized linear models, specified by giving a symbolic description of the linear predictor.

How do you visualize logistic regression?

To visualize the logistic regression fit, we first use the predict function to generate the model predictions about probability of survival as a function of age. Having generated the predicted probabilities of survival we can then add these prediction lines to our previous plot using geom_line .

Can you graph a logistic regression?

However, the advantage of logistic regression is that any number of variables can be included, and if desired, all predictor variables may be categorical. In SPSS, you can graph a logistic regression through the "Options" menu of the "Binary logistic regression" window.


2 Answers

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)

enter image description here

like image 108
Marc in the box Avatar answered Sep 19 '22 10:09

Marc in the box


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)
} 
like image 40
rheery Avatar answered Sep 19 '22 10:09

rheery