我想根据父亲和母亲的身高和性别来预测孩子的身高,并将其可视化。
没有性别变量,我仍然可以在3D图形中将其可视化。
但是,当我添加性别时,我希望有2个表面图,一个用于性别==男性,另一个用于性别==女性。是否有一种优雅的方法可以在fit2
模型中修复此性别变量,以便我可以对两者进行绘制?
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)
适合包括性别的外观如下:
> fit2
Call:
lm(formula = childHeight ~ mother + father + gender)
Coefficients:
(Intercept) mother father gendermale
16.5212 0.3176 0.3928 5.2150
但是,当我尝试绘制两个性别的两个平面时,我得到一个错误:
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
我想要这样的东西,有人知道这怎么可能吗?
s3d$plane3d(fit2(gendermale=1))
s3d$plane3d(fit2(gendermale=0))
您将需要调整颜色(为演示起见,我在第二个平面上仅使用了“红色”),但是请尝试以下操作:
s3d$plane3d(fit2$coefficients[1:3])
s3d$plane3d(fit2$coefficients[1:3] + c(fit2$coefficients["gendermale"], 0, 0), col = "red")
这个想法是忽略gendermale
第一个平面(where gender=="female"
)的系数,并将其添加到第二个平面(where gender=="male"
)的截距中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句