# MLE in R for exponential distribution [closed]

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  ## closed as off-topic by gung - Reinstate Monica♦, Reinstate Monica, Christoph Hanck, Silverfish, Sven HohensteinFeb 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. • 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. – dsaxton Feb 16 '16 at 3:01 • 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 – ppppp-rivers Feb 16 '16 at 3:18 • 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. – dsaxton Feb 16 '16 at 3:20 • 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. – gung - Reinstate Monica Feb 16 '16 at 3:32 ## 1 Answer 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)  0.8995502  which is roughly the same as using the optimization approach: > optimize(f=nloglik,x=x,interval = c(0,5))$minimum
 0.8995525

• thx so much, tried to do it for three days... – ppppp-rivers Feb 16 '16 at 3:23
• @QuinieQiao no problem. – RustyStatistician Feb 16 '16 at 3:24
• Please don't provide complete answers for people's homework, but hints only. Our policy is here. – gung - Reinstate Monica Feb 16 '16 at 3:42
• 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. – gung - Reinstate Monica Feb 16 '16 at 3:46
• 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. – gung - Reinstate Monica Feb 16 '16 at 3:49