Sheldon
Sheldon

Reputation: 604

Adding a 2nd plane to a scatterplot3d

I want to make a prediction for a child's height, based on the fathers+mothers height and the gender and visualize this.

Without the gender variable I can still visualize this in a 3D graph.

But when I add the gender I'd like to have 2 surface plots, one for gender==male and one for gender==female. Is there an elegant way to fix this gender variable in the fit2 model, so that I can plot both?

library(fields)
library(scatterplot3d)
library(ggplot2)
library(UsingR)

data(GaltonFamilies)
attach(GaltonFamilies)

fit <- lm(childHeight ~ mother + father)
fit2 <- lm(childHeight ~ mother + father + gender)

colorSet <- tim.colors(2)
s3d <- scatterplot3d(father,mother,childHeight,color=colorSet[gender])

detach(GaltonFamilies)

s3d$plane3d(fit)

scatterplot3d plot

The fit including gender looks like:

> fit2

Call:
lm(formula = childHeight ~ mother + father + gender)

Coefficients:
(Intercept)       mother       father   gendermale  
    16.5212       0.3176       0.3928       5.2150  

But when I tryto plot two surface planes for both genders I get an error:

s3d$plane3d(fit2)

Error in segments(x, z1, x + y.max * yx.f, z2 + yz.f * y.max, lty = ltya,  : 
cannot mix zero-length and non-zero-length coordinates

I'd like to have something like this, does anybody know how this is possible?

s3d$plane3d(fit2(gendermale=1))
s3d$plane3d(fit2(gendermale=0))

Upvotes: 2

Views: 1336

Answers (1)

Dominic Comtois
Dominic Comtois

Reputation: 10421

You'll need to adjust the colors (I just used "red" for the 2nd plane for the sake of demonstration), but try this:

s3d$plane3d(fit2$coefficients[1:3])
s3d$plane3d(fit2$coefficients[1:3] + c(fit2$coefficients["gendermale"], 0, 0), col = "red")

The idea is ignoring the gendermale coefficient for the 1st plane (where gender=="female"), and adding it to the intercept for the second plane (where gender=="male").

enter image description here

Upvotes: 2

Related Questions