lundi 4 novembre 2019

if else Inside function for Further Execution in R

I want to generate a function with some conditions. If those conditions are TRUE then those will be used for further execution. But my problem is that R is not finding that even that is TRUE. Following is my R code;

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")

but there is error:

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

I am getting any clue what is my mistake here. Please help me in this problem.

Aucun commentaire:

Enregistrer un commentaire