Psychologeek
Psychologeek

Reputation: 711

achieved convergence tolerance (and other outputs) from nls

So, I am using nls() to do nonlinear regression in R.

I now have some code which does it for me and I get the correct output (phew!). I can easily store the coefficients in a data frame using <- coeff(), but I also need to store some of the other data from the summary too.

Here's what I get when I run summary(Power.model)

Formula: Power.mean ~ a + (b * (Power.rep^-c))

Parameters:
  Estimate Std. Error t value Pr(>|t|)    
a 1240.197      4.075 304.358   <2e-16 ***
b   10.400     14.550   0.715    0.490    
c    6.829    230.336   0.030    0.977    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 13.97 on 11 degrees of freedom

Number of iterations to convergence: 17 
Achieved convergence tolerance: 4.011e-06

I can get the Estimates and calculate the Residual sum of squares, but I would really like to also store std.error, t value, residual std error, number of iterations and (most important of all) the achieved convergence tolerance in the table too.

I understand that I can use capture.output(summary(Power.model)) to capture these, but I just end up with a bunch of strings. What I really want is to capture only the numbers (ideally as numbers) without (a) all of the extras (e.g., the string "Achieved convergence tolerance: ") and (b) without having to convert the strings into regular (single/double) numbers (e.g., 4.011e-06 into 0.000004011).

I can't seem to find a list of all of the functions I can run on my nls output. The only ones I have found so far are coeff() and resid(). A list would be ideal, but otherwise any other advice on accessing the data in the summary without resorting to capture.output() and the string editing/conversion that would inevitably follow would be very much appreciated.

Upvotes: 0

Views: 2664

Answers (2)

G. Grothendieck
G. Grothendieck

Reputation: 269860

coef(summary(Power.model)) will give a matrix containing some of these items and Power.model$convInfo will give a list whose components contains other of these items. The residual sum of squares can be obtained using deviance(Power.model).

methods(class = "nls") will give a list of functions that act on "nls" objects and str(Power.model) and str(summary(Power.model))will show the internal components of "nls" and "summary.nls" objects.

For example, using the builtin BOD data frame:

> fm <- nls(demand ~ a + b * Time, BOD, start = list(a = 1, b = 1))
> coef(summary(fm))
  Estimate Std. Error  t value   Pr(>|t|)
a 8.521429  2.6589490 3.204811 0.03275033
b 1.721429  0.6386589 2.695380 0.05435392
> fm$convInfo
$isConv
[1] TRUE

$finIter
[1] 1

$finTol
[1] 3.966571e-09

$stopCode
[1] 0

$stopMessage
[1] "converged"

> deviance(fm)
[1] 38.06929

> sum(resid(fm)^2) # same
[1] 38.06929

You might also be interested in the broom package which will provide data frame representations of nls output like this:

> library(broom)
> tidy(fm)
  term estimate std.error statistic    p.value
1    a 8.521429 2.6589490  3.204811 0.03275033
2    b 1.721429 0.6386589  2.695380 0.05435392

> glance(fm)
     sigma isConv       finTol    logLik      AIC      BIC deviance df.residual
1 3.085016   TRUE 3.966571e-09 -14.05658 34.11315 33.48843 38.06929           4

Upvotes: 2

etienne
etienne

Reputation: 3678

use names(Power.model), it will returns you the names of the object and you can also use names(Power.model$...), with ... one of the names of Power.model. For example, Power.model$convInfo$finTol returns the Achieved convergence Tolerance.

If you are using RStudio you can click on the arrow near Power.model in the Environment window and it will displays all the names of Power.model with the value, which allow you to choose the correct name.

Upvotes: 0

Related Questions