Using Linear Mixed Models with two fixed factors and a random factor
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
I am trying to make a Linear Mixed Model to see if there is a statistical significance with two fixed factors ('ANTS and LABEL') with the ('STATE') as a random factor. Can anyone suggest to me how to proceed with such a model in Matlab?
Data file is attached alongside a sample code. Is the code right?
m = fitlme(data, 'RATE ~ (ANT*LABEL) + (1 | STATE)');
Fixed effects coefficients (95% CIs):
Name Estimate SE tStat DF pValue Lower Upper
{'(Intercept)' } 0.033027 0.0056879 5.8065 294 1.6533e-08 0.021832 0.044221
{'ANTS' } -0.00012262 0.00065256 -0.1879 294 0.85108 -0.0014069 0.0011617
{'LABEL_Poor nest' } -0.015508 0.0080189 -1.934 294 0.054076 -0.03129 0.00027342
{'ANTS:LABEL_Poor nest'} 0.00036122 0.0009203 0.3925 294 0.69497 -0.00145 0.0021724
Accepted Answer
the cyclist
on 11 Oct 2021
Edited: the cyclist
on 11 Oct 2021
You can fit such a model using the fitlme function from the Statistics and Machine Learning Toolbox.
I think the following code fits the model you mentioned:
data = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/764231/Entry_rates.xlsx'); % You can just read in your local file
mdl = fitlme(data,'RATE ~ ANTS + LABEL + (1|STATE)')
mdl =
Linear mixed-effects model fit by ML
Model information:
Number of observations 298
Fixed effects coefficients 3
Random effects coefficients 11
Covariance parameters 2
Formula:
RATE ~ 1 + ANTS + LABEL + (1 | STATE)
Model fit statistics:
AIC BIC LogLikelihood Deviance
-1158.9 -1140.4 584.45 -1168.9
Fixed effects coefficients (95% CIs):
Name Estimate SE tStat DF pValue Lower Upper
{'(Intercept)' } 0.031647 0.0044726 7.0757 295 1.0864e-11 0.022844 0.040449
{'ANTS' } 5.8999e-05 0.00046026 0.12819 295 0.89809 -0.00084681 0.00096481
{'LABEL_Poor nest'} -0.012768 0.0039439 -3.2373 295 0.0013442 -0.020529 -0.0050059
Random effects covariance parameters (95% CIs):
Group: STATE (11 Levels)
Name1 Name2 Type Estimate Lower Upper
{'(Intercept)'} {'(Intercept)'} {'std'} 7.5587e-18 NaN NaN
Group: Error
Name Estimate Lower Upper
{'Res Std'} 0.034041 0.031415 0.036887
But I strongly recommend you read the documentation carefully, to understand how to specify the model you want to fit.
6 Comments
Hari krishnan
on 11 Oct 2021
Edited: Hari krishnan
on 11 Oct 2021
In the model you have suggested, does the intercept means that their is a significant difference between the 'LABEL', while no significant difference between the 'ANTS' in each label?
I was wondering tha the output contains two P values for multiple labels..
the cyclist
on 11 Oct 2021
You really should make sure you understand Wilkinson notiation, and how to specify the exact model you want.
The difference between your formula and mine is that your formula will include the interaction term ANTS*LABEL.
Hari krishnan
on 11 Oct 2021
Thank you for your comment.
There is an interaction between the predictor variable (ANTS) and Category (LABEL) as they are related. But I should also get a P value for each 'LABEL' category as there are multiple groups in the each 'LABEL', right?
the cyclist
on 11 Oct 2021
I have two questions, trying to clarify:
- Is ANTS a continuous variable, or categorical?
- Are you trying to predict RATE from ANTS and LABEL (with STATE as random)? Or are you trying to predict LABEL from ANTS (with STATE as random, and ignoring RATE)? Or something else?
Hari krishnan
on 11 Oct 2021
- ANTS is a categorical variable
- I am trying to predict RATE from ANTS and LABEL (with STATE as random)
the cyclist
on 11 Oct 2021
The first model I suggested treats ANTS as continuous numeric by default, so we need to convert it to categorical before putting it in the model.
This model has the interaction term as well:
data = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/764231/Entry_rates.xlsx'); % You can just read in your local file
data.ANTS = categorical(data.ANTS);
m = fitlme(data, 'RATE ~ (ANTS*LABEL) + (1 | STATE)')
m =
Linear mixed-effects model fit by ML
Model information:
Number of observations 298
Fixed effects coefficients 32
Random effects coefficients 11
Covariance parameters 2
Formula:
RATE ~ 1 + ANTS*LABEL + (1 | STATE)
Model fit statistics:
AIC BIC LogLikelihood Deviance
-1102.9 -977.18 585.44 -1170.9
Fixed effects coefficients (95% CIs):
Name Estimate SE tStat DF pValue Lower Upper
{'(Intercept)' } 0.03472 0.010729 3.2361 266 0.0013653 0.013595 0.055845
{'ANTS_2' } -0.00018056 0.014824 -0.01218 266 0.99029 -0.029369 0.029007
{'ANTS_3' } -0.0037793 0.014824 -0.25494 266 0.79897 -0.032967 0.025409
{'ANTS_4' } -0.005943 0.014824 -0.40089 266 0.68882 -0.035131 0.023245
{'ANTS_5' } -0.003487 0.014824 -0.23522 266 0.81422 -0.032675 0.025701
{'ANTS_6' } 0.0032408 0.014824 0.21861 266 0.82712 -0.025947 0.032429
{'ANTS_7' } -0.0024946 0.014824 -0.16828 266 0.86649 -0.031683 0.026693
{'ANTS_8' } -0.0020313 0.014824 -0.13703 266 0.89111 -0.031219 0.027157
{'ANTS_9' } -0.0075855 0.014824 -0.5117 266 0.60929 -0.036773 0.021602
{'ANTS_10' } -0.0038417 0.014824 -0.25915 266 0.79572 -0.03303 0.025346
{'ANTS_11' } -0.0023158 0.015589 -0.14855 266 0.88202 -0.033009 0.028378
{'ANTS_12' } -0.0037099 0.01672 -0.22188 266 0.82458 -0.03663 0.029211
{'ANTS_13' } -0.0022144 0.01672 -0.13244 266 0.89474 -0.035135 0.030706
{'ANTS_14' } 0.00054688 0.01752 0.031214 266 0.97512 -0.03395 0.035043
{'ANTS_15' } -0.0039004 0.01752 -0.22262 266 0.824 -0.038397 0.030596
{'ANTS_16' } -0.0043122 0.018583 -0.23205 266 0.81668 -0.040901 0.032277
{'LABEL_Poor nest' } -0.011158 0.014824 -0.75269 266 0.4523 -0.040346 0.01803
{'ANTS_2:LABEL_Poor nest' } -0.0077307 0.020714 -0.37322 266 0.70928 -0.048514 0.033053
{'ANTS_3:LABEL_Poor nest' } 9.4789e-05 0.020714 0.0045762 266 0.99635 -0.040689 0.040878
{'ANTS_4:LABEL_Poor nest' } -0.0033669 0.020965 -0.1606 266 0.87253 -0.044645 0.037911
{'ANTS_5:LABEL_Poor nest' } -0.0050268 0.020714 -0.24268 266 0.80844 -0.04581 0.035757
{'ANTS_6:LABEL_Poor nest' } -0.0076487 0.020714 -0.36926 266 0.71223 -0.048432 0.033135
{'ANTS_7:LABEL_Poor nest' } -0.0012082 0.020714 -0.05833 266 0.95353 -0.041992 0.039575
{'ANTS_8:LABEL_Poor nest' } 0.00011595 0.020714 0.0055978 266 0.99554 -0.040668 0.0409
{'ANTS_9:LABEL_Poor nest' } 0.0022824 0.020714 0.11019 266 0.91234 -0.038501 0.043066
{'ANTS_10:LABEL_Poor nest'} 0.00078623 0.020714 0.037957 266 0.96975 -0.039997 0.04157
{'ANTS_11:LABEL_Poor nest'} -0.0023263 0.021807 -0.10667 266 0.91513 -0.045264 0.040611
{'ANTS_12:LABEL_Poor nest'} -0.00027695 0.023423 -0.011824 266 0.99058 -0.046396 0.045842
{'ANTS_13:LABEL_Poor nest'} -0.0039373 0.023423 -0.16809 266 0.86664 -0.050056 0.042182
{'ANTS_14:LABEL_Poor nest'} 0.00067151 0.024566 0.027335 266 0.97821 -0.047696 0.049039
{'ANTS_15:LABEL_Poor nest'} 0.0014948 0.024566 0.06085 266 0.95152 -0.046873 0.049863
{'ANTS_16:LABEL_Poor nest'} 0.0053685 0.026081 0.20584 266 0.83707 -0.045983 0.05672
Random effects covariance parameters (95% CIs):
Group: STATE (11 Levels)
Name1 Name2 Type Estimate Lower Upper
{'(Intercept)'} {'(Intercept)'} {'std'} 0 NaN NaN
Group: Error
Name Estimate Lower Upper
{'Res Std'} 0.033928 0.031311 0.036764
Does that align better with the output you would expect, and can interpret?
More Answers (0)
Categories
Find more on Cluster Analysis and Anomaly Detection in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)