Visualizing Multiple Linear Regression in 3D with R

We are going to create our own dataset to use for the multiple regression by using vectors concept in R and create a graph. The data we are going to use has two independent variables – age and experience and the dependent variable is income. Here the procedure same as the above codes.

Loading Packages

R




#loading the package rgl
library(rgl)


Building Linear Regression Model

R




#data to be used in the plot
age <- c(25,30,47,32,43,51,28,33,37,39,29,47,54,51,44,41,58,23,44,37)
exp <- c(1,3,2,5,10,7,5,4,5,8,1,9,5,4,12,6,17,1,9,10)
income <- c(30450,35670,31580,40130,47830,41630,41340,37650,40250,45150,27840,46110,
            36720,34800,5130,38900,63600,30870,44190,48700)
 
#multiple linear regression model
lm_model= lm(income ~ age + exp)
print(lm_model)


Output:

Call:
lm(formula = income ~ age + exp)
Coefficients:
(Intercept) age exp
28982.46 68.95 1082.37

Visualizing Regression Graph

R




#create new window for 3d plotting
open3d()
 
#plotting the model in the created window
plot3d(lm_model, plane.col='blue')
 
#title of the graph
title3d("Multiple Linear Regression 3D Plot")
 
# add animation for 30 sec duration
play3d(spin3d(axis = c(0, 0, 1)), duration = 30)


Output:

3D Multiple Regression Graph with rgl package in R

The output shown here helps us to predict the income when the experience and income is given. We can also observe that the dataset had an outlier, which may cause the model performance to decrease.

The output in R-studio looks like this:

Output in R studio

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