Generate correlated data (predictors) for one unit
genX.RdThis is used to generate data for one unit. It is recently re-designed to serve as a building block in a multi-level data simulation exercise. The new arguments "unit" and "idx" can be set as NULL to remove the multi-level unit and row naming features. This function uses the rockchalk::mvrnorm function, but introduces a convenience layer by allowing users to supply standard deviations and the correlation matrix rather than the variance.
Usage
genX(
N,
means,
sds,
rho,
Sigma = NULL,
intercept = TRUE,
col.names = NULL,
unit = NULL,
idx = FALSE
)Arguments
- N
Number of cases desired
- means
A vector of means for p variables. It is optional to name them. This implicitly sets the dimension of the predictor matrix as N x p. If no names are supplied, the automatic variable names will be "x1", "x2", and so forth. If means is named, such as c("myx1" = 7, "myx2" = 13, "myx3" = 44), those names will be come column names in the output matrix.
- sds
Standard deviations for the variables. If less than p values are supplied, they will be recycled.
- rho
Correlation coefficient for p variables. Several input formats are allowed (see
lazyCor). This can be a single number (common correlation among all variables), a full matrix of correlations among all variables, or a vector that is interpreted as the strictly lower triangle (a vech).- Sigma
P x P variance/covariance matrix.
- intercept
Default = TRUE, do you want a first column filled with 1?
- col.names
Names supplied here will override column names supplied with the means parameter. If no names are supplied with means, or here, we will name variables x1, x2, x3, ... xp, with Intercept at front of list if intercept = TRUE.
- unit
A character string for the name of the unit being simulated. Might be referred to as a "group" or "district" or "level 2" membership indicator.
- idx
If set TRUE, a column "idx" is added, numbering the rows from 1:N. If the argument unit is not NULL, then idx is set to TRUE, but that behavior can be overridded by setting idx = FALSE.
Value
A data frame with rownames to specify unit and individual values, including an attribute "unit" with the unit's name.
Details
Today I've decided to make the return object a data frame. This allows the possibility of including a character variable "unit" within the result. For multi-level models, that will help. If unit is not NULL, its value will be added as a column in the data frame. If unit is not null, the rownames will be constructed by pasting "unit" name and idx. If unit is not null, then idx will be included as another column, unless the user explicitly sets idx = FALSE.
Author
Paul Johnson [email protected]
Examples
X1 <- genX(10, means = c(7, 8), sds = 3, rho = .4)
X2 <- genX(10, means = c(7, 8), sds = 3, rho = .4, unit = "Kansas")
head(X2)
#> Intercept x1 x2 unit idx
#> Kansas_1 1 8.363843 5.069978 Kansas 1
#> Kansas_2 1 6.357625 3.976847 Kansas 2
#> Kansas_3 1 12.231521 9.340743 Kansas 3
#> Kansas_4 1 7.946069 9.880878 Kansas 4
#> Kansas_5 1 6.415264 9.911203 Kansas 5
#> Kansas_6 1 13.442421 11.244921 Kansas 6
X3 <- genX(10, means = c(7, 8), sds = 3, rho = .4, idx = FALSE, unit = "Iowa")
head(X3)
#> Intercept x1 x2 unit
#> Iowa_1 1 5.743964 7.661084 Iowa
#> Iowa_2 1 10.114761 10.326009 Iowa
#> Iowa_3 1 10.683645 9.841685 Iowa
#> Iowa_4 1 7.130520 8.106981 Iowa
#> Iowa_5 1 11.951717 4.894296 Iowa
#> Iowa_6 1 6.420875 13.471472 Iowa
X4 <- genX(10, means = c("A" = 7, "B" = 8), sds = c(3), rho = .4)
head(X4)
#> Intercept A B
#> 1 1 3.371200 4.322218
#> 2 1 6.788489 7.212775
#> 3 1 6.159042 10.813920
#> 4 1 6.249175 9.294637
#> 5 1 7.615681 6.959643
#> 6 1 7.021265 11.678233
X5 <- genX(10, means = c(7, 3, 7, 5), sds = c(3, 6),
rho = .5, col.names = c("Fred", "Sally", "Henry", "Barbi"))
head(X5)
#> Intercept Fred Sally Henry Barbi
#> 1 1 4.547838 -13.897762 6.817265 5.124141
#> 2 1 7.854951 13.646907 11.319716 11.103143
#> 3 1 6.190385 -1.591966 11.197730 15.359323
#> 4 1 10.670769 2.555322 9.836907 15.937259
#> 5 1 7.148640 12.724103 3.548295 9.146200
#> 6 1 6.007354 -6.143121 1.901871 5.527257
Sigma <- lazyCov(Rho = c(.2, .3, .4, .5, .2, .1), Sd = c(2, 3, 1, 4))
X6 <- genX(10, means = c(5, 2, -19, 33), Sigma = Sigma, unit = "Winslow_AZ")
head(X6)
#> Intercept x1 x2 x3 x4 unit idx
#> Winslow_AZ_1 1 6.009099 -3.7883015 -20.08131 36.14506 Winslow_AZ 1
#> Winslow_AZ_2 1 7.828074 7.0137443 -18.59109 30.12962 Winslow_AZ 2
#> Winslow_AZ_3 1 4.882271 4.9094999 -17.60511 35.06686 Winslow_AZ 3
#> Winslow_AZ_4 1 1.866331 0.4352758 -19.69176 28.84299 Winslow_AZ 4
#> Winslow_AZ_5 1 6.384119 5.2589863 -19.64411 36.70338 Winslow_AZ 5
#> Winslow_AZ_6 1 2.719711 -1.1128479 -20.10054 32.71106 Winslow_AZ 6