1

I'm needing help with the following question:

Consider the following R function, named negloglike that has two input arguments: lam and x, in that order. Use this function to produce a plot of the log-likelihood function over a range of values λ ∈ (0, 2).

negloglike <- function(lam, x) {
  l = -sum(log(dexp(x, lam)))
  return(l)
}

Can anyone please help? Is it possible to do something like this with ggplot? I've been trying to do it with a set value of lam (like 0.2 here for example) using stat_function:

ggplot(data = data.frame(x = 0), mapping = aes(x = x)) +
  stat_function(fun = negloglike, args = list(lam = 0.2)) +
  xlim(0,10)

but the plot always returns a horizontal line at some y-value instead of returning a curve.

Should I be possibly using a different geom? Or even a different package altogether?

Much appreciated!

ethy_32
  • 11
  • 4
  • 2
    You should edit **the question** and post the code you have used. To know what you are doing is a first step to know what is wrong with it. Tip: I would start by swapping the order of the arguments, `x` first. – Rui Barradas Aug 25 '18 at 08:41
  • Also, it's `-log(sum(.))`. – Rui Barradas Aug 25 '18 at 08:48
  • Thanks for the suggestion! I've added in my code to the question. The `negloglike` function works fine when I'm using it to calculate values but the graph that is produced by `stat_function` doesn't work the same (with the way that I've done it anyway). Changing it to `-log(sum(.))` doesn't seem to make any difference, nor does swapping the order of `x` and `lam` – ethy_32 Aug 25 '18 at 08:50
  • Note that the function sums over all values of `dexp`, so it will return one value only. – Rui Barradas Aug 25 '18 at 08:54
  • Yes, but isn't that usually the case when plotting a function? To plot one value for every x-value? Or am I misunderstanding the way the mechanics work? e.g. `negloglike(x = 1, lam = 0.2) = 1.809438`, `negloglike(x = 2, lam = 0.2) = 2.009438` etc.? – ethy_32 Aug 25 '18 at 08:57

1 Answers1

2

The trick is to Vectorize the function over the argument of interest.
Thanks for the tip go to the most voted answer to this question. It uses base graphics only, so here is a ggplot2 equivalent.
First I will define the negative log-likelihood using function dexp

library(ggplot2)

negloglike <- function(lam, x) {negloglike <- function(lam, x) {
    l = -sum(dexp(x, lam, log = TRUE))
    return(l)
}
nllv <- Vectorize(negloglike, "lam")

But it's better to use the analytic form, which is easy to establish by hand.

negloglike2 <- function(lam, x) {
    l = lam*sum(x) - length(x)*log(lam)
    return(l)
}

nllv2 <- Vectorize(negloglike2, "lam")

ggplot(data = data.frame(lam = seq(0, 2, by = 0.2)), mapping = aes(x = lam)) +
  stat_function(fun = nllv2, args = list(x = 0:10))

enter image description here

Both nllv and nllv2 give the same graph.

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • Awesome Rui, thanks so much! This helped heaps. I thought there had to be some vectorising in some shape or form after reading a couple of posts but I wasn't really sure about how to apply it here. – ethy_32 Aug 25 '18 at 13:09
  • @ethy_32 Sorry but I made a mistake with the log-likelihood. See the new code and graph. – Rui Barradas Aug 25 '18 at 13:42