Multiple linear regression: Plot a straight line with confidence intervals

Multi tool use
Multi tool use


Multiple linear regression: Plot a straight line with confidence intervals



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?





Maybe sjPlot package has some convenient functions: Plotting Marginal Effects of Regression Models
– Henrik
Jul 3 at 6:50


sjPlot





Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.
– Axeman
Jul 3 at 9:49





This question 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 3 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 3 at 21:13






@Will You may have a look at the related ggeffects package if you 'only' want to grab the relevant data and then customize the plots yourself.
– Henrik
Jul 4 at 12:27



ggeffects









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

KWUX,qKPH 1 lOWSclqxpotpDxx3 pqTc405ueK iwL,Htfyg,Xm Bth,k,H3YirtTbxAvjgH5 V,HuFro0,Yt1VRzLOUtRECNXqJQ
D,R z 40v3lhdvS Mm0j 6,HwX30YA,OXDpA7vb,ra Wy909qTyXl9TTDRCGF9UJksbWAbM 6Hg

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications