Description Usage Arguments Value Examples

Add correlated data to existing data.table

1 2 | ```
addCorData(dtOld, idname, mu, sigma, corMatrix = NULL, rho,
corstr = "ind", cnames = NULL)
``` |

`dtOld` |
Data table that is the new columns will be appended to. |

`idname` |
Character name of id field, defaults to "id". |

`mu` |
A vector of means. The length of mu must be nvars. |

`sigma` |
Standard deviation of variables. If standard deviation differs for each variable, enter as a vector with the same length as the mean vector mu. If the standard deviation is constant across variables, as single value can be entered. |

`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. |

`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. |

`cnames` |
Explicit column names. A single string with names separated by commas. If no string is provided, the default names will be V#, where # represents the column. |

The original data table with the additional correlated columns

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | ```
def <- defData(varname="xUni", dist="uniform", formula="10;20", id = "myID")
def <- defData(def, varname="xNorm", formula="xUni * 2", dist="normal", variance=8)
dt <- genData(250, def)
mu <- c(3, 8, 15)
sigma <- c(1, 2, 3)
dtAdd <- addCorData(dt, "myID", mu = mu, sigma = sigma, rho = .7, corstr = "cs")
dtAdd
round(var(dtAdd[,.(V1, V2, V3)]), 3)
round(cor(dtAdd[,.(V1, V2, V3)]), 2)
dtAdd <- addCorData(dt, "myID", mu = mu, sigma = sigma, rho = .7, corstr = "ar1")
round(cor(dtAdd[,.(V1, V2, V3)]), 2)
corMat <- matrix(c(1, .2, .8, .2, 1, .6, .8, .6, 1), nrow = 3)
dtAdd <- addCorData(dt, "myID", mu = mu, sigma = sigma, corMatrix = corMat)
round(cor(dtAdd[,.(V1, V2, V3)]), 2)
``` |

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

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.