genCorOrdCat: Generate correlated ordinal categorical data

Description Usage Arguments Value Examples

View source: R/genCorOrdCat.R

Description

Correlated ordinal categorical data is added to an existing data set.

Usage

1
2
genCorOrdCat(dtName, idname = "id", adjVar = NULL, baseprobs,
  prefix = "grp", rho, corstr, corMatrix = NULL)

Arguments

dtName

Name of complete data set

idname

A string. The id of the data.table that identifies a unique record. Defaults to "id".

adjVar

Vector of adjustment variables name in dtName - determines logistic shift. This is specified assuming a cumulative logit link. The vector can be NULL, of length 1, or a length equal to the number of new categorical variables.

baseprobs

A matrix of baseline probabilities. The row values must sum to 1. The number of rows represents the number of new categorical variables. The number of columns represents the number of possible responses - if an particular category has fewer possible responses, assign zero probability to non-relevant columns.

prefix

A string.The names of the new variables will be a concatenation of the prefix and a sequence of integers indicating the variable number.

rho

Correlation coefficient, -1 < rho < 1. Use if corMatrix is not provided.

corstr

Correlation structure of the variance-covariance matrix defined by sigma and rho. Options include "ind" for an independence structure, "cs" for a compound symmetry structure, and "ar1" for an autoregressive structure.

corMatrix

Correlation matrix can be entered directly. It must be symmetrical and positive semi-definite. It is not a required field; if a matrix is not provided, then a structure and correlation coefficient rho must be specified.

Value

Original data.table with added categorical fields

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#### Set definitions

baseprobs <- matrix(c(0.2, 0.1, 0.1, 0.6,
                      0.7, 0.2, 0.1, 0,
                      0.5, 0.2, 0.3, 0,
                      0.4, 0.2, 0.4, 0,
                      0.6, 0.2, 0.2, 0), 
                    nrow = 5, byrow = TRUE)
                    
set.seed(333)                     
dT <- genData(1000)

dX <- genCorOrdCat(dT, adjVar = NULL, baseprobs = baseprobs, 
                   prefix = "q", rho = .125, corstr = "cs")

dM <- melt(dX, id.vars = "id")
dProp <- dM[ , prop.table(table(value)), by = variable]
dProp[, response := c(1:4, 1:3, 1:3, 1:3, 1:3)]

dcast(dProp, variable ~ response, value.var = "V1", fill = 0)
                   

kgoldfeld/simstudy documentation built on Nov. 8, 2018, 7:41 p.m.