Penalty matrix in generalized additive models with smoothing through P-splines

Asked

Viewed 36 times

1

I’m studying generalized additive models using the package MGCV of R. Below is an illustrative example for a certain adjustment using the Gam function.

library(mgcv)


data(mcycle)

attach(mcycle)

mod_gam <- gam(accel ~ s(times, bs="ps", k = 7,m=c(2,2)), knots = list(knots),data = mcycle)

This function uses to adjust some quantities. Among them is the penalty matrix obtained as follows:

S <- mod_gam$smooth[[1]]$S

Segundo o livro do Wood, esta matriz é construída da seguinte maneira:

library (splines)


knots <- mod_gam$smooth[[1]]$knots

X <- splineDesign(knots = knots, times)

C <- rep(1, nrow(X)) %*% X

qrc <- qr(t(C))

Z <- qr.Q(qrc,complete=TRUE)[,(nrow(C)+1):ncol(C)]

XZ <- X%*%Z

D<-mod_gam$smooth[[1]]$D

S2<-t(Z)%*%t(D)%*%D%*%Z

However, they differ. Could anyone tell me what could be going on?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.