Learn R Programming

corcounts (version 1.4)

rcounts: Generate correlated count random variables

Description

'rcounts' is used to sample high-dimensional correlated count random variables with approximate prespecified Pearson correlation and exact margins.

Usage

rcounts(N, margins, mu, phi=rep(NA,length(margins)), omega=rep(NA,length(margins)), psi=rep(NA,length(margins)), corstr, corpar, conv=0.01)

Arguments

N
number of observations to be generated per margin (should be at least 500).
margins
Vector of margin tokens. Its length T is the dimension. See details.
mu
Vector of length T of means for the Poisson, GP, ZIP, ZIGP and NB margins.
phi
Vector of length T of dispersion parameters for the GP, and ZIGP margins. For Poisson, ZIP and NB margins, an 'NA' can be provided.
omega
Vector of length T of zero-inflation parameters for the ZIP and ZIGP margins. For Poisson, GP and NB margins, an 'NA' can be provided.
psi
Vector of length T of size parameters for the NB margins. For Poisson, GP, ZIP and ZIGP margins, an 'NA' can be provided.
corstr
Correlation structure. Can be 'ex' for exchangeable, 'AR1' for AR(1) and 'unstr' for unstructured.
corpar
Correlation parameter. Scalar correlation for 'ex' and 'AR1' and matrix of dimension TxT for 'unstr'.
conv
Convergence criterion

Value

The function will return a matrix of counts of dimension N x T.

Details

The entries in 'margins' can be specified as 'Poi' for Poisson, 'GP' for generalized Poisson, 'ZIP' for zero-inflated Poisson, 'ZIGP' for zero-inflated generalized Poisson and 'NB' for negative-binomial.

Examples

Run this code
N <- 5000

# high precision in dimension 2
margins <- c("ZIGP","GP")
mu <-    c(10,  15)
phi <-   c(1.5, 3.5)
omega <- c(.25, NA)
corstr <- "ex"
corpar <- .5
Y <- rcounts(N=N, margins=margins, mu=mu, phi=phi, omega=omega, corstr=corstr, 
             corpar=corpar, conv=0.0001)
cor(Y)


# five-dimensional examples
margins <- c("ZIGP","GP","Poi","NB","ZIP")
mu <-    c(10,  25,  12, 20, 28)
phi <-   c(1.5, 2,   NA, NA, NA)
omega <- c(.25, NA,  NA, NA, .2)
psi <-   c(NA,  NA,  NA, 7,  NA)

# Exchangeable structure with correlation of 0.5
corstr <- "ex"
corpar <- .5
Y <- rcounts(N=N, margins=margins, mu=mu, phi=phi, omega=omega, psi=psi, 
             corstr=corstr, corpar=corpar)
cor(Y)

# AR(1) structure with correlation of corr(Y(t1), Y(t2)) = .8 ^ |t1-t2|
corstr <- "AR1"
corpar <- .8
Y <- rcounts(N=N, margins=margins, mu=mu, phi=phi, omega=omega, psi=psi, 
             corstr=corstr, corpar=corpar)
cor(Y)

# Unstructured correlation. Create random symmetric positive definite 
# matrix using function 'unstructured'
corstr <- "unstr"
corpar <- unstructured(5)
corpar
Y <- rcounts(N=N, margins=margins, mu=mu, phi=phi, omega=omega, psi=psi, 
             corstr=corstr, corpar=corpar)
cor(Y)

Run the code above in your browser using DataLab