3
$\begingroup$

I'm trying to follow how maximum likelihood works by using R. I'm following the example here but with some other data.

But I'm confused by the output. How is the MLE so different from what I'd expect which is 0.7 i.e. number of successes / total number of trials?

# MLE for Binomial Distribution 
y<-c(0,0,0,1,1,1,1,1,1,1)
n<-length(y)
# formulation for the log likelihood for the binomial 
logL <- function(p) sum(log(dbinom(y, n, p)))
# again we can test the function for one value of p
logL(0.8)
#plot logL
p.seq <- seq(0, 0.99, 0.01)
plot(p.seq, sapply(p.seq, logL), type="l")
#optimum:
optimize(logL, lower=0, upper=1, maximum=TRUE)
$\endgroup$
4
  • 3
    $\begingroup$ I suspect you've gotten "n" wrong; in this case, it looks like you intend your data to be distributed Bernoulli(0.7) (i.e., Binomial with n=1 and p=0.7) and you have 10 observations. If your data really was Binomial with n=10, your 10 observations would be from a Binomial(n=10,p), and since your counts are all 0 or 1, p must be quite low! Try fixing n=1 inside logL and see what you get. $\endgroup$
    – jbowman
    Commented Dec 6, 2017 at 17:52
  • $\begingroup$ Thanks! That's what I was after. To clarify then, in this case are my data drawn from multiple Bernoulli distributions? $\endgroup$
    – adkane
    Commented Dec 6, 2017 at 18:08
  • 1
    $\begingroup$ Yes. You could look at it as 10 draws from a Bernoulli distribution or 1 draw from a Binomial distribution with n=10; in the latter case, you'd have to sum up the values to get "7", which would be the value of your one observation. If you write your likelihood both ways, you should get identical results from your plot and maximization. $\endgroup$
    – jbowman
    Commented Dec 6, 2017 at 18:17
  • $\begingroup$ Could you now answer your own question as you seem to have solved it? so it's not seen as unanswered? $\endgroup$ Commented Oct 19, 2018 at 22:21

1 Answer 1

3
$\begingroup$

As jbowman noted in the comments, all I need to do is change the value of n from the length of the vector to 1. Then I get the MLE as expected ~ 0.7.

# MLE for Binomial Distribution 
y<-c(0,0,0,1,1,1,1,1,1,1)
n<-1 # length(y) here's the change!
# formulation for the log likelihood for the binomial 
logL <- function(p) sum(log(dbinom(y, n, p)))
# again we can test the function for one value of p
logL(0.7)
#plot logL
p.seq <- seq(0, 0.99, 0.01)
plot(p.seq, sapply(p.seq, logL), type="l")
#optimum:
optimize(logL, lower=0, upper=1, maximum=TRUE)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.