在 R 中将 deltaMethod 与 nls 对象一起使用时出错
我正在尝试使用 car
库中的 deltaMethod,但出现了一个奇怪的错误。
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)")
我得到的错误是: Error in eval(expr, envir, enclos) : object 'ParaParam43' not find
在尝试解决此问题时,我注意到了几件事:
- ParaParam 后面的数字根据参数而变化询问
- 它是否可以与 lm 对象一起使用,尽管我还没有尝试过
- 上面示例中的其他支持的对象,但 deltaMethod(fit,"m") 可以工作,但是没有一个其他参数也可以。这可能是因为
names(fit)
中只有m
。显然默认方法使用names(fit)
而不是coef(fit)
(对于其他类型的对象),所以该方法是否无法识别< code>fit 作为nls
对象?我需要告诉deltaMethod
期望什么类型的对象吗?
我不久前将其与现已弃用的 alr3
delta.method
一起使用,没有出现任何问题,但现在它给出了相同的错误。
有人有什么想法吗?
我正在使用 R
版本 2.13 和 car
版本 2.0-10
谢谢
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 onlym
being innames(fit)
. Apparently the default method usesnames(fit)
instead ofcoef(fit)
(as for other types of objects), so could it be that the method isn't recognisingfit
as anls
object? Do I need to telldeltaMethod
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 deltaMethod.default 方法中的一个错误,应该向 R 开发团队报告。
该错误的原因在于代码中的以下几行:
这种相当令人惊奇的替换方式使得所有名称带有 Param 中任何字母的参数都会导致代码返回不需要的内容。它应该做的是将 para.name(本例中为“s”)更改为“Paramx”。当它循环时,它将把“s”更改为“Param3”,然后将“Param3”更改为“ParaParam43”,因为它用“Param4”替换了“m”。
明显的解决方案是重命名您的参数:
运行没有错误。
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 :
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 :
runs without error.
哇,这是一个值得追踪的有趣错误...
如果您查看 deltaMethod.default,您会发现它使用 gsub 将参数名称更改为“Param1”之类的内容:
在循环中。由于您有一个名为“m”的参数,因此您会得到双重替换,因为“Param1”中有一个“m”!这解释了您收到的错误。将参数从“m”更改为“z”允许代码为我运行。
我建议联系软件包作者并提供此信息。
Wow, that was a fun bug to track down...
If you look in
deltaMethod.default
, you'll see it altering the names of the parameters to things like 'Param1' usinggsub
:in a loop. Since you have a parameter named 'm', you get a double substitution since there is an 'm' in 'Param1'! This explains the error you get. Changing your parameter from 'm' to 'z' allowed the code to run for me.
I suggest contacting the package author with this information.