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:

3D Multiple Regression Graph with rgl package in R

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:

Output in R- studio

  • 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:

3D Multiple Regression Graph with rgl package in R

In the above code, we used the same dataset of mtcars. The output looks like this in the R studio:

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

Similar Reads

What is Multiple Regression?

Regression analysis is a powerful statistical tool for understanding the relationship between variables. One of the well-known regression techniques is Linear Regression, which deals with predicting a dependent variable using only one independent variable....

RGL Package

The rgl package in R is a great tool for creating interactive 3D visualizations, which makes it ideal for representing complex relationships in multiple regression models. It enables users to explore data interactively, visualize regression planes, and customize plots, thereby enhancing the understanding of the relationships between multiple dependent and independent variables....

Examples of Creating 3D Multiple Regression Graph

...

Visualizing Multiple Linear Regression in 3D with R

...

Conclusion

...

Contact Us