How do I include regression lines for all 6 characteristics of a variable

For my task I have to draw a scatterplot. Which I have done here:

plot(airbnb$distance_center,airbnb$price,pch=13,xlab="distance_center",ylab="price",bty="n")

Then I have to include regression lines that show the correlation of price and distance_center for all 6 characteristics of my variable "number_of_people"

Here my model:

mod<-lm(price~number_people+distance_center)

My try:

abline(mod$coeff[1]+mod$coeff[3],mod$coeff[2],col=2,lwd=2)

abline(mod$coeff[2]+mod$coeff[3],mod$coeff[2],col=2,lwd=2)

abline(mod$coeff[3]+mod$coeff[3],mod$coeff[2],col=2,lwd=2)

it shows 3 increasing ablines but I don't quite understand this way, I am not sure how to include all 6 as even if I increase it only shows 3. And I don't think this is right, has anyone a right idea?

First, I assume that by characteristics of the number_people variable you mean the possible values, such as 1 to 6.

Second, mod$coeff[1] is the intercept of the estimated equation and mod$coeff[2] and mod$coeff[3] are the slope coefficients for the number_people and distance_center variables, respectively. The slope for all of your ablines should be mod$coeff[3], not mod$coeff[2]

Third, the intercepts of the ablines are the intercept of the estimated equation mod$[1] plus the effects of different values of number_people. You need to multiply each possible value of number_people by the slope coefficient for number_people, mod$coeff[2]. Assuming the six possible values are 1 to 6:

abline(mod$coeff[1] + mod$coeff[2]*1, mod$coeff[3], col = 2, lwd = 2)
abline(mod$coeff[1] + mod$coeff[2]*2, mod$coeff[3], col = 2, lwd = 2)
abline(mod$coeff[1] + mod$coeff[2]*3, mod$coeff[3], col = 2, lwd = 2)
abline(mod$coeff[1] + mod$coeff[2]*4, mod$coeff[3], col = 2, lwd = 2)
abline(mod$coeff[1] + mod$coeff[2]*5, mod$coeff[3], col = 2, lwd = 2)
abline(mod$coeff[1] + mod$coeff[2]*6, mod$coeff[3], col = 2, lwd = 2)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.