Learn R Programming

quasar (version 0.1.0)

simulateData: Simulate data

Description

Simulates a main covariate X, a vector of additional covariates Z, and a response y drawn from the chosen distribution.

Usage

simulateData(n, beta = 0, gamma = 0, mu = 0, Sigma = NULL,
             sigma.y = 1, distribution = "normal", df = 5, seed = NULL)

Value

A data.frame with columns y, X, and Z1, ..., Zk.

Arguments

n

Integer. Number of observations.

beta

Numeric scalar. Effect of X.

gamma

Numeric vector. Effects of Z (length p - 1, where p = ncol(Sigma)).

mu

Numeric scalar. Intercept.

Sigma

Numeric p x p symmetric positive-definite covariance matrix for (X, Z). The first column corresponds to X, the remaining columns to Z1, Z2, ....

sigma.y

Either a numeric scalar or a one-sided expression/string (e.g., "0.3 * abs(X) + 0.1") defining the scale of y.

distribution

Character. One of "normal", "t", or "exponential". This is the distribution of y.

df

Numeric scalar > 0. Degrees of freedom for t-distribution.

seed

Numeric scalar > 0. Seed for random number generator.

Author

Angela Andreella

Details

The response is generated as y = mu + beta * X + Z %*% gamma + error. The error term can be drawn from a normal distribution, scaled Student-t with df degrees of freedom, or a shifted exponential. Its standard deviation is defined by sigma.y: if numeric, a fixed scale is used; if a character expression, the scale can vary with X and/or Z.

Examples

Run this code
set.seed(1)
p <- 3
Sigma <- diag(p)

# Normal
dat_n <- simulateData(n = 200, beta = 0.5, gamma = c(0.2,-0.1),
                      sigma.y = 0.5, distribution = "normal")

# Student-t
dat_t0 <- simulateData(n = 200, beta = 0.5, gamma = c(0.2,-0.1),
                       sigma.y = 0.5, distribution = "t", df = 7)
# Exponential
dat_e <- simulateData(n = 200, beta = 0.5, gamma = c(0.2,-0.1),
                      sigma.y = "0.3 * abs(X) + 0.1", distribution = "exponential")


Run the code above in your browser using DataLab