0

Somewhat newbie question here. I'd like to use ggplot2 to plot my multiple regression model, but I ran into an error. I did some research on the forum before deciding to ask the community.

I understand how to plot a simple linear regression:

ggplot(data, aes(x=X, y=Y))+geom_point()+ 
  geom_smooth(method='lm',formula=Y~X)

But when I tried to do it for multiple regression:

Model<-lm(Y~x1*x2*x3*x4*x5, data, na.action=na.omit)

ggplot(data, aes(x=X, y=Model))+geom_point()+ 
  geom_smooth(method='lm',formula=Y~x1*x2*x3*x4*x5)

I get this error:

Don't know how to automatically pick scale for object of type lm. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (209): x, y
  • 1
    Questions about *programming, debugging, or performing routine operations within a statistical computing platform* are [off-topic](http://stats.stackexchange.com/help/on-topic) here. You can find several online tutorials/courses about how to use`ggplot2` package. Here is one example: http://r-statistics.co/ggplot2-Tutorial-With-R.html . You might also want to check *ggplot2: Elegant Graphics for Data Analysis* by Hadley Wickham. If you check these sources you can see that in `aes()` you need to use variables. – T.E.G. Feb 14 '17 at 04:11
  • 1
    If you want to learn about plotting multiple regression model, check this question: http://stats.stackexchange.com/questions/73320/how-to-visualize-a-fitted-multiple-regression-model and this one: http://stats.stackexchange.com/questions/89747/how-to-describe-or-visualize-a-multiple-linear-regression-model – T.E.G. Feb 14 '17 at 04:12
  • You can't pass a model to ggplot2 like this. – Roland Feb 14 '17 at 08:25

1 Answers1

3

As the comments state, there are various answers to this, but two points of a more statistical or statistical thought nature:

  1. This is probably not the model you want to use. y~x1*x2*x3*x4 is like a model which translates to a quadruple-interaction with all possible sub-interaction (triple and double) which is hardly legible at the best of times. So unless you want a non-interaction model, in which case the * should be replaced with a +, you might want to think if the theoretical reasoning behind this kind of interaction is warranted. Sometime we want to cover our bases and create the most complex model we can with the data, but it will be difficult to interpret.
  2. I can't begin to imagine how a model with five predictors can be plotted, let alone with multiple interactions. Simple regression? Two dimensional plot. Two predictors? 3D plot (which in social sciences is rare to see). Three, four, five predictors? No idea how to plot together, and probably neither does ggplot. In ggplot, geom_smooth() is a line parameter, and hence needs to work with axis. Y is Y, X1 is X etc.. it just doesn't know what to do.
  3. Generally speaking, a plot is used for simplifying results. If it can't do that, it does not have much use.
Yuval Spiegler
  • 1,821
  • 1
  • 15
  • 31