deltamethod: Compute the Variance-Covariance Matrix of Functions using the...

Description Usage Arguments Value Author(s) Examples

View source: R/deltamethod.R

Description

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

Usage

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

Arguments

fn

A function in character strings or a vector of functions.

Covvars

Variance-covariance matrix of the variables.

vars

A vector of characters of the random variables.

Var.name

Name of the variances.

Cov.name

Name of the covariances.

simplify

Attempts to simplify the expression. Please note that it may not work well.

Value

Variance-covariance matrix of the functions.

Author(s)

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

Examples

 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
72
73
74
75
76
77
78
79
80
81
82
83
84
#### 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)"

symSEM documentation built on July 16, 2020, 9:06 a.m.