[R] [FORGED] How to find the likelihood, MLE and plot it?

Rolf Turner r.turner at auckland.ac.nz
Thu Nov 19 00:23:53 CET 2015


On 19/11/15 11:31, C W wrote:
> Dear R list,
>
> I am trying to find the MLE of the likelihood function.  I will plot the
> log-likelihood to check my answer.
>
> Here's my R code:
>
> xvec <- c(2,5,3,7,-3,-2,0)
>
> fn <- function(theta){
>
> sum(0.5 * (xvec - rep(theta, 7)) ^ 2 / 1 + 0.5 * log(1))
>
> }
>
> gn <- Vectorize(fn)
>
> curve(gn, -5, 20)
>
> optimize(gn, c(-5, 20))
>
> $minimum
>
> [1] 1.714286
>
> $objective
>
> [1] 39.71429
>
>
> The MLE using optimize() is 1.71, but what curve() gives me is the absolute
> minimum.
>
> I think 1.71 is the right answer, but why does the graph showing it's the
> minimum?  What is going on here?

Your graph shows that there is indeed a *minimum* at 1.71.  And 
optimise() is correctly finding that minimum.

If you want optimise() to find the maximum, set maximum=TRUE.  In which 
case it will return "20" (or something very close to 20).

Your function fn() appears not to be the log likelihood that you had in 
mind.  Perhaps you the negative of fn()???

cheers,

Rolf Turner

-- 
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276



More information about the R-help mailing list