Günal Günal - 3 months ago 14
R Question

How to plot the log-likelihood of binomial distribution

In order to solve (c), I think I need a plot of the log-likelihood of the binomial distribution. Can anyone please help me do it in R? The data and the question is as follows;

enter image description here

enter image description here

I think I need this kind of plot:

enter image description here

Answer

Something like this should work:

F <- c(18,31,34,33,27,33,28,23,33,12,19,25,14,4,22,7)
M <- c(11,22,27,29,24,29,25,26,38,14,23,31,20,6,34,12)
Y <- F
N <- F + M
#a)
Y / N
#b)
sum(Y) / sum(N)
#c)
logL <- function(p) sum(log(dbinom(Y, N, p)))
#plot logL:
p.seq <- seq(0.01, 0.99, 0.01)
plot(p.seq, sapply(p.seq, logL), type="l")
#optimum:
optimize(logL, lower=0, upper=1, maximum=TRUE)

enter image description here

As noted by Ben (see comments), the numerical accuracy is increased by the use of: logL <- function(p) sum(dbinom(Y,N,p,log=TRUE)) instead, especially it can "rescue" you in cases where dbinom() returns 0 but the likelihood-score is actually just close to 0.