Reputation: 67
I would like to generate data with the simsem package in R. So far, I've done this:
# create data with simsem package
## factor loadings
loading <- matrix(0, 7)
loading[1,1] <- NA
LY2 <- bind(loading, 1)
## residual correlation matrix
latent.cor <- matrix(NA, 1, 1)
diag(latent.cor) <- 1
RPS2 <- binds(latent.cor, 0.5)
# measurement error corr matrix
RTE2 <- binds(diag(7))
# total variance of indicators
VY2 <- bind(rep(NA,7),2)
CFA.Model2 <- model(LY = LY2, RPS = RPS2, RTE = RTE2, modelType = "CFA")
# generate data
dist <- bindDist("norm", list(mean = 4, sd = 1))
dat <- generate(CFA.Model2, n = 50, indDist = dist)
I was hoping to create item responses for 7 items that range from 1 to 5. The data that I create however looks like this:
head(dat, 4)
y1 y2 y3 y4 y5
1 -0.3042082 -0.009703124 0.070651822 1.9138537 -0.02754102
2 0.9574723 -1.691375825 0.441645186 -0.1770509 -0.35793280
3 -2.1808565 1.467395026 -0.350395973 -0.1660219 -0.42191898
4 -0.2367881 0.003594693 -0.002771362 -0.1323401 -1.44860960
y6 y7
1 -1.2557268 -1.52874138
2 -0.9963241 0.87807237
3 -1.7527848 0.31383091
4 1.2102580 0.03469505
How can I create data that ranges from 1 to 5? I would want the result to be the following:
head(dat, 4)
y1 y2 y3 y4 y5 y6 y7
1 3 5 3 5 5 5 5
2 5 5 5 5 5 3 5
3 3 4 1 2 2 2 4
4 5 4 1 2 4 5 4
Upvotes: 2
Views: 169
Reputation: 67
I solved it by specifying the indicator means:
### create data with simsem package
## factor loadings
loading <- matrix(NA, 7)
loading2 <- matrix("runif(1, 0, 0.5)", 7)
LY2 <- bind(loading, loading2)
## residual correlation matrix
latent.cor <- matrix(NA, 1, 1)
diag(latent.cor) <- "1"
RPS2 <- binds(latent.cor)
## measurement error corr matrix
RTE2 <- binds(diag(7))
## total variance of indicators
var <- matrix("runif(1, 0, 2)", 7)
VY2 <- bind(rep(NA,7), var)
## indicator means
indmeans <- matrix("runif(1, 2, 3)", 7)
MY <- bind(rep(NA,7), indmeans)
CFA.model <- model(MY = MY, LY = LY2, RPS = RPS2, RTE = RTE2, modelType = "CFA")
# Draw a parameter set for data generation.
param <- draw(CFA.model)
# create data
dat <- createData(param[[1]], n = 50)
b1 <- round(dat[,1], 0)
b2 <- round(dat[,2], 0)
b3 <- round(dat[,3], 0)
b4 <- round(dat[,4], 0)
b5 <- round(dat[,5], 0)
b6 <- round(dat[,6], 0)
b7 <- round(dat[,7], 0)
Upvotes: 1
Reputation: 389095
You can do this in base R using replicate
function to generate data randomly between 1 to 5.
n <- 10
replicate(7, sample(5, n, replace = TRUE))
# [,1] [,2] [,3] [,4] [,5] [,6] [,7]
# [1,] 5 3 3 3 1 4 3
# [2,] 5 3 4 5 2 5 1
# [3,] 1 3 1 2 1 5 1
# [4,] 4 5 3 1 5 3 3
# [5,] 4 4 1 3 2 3 2
# [6,] 3 1 2 4 5 5 4
# [7,] 1 4 1 2 5 4 4
# [8,] 5 5 3 5 4 4 1
# [9,] 2 1 3 1 1 5 4
#[10,] 1 5 4 2 5 1 5
Upvotes: 0