MATLAB: Compare 2 regression models

MATLABregressionstatistics

I wonder if there is any tool to compare 2 regression models on the same sample pool. In general more terms you put into the model the closer the fit, but you risk over fitting. For example:
>> mdl=fitglm(FitZV,FitDataV,'linear')
mdl =
Generalized linear regression model:
y ~ 1 + x1
Distribution = Normal
Estimated Coefficients:
Estimate SE tStat pValue
________ __________ _______ ______
(Intercept) 81.101 0.0085111 9528.9 0
x1 -0.22506 0.00058189 -386.77 0
9638 observations, 9636 error degrees of freedom
Estimated Dispersion: 0.698
F-statistic vs. constant model: 1.5e+05, p-value = 0
>> mdl2=fitglm(FitZV,FitDataV,'purequadratic')
mdl2 =
Generalized linear regression model:
y ~ 1 + x1 + x1^2
Distribution = Normal
Estimated Coefficients:
Estimate SE tStat pValue
___________ __________ _______ __________
(Intercept) 81.286 0.012269 6625.6 0
x1 -0.22447 0.00057029 -393.6 0
x1^2 -0.00086632 4.2147e-05 -20.555 6.3993e-92
9638 observations, 9635 error degrees of freedom
Estimated Dispersion: 0.668
F-statistic vs. constant model: 7.83e+04, p-value = 0
Both mdl and mdl2 are statically better than a constant model, but does mdl2 explain the dataset significantly better than mdl (or the oppsite)? From what I have found devianceTest only test the model to constant, but couldn't find a function to compare 2 models? If anyone can point me to the right direction that would be appreciated.

Best Answer

One way is to use likelihood ratio test (similar to R anova function). For more information see here.
y = randn(200, 1); % test response
x = randn(200, 1); % test predictor
model1 = fitlm(x, y)
model2 = fitlm([x, x.^2], y);
LR = 2*(model2.LogLikelihood - model1.LogLikelihood); % has a X2 distribution with a df equals to number of constrained parameters, here: 1
pval = 1 - chi2cdf(LR, 1);