我想修改R中的一个特定函数,称为strata.bh,分层包。我编写函数以获取代码:
> strata.bh
function (x, bh, n = NULL, CV = NULL, Ls = 3, certain = NULL,
alloc = list(q1 = 0.5, q2 = 0, q3 = 0.5), takenone = 0, bias.penalty = 1,
takeall = 0, takeall.adjust = TRUE, rh = rep(1, Ls), model = c("none",
"loglinear", "linear", "random"), model.control = list())
{
call.ext <- match.call()
out <- valid_args(obj_fct = as.list(environment()), call.ext = call.ext)
N <- out$N
findn <- out$findn
L <- out$L
rhL <- out$rhL
takenone <- out$takenone
takeall <- out$takeall
certain <- out$certain
xnoc <- out$xnoc
Nc <- out$Nc
Nnoc <- out$Nnoc
q1 <- out$q1
q2 <- out$q2
q3 <- out$q3
nmodel <- out$nmodel
beta <- out$beta
sig2 <- out$sig2
ph <- out$ph
pcertain <- out$pcertain
gamma <- out$gamma
epsilon <- out$epsilon
args <- out$args
out <- init_stat(obj_fct = as.list(environment()))
EX <- out$EX
EX2 <- out$EX2
EYc <- out$EYc
bhfull <- c(min(x), bh, max(x) + 1)
strata.bh.internal(bhfull = bhfull, takeallin = takeall,
takeall.adjust = takeall.adjust, obj_fct = as.list(environment()))
}
在此之后我粘贴相同的代码函数,只更改名称:
> stratas.bh<-function ...
我创建了一个向量x,我尝试应用'new'函数,但是我得到了这个错误代码:
> x<-c(1,2,3,4,5,6,7,8,9,10);
>stratas.bh(x, bh=c(2,7), CV=3)
Error in stratas.bh(x) : could not find function "valid_args"
我需要加载另一个包吗?!
答案 0 :(得分:0)
这是您在修改功能后应该使用的行,无论您是否给它一个新名称:environment(stratas.bh) <- asNamespace('stratification')
这里的完整示例
stratas.bh <- function (x, bh, n = NULL, CV = NULL, Ls = 3, certain = NULL,
alloc = list(q1 = 0.5, q2 = 0, q3 = 0.5), takenone = 0, bias.penalty = 1,
takeall = 0, takeall.adjust = TRUE, rh = rep(1, Ls), model = c("none",
"loglinear", "linear", "random"), model.control = list())
{
call.ext <- match.call()
out <- valid_args(obj_fct = as.list(environment()), call.ext = call.ext)
N <- out$N
findn <- out$findn
L <- out$L
rhL <- out$rhL
takenone <- out$takenone
takeall <- out$takeall
certain <- out$certain
xnoc <- out$xnoc
Nc <- out$Nc
Nnoc <- out$Nnoc
q1 <- out$q1
q2 <- out$q2
q3 <- out$q3
nmodel <- out$nmodel
beta <- out$beta
sig2 <- out$sig2
ph <- out$ph
pcertain <- out$pcertain
gamma <- out$gamma
epsilon <- out$epsilon
args <- out$args
out <- init_stat(obj_fct = as.list(environment()))
EX <- out$EX
EX2 <- out$EX2
EYc <- out$EYc
bhfull <- c(min(x), bh, max(x) + 1)
strata.bh.internal(bhfull = bhfull, takeallin = takeall,
takeall.adjust = takeall.adjust, obj_fct = as.list(environment()))
return("It works")
}
我改变的是添加最后一行:return("It works")
现在让我们添加一条神奇的线条,看看会发生什么:
environment(stratas.bh) <- asNamespace('stratification')
x<-c(1,2,3,4,5,6,7,8,9,10);
stratas.bh(x, bh=c(2,7), CV=3) # used to return an error, now returns "It works"