R 将第二个平面添加到散射图3D
我想根据父亲+母亲的身高和性别预测孩子的身高,并将其形象化 在没有性别变量的情况下,我仍然可以在3D图形中看到这一点 但是当我添加性别时,我希望有两个曲面图,一个用于性别==男性,一个用于性别==女性。在R 将第二个平面添加到散射图3D,r,lm,surface,data-fitting,R,Lm,Surface,Data Fitting,我想根据父亲+母亲的身高和性别预测孩子的身高,并将其形象化 在没有性别变量的情况下,我仍然可以在3D图形中看到这一点 但是当我添加性别时,我希望有两个曲面图,一个用于性别==男性,一个用于性别==女性。在fit2模型中,是否有一种优雅的方法来修正这个性别变量,以便我可以同时绘制这两个变量 library(fields) library(scatterplot3d) library(ggplot2) library(UsingR) data(GaltonFamilies) attach(Galt
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)
但当我试图为两种性别绘制两个平面时,我得到了一个错误:
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
系数(其中gender==“female”
),并将其添加到第二个平面的截距中(其中gender==“female”
)
s3d$plane3d(fit2$coefficients[1:3])
s3d$plane3d(fit2$coefficients[1:3] + c(fit2$coefficients["gendermale"], 0, 0), col = "red")