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? Thank so much! [[alternative HTML version deleted]]
Rolf Turner
2015-Nov-18 23:23 UTC
[R] [FORGED] How to find the likelihood, MLE and plot it?
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
Hi Rolf, I think the MLE should be 1.71, no? And yes, I am aware of the maximum=TRUE argument. I still feel something is wrong here. Thanks! On Wed, Nov 18, 2015 at 6:23 PM, Rolf Turner <r.turner at auckland.ac.nz> wrote:> 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 >[[alternative HTML version deleted]]