Variance test in multilevel model
Show older comments
How to perform a liklihood ratio test to compaire two modeles one with random effect (a model using fitglme() ) and the other without random effect (a model using fitglm() )
Answers (1)
Shantanu Dixit
on 22 Jan 2025
Edited: Shantanu Dixit
on 22 Jan 2025
Hi Bezalam,
Assuming you have two models (a model using 'fitglme' and another using 'fitglm'). You can perform a likelihood ratio test to compare the two models as follows:
- Compute the log-likelihood of both the models using the 'LogLikelihood' property.
- Derive the test-statistic as '-2* (difference of log likelihood of both the models)'
- Compute the degrees of freedom using the 'NumEstimatedCoefficients' property.
- Compute the p-value using 'chi2cdf' to compare the two models.
% Assuming model_glm and model_glme as the two models
logL_glm = model_glm.LogLikelihood;
logL_glme = model_glme.LogLikelihood;
test_statistic = -2 * (logL_glm - logL_glme);
% Degrees of freedom are computed as the difference in the number of estimated parameters.
df = model_glme.NumEstimatedCoefficients - model_glm.NumEstimatedCoefficients;
p_value = 1 - chi2cdf(test_statistic, df);
fprintf('Test Statistic: %.2f\n', test_statistic);
fprintf('Degrees of Freedom: %d\n', df);
fprintf('p-value: %.4f\n', p_value);
You can refer to the useful MathWorks documentation on Generalized Linear Models:
- 'GeneralizedLinearModel': https://www.mathworks.com/help/stats/generalizedlinearmodel.html
- 'GeneralizedLinearMixedModel': https://www.mathworks.com/help/stats/generalizedlinearmixedmodel-class.html
- 'chi2cdf': https://www.mathworks.com/help/stats/chi2cdf.html
- 'LogLikelihood': https://www.mathworks.com/help/stats/generalizedlinearmixedmodel-class.html#bubtdtx-LogLikelihood
- 'NumEstimatedCoefficients': https://www.mathworks.com/help/stats/generalizedlinearmixedmodel-class.html#bubtdtx-NumEstimatedCoefficients
Hope this helps!
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!