简体   繁体   English

如何从 R 中的随机多变量样本创建多个模拟数据集

[英]How to create multiple simulated datasets from a random multivariate sample in R

I have created a randomised, multivariate dataset similar to the below:我创建了一个类似于以下内容的随机化多变量数据集:

library(JWileymisc)
librarY(MASS)
library(dplyr)

V <- matrix(c(1,0.2,0.7,
            0.2,1,0.5,
            0.7,0.5,1)
           ,3,3)
sigma <- c(60,30,45)
mu <- c(25,10,15)

Sigma <-cor2cov(V,sigma)
data <-data.frame(mvrnorm(n=5,mu,Sigma,3,3))

data <- rename(data,outcome=X1,time=X2,exposure=X3)
data$exposure <- if_else(data$exposure>15,2,1)

I'm then wanting to use this randomised dataset to create many multiple simulated datasets.然后我想使用这个随机数据集来创建许多多个模拟数据集。 Is there an easy way to do this using a loop?有没有一种简单的方法可以使用循环来做到这一点? I've so far tried something of the following:到目前为止,我已经尝试了以下内容:

NSIM <- 10  #Number of data sets to simulate
set.seed(3465)
simulated_data <- rep(0, NSIM)

for (m in 1:NSIM) {
  simulated_data[m] <- data.frame(mvrnorm(n=5,mu,Sigma,3,3))
}

However, this doesn't really give me what I'm looking for and struggling to perform the rename/if_else components from the above.但是,这并没有真正给我我正在寻找和努力执行上面的重命名/if_else 组件的东西。 Any help would be most appreciated!非常感激任何的帮助!

Here I am using purrr::map_dfr to bind all the dataframes produced by the simulation (which are still identifiable and splittable by SN ).在这里,我使用purrr::map_dfr来绑定模拟产生的所有数据帧(它们仍然可以被SN识别和分割)。 You can perform the common operations such as rename and mutate(exposure, ...) on the merged dataframe. Eventually, you can split them using eg group_by(SN) followed by group_split()您可以对合并后的 dataframe 执行renamemutate(exposure, ...)等常见操作。最终,您可以使用group_by(SN)group_split()拆分它们

library(purrr)
library(dplyr)
NSIM <- 10  #Number of data sets to simulate
set.seed(3465)

map_dfr(1:NSIM,
        ~data.frame(mvrnorm(n=5,mu,Sigma,3,3)),
        .id = "SN") |>
  rename(outcome=X1,time=X2,exposure=X3) |>
  mutate(exposure = if_else(exposure>15,2,1)) |>
  sample_n(10)

##> +    SN     outcome        time exposure
##> 1   2 -35.3326059   3.2585097        1
##> 2   9  25.7304365   7.7347147        2
##> 3   7  68.3215424  -1.9466048        1
##> 4   2  77.4440558  61.0617621        2
##> 5   6 -46.3029760 -11.2115067        1
##> 6   9  92.7289232  52.0273595        2
##> 7  10   0.3859393  53.2966179        2
##> 8  10 -17.2009480 -29.6117604        1
##> 9   5  91.5425904  48.2142412        2
##> 10  3  73.6991481   0.8617149        2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM