# Pdf of skew-normal density
sn.target <- function(x, location, scale, shape){
val <- 2 * stats::dnorm(x, mean = location, sd = scale) *
pnorm(shape * (x - location) / scale)
return(val)
}
# Extract x and y coordinates from target
x.grid <- seq(-2, 6, length = 200)
y.grid <- sapply(x.grid, sn.target, location = 0, scale = 2, shape = 3)
# Computation of the fit and graphical illustration
fit <- snmatch(x.grid, y.grid)
domx <- seq(-2, 6, length = 1000)
plot(domx, sapply(domx, sn.target, location = 0, scale = 2, shape = 3),
type = "l", ylab = "f(x)", xlab = "x", lwd= 2)
lines(fit$xgrid, fit$snfit, type="l", col = "red", lwd = 2, lty = 2)
legend("topright", lty = c(1,2), col = c("black", "red"), lwd = c(2, 2),
c("Target","SN fit"), bty="n")
# Extract estimated parameters
fit$location # Estimated location parameter
fit$scale # Estimated scale parameter
fit$shape # Estimated shape parameter
Run the code above in your browser using DataLab