Plot logistic regression curve in R

0 votes

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)
Mar 26, 2022 in Machine Learning by Dev
• 6,000 points
2,424 views

1 answer to this question.

0 votes

The Code looks something like this:

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)

​The Graph looks like this:

enter image description here
The predictions are made with the newdatobject. This dataset has a considerably better resolution of possible hpvalues than the original dataset, and it is organised to make charting easier. When you constructed hr, you were on your way to implementing it right, but you didn't use it in the prediction stage, instead using newdata=dat.

Elevate Your Expertise with Our Machine Learning Certification Program!

answered Apr 4, 2022 by Nandini
• 5,480 points

Related Questions In Machine Learning

0 votes
1 answer

Plotting logistic regression in R with the Smarket dataset

The first, third, and fourth methods of ...READ MORE

answered Apr 12, 2022 in Machine Learning by Dev
• 6,000 points
999 views
0 votes
1 answer

Can we change the sigmoid with tanh in Logistic regression transforms??

Hi@Deepanshu, Yes, you can use tanh instead of ...READ MORE

answered May 12, 2020 in Machine Learning by MD
• 95,460 points
2,730 views
0 votes
1 answer
0 votes
1 answer

Screening (multi)collinearity in a regression model

The kappa() function can be of assistance. ...READ MORE

answered Mar 30, 2022 in Machine Learning by Dev
• 6,000 points
706 views
0 votes
1 answer

Save a plot as image on the disk using R

Consider for both the situations: 1. Image will ...READ MORE

answered Apr 14, 2018 in Data Analytics by Sahiti
• 6,370 points
2,159 views
0 votes
1 answer

How to add regression line equation and R2 on graph?

Below is one solution: # GET EQUATION AND ...READ MORE

answered Jun 1, 2018 in Data Analytics by DataKing99
• 8,250 points
6,743 views
0 votes
1 answer

How to export regression equations for grouped data?

First, you'll need a linear model with ...READ MORE

answered Mar 14, 2022 in Machine Learning by Dev
• 6,000 points
595 views
0 votes
1 answer

R: Calculate and interpret odds ratio in logistic regression

A logit, or the log of the ...READ MORE

answered Apr 4, 2022 in Machine Learning by Nandini
• 5,480 points
17,084 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP