Title: | The Normal Laplace Distribution |
---|---|
Description: | Functions for the normal Laplace distribution. Currently, it provides limited functionality. Density, distribution and quantile functions, random number generation, and moments are provided. |
Authors: | David Scott <[email protected]>, Jason Shicong Fu and Simon Potter |
Maintainer: | David Scott <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.3-2 |
Built: | 2024-10-24 20:15:43 UTC |
Source: | https://github.com/r-forge/rmetrics |
This package provides a collection of functions for Normal Laplace
distributions. Functions are provided for the density function,
distribution function, quantiles and random number generation. The mean,
variance, skewness and kurtosis of a given Normal Laplace distribution
are given by nlMean
, nlVar
, nlSkew
, and
nlKurt
respectively.
David Scott [email protected], Jason Shicong Fu
Maintainer: David Scott <[email protected]>
William J. Reed. (2006) The Normal-Laplace Distribution and Its Relatives. In Advances in Distribution Theory, Order Statistics and Inference, pp. 61–74. Birkhäuser, Boston.
dnl
, millsR
,
NormalLaplaceMeanVar
Calculates the Mills ratio
millsR(y, log = FALSE)
millsR(y, log = FALSE)
y |
Numeric. Value at which the Mills' Ratio is evaluated. |
log |
Logical. If |
The function calculates the Mills' Ratio.
Since the Mill's Ratio converges to zero for large positive
and infinity for large negative
. The range over which the
logarithm of the Mill's ratio may be calculated is greater than that
for which the Mill's ratio itself may be calculated.
The Mills' Ratio is
where and
are
respectively the distribution function and density function of the
standard normal distribution.
David Scott [email protected], Jason Shicong Fu
## compare millsR calculated directly with the millsR calculated ## by transforming to log scale and then back-transformed millsR(1:10) exp(millsR(1:10, log = TRUE)) exp(millsR(10*(1:10))) exp(millsR(10*(1:10), log = TRUE))
## compare millsR calculated directly with the millsR calculated ## by transforming to log scale and then back-transformed millsR(1:10) exp(millsR(1:10, log = TRUE)) exp(millsR(10*(1:10))) exp(millsR(10*(1:10), log = TRUE))
Given a set of parameters for the normal Laplace distribution, the functions checks the validity of each parameter and if they and if they correspond to the boundary cases.
nlCheckPars(param)
nlCheckPars(param)
param |
Numeric. Parameter values for the normal Laplace distribution. |
The vector param
takes the form c(mu, sigma, alpha, beta)
.
If any of sigma
, alpha
or beta
is negative or NA, an
error is returned.
David Scott [email protected], Simon Potter
William J. Reed. (2006) The Normal-Laplace Distribution and Its Relatives. In Advances in Distribution Theory, Order Statistics and Inference, pp. 61–74. Birkhäuser, Boston.
## Correct parameters nlCheckPars(c(0, 1.5, 1, 2)) nlCheckPars(c(3, 1, 1.5, 2)) ## Incorrect parameters, each error providing a different error message nlCheckPars(c(2, -1, 1, 1)) # invalid sigma nlCheckPars(c(2, 1, -1, 2)) # invalid alpha nlCheckPars(c(0, 1, 2, -1)) # invalid beta nlCheckPars(c(0, -0.01, -0.1, 1)) # sigma and alpha incorrect nlCheckPars(c(2, -0.5, 1, -0.2)) # sigma and beta incorrect nlCheckPars(c(1, 1, -0.2, -1)) # alpha and beta incorrect nlCheckPars(c(0, -0.1, -0.2, -0.3)) # all three parameters erroneous nlCheckPars(c(0.5, NA, 1, 1)) # NA introduced nlCheckPars(c(-1, 1, 1)) # incorrect number of parameters
## Correct parameters nlCheckPars(c(0, 1.5, 1, 2)) nlCheckPars(c(3, 1, 1.5, 2)) ## Incorrect parameters, each error providing a different error message nlCheckPars(c(2, -1, 1, 1)) # invalid sigma nlCheckPars(c(2, 1, -1, 2)) # invalid alpha nlCheckPars(c(0, 1, 2, -1)) # invalid beta nlCheckPars(c(0, -0.01, -0.1, 1)) # sigma and alpha incorrect nlCheckPars(c(2, -0.5, 1, -0.2)) # sigma and beta incorrect nlCheckPars(c(1, 1, -0.2, -1)) # alpha and beta incorrect nlCheckPars(c(0, -0.1, -0.2, -0.3)) # all three parameters erroneous nlCheckPars(c(0.5, NA, 1, 1)) # NA introduced nlCheckPars(c(-1, 1, 1)) # incorrect number of parameters
Fits a normal Laplace distribution to data. Displays the histogram, log-histogram (both with fitted densities), Q-Q plot and P-P plot for the fit which has the maximum likelihood.
nlFit(x, freq = NULL, breaks = "FD", paramStart = NULL, startMethod = "Nelder-Mead", startValues = c("MoM", "US"), method = c("Nelder-Mead", "BFGS", "L-BFGS-B", "nlm", "nlminb"), hessian = FALSE, plots = FALSE, printOut = FALSE, controlBFGS = list(maxit = 200), controlLBFGSB = list(maxit = 200), controlNLMINB = list(), controlNM = list(maxit = 1000), maxitNLM = 1500, ...) ## S3 method for class 'nlFit' print(x, digits = max(3, getOption("digits") - 3), ...) ## S3 method for class 'nlFit' plot(x, which = 1:4, plotTitles = paste(c("Histogram of ","Log-Histogram of ", "Q-Q Plot of ","P-P Plot of "), x$obsName, sep = ""), ask = prod(par("mfcol")) < length(which) & dev.interactive(), ...) ## S3 method for class 'nlFit' coef(object, ...) ## S3 method for class 'nlFit' vcov(object, ...)
nlFit(x, freq = NULL, breaks = "FD", paramStart = NULL, startMethod = "Nelder-Mead", startValues = c("MoM", "US"), method = c("Nelder-Mead", "BFGS", "L-BFGS-B", "nlm", "nlminb"), hessian = FALSE, plots = FALSE, printOut = FALSE, controlBFGS = list(maxit = 200), controlLBFGSB = list(maxit = 200), controlNLMINB = list(), controlNM = list(maxit = 1000), maxitNLM = 1500, ...) ## S3 method for class 'nlFit' print(x, digits = max(3, getOption("digits") - 3), ...) ## S3 method for class 'nlFit' plot(x, which = 1:4, plotTitles = paste(c("Histogram of ","Log-Histogram of ", "Q-Q Plot of ","P-P Plot of "), x$obsName, sep = ""), ask = prod(par("mfcol")) < length(which) & dev.interactive(), ...) ## S3 method for class 'nlFit' coef(object, ...) ## S3 method for class 'nlFit' vcov(object, ...)
x |
Data vector for |
freq |
A vector of weights with length equal to |
breaks |
Breaks for plotted histogram, defaults to those generated by
|
paramStart |
A user specified starting parameter vector taking the form
|
startMethod |
Method used by |
startValues |
Code giving the method of determining starting values for finding the maximum likelihood estimate of the parameters. |
method |
Different optimisation methods to consider. See Details. |
hessian |
Logical. If |
plots |
Logical. If |
printOut |
Logical. If |
controlBFGS |
A list of control parameters for |
controlLBFGSB |
A list of control parameters for |
controlNLMINB |
A list of control parameters for |
controlNM |
A list of control parameters for |
maxitNLM |
A positive integer specifying the maximum number of
iterations that are to be undertaken when using the |
object |
Object of class |
digits |
Desired number of digits to be shown when the object is printed. |
which |
If a subset of the plots if required, specify a subset of
the numbers |
plotTitles |
Titles to appear as the main title above the plots. |
ask |
Logical. If |
... |
Passes arguments to |
startMethod
must be "Nelder-Mead"
.
startValues
can only be "MoM"
when using the Method of
Moments for estimation, or "US"
for user-supplied parameters. For
details regarding the use of paramStart
, startMethod
and
startValues
, see nlFitStart
.
Three optimisation methods are available for use:
"BFGS"
Uses the quasi-Newton method "BFGS"
as
documented in optim
.
"L-BFGS-B"
Uses the constrained method "L-BFGS-B"
as
documented in optim
.
"Nelder-Mead"
Uses an implementation of the Nelder and
Mead method as documented in optim
.
"nlm"
Uses the nlm
function in R.
"nlminb"
Uses the nlminb
function in R,
with constrained parameters.
For details on how to pass control information for optimisation using
optim
and nlm
, see optim
and
nlm
.
When method = "nlm"
or method = "nlm"
is used, warnings
may be produced. However, these do not appear to be problematic.
A list with components:
param |
A vector giving the maximum likelihood estimate of
parameters, as
|
maxLik |
The value of maximised log-likelihood. |
hessian |
If |
method |
Optimisation method used. |
conv |
Convergence code. See the relevant documentation (either
|
iter |
Number of iterations made by the optimisation routine. |
obs |
The data used to fit the normal Laplace distribution. |
obsName |
A character vector with the actual |
paramStart |
Starting value of parameters returned by call to
|
svName |
Descriptive name for the method of finding start values. |
startValues |
Acronym for the method of finding start values. |
breaks |
The cell boundaries found by a call to
|
midpoints |
The cell midpoints found by a call to
|
empDens |
The estimated density found by a call to
|
David Scott [email protected], Simon Potter
optim
, nlm
, par
,
hist
, logHist
, qqnl
,
ppnl
, dnl
and
nlFitStart
.
param <- c(0, 2, 1, 1) dataVector <- rnl(1000, param = param) ## Let's see how well nlFit works nlFit(dataVector) nlFit(dataVector, plots = TRUE) fit <- nlFit(dataVector) par(mfrow = c(1, 2)) plot(fit, which = c(1, 3)) # See only histogram and Q-Q plot
param <- c(0, 2, 1, 1) dataVector <- rnl(1000, param = param) ## Let's see how well nlFit works nlFit(dataVector) nlFit(dataVector, plots = TRUE) fit <- nlFit(dataVector) par(mfrow = c(1, 2)) plot(fit, which = c(1, 3)) # See only histogram and Q-Q plot
Finds starting values for input to a maximum likelihood routine for fitting a normal Laplace distribution to data.
nlFitStart(x, breaks = "FD", paramStart = NULL, startValues = c("MoM", "US"), startMethodMoM = "Nelder-Mead", ...) nlFitStartMoM(x, startMethodMoM = "Nelder-Mead", ...)
nlFitStart(x, breaks = "FD", paramStart = NULL, startValues = c("MoM", "US"), startMethodMoM = "Nelder-Mead", ...) nlFitStartMoM(x, startMethodMoM = "Nelder-Mead", ...)
x |
Data vector. |
breaks |
Breaks for histogram. If missing, defaults to those
generated by
|
paramStart |
Starting values for parameter vector if
|
startValues |
Vector of the different starting value methods to consider. See Details. |
startMethodMoM |
Method used by call to |
... |
Passes arguments to |
Possible values of the argument startValues
are the following:
"US"
User-supplied.
"MoM"
Method of moments.
If startValues = "US"
then a value must be supplied for
paramStart
.
If startValues = "MoM"
, nlFitStartMoM
is
called.
If startValues = "MoM"
an initial optimisation is needed to find
the starting values. These optimisations call optim
.
nlFitStart
returns a list with components:
paramStart |
A vector with elements |
xName |
A character string with the actual |
breaks |
The cell boundaries found by a call to
|
midpoints |
The cell midpoints found by a call to
|
empDens |
The estimated density found by a call to
|
nlFitStartMoM
returns only the method of moments estimates
as a vector with elements mu
, sigma
, alpha
and
beta
.
David Scott [email protected], Simon Potter
param <- c(2, 2, 1, 1) dataVector <- rnl(500, param = param) nlFitStart(dataVector, startValues = "MoM")
param <- c(2, 2, 1, 1) dataVector <- rnl(500, param = param) nlFitStart(dataVector, startValues = "MoM")
qqnl
produces a normal Laplace Q-Q plot of the values in
y
.
ppnl
produces a normal Laplace P-P (percent-percent) or
probability plot of the values in y
.
Graphical parameters may be given as arguments to qqnl
,
and ppnl
.
qqnl(y, mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu, sigma, alpha, beta), main = "Normal Laplace Q-Q Plot", xlab = "Theoretical Quantiles", ylab = "Sample Quantiles", plot.it = TRUE, line = TRUE, ...) ppnl(y, mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu, sigma, alpha, beta), main = "Normal Laplace P-P Plot", xlab = "Uniform Quantiles", ylab = "Probability-integral-transformed Data", plot.it = TRUE, line = TRUE, ...)
qqnl(y, mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu, sigma, alpha, beta), main = "Normal Laplace Q-Q Plot", xlab = "Theoretical Quantiles", ylab = "Sample Quantiles", plot.it = TRUE, line = TRUE, ...) ppnl(y, mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu, sigma, alpha, beta), main = "Normal Laplace P-P Plot", xlab = "Uniform Quantiles", ylab = "Probability-integral-transformed Data", plot.it = TRUE, line = TRUE, ...)
y |
The data sample. |
mu |
|
sigma |
|
alpha |
|
beta |
|
param |
Parameters of the normal Laplace distribution. |
xlab , ylab , main
|
Plot labels. |
plot.it |
Logical. Should the result be plotted? |
line |
Add line through origin with unit slope. |
... |
Further graphical parameters. |
For qqnl
and ppnl
, a list with components:
x |
The x coordinates of the points that are to be plotted. |
y |
The y coordinates of the points that are to be plotted. |
Wilk, M. B. and Gnanadesikan, R. (1968) Probability plotting methods for the analysis of data. Biometrika. 55, 1–17.
par(mfrow = c(1, 2)) param <- c(2, 2, 1, 1) y <- rnl(200, param = param) qqnl(y, param = param, line = FALSE) abline(0, 1, col = 2) ppnl(y, param = param)
par(mfrow = c(1, 2)) param <- c(2, 2, 1, 1) y <- rnl(200, param = param) qqnl(y, param = param, line = FALSE) abline(0, 1, col = 2) ppnl(y, param = param)
Density function, distribution function, quantiles and random number
generation for the normal Laplace distribution, with parameters
(location),
(scale),
(skewness), and
(shape).
dnl(x, mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu,sigma,alpha,beta)) pnl(q, mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu,sigma,alpha,beta)) qnl(p, mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu,sigma,alpha,beta), tol = 10^(-5), nInterpol = 100, subdivisions = 100, ...) rnl(n, mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu,sigma,alpha,beta))
dnl(x, mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu,sigma,alpha,beta)) pnl(q, mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu,sigma,alpha,beta)) qnl(p, mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu,sigma,alpha,beta), tol = 10^(-5), nInterpol = 100, subdivisions = 100, ...) rnl(n, mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu,sigma,alpha,beta))
x , q
|
Vector of quantiles. |
p |
Vector of probabilities. |
n |
Number of random variates to be generated. |
mu |
Location parameter |
sigma |
Scale parameter |
alpha |
Skewness parameter |
beta |
Shape parameter |
param |
Specifying the parameters as a vector of the form |
tol |
Specified level of tolerance when checking if parameter beta is equal to 0. |
subdivisions |
The maximum number of subdivisions used to integrate the density and determine the accuracy of the distribution function calculation. |
nInterpol |
Number of points used in |
... |
Passes arguments to |
Users may either specify the values of the parameters individually or
as a vector. If both forms are specified, then the values specified by
the vector param
will overwrite the other ones.
The density function is
The distribution function is
The function is the Mills' Ratio, see
millsR
.
Generation of random observations from the normal Laplace distribution
using rnl
is based on the representation
where and
are independent random variables with
and following an asymmetric Laplace distribution with pdf
dnl
gives the density function, pnl
gives the
distribution function, qnl
gives the quantile function and
rnl
generates random variates.
David Scott [email protected], Jason Shicong Fu
William J. Reed. (2006) The Normal-Laplace Distribution and Its Relatives. In Advances in Distribution Theory, Order Statistics and Inference, pp. 61–74. Birkhäuser, Boston.
param <- c(0,1,3,2) par(mfrow = c(1,2)) ## Curves of density and distribution curve(dnl(x, param = param), -5, 5, n = 1000) title("Density of the Normal Laplace Distribution") curve(pnl(x, param = param), -5, 5, n = 1000) title("Distribution Function of the Normal Laplace Distribution") ## Example of density and random numbers par(mfrow = c(1,1)) param1 <- c(0,1,1,1) data1 <- rnl(1000, param = param1) curve(dnl(x, param = param1), from = -5, to = 5, n = 1000, col = 2) hist(data1, freq = FALSE, add = TRUE) title("Density and Histogram")
param <- c(0,1,3,2) par(mfrow = c(1,2)) ## Curves of density and distribution curve(dnl(x, param = param), -5, 5, n = 1000) title("Density of the Normal Laplace Distribution") curve(pnl(x, param = param), -5, 5, n = 1000) title("Distribution Function of the Normal Laplace Distribution") ## Example of density and random numbers par(mfrow = c(1,1)) param1 <- c(0,1,1,1) data1 <- rnl(1000, param = param1) curve(dnl(x, param = param1), from = -5, to = 5, n = 1000, col = 2) hist(data1, freq = FALSE, add = TRUE) title("Density and Histogram")
Functions to calculate the mean, variance, skewness and kurtosis of a specified normal Laplace distribution.
nlMean(mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu, sigma, alpha, beta)) nlVar(mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu, sigma, alpha, beta)) nlSkew(mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu, sigma, alpha, beta)) nlKurt(mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu, sigma, alpha, beta))
nlMean(mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu, sigma, alpha, beta)) nlVar(mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu, sigma, alpha, beta)) nlSkew(mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu, sigma, alpha, beta)) nlKurt(mu = 0, sigma = 1, alpha = 1, beta = 1, param = c(mu, sigma, alpha, beta))
mu |
Location parameter |
sigma |
Scale parameter |
alpha |
Skewness parameter |
beta |
Shape parameter |
param |
Specifying the parameters as a vector of the form |
Users may either specify the values of the parameters individually or
as a vector. If both forms are specified, then the values specified by
the vector param
will overwrite the other ones.
The mean function is
The variance function is
The skewness function is
The kurtosis function is
nlMean
gives the mean of the skew hyperbolic nlVar
the
variance, nlSkew
the skewness, and nlKurt
the kurtosis.
David Scott [email protected], Jason Shicong Fu
William J. Reed. (2006) The Normal-Laplace Distribution and Its Relatives. In Advances in Distribution Theory, Order Statistics and Inference, pp. 61–74. Birkhäuser, Boston.
param <- c(10,1,5,9) nlMean(param = param) nlVar(param = param) nlSkew(param = param) nlKurt(param = param) curve(dnl(x, param = param), -10, 10)
param <- c(10,1,5,9) nlMean(param = param) nlVar(param = param) nlSkew(param = param) nlKurt(param = param) curve(dnl(x, param = param), -10, 10)
summary
Method for class "nlFit"
.
## S3 method for class 'nlFit' summary(object, ...) ## S3 method for class 'summary.nlFit' print(x, digits = max(3, getOption("digits") - 3), ...)
## S3 method for class 'nlFit' summary(object, ...) ## S3 method for class 'summary.nlFit' print(x, digits = max(3, getOption("digits") - 3), ...)
object |
An object of class |
x |
An object of class |
digits |
The number of significant digits to use when printing. |
... |
Further arguments passed to or from other methods. |
summary.nlFit
calculates standard errors for the estimates
of ,
,
, and
of the normal laplace distribution parameter vector
param if the Hessian from the call to
nlFit
is available.
If the Hessian is available, summary.nlFit
computes
standard errors for the estimates of ,
,
, and
, and adds them to
object
as object$sds
. Otherwise, no calculations are performed and the
composition of object
is unaltered.
summary.nlFit
invisibly returns object
with class changed to summary.nlFit
.
See nlFit
for the composition of an object of class
nlFit
.
print.summary.nlFit
prints a summary in the same format as
print.nlFit
when the Hessian is not available from
the fit. When the Hessian is available, the standard errors for the
parameter estimates are printed in parentheses beneath the parameter
estimates, in the manner of fitdistr
in the package
MASS
.
## Continuing the nlFit() example: param <- c(2, 2, 1, 1) dataVector <- rnl(500, param = param) fit <- nlFit(dataVector, hessian = TRUE) print(fit) summary(fit)
## Continuing the nlFit() example: param <- c(2, 2, 1, 1) dataVector <- rnl(500, param = param) fit <- nlFit(dataVector, hessian = TRUE) print(fit) summary(fit)