Equivalent code to regress(), using fitglm()
2 views (last 30 days)
Show older comments
Given the same input data, I would expect
b = regress(log(Y),[ones(size(X,1),1) X]);
and
mdl = fitglm(X,Y,'Link','log');
B = mdl.Coefficients.Estimate;
to give the same coefficients.
Below is some sample code where they do not. Am I missing something simple?
NTRIALS = 31;
dateString = {'01-04-1993','01-07-1995','01-03-1996','01-03-1997', ...
'01-02-1998','01-07-1998','01-11-1998','01-08-1999', ...
'01-09-1999','01-09-2002','01-01-2006','01-03-2006', ...
'01-02-2008','01-05-2008','01-10-2008','01-01-2010', ...
'01-02-2010','01-03-2010','01-10-2011','01-11-2011', ...
'01-01-2012','01-04-2012','01-03-2012','01-04-2012', ...
'01-09-2012','01-09-2012','01-01-2012','01-01-2012', ...
'01-04-2013','01-06-2013','01-02-2014'...
}';
ARR = [1.27 0.84 0.90 1.26 ...
1.61 0.50 1.28 0.98 ...
1.08 1.24 0.61 0.67 ...
0.70 0.44 0.41 0.33 ...
0.40 0.90 0.54 0.80 ...
0.28 1.30 0.39 1.20 ...
0.40 0.36 0.501 0.40 ...
1.04 0.505 0.34 ]';
N = [372 251 301 150 ...
40 127 560 168 ...
293 49 1651 942 ...
104 267 257 1326 ...
1272 29 1088 218 ...
66 165 1106 120 ...
1417 1234 1169 1083 ...
80 1404 1331 ]';
dateNumber = datenum(dateString,'dd-mm-yyyy');
dateVector = datevec(dateNumber);
year = dateVector(:,1);
[b,bint,r,rint,stats] = regress(log(ARR),[ones(NTRIALS,1) year]);
mdl = fitglm(year,ARR,'Link','log');
B = mdl.Coefficients.Estimate;
figure
hold on
h = line(datenum({'1993-01-01','2014-01-01'}),exp(1).^[b(1)+b(2)*1994 b(1)+b(2)*2014]);
hg = line(datenum({'1993-01-01','2014-01-01'}),exp(1).^[B(1)+B(2)*1994 B(1)+B(2)*2014]);
set(h,'Color','r')
set(hg,'Color','g')
semilogy(dateNumber,ARR,'o')
axis tight
set(gca,'YLim',[0.1 2],'YTick',[0.2 0.4 0.6 0.8 1.0 1.5 2])
set(gca,'XTick',1994:2:2014)
datetick('x')
0 Comments
Accepted Answer
Tom Lane
on 4 Sep 2014
I believe these are not the same. For the fitglm model we are saying that Y has a normal distribution whose log(mean) has a linear relationship with the predictors. Y could take on negative values in that case. For the regress model we are saying that the log of Y has a normal distribution whose mean has a linear relationship with the predictors. Y would have to be positive in that case.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!