# Using fitglm for the generalized linear model in matlab

2 views (last 30 days)
MINA on 11 Jan 2017
Answered: Philip on 3 May 2018
I have two question regarding fitglm and glmfit.
1- After doing some research, I realized that t is better to use fitglm instead of glmfit. The reason is that the output of the first function is more complete. I wanted to know if that is correct!
2- Instead of giving the model I have the design matirx which I would like to give as an input to fitglm. Here is my code
x=rand(3000,1);
X=[ones(size(x)) ,x, x.^2, x.^3];
a=5;
b=3.4;
c=-3;
d=20;
func=[d a b c]*X';
fitglm(X,fun);
This is what I get as the coefficient
Estimate SE tStat pvalue
(Intercept) 20 2.21336782492520e-15 9.03600376529188e+15 0
x1 0 0 NaN NaN
x2 4.99999999999998 1.46537110155627e-14 341210495736530 0
x3 3.40000000000005 2.14250387190849e-14 158692828730872 0
x4 -3.00000000000002 1.10439613833154e-14 -271641659715711 0
I don't understand why it has five Coefficients which one of them is zero. And if I remove ones from the design matrix I don't get any Intercept.
Additional, it also gives the NumPredictors =4, namely x1 x2 x3 x4. I am assuming that it uses this number for computing the AIC but isn't the NumPredictors=3?
Thanks

Philip on 3 May 2018
The fitglm function assumes an intercept, so your design matrix is actually: [1s, 1s, x, x.^2, x.^3].
You need to turn the intercept parameter to false in order to manually manipulate the constants evaluation. The fitglm function is newer (r2013b I think?) and does have more stuff baked into it, so I would recommend using it over glmfit.
The older glmfit function has the same parameter except I think it was called 'constant'. I've only recently started converting much of my code from glmfit to fitglm and am finding the new baked in calculations to be hugely helpful.