views:

1578

answers:

6

What functions do you use in R to fit a curve to your data and test how well that curve fits? What results are considered good?

+6  A: 

The nls() function (http://sekhon.berkeley.edu/stats/html/nls.html) is pretty standard for nonlinear least-squares curve fitting. Chi squared (the sum of the squared residuals) is the metric that is optimized in that case, but it is not normalized so you can't readily use it to determine how good the fit is. The main thing you should ensure is that your residuals are normally distributed. Unfortunately I'm not sure of an automated way to do that.

Craig
+10  A: 

Just the first part of that question can fill entire books. Just some quick choices:

  • lm() for standard linear models
  • glm() for generalised linear models (eg for logistic regression)
  • rlm() from package MASS for robust linear models
  • lmrob() from package robustbase for robust linear models
  • loess() for non-linear / non-parametric models

Then there are domain-specific models as e.g. time series, micro-econometrics, mixed-effects and much more. Several of the Task Views as e.g. Econometrics discuss this in more detail. As for goodness of fit, that is also something one can spend easily an entire book discussing.

Dirk Eddelbuettel
@Dirk: Any recommendations for books or good websites on goodness of fit?
Dave Jarvis
+4  A: 

The Quick R site has a reasonable good summary of basic functions used for fitting models and testing the fits, along with sample R code:

ars
+7  A: 

The workhorses of canonical curve fitting in R are lm(), glm() and nls(). To me, goodness-of-fit is a subproblem in the larger problem of model selection. Infact, using goodness-of-fit incorrectly (e.g., via stepwise regression) can give rise to seriously misspecified model (see Harrell's book on "Regression Modeling Strategies"). Rather than discussing the issue from scratch, I recommend Harrell's book for lm and glm. Venables and Ripley's bible is terse, but still worth a reading. "Extending the Linear Model with R" by Faraway is comprehensive and readable. nls is not covered in these sources, but "Nonlinear Regression with R" by Ritz & Streibig fills the gap and is very hands-on.

gappy
+1  A: 

The main thing you should ensure is that your residuals are normally distributed. Unfortunately I'm not sure of an automated way to do that.

qqnorm() could probably be modified to find the correlation between the sample quantiles and the theoretical quantiles. Essentially, this would just be a numerical interpretation of the normal quantile plot. Perhaps providing several values of the correlation coefficient for different ranges of quantiles could be useful. For example, if the correlation coefficient is close to 1 for the middle 97% of the data and much lower at the tails, this tells us the distribution of residuals is approximately normal, with some funniness going on in the tails.

Ryan Rosario
+1  A: 

Best to keep simple, and see if linear methods work "well enuff". You can judge your goodness of fit GENERALLY by looking at the R squared AND F statistic, together, never separate. Adding variables to your model that have no bearing on your dependant variable can increase R2, so you must also consider F statistic.

You should also compare your model to other nested, or more simpler, models. Do this using log liklihood ratio test, so long as dependant variables are the same.

Jarque–Bera test is good for testing the normality of the residual distribution.

christian miner