函数blend.model.B(GAMM)

时间:2019-05-10 01:42:03

标签: r statistics mixed-models gam

有人使用过该功能

  

“ mixed.model.B”

?如果可以,我可以找到哪个包装?

数据库:http://halweb.uc3m.es/esp/Personal/personas/durban/esp/web/cursos/Maringa/gam-markdown/index.html

这是在 data.zip 部分中,数据为 leukemia.txt 。 注意:我正在使用指定的库:

library(nlme)
library(ggplot2)
library(GGally)
library(splines)
library(nlme)
library(fields)
library(lattice)
require(ISLR)
library(grid)
library(dplyr)
library(MASS)
library(mgcv)
library(latticeExtra)
library(fields)

请参见下面的代码:

attach(leukemia)
X=model.matrix(height~factor(treatment)*age)
treatment=factor(treatment)

MM=mixed.model.B(age,min(age)-0.5,max(age)+0.5,40,3,2,type="Eilers")

 Z=MM[[2]]

Id=factor(rep(1,length(height)))

Z.block4=list(treatment=pdIdent(~Z-1),case=pdSymm(~age))

data.fr <- groupedData(height ~ X[,-1] | Id, data = data.frame(height,X,Z,case,age))
model4 <- lme(height~X[,-1],data=data.fr,random=Z.block4) 

## Fitted individual trends for the smooth random intercept and slope model by treatment

b4 <- xyplot(fitted(fit4.gamm$lme) ~ age|factor(treatment),groups=case,col=tim.colors(length(unique(leukemia$case))),
       lwd=1,pch=19,data=leukemia,main="Treatment",cex=.35,type="a")
a2 + as.layer(b4)

1 个答案:

答案 0 :(得分:0)

在您列出的链接中,有一个下载R文件的链接: http://halweb.uc3m.es/esp/Personal/personas/durban/esp/web/cursos/Maringa/gam-markdown/Gams-code.R

在349行中,作者构建了该函数,我将其复制到下面:

# Mixed model representation of B-splines (using svd)
mixed.model.B<-function(x,xl,xr,ndx,bdeg,pord,type="Eilers"){
  Bbasis=bspline(x,xl,xr,ndx,bdeg)
  B <- Bbasis$B
  m=ncol(B)
  D=diff(diag(m),differences=pord)

  if(type=="Eilers"){
  Z <- B%*%t(D)%*%solve(D%*%t(D))
  }else if(type=="SVD"){ print("SVD method")
  P.svd=svd(t(D)%*%D)
  U=(P.svd$u)[,1:(m-pord)]
  d=(P.svd$d)[1:(m-pord)]
  Delta=diag(1/sqrt(d))
  Z=B%*%U%*%Delta
  }
  X=NULL
    for(i in 0:(pord-1)){
        X=cbind(X,x^i)
        }
  output <- list(X=X,Z=Z)
  return(output)
}