r - R 中 scatter3D() 曲面图的 GLM 预测

标签 r predict scatter3d plot3d

我正在尝试使用 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 或其他方法更好。

预先感谢您的帮助。

最佳答案

感谢@Ben Bolker提供的简单解决方案。

我取了是/否、0-1 变量的数值平均值,然后绘制了预测结果:

xy <- expand.grid( x = x.pred, y = y.pred, w = mean(w))

这使我能够生成一个看起来不错的图表,该图表在给定数据的情况下是有意义的,如下所示。

将 w 转换为数值后,具有第三个二分协变量 (w) 平均值的负二项式模型的 Scatter3D:

3D plot of interaction

关于r - R 中 scatter3D() 曲面图的 GLM 预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37976825/

相关文章:

r - 在多个图表中绘制垂直线

python - Z 标签未显示在 3d matplotlib 散点图中

python - 用于 3D 散点图的 Matplotlib 替代品

r - 使用 rpart 对新因子(分类)变量进行预测

r - 使用 R 删除特定变量中不包含 .(点)的行

r - 如何在 dplyr 中命名 group_split 输出的列表

machine-learning - 当输入有多个输出值时,我可以使用神经网络进行回归吗?

r - Predict.gbm() 的预测不一致

预测和预测生成器之间的 Keras 不同结果

r - 将 3D abline 添加到 Rlattice 包中的云图