makeFam: makeFam - a function to transform multivariate normal data to...

View source: R/dataMakers.R

makeFamR Documentation

makeFam - a function to transform multivariate normal data to that with a gamlss family distribution, following "http://www.econometricsbysimulation.com/2014/02/easily-generate-correlated-variables.html"

Description

makeFam - a function to transform multivariate normal data to that with a gamlss family distribution, following "http://www.econometricsbysimulation.com/2014/02/easily-generate-correlated-variables.html"

Usage

makeFam(Y, parms, family = "NO")

Arguments

Y

Multivariate normal data such as that simulated by rnorm, mvrnorm, or arima.sim.

parms

A named list of parameters for transforming normal random effects to some other distribution. These will be passed to a quantile function from gamlss.family, specifically mu, sigma, tau, and nu. If family doesn't use one of these parameters it will be ignored.

family

Default "qNO". A quoted gamlss.family quantile distribution.

Author(s)

Stephen Tueller stueller@rti.org

Examples

parms  <- list(mu=.5, sigma=.35, nu=.15, tau=.25)
design = polyICT$new(n=1000)
par(mfrow=c(2,2))

# beta distribution
YBEINF <- makeFam(design = design, parms = parms, family="qBEINF")

# use the same parameters for normal, ignoring nu and tau
YNO    <- makeFam(design = design, parms = parms, family="qNO")

# visualize
psych::pairs.panels(data.frame(YNO, YBEINF))

# quantile functions not in gamlss.family also work
norm <- makeFam(design, parms=list(mean=0, sd=20, lower.tail=FALSE), "qnorm")
#pois <- makeFam(design, parms=list(lambda=.3, lower.tail=FALSE), "qpois") # not working

# this example illustrates how makeFam works under the hood
## Not run: 
# requires library(psych), not required by PersonAlyticsPower
randFxCorMat <- matrix(.5, nrow=3, ncol=3) + diag(3)*.5
randFxCorMat[1,3] <- randFxCorMat[3,1] <- .25
randFxMean <- list(randFx=list(intercept=0, slope=.5, quad=.1),
                    fixdFx=list(phase=.5, phaseTime=.5, phaseTime2=.04))
randFxVar <- c(1,.2,.1)


design <- polyICT$new(n=10000, randFxCorMat=randFxCorMat, randFxMean=randFxMean,
                      randFxVar=randFxVar)

# first simulate multivariate normal data
Y <- mvrnorm( design$n, rep(0, design$randFxOrder + 1),  design$randFxCorMat)
psych::pairs.panels(Y)
all.equal(cor(Y), randFxCorMat, tolerance=.03)

# now get the propabilities
YpNorm <- data.frame(pnorm(Y))
psych::pairs.panels(YpNorm)
all.equal(unname(cor(YpNorm)), design$randFxCorMat, tolerance=.08)

# use the quantile functions on the probabilities to get non-normal data
# with approximately the same correlation matrix

# works well for symmetric distributions
YBEINF <-. doLapply(YpNorm, .fcn="qBEINF", mu=.5, sigma=.35)
psych::pairs.panels(YBEINF)
all.equal(unname(cor(YBEINF)), randFxCorMat, tolerance=.08)

# does not work as well for skewed distributions
YLOGNO <- .doLapply(YpNorm, "qLOGNO", mu=3, sigma=1)
psych::pairs.panels(YLOGNO)
all.equal(unname(cor(YLOGNO)), randFxCorMat, tolerance=.08)



## End(Not run)

ICTatRTI/PersonAlyticsPower documentation built on Dec. 13, 2024, 11:08 p.m.