Visualizing Regression models in R (ggplot2), including interaction effects and 3D
HTML-код
- Опубликовано: 9 фев 2025
- Regression models continue to be very popular in Statistics, Data Mining and Machine Learning. How can we visualize them? We look at simple linear regression, a parallel slopes model, an interaction effect, and a 3D visualization of two continuous predictors. Finally we look at some diagnostic plots.
The broom package offers the very convenient augment function which helps to use model predictions for plotting.
Packages used: ggplot2, broom, plotly, ggfortify (and dplyr for some data preparation)
Github and code:
github.com/fjo...
Contact me, e. g. to discuss (online) R workshops / trainings / webinars:
LinkedIn: / wolfriepl
Twitter: / statistikindd
Xing: www.xing.com/p...
Facebook: / statistikdresden
statistik-dres...
R Workshops: statistik-dres...
Blog (German, translate option): statistik-dres...
Hi! Interesting video, I have a question about the 3d visualization with the plane, you mention that you made the matrix manually? or is it automatically adjusted within the plotly function in R? If no then how do you make the matrix of the 3d plane? do you have the tutorial for it? thanks!
Sorry, missed your comment.
Github repo: github.com/fjodor/visualizing-regression
Plane is created manually in two steps: Create a grid of values (combinations of the two predictors), then add model predictions.
See the Visualizing-Regression-models.R file at Github.
Thanks a lot! Do you know how to add confidence intervals to the regression lines, using for example geom_ribbon? Tried to combine the augment() to fit the regression lines, and then add confidence intervals using tidy(), but cannot make it work. Thanks in advance!
The easiest way is probably to use the se parameter in the call to geom_smooth. Specify se = TRUE. Does that help?
Unfortunately not. This adds a new regression line for each group that is printed as a new layer on top of the "geom_line(aes(y = .fitted), ...)" that uses data from a fitted linear model. But thanks anyway!
@@melkerjohansson9165 Sorry for late reply. Here's a shot at geom_ribbon:
geom_ribbon(aes(ymin = .fitted - 2 * .se.fit, ymax = .fitted + 2 * .se.fit), alpha = 0.2) +
Full code for parallel slopes model (i. e. effect of disp on mpg, separate lines for automatic and manual):
library(dplyr)
mtcars$am
I just feel that using broom::augment may become more complicated than using the original data and applying geom_smooth. I just wanted to show that the model built "by hand" (using the lm function) yields the same result as the geom_smooth call.
Thanks. I ended up using "geom_ribbon(aes(ymin=.fitted-1.96*.se.fit, ymax=.fitted+1.96*.se.fit), alpha=0.2, linetype=0) + ..." just to make sure I got them right. And now I compared the two methods again, and I think geom_smooth(method = lm) gives slightly wider CIs both in your example and using my own data.
what is the plane? in your code u did not show where u obtain the plane from
Sorry, missed your comment.
Github repo: github.com/fjodor/visualizing-regression
Plane is created manually in two steps: Create a grid of values (combinations of the two predictors), then add model predictions.
See the Visualizing-Regression-models.R file at Github.
@@StatistikinDD Dear statistikin, i have succesfully made model 1, 2 and 3 from this file. However, as soon as i make model 4, it keeps telling me "object 'plane' not foun"... Do you know what my problem is here? Can you help me? Thanks in advance!
@@2mrRB Did you use the Visualizing-Regression-models.R file? The interactive 3D visualization should work - at least if you run it in plain R, not RStudio (maybe Javascript conflicts, didn't run in a stable way for me).
Apparently there was a non-critical bug in the rm() command in line 99, which tried to delete plane2 and m, which didn't exist. Sorry, updated the file on my github.
Awesome!
Thanks! All the best for your own visualizations!