[R] Discrepancies in weighted nonlinear least squares
Andrej-Nikolai Spiess
a.spiess at uke.de
Wed Sep 19 15:01:15 CEST 2012
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)
This is the unweighted fit, in the code of 'nls' one can see that 'nls'
generates a vector wts <- rep(1, n).
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 dcreasing weights for each of the 8 concentrations with 2
replicates.
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.
Why so?
If I define AIC based on residual sum-of-squares as found in the textbooks
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
log-likelihood
but
> AICrss(fm2DNase1)
[1] -56.69772
which is higher and fits perfectly to the also higher deviance of the second
model.
Could anyone enlighten me? Is the standard AIC implementation for 'nls'
models not appliccable in case of weighted fitting?
Cheers,
Andrej
___________________________
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. Guido Sauter (Vertreter des Vorsitzenden), Dr. Alexander Kirstein, Joachim Pr?l?, Prof. Dr. Dr. Uwe Koch-Gromus
More information about the R-help
mailing list