Ok, so this has me stumped for over more then 3 days and after still not being one step closer to the solution, I'm gonna try my luck here.
In the past, I've written some code for one sorted dataset in specific and it goes like this:
n <- length(data)
maxobs <- max(data)
minobs <- min(data)
FG <- function(m=NULL, h = NULL){
n<- length(data) #Number of observations
if (m<minobs){FG = 0} else {
if (m >maxobs){FG = 1} else {
FG = sum(pnorm((m - data)/h)-pnorm((minobs-data)/h))/sum(pnorm((maxobs - data)/h)-pnorm((minobs-data)/h))
}}
return(FG)
}
f<- function(m,h){
f<- FG(m,h)^n
return(f)
}
##Integration
max <- NULL
delta<- function(h,max=maxobs){
delta <- integrate(Vectorize(f), minobs, max, h)$value
return (delta)
}
which works PERFECTLY. For example, if one selects data := c(1,2,3,4,5), one gets the correct result for
> delta(0.1, maxobs)
[1] 0.6300001
However, now I'm trying to generalize it for each sorted dataset, so what I did was (to be clear: the dataset x is sorted before exercising all these functions)
FG <- function(x, m=NULL, h = NULL){
n<- length(x) #Number of observations
maxobs <- max(x)
minobs <- min(x)
if (m<minobs){FG = 0} else {
if (m >maxobs){FG = 1} else {
FG = sum(pnorm((m - x)/h)-pnorm((minobs-x)/h))/sum(pnorm((maxobs - x)/h)-pnorm((minobs-x)/h))
}}
return(FG)
}
f<- function(x,m,h){
n <- length(x)
f<- FG(x,m,h)^n
return(f)
}
##Integration
delta<- function(x,h,maxu= max(x)){
minobs <- min(x)
delta <- integrate(Vectorize(f), minobs, maxu, h)$value
return (delta)
}
But now, delta(data,0.1)
gives
delta(data,0.1)
[1] 0.
Which doesn't make any sense to me. Same function, same dataset, but now with a wrong value. What am I doing wrong?
Any help would be dearly appreciated.
EDIT: After taking a closer look to the Vectorize function and integrate function, I've now edited my delta function to:
delta<- function(x,h,maxu= max(x)){
minobs <- min(x)
delta <- integrate(Vectorize(f, vectorize.args= c("m","h")), minobs, maxu, h)$value
return (delta)
}
but this now just results in another error:
Error in integrate(Vectorize(f, vectorize.args = c("m", "h")), lower = minobs, : evaluation of function gave a result of wrong length
I thought Vectorize was supposed to prevent such errors?