【发布时间】:2014-12-10 11:12:05
【问题描述】:
我需要更改包中名为 mirt 的函数中的几行。
该函数是fscores.internal,不是主函数,您可以使用edit 或fix 更改代码行。
如何在 RStudio 中做到这一点?
我尝试了以下操作:
library(mirt)
data(LSAT7)
LSAT7=expand.table(LSAT7)
mod=mirt(LSAT7, 1,itemtype = '3PL')
fscor=fscores(object = mod,method = "ML",response.pattern = c(1,NA,NA,NA,NA),theta_lim = c(-4, 4))
fscor
Item.1 Item.2 Item.3 Item.4 Item.5 F1 SE_F1
[1,] 1 NA NA NA NA Inf NA
这是最终输出。我需要“theta_lim[2]”而不是“Inf”作为 F1 的输出,当您将 Inf 替换为 theta_lim[2] 和 -Inf 替换为 theta_lim[1] 时可以获得(分别在第 278 和 280 行,在 fscores .internal 代码。这是我要做的唯一更改。
使用fixInNamespace("fscores.internal", "mirt")后
出现一个窗口,其中包含:
function (object, ...)
standardGeneric("fscores.internal")
(fixInNamespace 没有帮助)
使用后
myfscores.internal <- function() { ... }
assignInNamespace("fscores.internal", myfscores.internal, "mirt")
运行时出现以下错误
fscor=fscores(object = mod,method = "ML",response.pattern = c(1,1,1,1,1),theta_lim = c(-4, 4))
## Error in .local(object, ...) : could not find function "ExtractGroupPars"
然后我单独编译ExtractGroupPars函数。
然后出局是
> fscor=fscores(object = mod,method = "ML",response.pattern = c(1,1,1,1,1),theta_lim = c(-4, 4))
Error in .local(object, ...) : could not find function "rotateLambdas"
在这之后我编译了rotateLambdas函数
我跑了
fscor=fscores(object = mod,method = "ML",response.pattern = c(1,1,1,1,1),theta_lim = c(-4, 4))
然后我得到以下错误:
Error in matrix(rep(sqrt(1 - h2), ncol(F)), ncol = ncol(F)) :
non-numeric matrix extent
Called from: .rs.breakOnError(TRUE)
不知道之后该怎么办。
【问题讨论】:
-
fscores.internal是纯R代码,可以从源代码压缩包轻松访问以进行编辑。也就是说,我强烈建议反对使用内部方法。如果包代码有错误,请通知维护者。如果您只是想做一些不同的事情,那么编写一个包装器或辅助函数来修改fscores的输出。内部函数并不意味着被搞砸。 -
您使用了
assignInNamespace或fixInNamespace?修改函数后尝试重新运行代码后发生了什么? -
@Thomas,我已进一步编辑问题,请添加您的建议。
标签: r