0
$\begingroup$

Given iid $X_i\sim{\rm Exp}\left(\lambda\right)$ for $i=1,\ldots,n$ and an observation $x=\left(x_1,\ldots,x_n\right)$, I want to plot the simultaneous density of $\left(X_1,\ldots,X_n\right)$ for $\lambda\in(0,\infty)$.

What I've got is this

#Define function
fun <- function(obs,lambda){
  p <- 1
  for (i in 1:10){
    p <- p * dexp(obs[i],1/lambda)
  }
  return(p)
}
#Observation
x <- c(.08,.3,.3,.15,.5,.8,.15,.05,.5,1.2)
#Range of lambda to plot
lambdarng <- seq(0.1,1.9,0.01)
#Plot of likelihood function
plot(
  lambdarng,
  fun(x,lambdarng),
  type='l'
)

which produces the following plot

plot

The way that I have defined the function seems inefficient to me. $Prod()$ produces the wrong results given that $x$ and $lambdarng$ both are vectors.

Any suggestions in regards to making this more efficient would be of great appreciation.

$\endgroup$

closed as off-topic by whuber Feb 10 at 13:04

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

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – whuber
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Compare this solution: fun <- Vectorize(function(x, lambda) exp(sum(dexp(x, 1/lambda, log=TRUE))), "lambda"). For most statistical work you wouldn't even exponentiate the result: you would stay with the log likelihood. $\endgroup$ – whuber Feb 10 at 13:18