Sim*_*mel 1 r function package
Effect.lm()R 包中的函数effects有一个 bug,可以通过添加 3 行代码来解决。
Effect.lm()我有下面的解决方案,但是有没有办法从 R 包实现此更改effects?
我打算间接使用Effect.lm()(通过另一个函数),并且更改必须可由其他用户复制。
附:我认为这个答案可能是相关的。不幸的是,正如他提到的,@Ben Bolker 的答案(他的 R 代码)并没有实现我上面的目标。
library(effects)
# Delete line 164 in the following
View(effects::Effect.lm)
# Then, in its place, add the following 3 lines
use <- !is.na(mod$coefficients) # new
mmat <- mod.matrix[, use] # new
if (any(is.na(V))) V <- V[use, use] # new
Run Code Online (Sandbox Code Playgroud)
最简单的方法是下载包源,适当编辑源,然后运行R CMD INSTALL以安装修改后的包。
如果您的工作流程只需要直接调用Effect.lm(即不需要通过其他包函数间接调用),那么您可以使用dump()将函数定义转储到文件,编辑该文件,然后就source()可以了。
有一些技巧涉及将函数体转换为字符向量、编辑它并替换它,例如
oldfun <- effects:::Effect.lm
## this is the 165th line of the *body*
replace_line <- 165
s <- deparse(body(oldfun))
new_lines <- c("use <- !is.na(mod$coefficients)",
"mmat <- mod.matrix[, use]",
"if (any(is.na(V))) { V <- V[use, use] } ")
s <- c(s[1:(replace_line-1)], new_lines, s[(replace_line+1):length(s)])
newfun <- oldfun
body(newfun) <- parse(text=s)
Run Code Online (Sandbox Code Playgroud)
但这是聪明且脆弱的,如果您尝试替换包环境中的功能,则将无法工作(或者,它可能会工作,但只有在尝试解锁环境等进行大量混乱之后)。
或者你可以给维护者发电子邮件并要求他们修复错误......