Error using deltaMethod with nls object in R

I am trying to use deltaMethod in the car library, but I am 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*(xm)))),data=gdat,start=list(a=-0.5,b=1,s=-0.6,m=5)) deltaMethod(fit,"ms*log((1/0.05)-1)") 

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

When trying to solve this, I noticed a couple of things:

  • the number after changing ParaParam depending on the specified parameters
  • it works with lm objects, although I have not tried other supported objects
  • in the above example, deltaMethod(fit,"m") works, but none of the other parameters do. This can only be attributed to the fact that m is in the names(fit) . Apparently, the default method uses names(fit) instead of coef(fit) (as for other types of objects), can it be that the method does not recognize fit as an nls object? I need to tell deltaMethod what type of object to expect?

I used this some time ago with the now deprecated alr3 delta.method without any problems, but now it gives the same error.

Does anyone have any ideas?

I am using R version 2.13 and car version 2.0-10

thanks

+4
source share
2 answers

This is a bug in the deltaMethod.default method and should be presented to the R development team.

The cause of the error is the following line 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 all the parameters, which are names with any letter from Param, make the code return what is not required. What he should do is change para.name ("s" in this case) to "Paramx". As it goes through it, it will change “s” to “Param3” and then change “Param3” to “ParaParam43” as it replaces “m” with “Param4”.

The obvious solution is to rename your options:

 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*(xk)))),data=gdat,start=list(d=-0.5,b=1,s=-0.6,k=5)) deltaMethod(fit,"ks*log((1/0.05)-1)") 

works without errors.

+6
source

Wow, that was a fun bug to track ...

If you look in deltaMethod.default , you will see that it changes the parameter names to such as "Param1" using gsub :

 g <- gsub(para.names[i], std.names.ordered[i], g) 

in a loop. Since you have a parameter named "m", you get a double replacement, since there is "m" in "Param1"! This explains your mistake. Changing your parameter from 'm' to 'z' allowed me to run the code.

I suggest contacting the package author with this information.

+4
source

All Articles