如何计算广义线性混合模型中缺失的变量个体输出值?
Hey there, let's work through how to calculate those missing individual values for your GLMM. First, let's lay out your model details clearly to make sure we're aligned:
Your GLMM Overview
- Model Type: Generalized Linear Mixed Model (GLMM), fit via Maximum Likelihood (Laplace Approximation) — object class:
glmerMod - Family/Link: Binomial with logit link
- Formula:
active ~ tair * breeding + (1 | id/family) - Dataset:
adata_sc
Fit Statistics
- AIC: 1948.6
- BIC: 2000.1
- logLik: -966.3
- Deviance: 1932.6
- Residual Degrees of Freedom: 4584
Scaled Residuals
Min 1Q Median 3Q Max
-24.1182 0.0720 0.1422 ...
Calculating Individual Values for tair & breeding
Since you mentioned "individual values", I'll cover the most common use cases (predicting response values per observation, and extracting variable-specific effects) using R code (since you're using glmer from the lme4 package):
1. Predict Individual-Level Response Probabilities
If you want the predicted probability of active for each observation (accounting for both fixed effects and your random id/family grouping), use the predict() function:
# Replace `your_model` with the name of your glmer object adata_sc$predicted_active <- predict(your_model, newdata = adata_sc, type = "response", re.form = NULL)
type = "response"gives probabilities (instead of logit-scale values)re.form = NULLincludes random effects to get individual-specific predictions; usere.form = ~0if you want population-average predictions (ignoring random effects)
2. Extract Fixed Effect Contributions for Each Variable
Your model includes an interaction between tair (continuous) and breeding (3-level factor), so we can break down their effects per observation:
First, extract the fixed effect coefficients:
fixed_coefs <- fixef(your_model)
Your coefficients will look something like this (matching your breeding level names):
(Intercept): Logit value whentair = 0andbreedingis at its reference leveltair: Slope oftairfor the referencebreedinglevelbreeding[Level2]: Difference in intercept for Level2 vs referencebreeding[Level3]: Difference in intercept for Level3 vs referencetair:breeding[Level2]: Difference intairslope for Level2 vs referencetair:breeding[Level3]: Difference intairslope for Level3 vs reference
Then calculate the logit-scale linear predictor for each observation, and convert to probability:
# Calculate logit-scale prediction adata_sc$linear_pred <- with(adata_sc, fixed_coefs["(Intercept)"] + fixed_coefs["tair"] * tair + fixed_coefs[paste0("breeding", breeding)] + # Match your breeding level labels fixed_coefs[paste0("tair:breeding", breeding)] * tair ) # Convert to probability scale adata_sc$prob_pred <- plogis(adata_sc$linear_pred)
3. Get Marginal Effects (Simpler Interpretation)
For a more intuitive view of how tair acts across each breeding level (or vice versa), use the emmeans package:
library(emmeans) # Get the slope of tair for each breeding level (logit scale) tair_trends <- emtrends(your_model, ~ breeding, var = "tair") # Convert to probability-scale effects tair_trends_prob <- regrid(tair_trends, transform = "response") # Or predict probabilities for specific tair values across breeding levels breeding_preds <- emmeans(your_model, ~ breeding, at = list(tair = seq(min(adata_sc$tair), max(adata_sc$tair), length.out = 5))) breeding_preds_prob <- regrid(breeding_preds, transform = "response")
内容的提问来源于stack exchange,提问作者Krista Oswald

