function - Solving for two parameters using `optim()` in R? -
i'm trying find shape1 , shape2 in dbeta() such answer dbeta() 2 input values: .6 , .8 become 3.
i'm using below optim() don't exact result, expect getting values shape1 , shape2 when used .6 , .8 give 3 , 3, don't, why?
f <- function(x) { y <- c(3, 3) - dbeta(c(.6, .8), shape1 = x[1], shape2 = x[2]) } aa = optim(c(1, 1), function(x) sum(f(x)^2), control = list(reltol = (.machine$double.eps))) parms = unname(aa$par) dbeta(c(.6, .8), parms[1], parms[2]) # here expect `3` `.6` , `.8` don't.
i had brief @ this. don't think there's problem fit: here's picture of likelihood surface:
library(emdbook) cc <- curve3d(g(c(x,y)),xlim=c(1,20),ylim=c(1,20), sys3d="none") pp <- which(cc$z==min(cc$z),arr.ind=true) png("betasurf.png") with(cc,image(x,y,z)) points(parms[1],parms[2],pch=16) points(cc$x[pp[1]],cc$y[pp[2]],pch=1) dev.off() filled circle fitted value, open circle minimum of grid; think difference numerical fuzz (i zoomed in few times make sure). in case, there's no evidence of weird multiple optima.
i believe issue you've set set of pair of points can't simultaneously matched any beta distribution; optim() giving best possible fit ...
png("betatmp.png") curve(dbeta(x,parms[1],parms[2]),from=0,to=1) points(c(0.6,0.8),c(3,3),pch=16) dev.off() 

Comments
Post a Comment