scatter3D()
我正在尝试使用该函数从二项式 GLM 中生成具有重叠点的曲面图。
为此,我使用predict()
来预测不同 x 和 y 值的 z 表面。
# Data:
library(plot3D)
structure(list(
x = c(0.572082281112671, -0.295024245977402, 0.295024245977402, 0.861117839813232, 0.572082281112671, -1.74020183086395, 0.861117839813232, 0.283046782016754, 0.861117839813232, 0.283046782016754, -0.295024245977402, 1.43918883800507, 1.43918883800507, -0.295024245977402, -0.00598874036222696, -0.873095273971558, -0.295024245977402, -0.00598874036222696, -0.00598874036222696, 0.861117839813232),
y = c(-1.09869265556335, -1.18406093120575, -0.0542464517056942, -0.192688703536987, -0.0208134315907955, 0.194501429796219, -0.126082852482796, 0.861439049243927, 0.624606966972351, -0.227061957120895, -1.32208430767059, -0.553429543972015, 0.538678884506226, 1.53797924518585, 0.230196505784988, 0.2959825694561, 0.158534705638885, 1.33240795135498, 0.0964559689164162, 0.740677952766418),
z = c(0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
w = structure(c(2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L), .Label = c("0", "1"), class = "factor")), .Names = c("x", "y", "z", "w"), row.names = c(NA, 20L), class = "data.frame")
型号等
fit <-glm(formula = z ~ x * y + w, family = binomial)
# x is continuous
# y is continuous
# w is dichotomous (yes, no, i.e. 0,1) [but see solution below]
# z is dichotomous, but kept as numeric for plotting
grid.lines = 100
x.pred <- seq(min(x), max(x), length.out = grid.lines)
y.pred <- seq(min(y), max(y), length.out = grid.lines)
xy <- expand.grid( x = x.pred, y = y.pred)
z.pred <- matrix(exp(predict(fit, newdata = xy)),
nrow = grid.lines, ncol = grid.lines)
# fitted points for droplines to surface
fitpoints <- exp(predict(fit))
但是,我收到此错误:
Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : variable lengths differ (found for 'w')
W 是保留在模型中的第三个变量,但我不知道如何在绘制其他变量的同时保持它不变。我知道我需要调整一些东西,但似乎无法弄清楚那是什么。
请注意,我对这些值取幂,因此它们是一个有意义的比例,介于 0 和 1 之间,是我绘制它们时的概率。如果这是不正确的,请告诉我。[这是不正确的-在下面的评论中指出]
我完成了这个:
scatter3D(x, y, z, pch = 21, type = "p",col=rgb(red=0, green=17, blue=255, maxColorValue = 255, alpha = 150), bg = "#FF0000",
ylab = "Z-AM-Testosterone", xlab = "Z-AR-CAGn", zlab = "Divorce",
theta = -70, phi = 20, ticktype = "detailed",
surf = list(x = x.pred, y = y.pred, z = z.pred,
fit = fitpoints))
我敢肯定这很简单,但如果有人能解释如何从预测中删除 w 或保持它不变以便我可以继续,我将非常感激。请不要建议其他 3D 绘图方法 -scatter3D()
比visreg
我的目的更好。
在此先感谢您的帮助。