[R] Extracting degrees of freedom from a gnls object

Marc Schwartz marc_schwartz at comcast.net
Wed Jan 7 16:23:21 CET 2009


on 01/07/2009 09:01 AM Christoph Scherber wrote:
> Dear all,
> 
> How can I extract the total and residual d.f. from a gnls object?
> 
> I have tried str(summary(gnls.model)) and str(gnls.model) as well as
> gnls(), but couldn´t find the entry in the resulting lists.
> 
> Many thanks!
> 
> Best wishes
> Christoph


Using the example from ?gnls:

library(nlme)

fm1 <- gnls(weight ~ SSlogis(Time, Asym, xmid, scal), Soybean,
            weights = varPower())

> fm1
Generalized nonlinear least squares fit
  Model: weight ~ SSlogis(Time, Asym, xmid, scal)
  Data: Soybean
  Log-likelihood: -486.8974

Coefficients:
     Asym      xmid      scal
17.356822 51.872316  7.620525

Variance function:
 Structure: Power of variance covariate
 Formula: ~fitted(.)
 Parameter estimates:
    power
0.8815436
Degrees of freedom: 412 total; 409 residual
Residual standard error: 0.3662752


When reviewing the structure of the 'fm1' object in comparison to the
output, you can see that the d.f. are stored in:

> fm1$dims
$p
[1] 3

$N
[1] 412

$REML
[1] FALSE


So:

> fm1$dims$N
[1] 412


and:

> fm1$dims$N - fm1$dims$p
[1] 409


If you are unsure about where things are stored, one approach is to look
at the code for the print method for the object class to see how the
output is generated. In this case it is print.gls(), as a gnls object
inherits from a gls object.


Thus, reviewing the apropos snippet from nlme:::print.gls():

    cat("Degrees of freedom:", dd[["N"]], "total;", dd[["N"]] -
        dd[["p"]], "residual\n")

where 'dd' is created earlier in the function as:

  dd <- x$dims


This is also covered in ?gnlsObject, which shows:

dims 	a list with basic dimensions used in the model fit, including
        the components N - the number of observations used in the fit
        and p - the number of coefficients in the nonlinear model.

HTH,

Marc Schwartz




More information about the R-help mailing list