Re: Mathematica calculates RSquared wrongly?
- To: mathgroup at smc.vnet.net
- Subject: [mg112729] Re: Mathematica calculates RSquared wrongly?
- From: Darren Glosemeyer <darreng at wolfram.com>
- Date: Tue, 28 Sep 2010 06:06:24 -0400 (EDT)
On 9/27/2010 4:47 AM, Lawrence Teo wrote: > sbbBN = {{-0.582258428`, 0.49531889`}, {-2.475512593`, > 0.751434565`}, {-1.508540016`, 0.571212292`}, {2.004747546`, > 0.187621117`}, {1.139972167`, 0.297735572`}, {-0.724053077`, > 0.457858443`}, {-0.830992757`, 0.313642502`}, {-3.830561204`, > 0.81639874`}, {-2.357296433`, 0.804397821`}, {0.986610836`, > 0.221932888`}, {-0.513640368`, 0.704999208`}, {-1.508540016`, > 0.798426867`}}; > > nlm = NonlinearModelFit[sbbBN, a*x^2 + b*x + c, {a, b, c}, x] > nlm["RSquared"] > > > The RSquared by Mathematica is 0.963173 > Meanwhile, Excel and manual hand calculation show that R^2 should be > equal to 0.7622. > > Is Mathematica wrong? Thanks! > > This is as designed. For nonlinear models, the corrected (i.e. with the mean subtracted out) sum of squares is sometimes used. This is consistent with comparing to a constant model, but most nonlinear models do not include a constant in an additive way. For this reason, NonlinearModelFit uses the uncorrected (i.e. without subtracting out the mean) sum of squares. Because the model you are using is a linear model, you could instead use LinearModelFit, which uses corrected sums of squares if a constant term is present and assumes a constant term is present unless it is told otherwise. In[1]:= sbbBN = {{-0.582258428`, 0.49531889`}, {-2.475512593`, 0.751434565`}, {-1.508540016`, 0.571212292`}, {2.004747546`, 0.187621117`}, {1.139972167`, 0.297735572`}, {-0.724053077`, 0.457858443`}, {-0.830992757`, 0.313642502`}, {-3.830561204`, 0.81639874`}, {-2.357296433`, 0.804397821`}, {0.986610836`, 0.221932888`}, {-0.513640368`, 0.704999208`}, {-1.508540016`, 0.798426867`}}; In[2]:= nlm = NonlinearModelFit[sbbBN, a*x^2 + b*x + c, {a, b, c}, x]; In[3]:= nlm["RSquared"] Out[3]= 0.963173 In[4]:= lm = LinearModelFit[sbbBN, {x, x^2}, x]; In[5]:= lm["RSquared"] Out[5]= 0.762242 Darren Glosemeyer Wolfram Research