Three-dimensional scatter plots with a regression plane

In this recipe, we will see how we can visualize a three-dimensional scatter plot with a fitted regression plane.

Getting ready

Let's recall the airquality data from the datasets library. In this particular recipe, we will use Ozone as a dependent variable and Solar.R and Temp as independent variables in order to fit the regression model, and then we visualized it.

How to do it…

To produce this plot, we will perform the following steps:

  1. Attach the dataset using the attach() function.
  2. Create a basic three-dimensional scatter plot and store it in an R object.
  3. Fit the linear regression model, relating Ozone as a dependent variable and Solar.R and Temp as independent variables and store it as an R object. This regression model estimated the linear regression coefficient of the independent variables in relation to the dependent variable.
  4. Plot the fitted regression object with the initial scatter plot.

Here is the R code that performs the preceding steps:

attach(airquality)
s3dplot<- scatterplot3d(Temp,Solar.R,Ozone)
model <- lm(Ozone~Solar.R+Temp)
s3dplot$plane3d(model)
How to do it…

How it works…

The scatterplot3d function produces the basic scatter plot for the three input numeric variables. The fitted line (the plane, in this case) is then embedded with the scatter plot using the plane3d function.

There's more…

We can now add a horizontal drop line with the existing plot using the type="h" argument. There are other arguments to write the customized axis labels and plot title, such as xlab, ylab, and main.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.22.202