rlinear-regressionscatter-plotabline

How do I make a regression line for all values of my variable?


My plot:

plot(airbnb$distance_center,airbnb$price,pch=13,xlab="distance_center",ylab="price",bty="n")
mod <- lm(price~number_people+distance_center)

Now I want to include regression lines that show the correlation of price & distance_center - including all 6 characteristics of my variable "number_people"

My idea:

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

The Plot

Unfortunately, I don't think this is quite right, I am not sure how to make a right step. Any idea?


Solution

  • You didn't post your data, so it's difficult to tell exactly how it's structured (is "number_people" numeric or factor?). In either case, you will need one regression line for each unique number_people.

    Here I have created a similar data set with the same names as yours:

    mod <- lm(price ~ number_people + distance_center, data = airbnb)
    
    plot(airbnb$distance_center, airbnb$price,
         pch = 13, xlab = "distance_center", ylab = "price", bty = "n")
    
    invisible(sapply(unique(airbnb$number_people), function(x) {
      abline(a = coef(mod)[1] + x * coef(mod)[2], b = coef(mod)[3])
    }))
    


    Made up data

    set.seed(1)
    
    airbnb <- data.frame(
      distance_center = sqrt(runif(100, 0, 10)^2 + runif(100, 0, 10)^2),
      number_people = sample(6, 100, TRUE))
      
    airbnb$price <- 50 + airbnb$number_people * 
                    50 / airbnb$distance_center + rnorm(100, 0, 5)^2
    

    Created on 2022-05-28 by the reprex package (v2.0.1)