Günal - 4 months ago 23

R Question

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;

I think I need this kind of plot:

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)
```

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.