Suppose somebody draw me an histogram and I want to smooth it, and get the smoothed function. Is their a way to do so in R? (The histogram is not coming from data, so kernel density estimators don't seem adapted. Please tell me if you think I am wrong on this.)
So far, I choose to fit a parametric distribution to my histogram. To do so I minimize the integrated square error between my histogram and a beta distribution. Here is my code, where h is a piece-wise constant function with support [0;1].
h<-function(x) (x>0 & x<1)*1
fit.beta<-function(h){
dist<-function(alpha,beta){
diff2<-function(x)(h(x)-dbeta(x,alpha,beta))^2
return(integrate(diff2,0,1))
}
res<-constrOptim(theta = c(1,1), f = dist,grad=NULL, ui = matrix(c(1,1),1,2), ci = c(0,0))
return<-res
}
And R says:
Error in dbeta(x, alpha, beta) :
argument "beta" is missing, with no default
I don't understand why R doesn't understand dbeta(x, alpha, beta). I also tryed with dbeta(x, shape1=alpha,shape2=beta) it doesn't work. Could you help me?