I need to find the maximum likelihood estimate for a vector of binomial data.
one like this:
binvec <- rbinom(1000, 1, 0.5)
I tried to first create the function and then optimize it with optim()
.
llbinom <- function(theta, x) {return(sum(dbinom(x=x,
size = theta[1],
prob = theta[2],log=TRUE)))}
mybin <- optim(c(0.5,0.5),fn=llbinom,x=binvec)
mybin
I do get some result but also error messages that NaN
s are being produced and the function cannot be evaluated at the initial parameters. I constructed it from an example that works for normally distributed data and believe I made a mistake in converting it.
Here, the original code I got:
ll <- function(theta,x) {return(-sum(dnorm(x=x,
mean=theta[1],sd=theta[2],log=TRUE)))}
mle <- optim(c(5,3),fn=ll,x=binvec)