MKdescr (version 0.5)

simCorVars: Simulate correlated variables.

Description

The function simulates a pair of correlated variables.

Usage

simCorVars(n, r, mu1 = 0, mu2 = 0, sd1 = 1, sd2 = 1, plot = TRUE)

Arguments

n

integer: sample size.

r

numeric: correlation.

mu1

numeric: mean of first variable.

mu2

numeric: mean of second variable.

sd1

numeric: SD of first variable.

sd2

numeric: SD of second variable.

plot

logical: generate scatter plot of the variables.

Value

data.frame with entries Var1 and Var2

Details

The function is mainly for teaching purposes and simulates n observations from a pair of normal distributed variables with correlation r.

By specifying plot = TRUE a scatter plot of the data is generated.

Examples

Run this code
# NOT RUN {
set.seed(123)
res <- simCorVars(n = 100, r = 0.8)
cor(res$Var1, res$Var2)
colMeans(res)
apply(res, 2, sd)
set.seed(123)
res <- simCorVars(n = 100, r = 0.8, mu1 = -1, mu2 = 1, sd1 = 2, sd2 = 0.5)
cor(res$Var1, res$Var2)
colMeans(res)
apply(res, 2, sd)
# }

Run the code above in your browser using DataCamp Workspace