rdatagen: Simulate discrete data.

View source: R/rdatagen.R

rdatagenR Documentation

Simulate discrete data.

Description

Simulate discrete data from either a correlation matrix or thresholds or probabilities.

Usage

rdatagen(n = 100, R = diag(1,2), Q = NULL, th = NULL, probs = NULL)

Arguments

n

Number of observations.

R

Correlation matrix.

Q

Number of discrete values in the random variables. It is a single value or a vector. If Q is set to 1 (default), the function returns continuous data distributed according to the normal standardized distribution.

th

List of thresholds; each element contains Q+1 values.

probs

List of probabilities; each elements contains Q values.

Value

Returns a list with four elements:

data

The simulated data matrix.

R

Correlation matrix.

thresholds

The list of thresholds.

probs

The list of probabilities.

Note

Defaults work like in the mvrnorm function of the MASS package.

Author(s)

Massimiliano Pastore, Luigi Lombardi & Marco Bressan

References

Lombardi, L., Pastore, M. (2014). sgr: A Package for Simulating Conditional Fake Ordinal Data. The R Journal, 6, 164-177.

Pastore, M., Lombardi, L. (2014). The impact of faking on Cronbach's Alpha for dichotomous and ordered rating scores. Quality & Quantity, 48, 1191-1211.

Examples

require(MASS)
## only default
rdatagen()

## set correlations only
R <- matrix(c(1,.4,.4,1),2,2)
Dx <- rdatagen(n=10000,R=R)$data

par(mfrow=c(1,2))
for (j in 1:ncol(Dx)) hist(Dx[,j])

## set correlations and Q
Dx <- rdatagen(n=10000,R=R,Q=2)$data

par(mfrow=c(1,2))
for (j in 1:ncol(Dx)) barplot(table(Dx[,j])/nrow(Dx))

## set correlations and thresholds
th <- list(c(-Inf,qchisq(pbinom(0:3,4,.5),1),Inf),
    c(-Inf,qnorm(pbinom(0:2,3,.5)),Inf))
Dx <- rdatagen(n=10000,R=R,th=th)$data

par(mfrow=c(1,2))
for (j in 1:ncol(Dx)) barplot(table(Dx[,j])/nrow(Dx))

## set correlations and probabilities [1]
probs <- list(c(.125,.375,.375,.125),c(.125,.375,.375,.125))
Dx <- rdatagen(n=10000,R=R,probs=probs)$data

par(mfrow=c(1,2))
for (j in 1:ncol(Dx)) barplot(table(Dx[,j])/nrow(Dx))

## set correlations and probabilities [2]
probs <- c(.125,.375,.375,.125)
Dx <- rdatagen(n=10000,R=R,probs=probs)$data

par(mfrow=c(1,2))
for (j in 1:ncol(Dx)) barplot(table(Dx[,j])/nrow(Dx))

## set different values for Q
Dx <- rdatagen(n=1000,Q=c(2,4),R=R)$data

par(mfrow=c(1,2))
for (j in 1:ncol(Dx)) barplot(table(Dx[,j])/nrow(Dx))

sgr documentation built on April 14, 2022, 5:08 p.m.

Related to rdatagen in sgr...