在线性混合模型(lme4)中评估似然函数

5
我目前正在编写一个脚本,用于评估线性混合模型中(受限制的)对数似然函数。我需要它来计算一些参数固定为任意值的模型的似然度。 也许这个脚本对你们中的一些人有帮助!
我使用lme4中的lmer()和logLik()来检查我的脚本是否按照预期工作。但似乎并没有! 由于我的教育背景并不真正关注这个统计水平,所以我有点迷失在找错中。
接下来,您将找到一个使用sleepstudy数据的简短示例脚本:
  # * * * * * * * * * * * * * * * * * * * * * * * * 
  # * example data

  library(lme4)
  data(sleepstudy)
  dat <- sleepstudy[ (sleepstudy$Days %in% 0:4) & (sleepstudy$Subject %in% 331:333) ,]
  colnames(dat) <- c("y", "x", "group")

  mod0 <- lmer( y ~ 1 + x + ( 1 | group ), data = dat)  


  # + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #
  #                                                             #
  #   Evaluating the likelihood-function for a LMM              #
  #   specified as: Y = X*beta + Z*b + e                        #
  #                                                             #
  # + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 

  # * * * * * * * * * * * * * * * * * * * * * * * * 
  # * the model parameters

  # n is total number of individuals
  # m is total number of groups, indexed by i
  # p is number of fixed effects
  # q is number of random effects

  q <- nrow(VarCorr(mod0)$group)                  # number of random effects
  n <- nrow(dat)                                  # number of individuals
  m <- length(unique(dat$group))                  # number of goups
  Y <- dat$y                                      # response vector

  X <- cbind(rep(1,n), dat$x)                     # model matrix of fixed effects (n x p)
  beta <- as.numeric(fixef(mod0))                 # fixed effects vector (p x 1)

  Z.sparse <- t(mod0@Zt)                          # model matrix of random effect (sparse format)
  Z <- as.matrix(Z.sparse)                        # model matrix Z (n x q*m)
  b <- as.matrix(ranef(mod0)$group)               # random effects vector (q*m x 1)

  D <- diag(VarCorr(mod0)$group[1:q,1:q], q*m)    # covariance matrix of random effects
  R <- diag(1,nrow(dat))*summary(mod0)@sigma^2    # covariance matrix of residuals
  V <- Z %*% D %*% t(Z) + R                       # (total) covariance matrix of Y

  # check: values in Y can be perfectly matched using lmer's information
  Y.test <- X %*% beta + Z %*% b + resid(mod0)
  cbind(Y, Y.test)

  # * * * * * * * * * * * * * * * * * * * * * * * * 
  # * the likelihood function

  # profile and restricted log-likelihood (Harville, 1997)
  loglik.p <- - (0.5) * (  (log(det(V))) + t((Y - X %*% beta)) %*% solve(V) %*% (Y - X %*% beta)  )
  loglik.r <- loglik.p - (0.5) * log(det( t(X) %*% solve(V) %*% X ))

  #check: value of above function does not match the generic (restricted) log-likelihood of the mer-class object
  loglik.lmer <- logLik(mod0, REML=TRUE)
  cbind(loglik.p, loglik.r, loglik.lmer)

也许这里有一些LMM专家可以提供帮助?无论如何,您的建议将不胜感激!
编辑:顺便说一下,LMM的似然函数可以在Harville(1977)中找到,(希望)可通过此链接访问: 最大似然方法用于方差分量估计和相关问题 问候, 西蒙

2
我强烈建议您获取lme4的开发版本(可能是通过devtools从github获取),该版本具有返回偏差函数的能力(mkDevfunOnly=TRUE)。 - Ben Bolker
谢谢!我查看了lme4的github版本,并使用devtools进行了安装。是否有关于devFunOnly=T参数及其生成的函数的进一步文档?我特别关注必须提供给生成的偏差函数的参数,因为这对我来说是最重要的一步! - SimonG
当 \code{devFunOnly} 为 \code{TRUE} 时,偏差函数返回一个单一的数值向量参数,表示 \code{theta} 向量。该向量定义了随机效应的方差-协方差函数,以 Cholesky 参数化方式表示。对于单个随机效应,这是一个等于随机效应标准差的单个值... - Ben Bolker
对于更复杂或多个随机效应,运行\code{getME(.,"theta")}以检索拟合模型的\code{theta}向量并检查向量的名称可能是确定\code{theta}向量元素和随机效应下三角形矩阵因子元素之间对应关系最简单的方法。(我刚刚将其添加到文档中。这是否有意义,或者您可以提出改进建议?) - Ben Bolker
我忘了说theta定义了缩放的方差协方差矩阵(即相对于残差方差)。 - Ben Bolker
1个回答

2
(截至2013年3月)解决方案是安装lme4的开发版本,并利用devFunOnly参数。该开发版本以及此功能与CRAN上的lme4一起提供,自2014年3月14日起可用,并且参考指南提供了解释,补充了软件包作者(Ben Bolker)对原问题的评论。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接