lundi 10 juin 2019

Apply density of Distribution command in if-Statement

I want to calculate a MSE of density estimation by using Function()and if statement which generate density of that specific distribution which is provided.like dexp for exponential, dgamma for Gamma, etc

mserig<-function(y,k,h){
n <- length(y)
x <- seq(min(y) + 0.05, max(y), length = k)

fhat <- rep(0, k)
 KRIG <- matrix(rep(0, k * n), ncol = k)

 for(j in 1:k) {
 for(i in 1:n) {

 KRIG[i, j] <- 1/(sqrt(2 * pi * h * y[i])) * exp(((-1 *(y[i] - h))/(2 * h) * (x[j]/(y[i] - h) - 2 +(y[i] - h)/x[j])))
 }
 fhat[j] <- 1/n * (sum(KRIG[, j]))
d1<-density(y,bw=h)
ftrue<-sapply(x,function(x) {
    if(y<-rexp){
    dexp
    } else if(y<-rgamma){
    dgamma
    } else
    print("Not available") #true density over the x-grid
    }}
print(mean((ftrue-fhat)^2))#mse of fhat w.r.t. the true density
 }}
 y<-rexp(100,1)
h<-0.79 * IQR(y) * length(y) ^ (-1/5)
mserig(y,200,h)

but output is following error message

"print("Not available") #true density over the x-grid
}}"

I expect the output is a single value which is required MSE. Please debug the mentioned code. Regards.

Aucun commentaire:

Enregistrer un commentaire