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!

xfirst.-log(sum(.)).negloglikefunction works fine when I'm using it to calculate values but the graph that is produced bystat_functiondoesn'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 ofxandlamdexp, so it will return one value only.negloglike(x = 1, lam = 0.2) = 1.809438,negloglike(x = 2, lam = 0.2) = 2.009438etc.?