【发布时间】:2020-08-19 01:44:03
【问题描述】:
我正在努力预测 gam 具有随机效应的模型,以通过 plot_ly 生成 3D 曲面图。
这是我的代码;
x <- runif(100)
y <- runif(100)
z <- x^2 + y + rnorm(100)
r <- rep(1,times=100) # random effect
r[51:100] <- 2 # replace 1 into 2, making two groups
df <- data.frame(x, y, z, r)
gam_fit <- gam(z ~ s(x) + s(y) + s(r,bs="re"), data = df) # fit
#create matrix data for `add_surface` function in `plot_ly`
newx <- seq(0, 1, len=20)
newy <- seq(0, 1, len=30)
newxy <- expand.grid(x = newx, y = newy)
z <- matrix(predict(gam_fit, newdata = newxy), 20, 30) # predict data as matrix
但是,最后一行导致错误;
Error in model.frame.default(ff, data = newdata, na.action = na.act) :
variable lengths differ (found for 'r')
In addition: Warning message:
In predict.gam(gam_fit, newdata = newxy) :
not all required variables have been supplied in newdata!
感谢之前的回答,我确信上面的代码不会产生随机效应,如here。
如何预测具有随机效应的游戏模型?
【问题讨论】:
标签: r plotly predict gam random-effects