title: R optimise log-likelihood
body: <p>I have a function that takes in lambda and a data sample, and finds the corresponding log-likelihood value for each data point:</p>
  <pre><code>data &lt;- rpois(n=25, lambda=4)
  generateLogLikelihood &lt;- function(lambda, y){
    return(dpois(y, lambda, log=TRUE))
  }
  
  LogLikelihood = generateLogLikelihood (4,data)
  LogLikelihood
  </code></pre>
  <p><strong>I'm asking for a solution to fit this requirement:</strong></p>
  <p>I need to use the optimise function to return the value of lambda which maximises the log-likelihood for the given data sample, but I'm getting stuck (this is my first attempt at using optimise). I need to adapt the function to only take 'data' as an input.</p>
  <p><strong>Where I'm getting stuck:</strong>
  I'm not sure how/when to apply optimise... given the requirement that my function only takes one input (the data - now called newdata), do I need to optimise outside of the function (but my function requires lambda values), so I'm not sure how to do this.</p>
  <p>My current code, which represents 2 separate parts that I don't know how to combine (or may be entirely wrong), is below:</p>
  <pre><code>newdata &lt;- c(23,16,18,14,19,20,12,15,15,21)
  newlambdas &lt;- seq(min(newdata),max(newdata),0.5)
  
  generateLogLikelihoodNew &lt;- function(y){
    return(dpois(y, lambda, log=TRUE))
  }
  
  LogLikelihood = optimise(generateLogLikelihoodNew,newdata,lower = min(newlambdas), upper = max(newlambdas), maximum = TRUE)
  LogLikelihood
  </code></pre>