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...

Комментарии •

  • @muhammadfareza3107
    @muhammadfareza3107 5 лет назад +2

    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!

    • @StatistikinDD
      @StatistikinDD  5 лет назад +1

      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.

    • @StatistikinDD
      @StatistikinDD  5 лет назад

      See the Visualizing-Regression-models.R file at Github.

  • @melkerjohansson9165
    @melkerjohansson9165 6 лет назад

    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!

    • @StatistikinDD
      @StatistikinDD  6 лет назад

      The easiest way is probably to use the se parameter in the call to geom_smooth. Specify se = TRUE. Does that help?

    • @melkerjohansson9165
      @melkerjohansson9165 6 лет назад

      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!

    • @StatistikinDD
      @StatistikinDD  6 лет назад

      @@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

    • @StatistikinDD
      @StatistikinDD  6 лет назад

      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.

    • @melkerjohansson9165
      @melkerjohansson9165 6 лет назад

      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.

  • @isaackyere1148
    @isaackyere1148 5 лет назад +1

    what is the plane? in your code u did not show where u obtain the plane from

    • @StatistikinDD
      @StatistikinDD  5 лет назад

      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.

    • @StatistikinDD
      @StatistikinDD  5 лет назад

      See the Visualizing-Regression-models.R file at Github.

    • @2mrRB
      @2mrRB 3 года назад

      @@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!

    • @StatistikinDD
      @StatistikinDD  3 года назад

      @@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.

  • @micholaileh8699
    @micholaileh8699 6 лет назад

    Awesome!

    • @StatistikinDD
      @StatistikinDD  6 лет назад

      Thanks! All the best for your own visualizations!