在聚类方法中使用薄板回归样条的系数

机器算法验证 r 聚类 毫克CV
2022-03-23 11:03:47

有许多基于正交基函数的函数数据聚类过程。我有一系列使用 GAMM 模型构建的模型,使用gamm()R 中的 mgcv 包。为了拟合长期趋势,我使用薄板回归样条曲线。接下来,我在随机分量中引入了一个 CAR1 模型来校正自相关。有关更多信息,请参见 Simon Wood 关于薄板回归样条曲线的论文或他关于 GAM 模型的书

现在我对如何从模型中得到正确的系数有点困惑。而且我更不相信我可以提取的系数是我应该用来对不同模型进行聚类的系数。

一个简单的例子,使用:

#runnable code
require(mgcv)
require(nlme)
library(RLRsim)
library(RColorBrewer)

x1 <- 1:1000
x2 <- runif(1000,10,500)

fx1 <- -4*sin(x1/50)
fx2 <- -10*(x2)^(1/4)
y <- 60+ fx1 + fx2 + rnorm(1000,0,5)

test <- gamm(y~s(x1)+s(x2))
# end runnable code

然后我可以使用 smoothCon 构造原始基础:

#runnable code
um <- smoothCon(s(x1),data=data.frame(x1=x1),
         knots=NULL,absorb.cons=FALSE)
#end runnable code

现在,当我查看基函数时,我可以使用

# runnable code
X <- extract.lmeDesign(test$lme)$X
Z <- extract.lmeDesign(test$lme)$Z

op <- par(mfrow=c(2,5),mar=c(4,4,1,1))
plot(x1,X[,1],ylab="Basis function",xlab="X",type="l",lwd=2)
plot(x1,X[,2],ylab="Basis function",xlab="X",type="l",lwd=2)
plot(x1,Z[,8],ylab="Basis function",xlab="X",type="l",lwd=2)
plot(x1,Z[,7],ylab="Basis function",xlab="X",type="l",lwd=2)
plot(x1,Z[,6],ylab="Basis function",xlab="X",type="l",lwd=2)
plot(x1,Z[,5],ylab="Basis function",xlab="X",type="l",lwd=2)
plot(x1,Z[,4],ylab="Basis function",xlab="X",type="l",lwd=2)
plot(x1,Z[,3],ylab="Basis function",xlab="X",type="l",lwd=2)
plot(x1,Z[,2],ylab="Basis function",xlab="X",type="l",lwd=2)
plot(x1,Z[,1],ylab="Basis function",xlab="X",type="l",lwd=2)
par(op)
# end runnable code

他们看起来已经很不一样了。我可以通过以下方式获得用于构建平滑器的最终系数

#runnable code
Fcoef <- test$lme$coef$fixed
Rcoef <- unlist(test$lme$coef$random)
#end runnable code

但我不确定这些是我要寻找的系数。我担心我不能只将这些系数用作聚类过程中的数据。我真的很想知道哪些系数用于将基函数从我得到的基函数转换为从smoothCon()gamm 对象的 lme 部分提取的基函数。如果可能的话,我可以在哪里找到它们。我已经阅读了相关文章,但不知何故我自己无法弄清楚。感谢所有帮助。

1个回答

如果我理解正确,我认为您想要$gam组件中的系数:

> coef(test$gam)
(Intercept)     s(x1).1     s(x1).2     s(x1).3     s(x1).4     s(x1).5 
 21.8323526   9.2169405  15.7504889  -3.4709907  16.9314057 -19.4909343 
    s(x1).6     s(x1).7     s(x1).8     s(x1).9     s(x2).1     s(x2).2 
  1.1124505  -3.3807996  21.7637766 -23.5791595   3.2303904  -3.0366406 
    s(x2).3     s(x2).4     s(x2).5     s(x2).6     s(x2).7     s(x2).8 
 -2.0725621  -0.6642467   0.7347857   1.7232242  -0.5078875  -7.7776700 
    s(x2).9 
-12.0056347

更新 1:为了获得我们可以predict(...., type = "lpmatrix")用来获得平滑矩阵的基函数:Xp

Xp <- predict(test$gam, type = "lpmatrix")

s(x1)然后可以使用以下方法恢复拟合的样条曲线(例如 for ):

plot(Xp[,2:10] %*% coef(test$gam)[2:10], type = "l")

您可以绘制这个()并看到它类似于Xpum[[1]]$X

layout(matrix(1:2, ncol = 2))
matplot(um[[1]]$X, type = "l")
matplot(Xp[,1:10], type = "l")
layout(1)

我思考为什么这些不完全一样。是不是因为原来的基函数在拟合的时候已经被惩罚回归了???

更新 2:您可以通过将可识别性约束包含在您的基函数中来使它们相同um

um2 <- smoothCon(s(x1), data=data.frame(x1=x1), knots=NULL, 
                 absorb.cons=TRUE)
layout(matrix(1:2, ncol = 2))
matplot(um2[[1]]$X, type = "l", main = "smoothCon()")
matplot(Xp[,2:10], type = "l", main = "Xp matrix") ##!##
layout(1)

注意我没有在标记的行中截取##!##

您应该能够直接从函数,该函数与.XpPredictMat()smoothCon()