Title: | The Unifed Distribution |
---|---|
Description: | Probability functions, family for glm() and Stan code for working with the unifed distribution (Quijano Xacur, 2019; <doi:10.1186/s40488-019-0102-6>). |
Authors: | Oscar Alberto Quijano Xacur [aut,cre] |
Maintainer: | Oscar Alberto Quijano Xacur <[email protected]> |
License: | GPL (>= 3) |
Version: | 1.1.6 |
Built: | 2024-12-08 07:15:59 UTC |
Source: | CRAN |
This data set is based on one-year vehicle insurance policies taken out in 2004 or 2005. There are 67856 policies, of which 4624 (6.8%) had at least one claim.
car.insurance
car.insurance
A data frame with 67,856 rows and 11 columns:
vehicle value, in $10,000s
Values between 0 and 1
occurrence of claim (0 = no, 1 = yes)
number of claims
claim amount (0 if no claim)
vehicle body, coded as
convertible
hatchback
hardtop
motorized caravan
minibus
panel van
roadster
station wagon
utility
age of vehicle: 1 (youngest), 2, 3, 4
gender of driver: M, F
driver's area of residence: A, B, C, D, E, F
driver's age category: 1 (youngest), 2, 3, 4, 5, 6
De Jong, P., and G.Z. Heller. 2008. Generalized Linear Models for Insurance Data. Cambridge University Press. http://dx.doi.org/10.1017/CBO9780511755408
Irwin-Hall density
dirwin.hall(x, n, log = FALSE)
dirwin.hall(x, n, log = FALSE)
x |
A number between 0 and |
n |
Number of uniform distributions in the unit interval to sum. |
log |
If it evaluates to |
Gives the density of the Irwin-Hall distribution. It is
the density of the sum of n
uniform distributions on the
interval (0,1).
where and
is a positive integer.
This function is not numerically stable. The examples have some cases of this.
dirwin.hall(2,5) # Numerically unstable example # Run the following one after the other # See how it goes from positive to negative (which means overflowing ) dirwin.hall(35,50) dirwin.hall(36,50) dirwin.hall(37,50) dirwin.hall(38,50)
dirwin.hall(2,5) # Numerically unstable example # Run the following one after the other # See how it goes from positive to negative (which means overflowing ) dirwin.hall(35,50) dirwin.hall(36,50) dirwin.hall(37,50) dirwin.hall(38,50)
Density, distribution function, quantile function and random generation for the unifed distribution.
dunifed(x, theta) unifed.lcdf(x, theta) punifed(q, theta) qunifed(p, theta) runifed(n, theta)
dunifed(x, theta) unifed.lcdf(x, theta) punifed(q, theta) qunifed(p, theta) runifed(n, theta)
x |
A vector of quantiles. They must be numbers between 0 and 1. |
theta |
The value of the canonical parameter. It must be of length one. |
q |
A vector of quantiles. |
p |
A vector of probabilities. |
n |
number of observations |
dunifed
gives the density function.
unifed.lcdf
returns the log of the cumulative
distribution function of the unifed.
punifed
gives the distribution function.
qunifed
gives the quantile function.
runifed
generates random observations.
Quijano Xacur, O.A. The unifed distribution. J Stat Distrib App 6, 13 (2019). doi:10.1186/s40488-019-0102-6.
dunifed( c(0.1,0.3,0.7), 10) x <- c(0.3,0.6,0.9) unifed.lcdf(x,5) x <- c(0.1,0.4,0.7,1) punifed(x,-5) p <- 1:9/10 qunifed(p,5) runifed(20,-3.3)
dunifed( c(0.1,0.3,0.7), 10) x <- c(0.3,0.6,0.9) unifed.lcdf(x,5) x <- c(0.1,0.4,0.7,1) punifed(x,-5) p <- 1:9/10 qunifed(p,5) runifed(20,-3.3)
Wrapper function for summary.glm.
summary_unifed_glm(object, ...)
summary_unifed_glm(object, ...)
object |
an object of class "glm". |
... |
Other arguments for This wrapper function was created in order to automatically set to 1 the dispersion parameter of a fitted unifed GLM. When the package is loaded the summary method of the glm class is rewritten using this function. |
Family object for the unifed distribution
unifed(link = "logit", ...) quasiunifed(link = "logit", ...) unifed.canonical.link()
unifed(link = "logit", ...) quasiunifed(link = "logit", ...) unifed.canonical.link()
link |
a specification for the model link function. This can
be a name/expression, a literal character string, a length-one
character vector or an object of class ‘"link-glm"’ (such as
generated by ‘make.link’) provided it is not specified via one
of the accepted names. The |
... |
Optional |
The link 'canonical' is not part of the standard names accepted by
make.link()
from the stats package. It corresponds to the
canonical link function for the unifed distribution, which is the
inverse of the derivative of its cumulant generator. There is no
explicit formula for it. The function
unifed.kappa.prime.inverse()
implements it using the
Newthon-Raphson method.
This function is used inside of unifed()
when the link
parameter is set to "canonical"
. It returns the link
function, inverse link function, the derivative dmu/deta and a
function for domain checking for the unifed distribution canonical
link.
unifed
returns a family object for using the unifed
distribution with the glm
function.
The quasiunifed
family differs from the
unifed
only in that the dispersion parameter is not
fixed to one.
An object of class "link-glm"
.
Jørgensen, Bent (1992). The Theory of Exponential Dispersion Models and Analysis of Deviance. Instituto de Matemática Pura e Aplicada, (IMPA), Brazil.
Wedderburn, R. W. M. (1974). Quasi-likelihood functions, generalized linear models, and the Gauss—Newton method. Biometrika. 61 (3): 439–447.
McCullagh, Peter; Nelder, John (1989). Generalized Linear Models (second ed.). London: Chapman and Hall.
Gamma
unifed.kappa.prime.inverse
make.link
Deviance of the unifed distribution
unifed.deviance(y.v, mu.v, wt = 1, ...) unifed.unit.deviance(y, mu, tol = 1e-07, maxit = 50)
unifed.deviance(y.v, mu.v, wt = 1, ...) unifed.unit.deviance(y, mu, tol = 1e-07, maxit = 50)
y.v |
A numeric vector with values between 0 and 1 |
mu.v |
A numeric vector with values between 0 and 1 |
wt |
(default value: 1) The weight vector. It contains the weight of each observation. It must contain positive integers only. |
... |
Additional parameters of |
y |
A vector with values between 0 and 1. |
mu |
A vector with values between 0 and 1. |
tol |
Tolerance level for the Newton-Raphson algorithm for computing the inverse of the derivative of the cumulant generator of the family. |
maxit |
Maximum number of iterations for the Newton-Raphson algorithm for computing the inverse of the derivative of the cumulant generator of the family. |
unifed.unit.deviance
uses the following expression
for the deviance of regular exponential dispersion families
is computed with the function
unifed.kappa.prime.inverse
from this package.
unifed.deviance
returns the deviance of a GLM with a
unifed response distribution. This is
Where is the unit deviance of the
unifed distribution between the i-th entry of
and
.
is the i-th entry of the weight
vector.
unifed.unit.deviance
is used to get the value
of .
unifed.unit.deviance
Cumulant generator of the unifed distribution
unifed.kappa(theta) unifed.kappa.prime(theta) unifed.kappa.double.prime(theta) unifed.kappa.prime.inverse(mu, ...) unifed.kappa.prime.inverse.one(mu, tol = 1e-07, maxit = 1e+07)
unifed.kappa(theta) unifed.kappa.prime(theta) unifed.kappa.double.prime(theta) unifed.kappa.prime.inverse(mu, ...) unifed.kappa.prime.inverse.one(mu, tol = 1e-07, maxit = 1e+07)
theta |
A numeric vector. |
mu |
A vector of numbers between 0 and 1 |
... |
Other parameters of |
tol |
Tolerance level. The algorithm stops if the proportional difference between the new and old value of an iteration is less or equal than this number. |
maxit |
Maximum number of iterations of the algorithm to look for convergence. |
The cumulant generator of the unifed distribution is defined as
unifed.kappa.prime.inverse.one
uses the
Newthon-Raphson method for finding the inverse of
unifed.kappa.prime
for a single value.
unifed.kappa
returns a vector that contains the
cumulant generator of the unifed distribution applied to each
element of theta.
unifed.kappa.prime
returns a vector that contains
the derivative of the cumulant generator of the unifed
distribution for each element of theta.
unifed.kappa.double.prime
returns a vector that
contains the second derivative of the cumulant generator of the
unifed distribution for each element of theta.
unifed.kappa.prime.inverse
returns a vector with
unifed.kappa.prime.inverse.one
evaluated at every entry
of mu
.
unifed.kappa.prime.inverse.one
if the tolerance
level is reached within maxit
iterations, the function
returns the value of the last iteration. Otherwise it returns
NA
.
Quijano Xacur, O.A. The unifed distribution. J Stat Distrib App 6, 13 (2019). doi:10.1186/s40488-019-0102-6.
Jørgensen, Bent (1997). The Theory of Dispersion Models. Chapman & Hall, London.
unifed.kappa(1) unifed.kappa(-5:5) unifed.kappa.prime(4.5) unifed.kappa.double.prime(0) unifed.kappa.prime.inverse(0.5) unifed.kappa.prime.inverse(c(0.1,0.7,0.9))
unifed.kappa(1) unifed.kappa(-5:5) unifed.kappa.prime(4.5) unifed.kappa.double.prime(0) unifed.kappa.prime.inverse(0.5) unifed.kappa.prime.inverse(c(0.1,0.7,0.9))
Maximum Likelihood Estimate for the unifed distribution
unifed.mle(x)
unifed.mle(x)
x |
A numeric vector with values in the interval [0,1]. Computes the maximum likelihood estimator of the canonical
parameter of the unifed distribution. It is assumed that the
elements of |
a.unifed.sample <- runifed(1000,10) theta.mle <- unifed.mle(a.unifed.sample)
a.unifed.sample <- runifed(1000,10) theta.mle <- unifed.mle(a.unifed.sample)
Stan functions for working with the unifed distribution
A script with stan functions of the unifed is provided. The script
can be included in stan code. The full path to the script can be
obtained with the function unifed.stan.path
. The
following list are the names of functions that take one real value:
real unifed_kappa(real theta)
Computes the cumulant generator of the unifed distribution.
real unifed_kappa_prime(real theta)
Computes the first derivative of the cumulant generator.
real unifed_kappa_double_prime(real theta)
Computes the second derivative of the cumulant generator.
real unifed_lpdf(real x,real theta)
Computes the
logarithm of the probability density function of a unifed
distribution. theta
is the value of the canonical
parameter of the unifed and x
if the value where the
density is evaluated.
real unifed_quantile(real p,real theta)
Returns the
p-th quantile of a unifed distribution with canonical parameter
theta
.
real unifed_rng(real theta)
Returns a simulated value
of a unifed distribution with canonical parameter
theta
.
real unifed_lcdf(real x,real theta)
Computes the
logarithm of the cumulative density function of a unifed
distribution. theta
is the value of the canonical
parameter of the unifed and x
if the value where the
density is evaluated.
real unifed_kappa_prime_inverse(real mu)
Returns the inverse of the derivative of the unifed cumulant generator
real unifed_unit_deviance(real y,real mu)
Unit deviance function of the unifed.
The following functions take vectors as arguments
vector unifed_kappa_v(vector theta)
Vectorized
version of unifed_kappa
.
vector unifed_kappa_prime_inverse_v(vector
mu)
Vectorized version of unifed_kappa_prime_inverse
.
void unifed_glm_lp(vector y, vector theta, vector
weights)
Adds to the Log Probability Accumulator the
logarithm of the likelihood function of a GLM with observed
response y
, estimated canonical parameter theta
and weights weights
.
The unifed.stan provided by the file contains functions for using the unifed distribution in stan. The file can be included (with #include) insided the functions block of a stan program or its contents can be copied and pasted.
unifed.stan.path() unifed.stan.folder()
unifed.stan.path() unifed.stan.folder()
The full path to the unifed.stan file provided by the package.
unifed.stan.folder
returns a string containing the
path to the folder containing the unifed.stan file. This can be
used as the isystem
parameter in stan functions.
Variance function of the unifed distribution
unifed.varf(mu)
unifed.varf(mu)
mu |
A vector with numbers between 0 and 1. |
It returns
unifed.kappa.double.prime(unifed.kappa.prime.inverse(mu))
.