# How to read the the goodness of fit on nls of R?

I am trying to interpreting the output of nls(). I have read this post but I still don’t understand how to choose the best fit. From my fits I have two outputs:

``````> summary(m)

Formula: y ~ I(a * x^b)

Parameters:
Estimate Std. Error t value Pr(>|t|)
a 479.92903   62.96371   7.622 0.000618 ***
b   0.27553    0.04534   6.077 0.001744 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 120.1 on 5 degrees of freedom

Number of iterations to convergence: 10
Achieved convergence tolerance: 6.315e-06
``````

and

``````> summary(m1)

Formula: y ~ I(a * log(x))

Parameters:
Estimate Std. Error t value Pr(>|t|)
a   384.49      50.29   7.645 0.000261 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 297.4 on 6 degrees of freedom

Number of iterations to convergence: 1
Achieved convergence tolerance: 1.280e-11
``````

The first one has two parameters and smaller residual error. The second only one parameter but worst residual error. Which is the best fit?

You can simply use the F test and anova to compare them. Here are some codes.

``````> x <- 1:10
> y <- 2*x + 3
> yeps <- y + rnorm(length(y), sd = 0.01)
>
>
> m1=nls(yeps ~ a + b*x, start = list(a = 0.12345, b = 0.54321))
> summary(m1)

Formula: yeps ~ a + b * x

Parameters:
Estimate Std. Error t value Pr(>|t|)
a 2.9965562  0.0052838   567.1   <2e-16 ***
b 2.0016282  0.0008516  2350.6   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.007735 on 8 degrees of freedom

Number of iterations to convergence: 2
Achieved convergence tolerance: 3.386e-09

>
>
> m2=nls(yeps ~ a + b*x+c*I(x^5), start = list(a = 0.12345, b = 0.54321,c=10))
> summary(m2)

Formula: yeps ~ a + b * x + c * I(x^5)

Parameters:
Estimate Std. Error  t value Pr(>|t|)
a 3.003e+00  5.820e-03  516.010   <2e-16 ***
b 1.999e+00  1.364e-03 1466.004   <2e-16 ***
c 2.332e-07  1.236e-07    1.886    0.101
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.006733 on 7 degrees of freedom

Number of iterations to convergence: 2
Achieved convergence tolerance: 1.300e-06

>
> anova(m1,m2)
Analysis of Variance Table

Model 1: yeps ~ a + b * x
Model 2: yeps ~ a + b * x + c * I(x^5)
Res.Df Res.Sum Sq Df     Sum Sq F value Pr(>F)
1      8 0.00047860
2      7 0.00031735  1 0.00016124  3.5567 0.1013
>
``````