makeFam | R 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"
makeFam(Y, parms, family = "NO")
Y |
Multivariate normal data such as that simulated by |
parms |
A named list of parameters for transforming normal random effects
to some other distribution. These will be passed to a quantile function from
|
family |
Default "qNO". A quoted |
Stephen Tueller stueller@rti.org
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.