# NAG FL Interfaceg02gkf (glm_​constrain)

## 1Purpose

g02gkf calculates the estimates of the parameters of a generalized linear model for given constraints from the singular value decomposition results.

## 2Specification

Fortran Interface
 Subroutine g02gkf ( ip, v, ldv, c, ldc, b, s, se, cov, wk,
 Integer, Intent (In) :: ip, iconst, ldv, ldc Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: v(ldv,ip+7), c(ldc,iconst), s Real (Kind=nag_wp), Intent (Inout) :: b(ip) Real (Kind=nag_wp), Intent (Out) :: se(ip), cov(ip*(ip+1)/2), wk(2*ip*ip+ip*iconst+2*iconst*iconst+4*iconst)
#include <nag.h>
 void g02gkf_ (const Integer *ip, const Integer *iconst, const double v[], const Integer *ldv, const double c[], const Integer *ldc, double b[], const double *s, double se[], double cov[], double wk[], Integer *ifail)
The routine may be called by the names g02gkf or nagf_correg_glm_constrain.

## 3Description

g02gkf computes the estimates given a set of linear constraints for a generalized linear model which is not of full rank. It is intended for use after a call to g02gaf, g02gbf, g02gcf or g02gdf.
In the case of a model not of full rank the routines use a singular value decomposition to find the parameter estimates, ${\stackrel{^}{\beta }}_{\text{svd}}$, and their variance-covariance matrix. Details of the SVD are made available in the form of the matrix ${P}^{*}$:
 $P*= D-1 P1T P0T$
as described by g02gaf, g02gbf, g02gcf and g02gdf. Alternative solutions can be formed by imposing constraints on the parameters. If there are $p$ parameters and the rank of the model is $k$ then ${n}_{\mathrm{c}}=p-k$ constraints will have to be imposed to obtain a unique solution.
Let $C$ be a $p$ by ${n}_{\mathrm{c}}$ matrix of constraints, such that
 $CTβ=0,$
then the new parameter estimates ${\stackrel{^}{\beta }}_{\mathrm{c}}$ are given by:
 $β^c =Aβ^svd =I-P0CTP0-1β^svd, where ​I​ is the identity matrix,$
and the variance-covariance matrix is given by
 $AP1D-2 P1T AT$
provided ${\left({C}^{\mathrm{T}}{P}_{0}\right)}^{-1}$ exists.
Golub G H and Van Loan C F (1996) Matrix Computations (3rd Edition) Johns Hopkins University Press, Baltimore
McCullagh P and Nelder J A (1983) Generalized Linear Models Chapman and Hall
Searle S R (1971) Linear Models Wiley

## 5Arguments

1: $\mathbf{ip}$Integer Input
On entry: $p$, the number of terms in the linear model.
Constraint: ${\mathbf{ip}}\ge 1$.
2: $\mathbf{iconst}$Integer Input
On entry: the number of constraints to be imposed on the parameters, ${n}_{\mathrm{c}}$.
Constraint: $0<{\mathbf{iconst}}<{\mathbf{ip}}$.
3: $\mathbf{v}\left({\mathbf{ldv}},{\mathbf{ip}}+7\right)$Real (Kind=nag_wp) array Input
On entry: the array v as returned by g02gaf, g02gbf, g02gcf or g02gdf.
4: $\mathbf{ldv}$Integer Input
On entry: the first dimension of the array v as declared in the (sub)program from which g02gkf is called.
Constraint: ${\mathbf{ldv}}\ge {\mathbf{ip}}$.
ldv should be as supplied to g02gaf, g02gbf, g02gcf or g02gdf
5: $\mathbf{c}\left({\mathbf{ldc}},{\mathbf{iconst}}\right)$Real (Kind=nag_wp) array Input
On entry: contains the iconst constraints stored by column, i.e., the $i$th constraint is stored in the $i$th column of c.
6: $\mathbf{ldc}$Integer Input
On entry: the first dimension of the array c as declared in the (sub)program from which g02gkf is called.
Constraint: ${\mathbf{ldc}}\ge {\mathbf{ip}}$.
7: $\mathbf{b}\left({\mathbf{ip}}\right)$Real (Kind=nag_wp) array Input/Output
On entry: the parameter estimates computed by using the singular value decomposition, ${\stackrel{^}{\beta }}_{\text{svd}}$.
On exit: the parameter estimates of the parameters with the constraints imposed, ${\stackrel{^}{\beta }}_{\mathrm{c}}$.
8: $\mathbf{s}$Real (Kind=nag_wp) Input
On entry: the estimate of the scale parameter.
For results from g02gaf and g02gdf then s is the scale parameter for the model.
For results from g02gbf and g02gcf then s should be set to $1.0$.
Constraint: ${\mathbf{s}}>0.0$.
9: $\mathbf{se}\left({\mathbf{ip}}\right)$Real (Kind=nag_wp) array Output
On exit: the standard error of the parameter estimates in b.
10: $\mathbf{cov}\left({\mathbf{ip}}×\left({\mathbf{ip}}+1\right)/2\right)$Real (Kind=nag_wp) array Output
On exit: the upper triangular part of the variance-covariance matrix of the ip parameter estimates given in b. They are stored packed by column, i.e., the covariance between the parameter estimate given in ${\mathbf{b}}\left(i\right)$ and the parameter estimate given in ${\mathbf{b}}\left(j\right)$, $j\ge i$, is stored in ${\mathbf{cov}}\left(\left(j×\left(j-1\right)/2+i\right)\right)$.
11: $\mathbf{wk}\left(2×{\mathbf{ip}}×{\mathbf{ip}}+{\mathbf{ip}}×{\mathbf{iconst}}+2×{\mathbf{iconst}}×{\mathbf{iconst}}+4×{\mathbf{iconst}}\right)$Real (Kind=nag_wp) array Workspace
Note:  a simple upper bound for the size of the workspace is $5×{\mathbf{ip}}×{\mathbf{ip}}+4×{\mathbf{ip}}$.
12: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{iconst}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{iconst}}>0$.
On entry, ${\mathbf{iconst}}=〈\mathit{\text{value}}〉$ and ${\mathbf{ip}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{iconst}}<{\mathbf{ip}}$.
On entry, ${\mathbf{ip}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ip}}\ge 1$.
On entry, ${\mathbf{ldc}}=〈\mathit{\text{value}}〉$ and ${\mathbf{ip}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldc}}\ge {\mathbf{ip}}$.
On entry, ${\mathbf{ldv}}=〈\mathit{\text{value}}〉$ and ${\mathbf{ip}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldv}}\ge {\mathbf{ip}}$.
On entry, ${\mathbf{s}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{s}}>0.0$.
${\mathbf{ifail}}=2$
c does not give a model of full rank.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

It should be noted that due to rounding errors an argument that should be zero when the constraints have been imposed may be returned as a value of order machine precision.

## 8Parallelism and Performance

g02gkf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g02gkf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

g02gkf is intended for use in situations in which dummy ($0–1$) variables have been used such as in the analysis of designed experiments when you do not wish to change the parameters of the model to give a full rank model. The routine is not intended for situations in which the relationships between the independent variables are only approximate.

## 10Example

A loglinear model is fitted to a $3$ by $5$ contingency table by g02gcf. The model consists of terms for rows and columns. The table is
 $141 67 114 79 39 131 66 143 72 35 36 14 38 28 16 .$
The constraints that the sum of row effects and the sum of column effects are zero are then read in and the parameter estimates with these constraints imposed are computed by g02gkf and printed.

### 10.1Program Text

Program Text (g02gkfe.f90)

### 10.2Program Data

Program Data (g02gkfe.d)

### 10.3Program Results

Program Results (g02gkfe.r)