Reputation: 23
Im doing some maximum likelihood in R. I have a dataset c(3,33,12,22,23) which corresponds to how long time a battery will last. The time a battery will last is Exp(theta) distributed. Im supposed to plot the Maximum likelihood function. I have this:
liklihood <- function(theta, n){
rpois(theta,n)
}
ac <- c(3,33,12,22,23)
theta <- seq(from=0, to=30, length=10)
plot(theta, liklihood(theta, n=length(ac)), type="l",xlab=expression(theta),
ylab=expression(L(theta)), col="blue",
main="1 (b)")
but it does not look right, can anyone please help we if you see what I do incorrect.
Upvotes: 0
Views: 2024
Reputation: 23
liklihood1 <- function(theta, x){
prod(dexp(n, 1/theta))
}
ac <- c(3,33,12,22,23)
theta <- seq(0, 100, by = 1)
plot(theta, unlist(lapply(theta, liklihood1, x =
ac)),type="l",xlab=expression(theta), ylab=expression(L(theta)),
col="blue", main="1 (b)")
is this correct for an exponential distribution?
Upvotes: 0
Reputation: 5232
You should check the definition of the likelihood. Likelihood is product of densities of random sample and it is function of parameter. Your function liklihood
samples (which is wrong) from Poisson distribution with probably wrong parameters - check ?rpois
(first parameter is sample size and second is lambda).
For Poisson distribution:
likelihood <- function(theta, x) prod(dpois(x, theta))
log_likelihood <- function(theta, x) sum(log(dpois(x, theta)))
Now you want to calculate likelihood for different values of theta keeping vector x
same (in your case that is vector ac
):
theta <- seq(0, 30, by = 0.5)
plot(theta, unlist(lapply(theta, likelihood, x = ac)))
For exponential distribution change dpois
with dexp
but be aware that mean of exponential is 1/rate.
Upvotes: 1