getBiCop: Get bivariate random variables

Description Usage Arguments Details Value Note Examples

View source: R/pub01_utilityFuncs.R

Description

Compute a bivariate sample distribution drawn from a population with a given . It either computes two random variables, or it takes one existing variable (passed as parameter x) and creates a second variable with the desired correlation.

Usage

1
getBiCop(n, rho, mar.fun = rnorm, x = NULL, drop.x = FALSE, ...)

Arguments

n

a integer. number of observations

rho

a numeric. the desired correlation

mar.fun

the marginal distribution function

x

a vector. the existing variable.

drop.x

logical. If true, then return a vector of the generated variable.

...

other arguments to mar.fun

Details

Get from a forum post: http://stats.stackexchange.com/questions/15011/generate-a-random-variable-with-a-defined-correlation-to-an-existing-variable/15035#15035

Value

a data frame of two variables which correlate with a population correlation of rho, or a vector when drop.x is true.

Note

this is only an approximate solution, i.e., the empirical correlation is not exactly equal to rho.

Examples

1
2
3
4
5
6
7
# -- generate two random variables
df <- getBiCop(100,0.2)
cor(df)
# -- generate another variable with desired correlation on a existing variable
x <- rnorm(100)
df2 <- getBiCop(100,0.2,x=x)
cor(df2)

raphael210/QUtility documentation built on May 26, 2019, 11:05 p.m.