if else Inside函数,用于在R中进一步执行

我想在某些条件下生成函数。如果这些条件为TRUE,则将这些条件用于进一步执行。但是我的问题是R仍然没有发现那是真的。以下是我的R代码;

require(KernSmooth)
require(kerdiest)

mini<-function(y,k,h1=TRUE,h2=TRUE,h3=TRUE,type){
n <- length(y)
x <- seq(min(y) + 0.05,max(y),length = k)

if(h1==TRUE){
     h1 <- abs(dpik(y) ) 
}     else {     print("DPI is not selected")
}


if(h2==TRUE){
     h2<-abs(ALbw(vec_data=y)) 
}     else {     print("AL is not selected")
}

if(h3==TRUE){
    h3 <-abs(1.06*sd(y)*(n^(-1/5)))
}     else {    print("NSR is not selected")
}

ftrue<-switch(type,Exp = dexp(x,(1/mean(x))),Gamma = dgamma(x,(mean(x)/(var(x)/mean(x))),(var(x)/mean(x)))
)

dpi<-h1-sum(ftrue)
AL<-h2*3
nsr<-h3*4
v<-c("DPI"=dpi,"Altaman"=AL,"NSR"=nsr)
v[which.min(v)]
}#function end

y<-rexp(100,1)
mini(y,200,h1,h2,h3,"exp")

但是有错误:

Error in mini(y,"exp") : object 'h1' not found

我知道这里的错误是什么。请帮助我解决这个问题。

biyesheng630 回答:if else Inside函数,用于在R中进一步执行

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3166842.html

大家都在问