5

Here is my question:

1) I ran a multiple linear regression: suppose like:

lm(attitude~quality+price+location+Income)

I mainly care about the relationship between attitude and quality, and other variables are control variables.

2) Then I wanted to do a scatter plot between attitude and quality. It is easy:

Q <-ggplot(data=data, aes(x=quality, y=attitude)) 
Q + geom_point(size = 1)

3) I further wanted to plot the fitted line between x and y, and the slope should be the partial regression coefficient from the multiple linear regression. That is, it should be the b1 in the following formula: attitude=b1*quality+b2*price+b3*location+b4*Income, rather than the b in the following formula: attitude=b*quality. Therefore, the following code does not work correctly, as it will plot the slope of b rather than b1.

g <- g + geom_smooth(method = lm)

Someone asked a very similar question, see here

The answer provided looks like this (replaced with my variables):

g <- g + geom_smooth(data=data, aes(x=quality, y=attitude, ymin=lcl, ymax=ucl))

However, this is a LOWESS plot (as you can see the figure posted in the post), not a linear straight line plot.

My question: how can I add a straight line of slope b1, with confidence interval band?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Will
  • 51
  • 4
  • 2
    Maybe `sjPlot` package has some convenient functions: [Plotting Marginal Effects of Regression Models](http://www.strengejacke.de/sjPlot/articles/plot_marginal_effects.html) – Henrik Jul 03 '18 at 06:50
  • Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – Axeman Jul 03 '18 at 09:49
  • This [question](https://stackoverflow.com/questions/42191567/how-to-plot-predictions-of-binomial-glm-that-has-both-continuous-and-categorical/42191835#42191835) gives the exact code needed to this, except that it concerns a glm instead of lm, so I'd say close enough for a duplicate? – Axeman Jul 03 '18 at 11:13
  • Hi (1) Thanks zx8754's edit. (2) Thanks Axeman's link and comment, but I can not understand their question and code very well. (3) Thanks Henrik's link. The sjPlot works, and I wrote the following code: plot_model(fit, type = "pred", terms = "quality", show.data = TRUE). I have a following question for that: is it possible to add a layer to use the geom_label_repel, as I wanted to show the label for the points on the scatter plot. It seems the plot_model does not allow to add this layer: geom_label_repel(aes(x=quality, y=attitude, label = Region)), even though I can do it in ggplots. – Will Jul 03 '18 at 21:13
  • @Will You may have a look at the related [`ggeffects`](https://cran.r-project.org/web/packages/ggeffects/) package if you 'only' want to grab the relevant data and then customize the plots yourself. – Henrik Jul 04 '18 at 12:27

1 Answers1

0

If you want to see b1, you should draw the partial regression plots, as I know.

In this case,

  1. regress attitude on every variable except quality
  2. regress quality on the other predictors
  3. plot residual of these fits:

    X <-
      data_frame(
        x = lm(quality ~.-attitude, data = data)$resid,
        y = lm(attitude ~ .-quality, data = data)$resid
      )
    X %>%
      ggplot(aes(x, y)) +
      geom_smooth(method = "lm")
    

This line might be same as b1 although not x,y points.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
younggeun
  • 923
  • 1
  • 12
  • 19