mgcv:如何设置样条线的结数和/或位置 [英] mgcv: How to set number and / or locations of knots for splines

查看:284
本文介绍了mgcv:如何设置样条线的结数和/或位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在mgcv软件包中使用函数gam:

I want to use function gam in mgcv packages:

 x <- seq(0,60, len =600)
 y <- seq(0,1, len=600) 
 prova <- gam(y ~ s(x, bs='cr')

我可以在s()中设置结的数量吗?然后我可以知道花键在哪里打结?谢谢!

can I set the number of knots in s()? and then can I know where are the knots that the spline used? Thanks!

推荐答案

看到错误的答案总是令人失望...虽然设置k是正确的方法,但fx = TRUE绝对不正确:它将强制使用纯回归样条而不加惩罚.

It is always disappointing to see a wrong answer... While setting k is the correct way to go, fx = TRUE is definitely not right: it will force using pure regression spline without penalization.

结的位置

对于受罚的回归样条,精确的位置并不重要,只要:

For penalized regression spline, the exact locations are not important, as long as:

  • k足够大;
  • 结的扩散具有良好,合理的覆盖范围.
  • k is adequately big;
  • the spread of knots has good, reasonable coverage.

默认情况下:

  • 自然立方回归样条bs = 'cr'分位数进行结;
  • B样条曲线族(bs = 'bs'bs = 'ps'bs = 'ad')放置结均匀.
  • natural cubic regression spline bs = 'cr' places knots by quantile;
  • B-splines family (bs = 'bs', bs = 'ps', bs = 'ad') place knots evenly.

比较以下内容:

library(mgcv)

## toy data
set.seed(0); x <- sort(rnorm(400, 0, pi))  ## note, my x are not uniformly sampled
set.seed(1); e <- rnorm(400, 0, 0.4)
y0 <- sin(x) + 0.2 * x + cos(abs(x))
y <- y0 + e

## fitting natural cubic spline
cr_fit <- gam(y ~ s(x, bs = 'cr', k = 20))
cr_knots <- cr_fit$smooth[[1]]$xp  ## extract knots locations

## fitting B-spline
bs_fit <- gam(y ~ s(x, bs = 'bs', k = 20))
bs_knots <- bs_fit$smooth[[1]]$knots  ## extract knots locations

## summary plot
par(mfrow = c(1,2))
plot(x, y, col= "grey", main = "natural cubic spline");
lines(x, cr_fit$linear.predictors, col = 2, lwd = 2)
abline(v = cr_knots, lty = 2)
plot(x, y, col= "grey", main = "B-spline");
lines(x, bs_fit$linear.predictors, col = 2, lwd = 2)
abline(v = bs_knots, lty = 2)

您可以看到结位置的不同.

You can see the difference in knots placement.

设置自己的结位置:

您还可以通过gam()knots参数提供自定义的打结位置(是的,打结不会馈入s(),而是馈给gam()).例如,您可以为cr做均匀间隔的结:

You can also provide your customized knots locations via the knots argument of gam() (yes, knots are not fed to s(), but to gam()). For example, you can do evenly spaced knots for cr:

xlim <- range(x)  ## get range of x
myfit <- gam(y ~ s(x, bs = 'cr', k =20),
         knots = list(x = seq(xlim[1], xlim[2], length = 20)))

现在您可以看到:

my_knots <- myfit$smooth[[1]]$xp
plot(x, y, col= "grey", main = "my knots");
lines(x, myfit$linear.predictors, col = 2, lwd = 2)
abline(v = my_knots, lty = 2)

但是,通常不需要自己打结.但是,如果您确实想这样做,则必须清楚自己在做什么.另外,您提供的结数必须与s() 中的k相匹配.

However, there is usually no need to set knots yourself. But if you do want to do this, you must be clear what you are doing. Also, the number of knots you provided must match k in the s().

这篇关于mgcv:如何设置样条线的结数和/或位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