Examples of Creating 3D Multiple Regression Graph
Now let us see an example to understand the concept. The code follows the same procedure as given in the above steps.
A 3D Plot for Multiple Linear Regression without Squared Variable
R
#loading the rgl package into R-studio library (rgl) #creates a 3d plot in new window open3d () #multiple linear regression model- without squared variable lm_model= lm (mpg ~ wt + qsec, data = mtcars) #lm model plotted on the 3d plot we created plot3d (lm_model, plane.col= 'red' ) #add a title title3d ( "Multiple Linear Regression 3D Plot" ) # add animation to the plot for visual clarity play3d ( spin3d (axis = c (0, 0, 1)), duration = 30) |
Output:
The outputs for this code will appear in a new small window because of the function βopen_3dβ and not the console of R studio.
It looks like this:
- Firstly, it installs and loads the βrglβ package, which enables 3D visualization capabilities. Next, it opens a new 3D plotting window because of βopen3dβ for visualization. The code then proceeds to create a multiple linear regression model (`lm_model`) using the βmtcarsβ dataset, with βmpgβ (miles per gallon) as the response variable and βwtβ (weight) and βqsecβ (quarter mile time) as predictor variables.
- This model does not include a squared term, meaning it assumes a linear relationship between the predictors and the response. This implies, it may not effectively capture nonlinear patterns in the data. The code then generates a 3D plot of the regression model, representing the regression plane in red because of βplot3dβ function, and labels it as βMultiple Linear Regression 3D Plotβ using the βtitle3dβ function. Lastly, it adds an animation using the βplay3dβ function that rotates the 3D plot around the z-axis for 30 seconds, allowing for a dynamic exploration of the plot from various angles. You may even change the duration of the animation to occur.
- We can observe from the output that the plot shows the regression plot, which means the plane there is going to predict the mpg of the car using the input wt and qsec when provided.
A 3D Plot for Multiple Linear Regression with Squared Variable
R
#loading the package rgl library (rgl) # initialize new 3d plot window open3d () #multiple linear regression model- with squared variable lm_model= lm (mpg ~ wt + I (wt^2) + qsec, data = mtcars) #3d plot of model in the new window plot3d (lm_model, plane.col= 'green' ) #add title for the 3d plot title3d ( "Multiple Linear Regression 3D Plot" ) # add animation to spin for 30 seconds play3d ( spin3d (axis = c (0, 0, 1)), duration = 30) |
Output:
In the above code, we used the same dataset of mtcars. The output looks like this in the R studio:
- We used βwt^2β as one of the independent variable parameters, which is the square of βwtβ variable. This may be necessary many times. In some real-world data, the relationship between the dependent variable and an independent variable may not be linear. Squaring the variable allows us to capture nonlinear relationships. Hence, we squared it. We can see, in the graph that a non-linearity is produced in the graph, which touches most of the points in the plot, thereby a better prediction/regression model.
3D Multiple Regression Graph with rgl package in R
In this article, we are going to walk through the process of creating a 3D multiple regression graph using the rgl package in R programming language in detail.
Contact Us