Nothing
# Reconstruct marginal covariance matrices independently from their free
# parameters, then differentiate the scalar Gaussian likelihood directly.
independent_profile_objective_2year <- function(theta, fit, side, persistence) {
years <- 1L + as.numeric(fit$X[,2])
n <- length(fit$y); q <- ncol(fit$Z)
Z <- as.matrix(fit$Z); X <- as.matrix(fit$X)
G <- matrix(0,q,q); cursor <- 0L; col <- 0L
if(side=='G'){
R <- diag(theta[years]);cursor <- fit$num.year
G[seq_len(fit$num.student),seq_len(fit$num.student)] <- diag(theta[cursor+1L],fit$num.student)
cursor <- cursor+1L;col <- fit$num.student
}else{
Ri <- matrix(0,2,2);Ri[lower.tri(Ri,diag=TRUE)]<-theta[1:3]
Ri<-Ri+t(Ri)-diag(diag(Ri));R<-kronecker(diag(fit$num.student),Ri);cursor<-3L
}
for(g in seq_len(fit$num.year)){
k<-fit$Kg[g];S<-matrix(0,k,k);nk<-k*(k+1)/2
S[lower.tri(S,diag=TRUE)]<-theta[cursor+seq_len(nk)]
S<-S+t(S)-diag(diag(S),nrow=k);cursor<-cursor+nk
for(j in seq_len(fit$num.teach[g])){
ids<-col+seq_len(k);G[ids,ids]<-S;col<-col+k
}
}
if(persistence=='VP'){
a<-theta[cursor+1L]
idx <- which(years==2L)
cols <- seq_len(fit$num.teach[1])
zz<-Z[idx,cols,drop=FALSE];zz[zz!=0]<-a;Z[idx,cols]<-zz
}
V<-R+Z%*%G%*%t(Z); Vi<-solve(V)
xv<-crossprod(X,Vi%*%X)
beta<-solve(xv,crossprod(X,Vi%*%fit$y));r<-fit$y-X%*%beta
val<-0.5*(n*log(2*pi)+as.numeric(determinant(V,TRUE)$modulus)+sum(r*(Vi%*%r)))
if(fit$REML)val<-val-0.5*ncol(X)*log(2*pi)+0.5*as.numeric(determinant(xv,TRUE)$modulus)
val
}
test_that("profile information matches direct ML and REML likelihood Hessians", {
d <- vam_fixture(24L, 2L)
for (side in c("R", "G")) {
models <- if (side == "G") "GP" else c("GP", "rGP", "CP", "VP", "ZP")
for (model in models) for (reml in c(FALSE, TRUE)) {
fit <- suppressWarnings(GPvam(d, ~0+as.factor(year)+x, student.side = side,
persistence = model, REML = reml, max.iter.EM = 12, hessian = TRUE,
hes.method = "richardson", verbose = FALSE))
objective <- function(theta) independent_profile_objective_2year(theta,
fit, side, model)
expect_equal(objective(fit$covariance_parameters), -fit$loglik,
tolerance = 1e-8, info = paste(side, model, reml))
expected <- numDeriv::hessian(objective, fit$covariance_parameters)
expect_equal(as.matrix(fit$Hessian), expected, tolerance = 2e-5,
ignore_attr = TRUE, info = paste(side, model, reml))
}
}
})
test_that('Hessian calculations respect response units in both methods', {
d <- vam_fixture(24L, 2L)
small <- d
small$y <- d$y / 1000
for (side in c('R', 'G')) {
models <- if (side == 'G') 'GP' else c('GP', 'CP', 'VP')
for (model in models) for (reml in c(FALSE, TRUE)) {
for (method in c('simple', 'richardson')) {
fit <- suppressWarnings(GPvam(d, ~0+as.factor(year)+x,
persistence = model, student.side = side, REML = reml,
max.iter.EM = 8, hessian = TRUE, hes.method = method, verbose = FALSE))
scaled <- suppressWarnings(GPvam(small, ~0+as.factor(year)+x,
persistence = model, student.side = side, REML = reml,
max.iter.EM = 8, hessian = TRUE, hes.method = method, verbose = FALSE))
factors <- rep(1e-6, nrow(fit$Hessian))
if (model == 'VP') factors[length(factors)] <- 1
expect_true(all(is.finite(scaled$Hessian)))
expect_equal(scaled$Hessian * outer(factors, factors), fit$Hessian,
tolerance = 2e-4, ignore_attr = TRUE,
info = paste(side, model, reml, method))
}
}
}
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.