SAS混合模型转R实现验证及AIC等指标差异原因咨询
Model Conversion Correctness
First, let's break down the random effect structure in your SAS and R code:
- Your SAS code
Random A(B);defines a random intercept for subjects (A) nested within sequence patterns (B). This accounts for variability between subjects who belong to the same sequence, giving each subject in a sequence its own random intercept. - Your current nlme code
random = ~1| A / Bactually specifies sequence patterns (B) nested within subjects (A)—which doesn’t match your study design, since each subject only belongs to one sequence. In practice, this ends up acting like a simple subject-level random intercept (~1|A) because each A maps to exactly one B. That’s why your fixed effect estimates are close: if sequence-level variability is small, omitting it doesn’t shift fixed effects much.
The correct nlme equivalent to SAS’s A(B) is:
random = ~1 | B/A
The B/A syntax in nlme means "A nested within B", which expands to ~1 | B + B:A—matching SAS’s random effect structure exactly (sequence-level random intercepts plus subject-level intercepts nested within sequences).
Why AIC, BIC, LogLik Differ
Even with a correctly specified model, you’ll often see differences in these metrics between SAS and nlme. Here’s the key reasons:
Constant Term Variation in Log-Likelihood
SAS and nlme handle the constant terms in the REML marginal likelihood differently. These constants (e.g., terms involving sample size or determinant of covariance matrices) don’t affect model comparisons, but they change the absolute value of the log-likelihood. As a result, AIC and BIC (derived from log-likelihood) will differ in absolute value, but relative differences between models will stay consistent.Different Optimization Algorithms
SAS’sProc Mixeduses Newton-Raphson or Fisher scoring for REML estimation, while nlme’slmedefaults to the EM (Expectation-Maximization) algorithm. These algorithms can produce slightly different estimates of covariance parameters, which in turn shift the log-likelihood value. You can adjust nlme’s algorithm with themethodparameter if you want to align closer to SAS.AIC/BIC Definition Nuances
- Parameter Count: Both tools count fixed and random effects parameters, but there may be subtle differences (e.g., whether residual variance is included in the parameter count for AIC).
- Sample Size for BIC: SAS typically uses the total number of observations for the
nin BIC’s formula (-2logL + k*log(n)), while nlme may use the number of independent groups (e.g., number of subjects) instead. This can lead to noticeable BIC differences.
Degrees of Freedom Calculations
Your SAS code usesddfm=kenwardrogerfor denominator degrees of freedom, which affects p-values but not directly AIC/BIC. However, if the two tools calculate effective degrees of freedom for random effects differently, it can indirectly influence the log-likelihood estimates.
内容的提问来源于stack exchange,提问作者S.Gradit

