[Rd] Bug in logLik.nls (PR#2354)

arnima@u.washington.edu arnima@u.washington.edu
Sun Dec 8 00:29:03 2002


Platform: i386-pc-mingw32 (Windows XP)
Program:  R (1.6.1, 1 Nov 2002)
Package:  nls (1.6.1, 8 Nov 2002)



Dear R Core Team,


I believe there is a bug in logLik.nls because it counts model degrees of
freedom incorrectly, misleading AIC. Consider the following example:

my.lm <- lm(y~x)                        # simple linear regression
my.nls <- nls(y~b0+b1*x, start=c(0,0))  # ditto
logLik(my.lm)  # returns `log Lik.' -290.5322 (df=3), correct
logLik(my.nls) # returns `log Lik.' -290.5322 (df=1), incorrect
AIC(my.lm)     # returns 587.0645, correct
AIC(my.nls)    # returns 583.0645, incorrect


It looks like the culprit is line 10 in logLik.nls:

attr(val, "df") <- length(object[["parameters"]]) + 1

where object[["parameters"]] works in S-Plus but fails in R. This line
could be replaced with something like:

attr(val, "df") <- length(coef(object)) + 1


All the best,
Arni