2

I am trying to plot a 3D Multiple polynomial regression. I have two independent variables (area and number of bedrooms) and one dependent variable (price). Each independent variable has its own polynomial degree (the area has 3 degrees polynomial, and the number of bedrooms has 4 degrees polynomial).

So far, I was only able to plot in 3D with the package "scatterplot3d", but I was not able to create the fitted regression 3D plane in my plot.

My questions: I want to create a 3D graph with the fitted regression 3D plane and the scatter plot, which package should I use? How would the code look like to have the fitted regression 3D plane in my plot (if you have a general idea)?

Thank You for your help :)

Note : I am using r language in r studio cloud.

r2evans
  • 141,215
  • 6
  • 77
  • 149
Zach
  • 29
  • 1
  • 4
  • Following this link you can learn how to plot the 3D scatter plot with the regression plane using the package `plotly`: https://stackoverflow.com/questions/44322350/add-regression-plane-in-r-using-plotly/44324365#44324365 – Marco Sandri Oct 11 '19 at 21:47
  • FYI, I removed the [tag:rstudio] tag, since that's about RStudio, not the R language itself. I know you're using it in the RStudio Cloud, but I doubt that your question or issue is specific to that. – r2evans Oct 11 '19 at 21:51
  • Zach, this is close to *"recommend or find a book, tool, software library"* which is [off-topic](https://stackoverflow.com/help/on-topic). It would help immensely if you provided a reproducible question, including sample data and code you've tried. The key words in MWE are *minimal* and *working*. Please see https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – r2evans Oct 11 '19 at 21:53

1 Answers1

3

Here's an example using plotPlane from the rockchalk package, which uses the persp function "under the hood" but simplifies the details of the plotting operations. See the help for plotPlane (type ?plotPlane) for details on how the function works and the wide range of options for customizing the plot.

library(rockchalk)

m1 = lm(mpg ~ poly(wt,2) + disp, data=mtcars)

old.par = par(mfrow=c(1,2), mar=c(1,1,1,1))

plotPlane(m1, "wt", "disp", pch=16, col=rgb(0,0,1,0.1), drawArrows=TRUE, alength=0, 
          acol="red", alty=1,alwd=1, theta=25, phi=0)
plotPlane(m1, "wt", "disp", pch=16, col=rgb(0,0,1,0.1), drawArrows=TRUE, alength=0, 
          acol="red", alty=1,alwd=1, theta=35, phi=20)

enter image description here

If you have more than two dependent variables in the model, plotPlane will set the additional variables (the ones not being plotted) to their mean (for numeric variables) or mode (for factors). For example:

m2 = lm(mpg ~ poly(wt,2) + disp + poly(hp,2) + poly(wt,2), data=mtcars)

plotPlane(m2, "wt", "disp", pch=16, col=rgb(0,0,1,0.1), drawArrows=TRUE, alength=0, 
          acol="red", alty=1,alwd=1, theta=25, phi=0)
plotPlane(m2, "wt", "disp", pch=16, col=rgb(0,0,1,0.1), drawArrows=TRUE, alength=0, 
          acol="red", alty=1,alwd=1, theta=35, phi=20)

enter image description here

# Reset graphical parameters back to defaults
par(old.par)
eipi10
  • 91,525
  • 24
  • 209
  • 285