Open In App

3D Multiple Regression Graph with rgl package in R

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Regression analysis is a statistical tool used for understanding relationship within data. A 3D plot graph is used to represent complex regression models especially when dealing with multiple predictors. By plotting the data in three dimensions we can gain deeper insights into model working and data. This article will guide you to create 3D plot for regression analysis in R programming language using rgl pacakage.

Let us see step by step procedure to create a 3D multiple regression graph in R:

1. Installing and Loading RGL package

We will install and load the RGL package in our R script. RGL package is a 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.

R
install.packages("rgl")
library(rgl)

2. Preparing Data

We'll generate some sample data containing two independent variables x_1 and x_2 and one dependent variable y. rnorm is the R function that simulates random varieties having a specified normal distribution.

R
set.seed(123)

n <- 100
x1 <- rnorm(n)
x2 <- rnorm(n)
y <- 2 * x1 + 3 * x2 + rnorm(n)

3. Fitting the Multiple Linear Regression Model

We will fit a multiple linear regression model to our data using lm() function which stands for linear model and is used to create a simple regression model. The same function can be used to generate linear regression model with some changes in parameters.

R
lm_model <- lm(y ~ x1 + x2)

4. PLoting 3D Plot for Multiple Linear Regression without Squared Variable

The open3d function opens a 3D plotting window. We then create a multiple linear regression model lm_model using mtcars dataset, predicting mpg (miles per gallon) with wt (weight) and qsec (quarter-mile time) as predictors. Since no squared term is included the model assumes a linear relationship. The plot3d function generates a 3D regression plane labeled as "Multiple Linear Regression 3D Plot".

R
library(rgl)
open3d()

lm_model= lm(mpg ~ wt + qsec, data = mtcars)

plot3d(lm_model, plane.col='red')

title3d("Multiple Linear Regression 3D Plot")
play3d(spin3d(axis = c(0, 0, 1)), duration = 30)

Output:

ezgifcom-optimize
3D Multiple Regression Graph with rgl package in R

5. Plotting A 3D Plot for Multiple Linear Regression with Squared Variable

We will now use wt^2 which is the square of the wt variable as one of the independent variables. This is often necessary because in some real-world data the relationship between the dependent and independent variables isn't always linear. Squaring the variable helps capturing non linear relationships. In the graph we can see that the squared term introduces non-linearity improving the model's ability to predict the data more accurately.

R
library(rgl)
open3d()

lm_model= lm(mpg ~ wt + I(wt^2) + qsec, data = mtcars)

plot3d(lm_model, plane.col='green')

title3d("Multiple Linear Regression 3D Plot")
play3d(spin3d(axis = c(0, 0, 1)), duration = 30)

Output:

ezgifcom-optimize-(5)
3D Multiple Regression Graph with rgl package in R

With 3D plots we can clearly see and visualize difference between the squared and non squared model. This 3D visualizations helps us in understanding our data and model better.


Next Article

Similar Reads