问题
I'm trying to produce a surface plot with overlain points from a binomial GLM using the scatter3D() function.
To do this I am using predict() to predict the z-surface for different values of x and y.
# 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")
Model etc.
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))
However, I am getting this error:
Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : variable lengths differ (found for 'w')
W is a third variable that is important to keep in the model, but I can't figure out how to keep it constant while still plotting the other variables. I understand that I need to tweak something, but can't seem to figure out exactly what that is.
Note that I exponentiate the values so they are a scale that makes sense, between 0 and 1, a probability when I graph them. If this is incorrect, let me know. [This was incorrect - pointed out in comments below]
I finish with this:
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))
I'm sure it's simple, but if someone could explain how to remove w from the prediction or hold it constant so I can go ahead, I'd be much obliged. Please don't suggest another 3D plotting method - scatter3D is better than visreg or other for my purposes.
Thanks in advance for your help.
回答1:
Thanks for the simple solution, @Ben Bolker.
I took the mean of the numeric equivalent of the yes/no, 0-1 variable and just plotted the predictions from that:
xy <- expand.grid( x = x.pred, y = y.pred, w = mean(w))
This allowed me to produce a decent looking graph that makes sense given the data, shown below.
Scatter3D for negative binomial model with the mean of a third dichotomous covariate (w), after converting w to numeric:
来源:https://stackoverflow.com/questions/37976825/glm-prediction-for-surface-plot-in-scatter3d-in-r