steiny steiny - 3 months ago 15
R Question

Error using deltaMethod with nls object in R

I'm trying to use the deltaMethod in the

car
library, but I'm getting an odd error.

library(car)
x=c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11)
y=c(-0.78636545259908996, -0.48499513735893701, -0.61035206318152102, -0.60156864486986295, -0.61323703005521701, -0.33091952573467498, -0.269895273193686, -0.32222378534205598, -0.53183084634683997, -0.96631869084439304, -0.77105781684519603, -0.524039870915605, -0.41181303531095498, -0.27581842299642001, -0.72085673574325404, -0.35874718580022702, -0.30752543764527501, -0.090745334342823197, -0.465889655296298, -0.20115970219526799, -0.0511742487116199, 0.0100170907454752, -0.176138595601495, 0.042138062483845398, 0.00081247733328697303, -0.0045220167465173499, 0.57326735553016905, 0.116862163616526, 0.0072264835163109399, 0.48714531471859701, 0.83738659120408598, 0.83740077959237003, 0.48762419789728001, 0.20072016467283199, 0.56916547038663201, 0.14651949468445999, 0.575517323481333, 0.72715907067082697, 0.99958886855260898, 0.36070109242748599, 0.49335611371191601, 0.27098248212991599, 1.28001727666798, 0.36192955257384501)
gdat=data.frame(x,y)
fit=nls(y~a+b*(exp(-exp(s*(x-m)))),data=gdat,start=list(a=-0.5,b=1,s=-0.6,m=5))
deltaMethod(fit,"m-s*log((1/0.05)-1)")


The error I get is:
Error in eval(expr, envir, enclos) : object 'ParaParam43' not found


While trying to solve this I noticed a couple things:


  • the number after ParaParam changes depending on the parameters asked

  • it works with
    lm
    objects, although I haven't tried the other supported objects

  • in the above example,
    deltaMethod(fit,"m")
    works, but none of the other parameters do. This might be due to only
    m
    being in
    names(fit)
    . Apparently the default method uses
    names(fit)
    instead of
    coef(fit)
    (as for other types of objects), so could it be that the method isn't recognising
    fit
    as a
    nls
    object? Do I need to tell
    deltaMethod
    what type of object to expect?



I used this a while back with the now deprecated
alr3
delta.method
without problems, but it now gives the same error.

Anyone have any ideas?

I'm using
R
version 2.13 and
car
version 2.0-10

Thanks

Answer

That's a bug in the deltaMethod.default method, and should be reported to the R developer team.

The reason for the bug is in following lines in the code :

for (i in seq(along = para.names)) {
    g <- gsub(para.names[i], std.names.ordered[i], g)
}

This rather amazing way of replacing things makes that all parameters that are names with any letter from Param will cause the code to return something that is not wanted. What it is supposed to do, is change the para.name ("s" in this case) into "Paramx". As it loops over it, it will change "s" to "Param3", and then it will change "Param3" to "ParaParam43", as it replaces the "m" by "Param4".

Obvious solution is to rename your parameters :

library(car)
x=c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11)
y=c(-0.78636545259908996, -0.48499513735893701, -0.61035206318152102, -0.60156864486986295, -0.61323703005521701, -0.33091952573467498, -0.269895273193686, -0.32222378534205598, -0.53183084634683997, -0.96631869084439304, -0.77105781684519603, -0.524039870915605, -0.41181303531095498, -0.27581842299642001, -0.72085673574325404, -0.35874718580022702, -0.30752543764527501, -0.090745334342823197, -0.465889655296298, -0.20115970219526799, -0.0511742487116199, 0.0100170907454752, -0.176138595601495, 0.042138062483845398, 0.00081247733328697303, -0.0045220167465173499, 0.57326735553016905, 0.116862163616526, 0.0072264835163109399, 0.48714531471859701, 0.83738659120408598, 0.83740077959237003, 0.48762419789728001, 0.20072016467283199, 0.56916547038663201, 0.14651949468445999, 0.575517323481333, 0.72715907067082697, 0.99958886855260898, 0.36070109242748599, 0.49335611371191601, 0.27098248212991599, 1.28001727666798, 0.36192955257384501)
gdat=data.frame(x,y)
fit=nls(y~d+b*(exp(-exp(s*(x-k)))),data=gdat,start=list(d=-0.5,b=1,s=-0.6,k=5))
deltaMethod(fit,"k-s*log((1/0.05)-1)")

runs without error.