gpt4 book ai didi

r - mgcv 中基于张量积的高斯过程更平滑

转载 作者:行者123 更新时间:2023-12-04 10:52:08 26 4
gpt4 key购买 nike

我正在尝试运行一个 GAM 模型,其中使用高斯过程对 X 和 Y 之间的交互进行建模。在 mgcv 中使用默认平滑 (s()) 时,下面的代码工作正常,但我想用张量积 (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 - mgcv 中基于张量积的高斯过程更平滑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53878195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com