scatter3D()関数を使用して、バイナリGLMのオーバーレイポイントを持つサーフェスプロットを作成しようとしています。scatter3D()のサーフェスプロットのGLM予測R
これを行うには、xとyの異なる値に対してz面を予測するためにpredict()を使用します。
# 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がモデルに保つことが重要である3番目の変数ですが、私ができます」他の変数をプロットしながらそれを一定に保つ方法を見つけ出す。私は何かを微調整する必要があることを理解していますが、それが何であるかを正確に把握することはできません。
私はそれらをグラフ化するときに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やその他より優れています。
ご協力いただきありがとうございます。
あなたは[再現可能な例]をご提供しますデータが含まれてくださいすることができ(http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? –
予測しようとする前に 'xy $ w < -'(ここで "typical"は平均値、中央値、または他の感知可能なベースライン値です)を実行するとどうなりますか? –
申し訳ありませんが、長い時間のlurker新しいポスター。サンプルデータが提供されました。希望通りに必要に応じて(私の実際のデータセットN = 675)。 – CPR