问题
I have been trying to fit a penalized spline regression model in R using the connection between penalized splines and linear mixed models. While I am quite familiar with the R function lme
, using its competitor lmer
presents some difficulties. Here is a toy example I would like to generalize to lmer
:
require(nlme)
grid <- seq(0, 1, len = 100)
y <- rep(0, length(grid))
for(i in 1:length(grid)){
y[i] <- sin(3*pi*grid[i]) + rnorm(1, 0, 1)
}
X <- cbind(rep(1, length(grid)), grid, grid^2, grid^3)
knots <- seq(0.01, 0.99, len = 100)
Z <- outer(grid, knots, "-")
Z <- Z*(Z>0)
Z <- Z^3
data.mixed <- data.frame(X, Z)
data.mixed$all <- rep(1 ,nrow(data.mixed))
fit.mixed <- lme(y~ X-1, random = list( all = pdIdent(~Z-1) ), data = data.mixed )
curve(sin(3*pi*x), 0, 1, lwd = 2, ylim = c(-1.5, 1.5))
beta.hat <- fit.mixed$coefficients$fixed
u.hat <- unlist(fit.mixed$coefficients$random)
f.hat <- X%*%beta.hat + Z%*%u.hat
lines(grid, f.hat, lwd = 2, col = "red")
The tricky part is pdIdent function which specifies that while we have several random effects their covariance structure is a multiple of the identity matrix; lme
then estimates this scalar multiple.
If anyone is familiar with lmer
, I would greatly appreciate some hints on how to accomplish this with that function.
Thank you in advance
来源:https://stackoverflow.com/questions/50046342/specified-covariance-matrix-in-lmer-in-r-for-penalized-splines