R Under development (unstable) (2023-12-13 r85679 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > > library(aster) > library(trust) > > data(radish) > > pred <- c(0,1,2) > fam <- c(1,3,2) > > ### need object of type aster to supply to penmlogl and pickle > > aout <- aster(resp ~ varb + fit : (Site * Region + Block + Pop), + pred, fam, varb, id, root, data = radish) > > ### model matrices for fixed and random effects > > modmat.fix <- model.matrix(resp ~ varb + fit : (Site * Region), + data = radish) > modmat.blk <- model.matrix(resp ~ 0 + fit:Block, data = radish) > modmat.pop <- model.matrix(resp ~ 0 + fit:Pop, data = radish) > > rownames(modmat.fix) <- NULL > rownames(modmat.blk) <- NULL > rownames(modmat.pop) <- NULL > > idrop <- match(aout$dropped, colnames(modmat.fix)) > idrop <- idrop[! is.na(idrop)] > modmat.fix <- modmat.fix[ , - idrop] > > nfix <- ncol(modmat.fix) > nblk <- ncol(modmat.blk) > npop <- ncol(modmat.pop) > > ### try penmlogl > > theta.start <- c(1, 1) > > alpha.start <- aout$coefficients[match(colnames(modmat.fix), + names(aout$coefficients))] > parm.start <- c(alpha.start, rep(0, nblk + npop)) > > tout <- trust(objfun = penmlogl, parm.start, rinit = 1, rmax = 10, + sigma = theta.start, fixed = modmat.fix, + random = list(modmat.blk, modmat.pop), obj = aout) > tout$converged [1] TRUE > > ### crude estimate of variance components > > eff.blk <- tout$argument[seq(nfix + 1, nfix + nblk)] > eff.pop <- tout$argument[seq(nfix + nblk + 1, nfix + nblk + npop)] > theta.crude <- sqrt(c(var(eff.blk), var(eff.pop))) > > ### now for the testing > > pout <- penmlogl(tout$argument, theta.crude, fixed = modmat.fix, + random = list(modmat.blk, modmat.pop), obj = aout) > names(pout) [1] "value" "gradient" "hessian" "argument" [5] "scale" "mlogl.hessian" "mlogl.gradient" > > eff.fix <- tout$argument[seq(1, nfix)] > eff.blk <- tout$argument[seq(nfix + 1, nfix + nblk)] > eff.pop <- tout$argument[seq(nfix + nblk + 1, nfix + nblk + npop)] > eff.blk <- theta.crude[1] * eff.blk > eff.pop <- theta.crude[2] * eff.pop > > beta <- c(eff.fix, eff.blk, eff.pop) > scalevec <- rep(c(1, theta.crude), times = c(nfix, nblk, npop)) > penaltyvec <- rep(c(0, 1, 1), times = c(nfix, nblk, npop)) > > modmat <- cbind(modmat.fix, modmat.blk, modmat.pop) > mout <- mlogl(beta, pred, fam, aout$x, aout$root, modmat, deriv = 2) > > all.equal(mout$hessian * outer(scalevec, scalevec), pout$mlogl.hessian) [1] TRUE > all.equal(tout$argument, pout$argument) [1] TRUE > all.equal(scalevec, pout$scale) [1] TRUE > > all.equal(pout$value, + mout$value + sum(penaltyvec * pout$argument^2) / 2) [1] TRUE > all.equal(pout$gradient, + mout$gradient * scalevec + penaltyvec * pout$argument) [1] TRUE > all.equal(pout$hessian, pout$mlogl.hessian + diag(penaltyvec)) [1] TRUE > > epsilon <- 1e-7 > mygradient <- 0 * pout$gradient > myhessian <- 0 * pout$hessian > for (i in seq(along = mygradient)) { + arg <- tout$argument + arg[i] <- arg[i] + epsilon + pout.epsilon <- penmlogl(arg, theta.crude, fixed = modmat.fix, + random = list(modmat.blk, modmat.pop), obj = aout) + mygradient[i] <- (pout.epsilon$value - pout$value) / epsilon + myhessian[i, ] <- (pout.epsilon$gradient - pout$gradient) / epsilon + } > all.equal(mygradient, pout$gradient, tol = 100 * epsilon) [1] TRUE > all.equal(myhessian, pout$hessian, tol = 100 * epsilon) [1] TRUE > > ### penmlogl2 > > foo <- pout$argument > alpha2 <- foo[seq(1, nfix)] > parm2 <- foo[- seq(1, nfix)] > pout2 <- penmlogl2(parm2, alpha2, theta.crude, fixed = modmat.fix, + random = list(modmat.blk, modmat.pop), obj = aout) > identical(pout$value, pout2$value) [1] TRUE > identical(pout$argument, pout2$argument) [1] TRUE > identical(pout$scale[- seq(1, nfix)], pout2$scale) [1] TRUE > identical(pout$mlogl.hessian, pout2$mlogl.hessian) [1] TRUE > identical(pout$mlogl.gradient, pout2$mlogl.gradient) [1] TRUE > idx <- seq(nfix + 1, nfix + nblk + npop) > identical(pout2$gradient, pout$gradient[idx]) [1] TRUE > foom <- pout$hessian > foom <- foom[idx, ] > foom <- foom[ , idx] > identical(pout2$hessian, foom) [1] TRUE > > > proc.time() user system elapsed 0.46 0.10 0.51