I am working with cubic cyclic splines in the mgcv package.
I understand that the model matrix for mgcv includes the intercept and has been reparamaterized using QR decomposition + eigen value decomposition as noted in Simon Wood's notes . But, I absolutely can not work out how to convert the beta values from mgcv back into betas for the original basis function.
I generate a toy example:
require(mgcv)
set.seed(6)
x <- sort(runif(200)*10)
z <- runif(200)
f <- sin(x*2*pi/10)+.5
y <- f + rnorm(200,0, 0.1)
plot(y)
And fit the model with the gam function (mgcv). For reference, I also define the original cyclic spline basis function, with the same knots:
## finished simulating data, now fit model...
knots_x <- seq(0,10,length=12)
b <- gam(y ~ s(x,bs="cc",k=12), knots=list(x=knots_x))
plot(x,y);plot(b,select=1,shade=TRUE);lines(x,f-mean(f),col=2)
## Create cyclic spline basis (so day 1 and 365 connect)
B <- cSplineDes(x,knots= knots_x, ord=4) ## get cyclic spline model matrix
plot(x,B[,1],type="l"); for (i in 2:11) lines(x,B[,i],col=i)
What I can't seem to figure out is how to work backwords to recover betas in the original basis:
head(B)
head(model.matrix.gam(b))
I have been working through Simon's notes and have located the QR decomposition, but I'm still short a column to make the dimensions work out.
qr.X(attributes(b$smooth[[1]])$qrc)