Learn R Programming

symSEM (version 0.4)

deltamethod: Compute the Variance-Covariance Matrix of Functions using the first-order Delta Method

Description

It computes the variance-covariance matrix of functions using the first-order delta method.

Usage

deltamethod(fn, Covvars, vars, Var.name = "V", Cov.name = "C", simplify = TRUE)

Value

Variance-covariance matrix of the functions.

Arguments

fn

A function in character strings or a vector of functions.

Covvars

Variance-covariance matrix of the variables. Users must ensure the order of variables is the same as that in vars; Otherwise, the results are likely incorrect. If it is not specified, they are automatically generated.

vars

A vector of characters of the random variables. If the random variables are not listed in `vars`, they are treated as constants. If `vars` is missing, all names in `RAM` are treated as random variables.

Var.name

Name of the variances.

Cov.name

Name of the covariances.

simplify

Attempt to simplify the output.

Author

Mike W.-L. Cheung <mikewlcheung@nus.edu.sg>

Examples

Run this code
if (FALSE) {

#### Fisher-z-transformation
fn  <- "0.5*log((1+r)/(1-r))"

## Sampling variance of r
Covvars <- "(1-r^2)^2/n"

deltamethod(fn=fn, Covvars=Covvars, vars="r")
## $fn
##     [,1]
## fn1 "0.5*log((r+1)/(1-r))"

## $Covfn
##     fn1
## fn1 "1/n"

## $vars
## [1] "r"

## $Covvars
##   r
## r "(1-r^2)^2/n"

## $Jmatrix
##     r
## fn1 "(0.5*(1-r+r+1)*(1-r))/((1-r)^2*(r+1))"

#### Raw mean difference: y_treatment - y_control
fn <- "yt - yc"

## Sampling covariance matrix
## S2p: pooled variance
## nt: n_treatment
## nc: n_control
Covvars <- matrix(c("S2p/nt", 0,
                    0, "S2p/nc"),
                  ncol=2, nrow=2)

deltamethod(fn=fn, Covvars=Covvars, vars=c("yt", "yc"))
## $fn
##     [,1]
## fn1 "yt-yc"

## $Covfn
##     fn1
## fn1 "(S2p*nt+S2p*nc)/(nt*nc)"

## $vars
## [1] "yt" "yc"

## $Covvars
##    yt       yc
## yt "S2p/nt" "0"
## yc "0"      "S2p/nc"

## $Jmatrix
##     yt  yc
## fn1 "1" "-1"

#### log(odds)
fn <- "log(p/(1-p))"

## Sampling variance of p
Covvars <- "p*(1-p)/n"

## Though it is correct, the simplification does not work well.
deltamethod(fn=fn, Covvars=Covvars, vars="p")
## $fn
##     [,1]
## fn1 "log(p/(1-p))"

## $Covfn
##     fn1
## fn1 "(3*p^2-p^3-3*p+1)/((p^4-4*p^3+6*p^2-4*p+1)*p*n)"

## $vars
## [1] "p"

## $Covvars
##   p
## p "(p*(1-p))/n"

## $Jmatrix
##     p
## fn1 "((1-p+p)*(1-p))/((1-p)^2*p)"
}

Run the code above in your browser using DataLab