基于估计参数的对数似然计算:R包中是否存在对应函数?
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
Oas 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
mkdevfunto create a deviance function tailored to your new datasetN. 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 theREMLargument since it doesn't apply to those models. mkdevfunuses the exact same likelihood calculation code aslmer/glmer, so you're getting the same result as if you'd fitted the model toNwith fixed parameters.- If you need to verify, you can cross-check by refitting the model to
Nwith 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

