-2
$\begingroup$

I am trying to find the MLE based on this data set. However, I am always getting errors. Here is the code:

x       <- c(1.636, 0.374, 0.534, 3.015, 0.932, 0.179)
nloglik <- function(x, theta){ sum(-dexp(x=x, rate=theta, log=T)) }
optimize(f=nloglik, x=x, interval=c(0,5))$par
$\endgroup$

closed as off-topic by gung - Reinstate Monica, Reinstate Monica, Christoph Hanck, Silverfish, Sven Hohenstein Feb 16 '16 at 12:59

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Self-study questions (including textbook exercises, old exam papers, and homework) that seek to understand the concepts are welcome, but those that demand a solution need to indicate clearly at what step help or advice are needed. For help writing a good self-study question, please visit the meta pages." – gung - Reinstate Monica, Reinstate Monica, Christoph Hanck, Silverfish, Sven Hohenstein
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    $\begingroup$ What do you mean the MLE of a data set? You can have MLEs of parameters, and if you have an exponential distribution it is not hard to obtain the MLE for the mean parameter without software. $\endgroup$ – dsaxton Feb 16 '16 at 3:01
  • $\begingroup$ thx for the reply. for my knowledge mle for exp(lamda) is just sample mean, but my homework required to do it by R..so $\endgroup$ – ppppp-rivers Feb 16 '16 at 3:18
  • $\begingroup$ In that case just enter mean(c(1.636, 0.374, 0.534, 3.015, 0.932, 0.179)). There's no need to maximize the likelihood numerically once you have a closed form analytical solution. $\endgroup$ – dsaxton Feb 16 '16 at 3:20
  • 1
    $\begingroup$ Please add the [self-study] tag & read its wiki. Then tell us what you understand thus far, what you've tried & where you're stuck. We'll provide hints to help you get unstuck. $\endgroup$ – gung - Reinstate Monica Feb 16 '16 at 3:32
2
$\begingroup$

It is the $par that is messing up the code. Try this instead:

x<- c(1.636, 0.374, 0.534, 3.015, 0.932, 0.179)

nloglik<- function(x,theta) sum(-dexp(x=x,rate=theta,log=T))

optimize(f=nloglik,x=x,interval = c(0,5))$minimum

and so the minimum value returned by the optimize function corresponds to the value of the MLE. You can check this by recalling the fact that the MLE for an exponential distribution is:

$$\hat\lambda=\frac{1}{\bar x}$$ where $\bar x= \frac{1}{n}\sum_{i=1}^n x_i$.

Calculating that in R gives the following:

> 1/mean(x)
[1] 0.8995502

which is roughly the same as using the optimization approach:

> optimize(f=nloglik,x=x,interval = c(0,5))$minimum
[1] 0.8995525
$\endgroup$
  • $\begingroup$ thx so much, tried to do it for three days... $\endgroup$ – ppppp-rivers Feb 16 '16 at 3:23
  • $\begingroup$ @QuinieQiao no problem. $\endgroup$ – RustyStatistician Feb 16 '16 at 3:24
  • 2
    $\begingroup$ Please don't provide complete answers for people's homework, but hints only. Our policy is here. $\endgroup$ – gung - Reinstate Monica Feb 16 '16 at 3:42
  • 2
    $\begingroup$ You can do either, at your discretion. I usually provide hints in a long back & forth in comments to the Q, & then summarize them into an answer after they got it themselves. Sometimes I start an answer with a prompt & have the comment conversation below the answer. It just depends. Glen_b is the king of self-study; for an example of where I've done this, see here. $\endgroup$ – gung - Reinstate Monica Feb 16 '16 at 3:46
  • 1
    $\begingroup$ You shouldn't. You can also clearly state at the beginning of your answer that you are just giving them hints / partial information to nudge them along. $\endgroup$ – gung - Reinstate Monica Feb 16 '16 at 3:49

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