You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于估计参数的对数似然计算:R包中是否存在对应函数?

Calculating Log-Likelihood of New Data with lme4 Model Parameters

Great question! There isn't a direct ll_given_modPars function in lme4, but the package provides low-level tools that let you exactly compute the log-likelihood of a new dataset using parameters estimated from another model. Here's how to do it:

Step-by-Step Implementation

  • First, fit your original model on dataset O as you already did:

    library(lme4)
    o_model <- lmer(observed ~ fixed.id + (1|random.id), data = O, REML = FALSE)
    
  • Extract the parameter estimates from your fitted model. You'll need both the fixed effects coefficients (beta) and the variance parameters for random effects (theta, which are transformed to ensure positivity during fitting):

    mod_pars <- getME(o_model, c("beta", "theta"))
    
  • Use mkdevfun to create a deviance function tailored to your new dataset N. Deviance equals -2 * log-likelihood, so we'll convert it later:

    # Create deviance function for dataset N, using the same model structure
    devfun_N <- mkdevfun(terms(o_model), data = N, REML = FALSE)
    
  • Calculate the log-likelihood by evaluating the deviance function with your extracted parameters, then converting it:

    n_logLik <- -devfun_N(mod_pars) / 2
    

Key Notes

  • This method works for both linear mixed models (lmer) and generalized linear mixed models (glmer). For GLMMs, just omit the REML argument since it doesn't apply to those models.
  • mkdevfun uses the exact same likelihood calculation code as lmer/glmer, so you're getting the same result as if you'd fitted the model to N with fixed parameters.
  • If you need to verify, you can cross-check by refitting the model to N with fixed parameters (though that's less efficient):
    # Refit model to N with fixed parameters (for verification)
    fixed_model <- update(o_model, data = N, start = mod_pars, control = lmerControl(optCtrl = list(maxfun = 0)))
    n_logLik_verify <- logLik(fixed_model)
    

内容的提问来源于stack exchange,提问作者pFiaux

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:28:51