mt1*_*022 5 r smoothing gam mgcv
z我正在尝试适应光滑的表面x并使用具有包中功能的y公式。我的目标是根据和的新值预测响应。z ~ s(x, y)gammgcvzxy
在我的实际情况下,z应该是正数负数z是没有意义的。然而,预测的zs 有时是负数。似乎对于某些区域,训练数据中没有足够的点来z
准确估计。
我的问题是:有没有办法在 smooth in 期间指定 的下界,以便以后我不会得到负数s ?zgamzpredict
下面是重现此问题的最小示例。
library(mgcv)
x <- seq(0.1, 1, by = 0.01)
y <- seq(0.1, 1, by = 0.01)
dtt <- expand.grid(x = x, y = y)
set.seed(123)
dtt$xp <- dtt$x + rnorm(nrow(dtt)) / 100
dtt$yp <- dtt$y + rnorm(nrow(dtt)) / 100
dtt$z <- 1 / (dtt$xp^2 + dtt$yp^2)
m <- sample.int(nrow(dtt), 3000)
dtt.train <- dtt[m, ]
dtt.test <- dtt[!(1:nrow(dtt) %in% m), ]
fit <- gam(z ~ s(x, y), data = dtt.train)
p <- predict(fit, newdata = dtt.test)
plot(dtt.test$z, p, xlab = 'Real', ylab = 'Predicted', pch = 19, col = 1 + (p < 0))
abline(h = 0, v = 0)
Run Code Online (Sandbox Code Playgroud)
如您所见,对于红点。实际值为正,但预测值为负。