【问题标题】:Generate data with simsem package in R在 R 中使用 simsem 包生成数据
【发布时间】:2021-01-07 22:19:00
【问题描述】:

我想用 R 中的simsem 包生成数据。到目前为止,我已经做到了:

    # 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)

我希望为从 1 到 5 的 7 个项目创建项目响应。然而,我创建的数据如下所示:

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

如何创建范围从 1 到 5 的数据?我希望结果如下:

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

【问题讨论】:

    标签: r simulation


    【解决方案1】:

    您可以在 base R 中使用replicate 函数执行此操作,以随机生成 1 到 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
    

    【讨论】:

    • 谢谢,但这不是我想要做的。我想模拟 SEM 的数据。
    【解决方案2】:

    我通过指定指标手段解决了:

    ### 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-03
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 2021-10-27
      • 1970-01-01
      • 2018-06-14
      相关资源
      最近更新 更多