Thread Subject: Goodness of a fit or Chi-square?

Subject: Goodness of a fit or Chi-square?

From: Nima Azar

Date: 29 May, 2008 21:27:01

Message: 1 of 7

Hi Guys:
So I'm using "lsqcurvefit" to fit my data to a known
function, I'd like to know how good of a fit I get either
by getting the %error or the the chi-square value or both.
I looked at the help file for "lsqcurvefit" but couldnt
find anything there, does matlab have any function for this
issue?

thank you.

-Nima

Subject: Goodness of a fit or Chi-square?

From: Tom Lane

Date: 29 May, 2008 21:41:31

Message: 2 of 7

> So I'm using "lsqcurvefit" to fit my data to a known
> function, I'd like to know how good of a fit I get either
> by getting the %error or the the chi-square value or both.
> I looked at the help file for "lsqcurvefit" but couldnt
> find anything there, does matlab have any function for this
> issue?

Nima, it should be easy enough to compute using the information available.

I'm not sure what you intend by %error or chi-square, but let's compute
R-square. It's the proportion of the variance explained by the model.
Maybe it's one minus the %error value you want. It's computed by a
Statistics Toolbox function, and it can be computed using the results from
lsqcurvefit:

>> load census
>> s = regstats(pop,cdate,'linear','rsquare')
s =
     source: 'regstats'
    rsquare: 0.9210
>> [b,resnorm] = lsqcurvefit(@(b,x)b(1)+b(2)*x,[1 1],cdate,pop)
Optimization terminated: relative function value
 changing by less than OPTIONS.TolFun.
b =
  1.0e+003 *
   -2.2120 0.0012
resnorm =
  9.7572e+003
>> 1 - resnorm / norm(pop-mean(pop))^2
ans =
    0.9210

-- Tom


Subject: Goodness of a fit or Chi-square?

From: Nima Azar

Date: 29 May, 2008 22:46:03

Message: 3 of 7

"Tom Lane" <tlane@mathworks.com> wrote in message
<g1n7uc$avl$1@fred.mathworks.com>...
> > So I'm using "lsqcurvefit" to fit my data to a known
> > function, I'd like to know how good of a fit I get
either
> > by getting the %error or the the chi-square value or
both.
> > I looked at the help file for "lsqcurvefit" but couldnt
> > find anything there, does matlab have any function for
this
> > issue?
>
> Nima, it should be easy enough to compute using the
information available.
>
> I'm not sure what you intend by %error or chi-square, but
let's compute
> R-square. It's the proportion of the variance explained
by the model.
> Maybe it's one minus the %error value you want. It's
computed by a
> Statistics Toolbox function, and it can be computed using
the results from
> lsqcurvefit:
>
> >> load census
> >> s = regstats(pop,cdate,'linear','rsquare')
> s =
> source: 'regstats'
> rsquare: 0.9210
> >> [b,resnorm] = lsqcurvefit(@(b,x)b(1)+b(2)*x,[1
1],cdate,pop)
> Optimization terminated: relative function value
> changing by less than OPTIONS.TolFun.
> b =
> 1.0e+003 *
> -2.2120 0.0012
> resnorm =
> 9.7572e+003
> >> 1 - resnorm / norm(pop-mean(pop))^2
> ans =
> 0.9210
>
> -- Tom
>
>
Thank you Tom:
however is there any function that gives you the Chi-square
value or the percent error? to see exactly how good of a
fit you got? I was under the impression that R^2 is only
for linear graphs, while I'm doing a nonlinear lsq curve
fitting.
thanks

Subject: Goodness of a fit or Chi-square?

From: Tom Lane

Date: 30 May, 2008 13:58:44

Message: 4 of 7

> Thank you Tom:
> however is there any function that gives you the Chi-square
> value or the percent error? to see exactly how good of a
> fit you got? I was under the impression that R^2 is only
> for linear graphs, while I'm doing a nonlinear lsq curve
> fitting.
> thanks

Nima, would you explain what you mean by percent error or chi-square?

