简体   繁体   中英

Fit a parametric density to an constant piecewise Function in R, argument is missing error

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?

I found the solution to the syntax problem. The constrOptim function only optimize the first argument so it works if the optimized function as only one argument.

   fit.norm<-function(h){
  dist<-function(ab){
    diff2<-function(x)(h(x)-dnorm(x,ab[1], ab[2]))^2
    return(integrate(diff2,0,1)$value)
  }
  res<-constrOptim(theta = c(0.5,1), f = dist,grad=NULL, ui = rbind(c(1,0),c(-1,0),c(0,1)), ci = c(0,-1,0)) 
  return<-list(res)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM