Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_univar_estim_weibull (g07be)

## Purpose

nag_univar_estim_weibull (g07be) computes maximum likelihood estimates for arguments of the Weibull distribution from data which may be right-censored.

## Syntax

[beta, gamma, sebeta, segam, corr, dev, nit, ifail] = g07be(cens, x, ic, gamma, tol, maxit, 'n', n)
[beta, gamma, sebeta, segam, corr, dev, nit, ifail] = nag_univar_estim_weibull(cens, x, ic, gamma, tol, maxit, 'n', n)

## Description

nag_univar_estim_weibull (g07be) computes maximum likelihood estimates of the arguments of the Weibull distribution from exact or right-censored data.
For $n$ realizations, ${y}_{i}$, from a Weibull distribution a value ${x}_{i}$ is observed such that
 $xi≤yi.$
There are two situations:
 (a) exactly specified observations, when ${x}_{i}={y}_{i}$ (b) right-censored observations, known by a lower bound, when ${x}_{i}<{y}_{i}$.
The probability density function of the Weibull distribution, and hence the contribution of an exactly specified observation to the likelihood, is given by:
 $fx;λ,γ=λγxγ-1exp-λxγ, x>0, for ​λ,γ>0;$
while the survival function of the Weibull distribution, and hence the contribution of a right-censored observation to the likelihood, is given by:
 $Sx;λ,γ=exp-λ xγ, x> 0, for ​ λ ,γ> 0.$
If $d$ of the $n$ observations are exactly specified and indicated by $i\in D$ and the remaining $\left(n-d\right)$ are right-censored, then the likelihood function, $\text{Like ​}\left(\lambda ,\gamma \right)$ is given by
 $Likeλ,γ∝λγd ∏i∈Dxiγ-1 exp-λ∑i=1nxiγ .$
To avoid possible numerical instability a different parameterisation $\beta ,\gamma$ is used, with $\beta =\mathrm{log}\left(\lambda \right)$. The kernel log-likelihood function, $L\left(\beta ,\gamma \right)$, is then:
 $Lβ,γ=dlogγ+dβ+γ-1∑i∈Dlogxi-eβ∑i=1nxiγ.$
If the derivatives $\frac{\partial L}{\partial \beta }$, $\frac{\partial L}{\partial \gamma }$, $\frac{{\partial }^{2}L}{{\partial \beta }^{2}}$, $\frac{{\partial }^{2}L}{\partial \beta \partial \gamma }$ and $\frac{{\partial }^{2}L}{{\partial \gamma }^{2}}$ are denoted by ${L}_{1}$, ${L}_{2}$, ${L}_{11}$, ${L}_{12}$ and ${L}_{22}$, respectively, then the maximum likelihood estimates, $\stackrel{^}{\beta }$ and $\stackrel{^}{\gamma }$, are the solution to the equations:
 $L1β^,γ^=0$ (1)
and
 $L2β^,γ^=0$ (2)
Estimates of the asymptotic standard errors of $\stackrel{^}{\beta }$ and $\stackrel{^}{\gamma }$ are given by:
 $seβ^=-L22 L11L22-L122 , seγ^=-L11 L11L22-L122 .$
An estimate of the correlation coefficient of $\stackrel{^}{\beta }$ and $\stackrel{^}{\gamma }$ is given by:
 $L12L12L22 .$
Note:  if an estimate of the original argument $\lambda$ is required, then
 $λ^=expβ^ and seλ^=λ^seβ^.$
The equations (1) and (2) are solved by the Newton–Raphson iterative method with adjustments made to ensure that $\stackrel{^}{\gamma }>0.0$.

## References

Gross A J and Clark V A (1975) Survival Distributions: Reliability Applications in the Biomedical Sciences Wiley
Kalbfleisch J D and Prentice R L (1980) The Statistical Analysis of Failure Time Data Wiley

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{cens}$ – string (length ≥ 1)
Indicates whether the data is censored or non-censored.
${\mathbf{cens}}=\text{'N'}$
Each observation is assumed to be exactly specified. ic is not referenced.
${\mathbf{cens}}=\text{'C'}$
Each observation is censored according to the value contained in ${\mathbf{ic}}\left(\mathit{i}\right)$, for $\mathit{i}=1,2,\dots ,n$.
Constraint: ${\mathbf{cens}}=\text{'N'}$ or $\text{'C'}$.
2:     $\mathrm{x}\left({\mathbf{n}}\right)$ – double array
${\mathbf{x}}\left(\mathit{i}\right)$ contains the $\mathit{i}$th observation, ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
Constraint: ${\mathbf{x}}\left(\mathit{i}\right)>0.0$, for $\mathit{i}=1,2,\dots ,n$.
3:     $\mathrm{ic}\left(:\right)$int64int32nag_int array
The dimension of the array ic must be at least ${\mathbf{n}}$ if ${\mathbf{cens}}=\text{'C'}$, and at least $1$ otherwise
If ${\mathbf{cens}}=\text{'C'}$, then ${\mathbf{ic}}\left(\mathit{i}\right)$ contains the censoring codes for the $\mathit{i}$th observation, for $\mathit{i}=1,2,\dots ,n$.
If ${\mathbf{ic}}\left(i\right)=0$, the $i$th observation is exactly specified.
If ${\mathbf{ic}}\left(i\right)=1$, the $i$th observation is right-censored.
If ${\mathbf{cens}}=\text{'N'}$, then ic is not referenced.
Constraint: if ${\mathbf{cens}}=\text{'C'}$, then ${\mathbf{ic}}\left(\mathit{i}\right)=0$ or $1$, for $\mathit{i}=1,2,\dots ,n$.
4:     $\mathrm{gamma}$ – double scalar
Indicates whether an initial estimate of $\gamma$ is provided.
If ${\mathbf{gamma}}>0.0$, it is taken as the initial estimate of $\gamma$ and an initial estimate of $\beta$ is calculated from this value of $\gamma$.
If ${\mathbf{gamma}}\le 0.0$, then initial estimates of $\gamma$ and $\beta$ are calculated, internally, providing the data contains at least two distinct exact observations. (If there are only two distinct exact observations, then the largest observation must not be exactly specified.) See Further Comments for further details.
5:     $\mathrm{tol}$ – double scalar
The relative precision required for the final estimates of $\beta$ and $\gamma$. Convergence is assumed when the absolute relative changes in the estimates of both $\beta$ and $\gamma$ are less than tol.
If ${\mathbf{tol}}=0.0$, then a relative precision of $0.000005$ is used.
Constraint:  or ${\mathbf{tol}}=0.0$.
6:     $\mathrm{maxit}$int64int32nag_int scalar
The maximum number of iterations allowed.
If ${\mathbf{maxit}}\le 0$, then a value of $25$ is used.

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the array x.
$n$, the number of observations.
Constraint: ${\mathbf{n}}\ge 1$.

### Output Parameters

1:     $\mathrm{beta}$ – double scalar
The maximum likelihood estimate, $\stackrel{^}{\beta }$, of $\beta$.
2:     $\mathrm{gamma}$ – double scalar
Contains the maximum likelihood estimate, $\stackrel{^}{\gamma }$, of $\gamma$.
3:     $\mathrm{sebeta}$ – double scalar
An estimate of the standard error of $\stackrel{^}{\beta }$.
4:     $\mathrm{segam}$ – double scalar
An estimate of the standard error of $\stackrel{^}{\gamma }$.
5:     $\mathrm{corr}$ – double scalar
An estimate of the correlation between $\stackrel{^}{\beta }$ and $\stackrel{^}{\gamma }$.
6:     $\mathrm{dev}$ – double scalar
The maximized kernel log-likelihood, $L\left(\stackrel{^}{\beta },\stackrel{^}{\gamma }\right)$.
7:     $\mathrm{nit}$int64int32nag_int scalar
The number of iterations performed.
8:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{cens}}\ne \text{'N'}$ or $\text{'C'}$, or ${\mathbf{n}}<1$, or ${\mathbf{tol}}<0.0$, or , or ${\mathbf{tol}}>1.0$.
${\mathbf{ifail}}=2$
 On entry, the $i$th observation, ${\mathbf{x}}\left(i\right)\le 0.0$, for some $i=1,2,\dots ,n$, or the $i$th censoring code, ${\mathbf{ic}}\left(i\right)\ne 0$ or $1$, for some $i=1,2,\dots ,n$ and ${\mathbf{cens}}=\text{'C'}$.
${\mathbf{ifail}}=3$
On entry, there are no exactly specified observations, or the function was requested to calculate initial values and there are either less than two distinct exactly specified observations or there are exactly two and the largest observation is one of the exact observations.
${\mathbf{ifail}}=4$
The method has failed to converge in maxit iterations. You should increase tol or maxit.
${\mathbf{ifail}}=5$
Process has diverged. The process is deemed divergent if three successive increments of $\beta$ or $\gamma$ increase or if the Hessian matrix of the Newton–Raphson process is singular. Either different initial estimates should be provided or the data should be checked to see if the Weibull distribution is appropriate.
${\mathbf{ifail}}=6$
A potential overflow has been detected. This is an unlikely exit usually caused by a large input estimate of $\gamma$.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

Given that the Weibull distribution is a suitable model for the data and that the initial values are reasonable the convergence to the required accuracy, indicated by tol, should be achieved.

The initial estimate of $\gamma$ is found by calculating a Kaplan–Meier estimate of the survival function, $\stackrel{^}{S}\left(x\right)$, and estimating the gradient of the plot of $\mathrm{log}\left(-\mathrm{log}\left(\stackrel{^}{S}\left(x\right)\right)\right)$ against $x$. This requires the Kaplan–Meier estimate to have at least two distinct points.
The initial estimate of $\stackrel{^}{\beta }$, given a value of $\stackrel{^}{\gamma }$, is calculated as
 $β^=logd∑i=1nxiγ^ .$

## Example

In a study, $20$ patients receiving an analgesic to relieve headache pain had the following recorded relief times (in hours):
(See Gross and Clark (1975).) This data is read in and a Weibull distribution fitted assuming no censoring; the parameter estimates and their standard errors are printed.
```function g07be_example

fprintf('g07be example results\n\n');

x = [1.1;     1.4;     1.3;     1.7;     1.9;
1.8;     1.6;     2.2;     1.7;     2.7;
4.1;     1.8;     1.5;     1.2;     1.4;
3.0;     1.7;     2.3;     1.6;     2];

% Control parameters and initial estimate for gamma
cens  = 'No censor';
ic    = [int64(0)];
gamma = 0;
tol   = 0;
maxit = int64(0);

% Calculate estimates
[beta, gamma, sebeta, segam, corr, dev, nit, ifail] = ...
g07be( ...
cens, x, ic, gamma, tol, maxit);

fprintf('  Beta  = %10.4f Standard error = %10.4f\n', beta, sebeta);
fprintf('  Gamma = %10.4f Standard error = %10.4f\n', gamma, segam);

```
```g07be example results

Beta  =    -2.1073 Standard error =     0.4627
Gamma =     2.7870 Standard error =     0.4273
```