The R^2 measure is okay for nonlinear fits as long as the nonlinear equation
contains a constant (as in a+b*x^c) or can be constant (as in b*x^c which is
constant if c=0). If there's no constant, then there's ambiguity about
whether you ought to be comparing the residuals to the sum of squared
responses or to the sum of their squared deviations from their mean.

The quanitity 1-R^2 could be interpreted as a percent error. It's the
proportion of the response variance not explained by the model.

Maybe I'm just not thinking right, but I can't think of what chi-square
computation you want.

-- Tom


Subject: Goodness of a fit or Chi-square?

From: Nima Azar

Date: 2 Jun, 2008 00:01:04

Message: 5 of 7

So Once the fit is complete, I would like to know how good
of a fit I have. By Percent error I refer to a value that
states wheter or not my fit is significant.
i.e. the lower the chi-square the better fit you have. I'm
wondering if I can calculate that with my fit.
I dont think 1-R^2 is what I'm looking for since it doesnt
make sence with the published data.

Nima.

"Tom Lane" <tlane@mathworks.com> wrote in message
<g1p16k$biq$1@fred.mathworks.com>...
> > Thank you Tom:
> > however is there any function that gives you the Chi-
square
> > value or the percent error? to see exactly how good of a
> > fit you got? I was under the impression that R^2 is only
> > for linear graphs, while I'm doing a nonlinear lsq curve
> > fitting.
> > thanks
>
> Nima, would you explain what you mean by percent error or
chi-square?
>
> The R^2 measure is okay for nonlinear fits as long as the
nonlinear equation
> contains a constant (as in a+b*x^c) or can be constant
(as in b*x^c which is
> constant if c=0). If there's no constant, then there's
ambiguity about
> whether you ought to be comparing the residuals to the
sum of squared
> responses or to the sum of their squared deviations from
their mean.
>
> The quanitity 1-R^2 could be interpreted as a percent
error. It's the
> proportion of the response variance not explained by the
model.
>
> Maybe I'm just not thinking right, but I can't think of
what chi-square
> computation you want.
>
> -- Tom
>
>

Subject: Goodness of a fit or Chi-square?

From: Peter Perkins

Date: 2 Jun, 2008 13:47:25

Message: 6 of 7

Nima Azar wrote:
> So Once the fit is complete, I would like to know how good
> of a fit I have. By Percent error I refer to a value that
> states wheter or not my fit is significant.
> i.e. the lower the chi-square the better fit you have. I'm
> wondering if I can calculate that with my fit.
> I dont think 1-R^2 is what I'm looking for since it doesnt
> make sence with the published data.

Nima, chi-square is usually something you use when you're fitting a single
distribution, i.e., only a single variable. As I read your messages, you're
fitting a regression model, i.e., (at least) two variables, a(several)
predictor(s) and a response.

r^2 is a measure of how well as regression model predicts.

Forget about what method you need to use. What's the question you want to answer?

Subject: Goodness of a fit or Chi-square?

From: Nima Azar

Date: 5 Jun, 2008 20:45:04

Message: 7 of 7

Peter Perkins <Peter.PerkinsRemoveThis@mathworks.com> wrote
in message <g20tld$sn4$2@fred.mathworks.com>...
> Nima Azar wrote:
> > So Once the fit is complete, I would like to know how
good
> > of a fit I have. By Percent error I refer to a value
that
> > states wheter or not my fit is significant.
> > i.e. the lower the chi-square the better fit you have.
I'm
> > wondering if I can calculate that with my fit.
> > I dont think 1-R^2 is what I'm looking for since it
doesnt
> > make sence with the published data.
>
> Nima, chi-square is usually something you use when you're
fitting a single
> distribution, i.e., only a single variable. As I read
your messages, you're
> fitting a regression model, i.e., (at least) two
variables, a(several)
> predictor(s) and a response.
>
> r^2 is a measure of how well as regression model predicts.
>
> Forget about what method you need to use. What's the
question you want to answer?

Thanks Peter,
So I'm trying to see how good of a fit I get. so from your
explanation I undrestand that R^2 would do it for me, it's
just that in the publications I have seen people mentioning
Percent Error. I'm just wondering what that is and how it
can be obtained?

thanks.

-Nima

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com