# Dvar: Differentiate the Variance Function of a Nonlinear Model In nlreg: Higher Order Inference for Nonlinear Heteroscedastic Models

## Description

Calculates the gradient and Hessian of the variance function of a nonlinear heteroscedastic model.

## Usage

 1 Dvar(nlregObj, hessian = TRUE) 

## Arguments

 nlregObj a nonlinear heteroscedastic model fit as obtained from a call to nlreg. hessian logical value indicating whether the Hessian should be computed. The default is TRUE.

## Details

The variance function is differentiated with respect to the variance parameters specified in the varPar component of the nlregObj object and, if the variance function depends on them, with respect to the regression coefficients specified in the coef component. The returned function definition includes all parameters. When evaluated, it implicitly refers to the data to whom the nlreg object was fitted and which must be on the search list. The gradient and Hessian are calculated for each data point: the gradient attribute is a \emph{n x p} matrix, and the hessian attribute is a \emph{n x p x p} array, where \emph{n} and \emph{p} are respectively the number of data points and the number of regression coefficients.

## Value

a function whose arguments are named according to the parameters of the nonlinear model nlregObj. When evaluated, it returns the value of the variance function along with attributes called gradient and hessian, the latter if requested. These are the gradient and Hessian of the variance function with respect to the model parameters.

## Note

Dmean and Dvar are the two workhorse functions of the nlreg library. The details are given in Brazzale (2000, Section 6.1.2).

The symbolic differentiation algorithm is based upon the D function. As this algorithm is highly recursive, the hessian = TRUE argument should only be used if the Hessian matrix is needed. Whenever possible, derivatives should be stored so as to be re-used in further calculations. This is, for instance, achieved for the nonlinear heteroscedastic model fitting routine nlreg through the argument hoa = TRUE.

## References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language: A Programming Environment for Data Analysis and Graphics. London: Chapman \& Hall. Section 9.6.

Brazzale, A. R. (2000) Practical Small-Sample Parametric Inference. Ph.D. Thesis N. 2230, Department of Mathematics, Swiss Federal Institute of Technology Lausanne.

Dmean, nlreg.object, deriv3, D
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 library(boot) data(calcium) calcium.nl <- nlreg( cal ~ b0*(1-exp(-b1*time)), start = c(b0 = 4, b1 = 0.1), data = calcium ) Dvar( calcium.nl ) ##function (b0, b1, logs) ##{ ## .expr1 <- exp(logs) ## .value <- .expr1 ## .grad <- array(0, c(length(.value), 1), list(NULL, c("logs"))) ## .hessian <- array(0, c(length(.value), 1, 1), list(NULL, ## c("logs"), c("logs"))) ## .grad[, "logs"] <- .expr1 ## .hessian[, "logs", "logs"] <- .expr1 ## attr(.value, "gradient") <- .grad ## attr(.value, "hessian") <- .hessian ## .value ##} ## attach( calcium ) calcium.vd <- Dvar( calcium.nl ) param( calcium.nl ) ## b0 b1 logs ## 4.3093653 0.2084780 -1.2856765 ## attr( calcium.vd( 4.31, 0.208, -1.29 ), "gradient" ) ## logs ##[1,] 0.2752708 ## calcium.nl <- update( calcium.nl, weights = ~ ( 1+time^g )^2, start = c(b0 = 4, b1 = 0.1, g = 1)) Dvar( calcium.nl ) ##function (b0, b1, g, logs) ##{ ## .expr1 <- time^g ## .expr2 <- 1 + .expr1 ## .expr4 <- exp(logs) ## .expr5 <- .expr2^2 * .expr4 ## .expr6 <- log(time) ## .expr7 <- .expr1 * .expr6 ## .expr10 <- 2 * (.expr7 * .expr2) * .expr4 ## .value <- .expr5 ## .grad <- array(0, c(length(.value), 2), list(NULL, c("g", ## "logs"))) ## .hessian <- array(0, c(length(.value), 2, 2), list(NULL, ## c("g", "logs"), c("g", "logs"))) ## .grad[, "g"] <- .expr10 ## .hessian[, "g", "g"] <- 2 * (.expr7 * .expr6 * .expr2 + .expr7 * ## .expr7) * .expr4 ## .hessian[, "g", "logs"] <- .hessian[, "logs", "g"] <- .expr10 ## .grad[, "logs"] <- .expr5 ## .hessian[, "logs", "logs"] <- .expr5 ## attr(.value, "gradient") <- .grad ## attr(.value, "hessian") <- .hessian ## .value ##} ## calcium.vd <- Dvar( calcium.nl ) param( calcium.nl ) ## b0 b1 g logs ## 4.3160408 0.2075937 0.3300134 -3.3447585 ## attr( calcium.vd(4.32, 0.208, 0.600, -2.66 ), "gradient" ) ## g logs ## [1,] -0.11203422 0.1834220 ## [2,] -0.11203422 0.1834220 ## [3,] -0.11203422 0.1834220 ## [4,] 0.09324687 0.3295266 ## \dots ## detach()