[R] Unable to retrieve residual sum of squares from nls output
Gabor Grothendieck
ggrothendieck at gmail.com
Fri Aug 13 23:32:33 CEST 2010
On Fri, Aug 13, 2010 at 5:21 PM, Dennis Fisher <fisher at plessthan.com> wrote:
> Colleagues,
>
> I am using "nls" successfully (2.11.1, OS X) but I am having difficulties retrieving part of the output - residual sum of squares. I have assigned the output to FIT:
>> > FIT
>> Nonlinear regression model
>> model: NEWY ~ PMESOR + PAMPLITUDE * cos(2 * pi * (NEWX - POFFSET)/PERIOD)
>> data: parent.frame()
>> PMESOR PAMPLITUDE POFFSET
>> 1153.02 -1183.09 24.58
>> residual sum-of-squares: 1815056
>>
>> Number of iterations to convergence: 8
>> Achieved convergence tolerance: 1.643e-08
>
> As you can see, the object contains "residual sum-of-squares: 1815056". However, I can't figure out how to grab that value. The tried using "str":
>> > str(FIT)
>> List of 6
>> $ m :List of 16
>> ..$ resid :function ()
>> ..$ fitted :function ()
>> ..$ formula :function ()
>> ..$ deviance :function ()
>> ..$ lhs :function ()
>> ..$ gradient :function ()
>> ..$ conv :function ()
>> ..$ incr :function ()
>> ..$ setVarying:function (vary = rep(TRUE, length(useParams)))
>> ..$ setPars :function (newPars)
>> ..$ getPars :function ()
>> ..$ getAllPars:function ()
>> ..$ getEnv :function ()
>> ..$ trace :function ()
>> ..$ Rmat :function ()
>> ..$ predict :function (newdata = list(), qr = FALSE)
>> ..- attr(*, "class")= chr "nlsModel"
>> $ convInfo :List of 5
>> ..$ isConv : logi TRUE
>> ..$ finIter : int 8
>> ..$ finTol : num 1.64e-08
>> ..$ stopCode : int 0
>> ..$ stopMessage: chr "converged"
>> $ data : language parent.frame()
>> $ call : language nls(formula = NEWY ~ PMESOR + PAMPLITUDE * cos(2 * pi * (NEWX - POFFSET)/PERIOD), start = list(PMESOR = MESOR, PAMPLITUDE = AMPLITUDE, ...
>> $ dataClasses: Named chr "numeric"
>> ..- attr(*, "names")= chr "NEWX"
>> $ control :List of 2
>> ..$ maxiter : num 1000
>> ..$ warnOnly: logi TRUE
>> - attr(*, "class")= chr "nls"
>
> However, none of these elements appears to contain the residual sum of squares.
> Help! Thanks.
Try:
sum(resid(FIT)^2)
or
FIT$m$deviance()
More information about the R-help
mailing list