[R] Deviance and AIC in weighted NLS

Andrej-Nikolai Spiess a.spiess at uke.de
Tue Feb 12 13:43:42 CET 2013

Dear All,


I encounter some discrepancies when comparing the deviance of a weighted and
unweigthed model with the AIC values. A general example (from 'nls'):

DNase1 <- subset(DNase, Run == 1)
fm1DNase1 <- nls(density ~ SSlogis(log(conc), Asym, xmid, scal), DNase1)

Now for a weighted fit:

fm2DNase1 <- nls(density ~ SSlogis(log(conc), Asym, xmid, scal), 
                 DNase1, weights = rep(1:8, each = 2))

in which I assign increasing weights for each of the 8 concentrations with 2

Now with deviance I get:

> deviance(fm1DNase1)
[1] 0.004789569
> deviance(fm2DNase1)
[1] 0.0164259

telling me that the weighted fit has a significantly higher deviance (is a
worse fit).

Now with AIC (or BIC) I get

> AIC(fm1DNase1)
[1] -76.41642
> AIC(fm2DNase1)
[1] -372.5437

which tells me that the second fit is by orders of magnitude the better one
(lower AIC). 

However, if I define AIC based on residual sum-of-squares as found in the

RSS <- function (object) 
    w <- object$weights
    r <- residuals(object)
    if (is.null(w)) 
        w <- rep(1, length(r))
    sum(w * residuals(object)^2)
AICrss <- function(object)
  n <- nobs(object)
  k <- length(coef(object))
  rss <- RSS(object)
  n * log((2 * pi)/n) + n + 2 + n * log(rss) + 2 * k

I get 

> AICrss(fm1DNase1)
[1] -76.41642

which is the same value as the above AIC (stats:::AIC.logLik) based on


> AICrss(fm2DNase1)
[1] -56.69772

which is higher and fits perfectly to the also higher deviance of the second

Could anyone enlighten me? Is the standard AIC implementation for 'nls'
models not applicable in case of weighted fitting?

Do the weights have to be normalized somehow? I tried different stuff, but
to no avail.

Thanks in advance,








Dr. rer. nat. Andrej-Nikolai Spiess

Department of Andrology

University Hospital Hamburg-Eppendorf

Martinistr. 52

20246 Hamburg


phone: +49-40-7410-51585

fax: +49-40-7410-51554


-------------- next part --------------
Pflichtangaben gem?? Gesetz ?ber elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG):

Universit?tsklinikum Hamburg-Eppendorf; K?rperschaft des ?ffentlichen Rechts; Gerichtsstand: Hamburg

Vorstandsmitglieder: Prof. Dr. Martin Zeitz (Vorsitzender), Dr. Alexander Kirstein, Joachim Pr?l?, Prof. Dr. Dr. Uwe Koch-Gromus

More information about the R-help mailing list