2
library(sn)
library(fGarch)
library(maxLik)

set.seed(12)
nl = 100
locl = 0
scalel = 1
shapel = 1

#data
y = c(rsn(n=nl, xi=locl, omega=scalel, alpha=shapel, tau=0, dp=NULL))

Assume only the shape parameter is unknown.

snormFit <- function(x, ...){   
   start = c(mean = 0, sd = 1, xi = 1)

   # Log-likelihood Function:
  loglik = function(x, y = x){ 
    f = -sum(log(dsnorm(y, 0, 1, x[3])))
    f 
   }
   # Minimization:
   fit = nlminb(start = start, objective = loglik, lower = c(-Inf, 0,   0), 
                upper = c( Inf, Inf, Inf), y = x)

   # Return Value:
   fit
 }   


shape.l = snormFit(y)$par
Warning message:
In nlminb(start = start, objective = loglik, lower = c(-Inf, 0,  :
  NA/NaN function evaluation
> shape.l
     mean        sd        xi 
0.0000000 1.0000000 0.8216856 

# Likelihood

logLik.sn = sum(log(dnorm(shape.l*y)))
Warning message:
In shape.l * y :
  longer object length is not a multiple of shorter object length
 logLik.sn
[1] -112.9638

IS the way I computed the log-likelihood correct? However, I am getting some warning messages. What is the reason for this? Are there any other ways to compute the log-likelihood for skew normal distribution in R?

Thank you in advance.

score324
  • 687
  • 10
  • 18

0 Answers0