title: Maximum likelihood estimation of the log-normal distribution using R
body: <p>I'm trying to estimate a linear model with a log-normal distributed error term. I already have working code for a linear model with normally distributed errors:</p>
  
  <pre><code>library(Ecdat)
  library(assertthat)
  library(maxLik)
  
  # Load the data
  data(Wages1)
  
  # Check what R says
  summary(lm(wage ~ school + exper + sex, data = Wages1))
  
  
  # Use maxLik from package maxLik
  # The likelihood function
  my_log_lik_pos &lt;- function(theta, data){
      y &lt;- data[, 1]
      x &lt;- data[, -1]
      beta &lt;- head(theta, -1)
      sigma &lt;- tail(theta, 1)
      xb &lt;- x%*%beta
      are_equal(dim(xb), c(nrow(my_data), 1))
      return(sum(log(dnorm(y, mean = xb, sd = sigma))))
  }
  
  # Bind the data
  my_data &lt;- cbind(Wages1$wage, 1, Wages1$school, Wages1$exper, Wages1$sex)
  
  my_problem &lt;- maxLik(my_log_lik_pos, data = my_data, 
                start = rep(1,5), method = "BFGS")
  
  summary(my_problem)
  </code></pre>
  
  <p>I get approximately the same results. Now I try to do the same, but using the log-normal likelihood. For this, I have to first simulate some data:</p>
  
  <pre><code>true_beta &lt;- c(0.1, 0.2, 0.3, 0.4, 0.5)
  
  ys &lt;- my_data[, -1] %*% head(true_beta, -1) + 
        rlnorm(nrow(my_data), 0, tail(true_beta, 1))
  
  my_data_2 &lt;- cbind(ys, my_data[, -1])
  </code></pre>
  
  <p>And the log-likelihood function:</p>
  
  <pre><code>my_log_lik_lognorm &lt;- function(theta, data){
      y &lt;- data[, 1]
      x &lt;- data[, -1]
      beta &lt;- head(theta, -1)
      sigma &lt;- tail(theta, 1)
      xb &lt;- x%*%beta
      are_equal(dim(xb), c(nrow(data), 1))
      return(sum(log(dlnorm(y, mean = xb, sd = sigma))))
  }
  
  my_problem2 &lt;- maxLik(my_log_lik_lognorm, data = my_data_2, 
                 start = rep(0.2,5), method = "BFGS")
  
  summary(my_problem2)
  </code></pre>
  
  <p>The estimated parameters should be around the values of true_beta, but for some reason I find completely different values. I tried with different methods, different starting values but to no avail. I'm sure that I'm missing something obvious, but I don't see what. </p>
  
  <p>Am I right to assume that the log-likelihood of the log-normal distribution is:</p>
  
  <pre><code>sum(log(dlnorm(y, mean = .., sd = ...))
  </code></pre>
  
  <p>Unless I'm mistaken, this is the definition of the log-likelihood (sum of the logs of the densities).</p>