我正在尝试运行GAM模型,其中使用高斯过程对X和Y之间的交互进行建模。当在s()
中使用默认的平滑(mgcv
)时,以下代码可以正常工作,但是我想用张量积(te()
)为数据建模,因为我了解te
产品专门解决各向异性相互作用。但是,当我使用te()
时,似乎无法将所需参数传递给模型。下面的例子。
library(mgcv)
set.seed(540)
df <- gamSim(2, n = 300, scale = 0.15)[[1]]
df$x <- scales::rescale(df$x, to = c(-180,180))
df$y <- scales::rescale(df$y, to = c(-90,90))
head(df)
# works fine using s()
m1 <- gam(z ~ s(x, y, bs = "gp", m = c(2, 5, 2), k = 30), data = df)
summary(m1)
Family: gaussian
Link function: identity
Formula:
z ~ s(x, y, bs = "gp", m = c(2, 5, 2), k = 30)
Parametric coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.46143 0.01581 29.19 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Approximate significance of smooth terms:
edf Ref.df F p-value
s(x,y) 8.055 12.73 0.333 0.983
R-sq.(adj) = 0.0248 Deviance explained = 5.11%
GCV = 0.077292 Scale est. = 0.074959 n = 300
# Fails
# pass list as per example in ?te for multivariate marginal
mp <- list(c(2, 5, 2))
m2 <- gam(z ~ te(x, y, bs = "gp", m = mp, k = 30), data = df)
Error in gpE(knt, knt, object$p.order) :
incorrect arguments to GP smoother
In addition: Warning message:
In mgcv::te(x, y, bs = "gp", m = mp, k = 30) : m wrong length and ignored.
# try passing directly
m2 <- gam(z ~ te(x, y, bs = "gp", m = c(2,5,2), k = 30), data = df) # FAILS
Error in gpE(knt, knt, object$p.order) :
incorrect arguments to GP smoother
In addition: Warning message:
In mgcv::te(x, y, bs = "gp", m = c(2, 5, 2), k = 30) :
m wrong length and ignored.
# don't pass m
m2 <- gam(z ~ te(x, y, bs = "gp", k = 10), data = df) # works
summary(m2)
Family: gaussian
Link function: identity
Formula:
z ~ te(x, y, bs = "gp", k = 10)
Parametric coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.46143 0.01299 35.52 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Approximate significance of smooth terms:
edf Ref.df F p-value
te(x,y) 14.69 20.14 7.633 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
R-sq.(adj) = 0.341 Deviance explained = 37.4%
GCV = 0.05341 Scale est. = 0.050618 n = 300
gam.check(m2) # k has been ignored!?
Method: GCV Optimizer: magic
Smoothing parameter selection converged after 15 iterations.
The RMS GCV score gradient at convergence was 6.499192e-07 .
The Hessian was positive definite.
Model rank = 100 / 100
Basis dimension (k) checking results. Low p-value (k-index<1) may
indicate that k is too low, especially if edf is close to k'.
k' edf k-index p-value
te(x,y) 99.0 14.7 1.08 0.94
最佳答案
我意识到必须在d
函数中显式设置te()
后解决了此问题。下面的代码有效。
library(mgcv)
set.seed(540)
df <- gamSim(2, n = 300, scale = 0.15)[[1]]
df$x <- scales::rescale(df$x, to = c(-180,180))
df$y <- scales::rescale(df$y, to = c(-90,90))
head(df)
m1 <- gam(z ~ s(x, y, bs = "gp", m = c(2, 5, 2), k = 30), data = df, method = "ML")
mp <- list(c(2, 5, 2))
m2 <- gam(z ~ te(x, y, bs = "gp", m = mp, d = 2, k = 30), data = df, method = "ML")
关于r - 基于Tensor积的高斯过程在mgcv中更平滑,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53878195/