Home > other >  Generating 99 datasets at once in R
Generating 99 datasets at once in R

Time:05-22

#Generating 99 pairs of datasets
for (i in 1:99) {
  set.seed(i)
  X1 <- rnorm(100, mean=3, sd=sqrt(10))
  X2 <- rnorm(100, mean=-2, sd=sqrt(3))
  X1sq <- X1^2
  X2sq <- X2^2
  X1X2 <- X1*X2
  a <- exp(0.2*X1sq   0.3*X2sq   0.7*X1X2)
  b <- 1   exp(0.2*X1sq   0.3*X2sq   0.7*X1X2)
  px3 <- a/b
  Y <- rbinom(100, 1, px3)
  newtest <- data.frame(X1, X2, Y)
}

Hi guys, essentially I would like to generate 99 pairs of data sets, via the loop function. And each new test should be a data frame consisting of new sets of values of X1, X2 and Y respectively.I am not sure if my loop written above is correct so I would like to seek help if possible!

CodePudding user response:

You can add data frames as list elements to an empty list like this:

n = 99
dfs <- list()
for (i in 1:n) {
  set.seed(i)
  X1 <- rnorm(100, mean=3, sd=sqrt(10))
  X2 <- rnorm(100, mean=-2, sd=sqrt(3))
  X1sq <- X1^2
  X2sq <- X2^2
  X1X2 <- X1*X2
  a <- exp(0.2*X1sq   0.3*X2sq   0.7*X1X2)
  b <- 1   exp(0.2*X1sq   0.3*X2sq   0.7*X1X2)
  px3 <- a/b
  Y <- rbinom(100, 1, px3)
  newtest <- data.frame(X1, X2, Y)
  dfs[[i]] <- newtest
}

Output:

> length(dfs)
[1] 99
> dfs[[1]]
             X1          X2 Y
1    1.01897911 -3.07450660 1
2    3.58073118 -1.92705317 0
3    0.35751031 -3.57776258 0
4    8.04472084 -1.72628614 1
5    4.04199507 -3.13377386 1
6    0.40545116  1.06103134 0
7    4.54138600 -0.75862624 1
8    5.33478772 -0.42353199 1
9    4.82078051 -1.33457144 1
10   2.03427713  0.91361444 0
11   7.78067182 -3.10112784 1
12   4.23279256 -2.79959213 0
13   1.03546479  0.48078561 0
14  -4.00349598 -3.12703914 1
15   6.55734391 -2.35919398 1
16   2.85790745 -2.68036329 0
17   2.94880189 -2.55424391 0
18   5.98467216 -2.48343842 0
19   5.59692944 -1.14404070 1
20   4.87808088 -2.30714541 1
21   5.90606161 -2.87634403 0
22   5.47333215  0.32621148 1
23   3.23579518 -2.37166244 0
24  -3.29088243 -2.31100103 1
25   4.96006112 -2.17353545 0
26   2.82250534 -0.76562575 1
27   2.50733135 -2.12741729 0
28  -1.65092741 -2.06518430 1
29   1.48795676 -3.18067058 0
30   4.32164726 -2.56165259 0
31   7.29652199 -1.89579906 0
32   2.67495667 -3.01999517 0
33   4.22592528 -1.07942159 1
34   2.82985352 -4.62993570 0
35  -1.35464467 -1.46902621 1
36   1.68767196 -4.66120916 1
37   1.75314569 -2.52130594 1
38   2.81243457 -2.91500764 0
39   6.47858566 -3.12946129 0
40   5.41337362 -2.09854811 1
41   2.47973071 -5.31576779 1
42   2.19880002  0.03790208 1
43   5.20399171 -4.88381685 0
44   4.76032360 -2.80285821 0
45   0.82196325 -3.93283032 1
46   0.76270387 -3.30045666 1
47   4.15290939  1.61507850 1
48   5.43031450 -1.96986990 1
49   2.64473008 -4.22793787 0
50   5.78630728 -4.84161214 0
51   4.25892133 -1.22025307 0
52   1.06460261 -2.03214657 0
53   4.07871518 -2.55091058 0
54  -0.57135969 -3.60970246 1
55   7.53161884 -4.57635683 0
56   9.26257436 -3.86228769 0
57   1.83874373 -0.26789930 1
58  -0.30184360 -3.07606548 0
59   4.80161165 -4.39789764 0
60   2.57291984  1.23770633 1
61  10.59458219 -1.26370455 1
62   2.87591222 -2.41334890 0
63   5.18114738 -0.16665358 1
64   3.08855060 -0.46467093 1
65   0.64956374 -3.07256042 1
66   3.59701367  1.82108156 1
67  -2.70778035 -2.44171977 1
68   7.63449140 -4.46729711 0
69   3.48462961 -2.25010745 0
70   9.87040135 -1.64053305 1
71   4.50369316  1.99753585 1
72   0.75495226 -1.81674492 0
73   4.93128630 -1.20845485 1
74   0.04612393 -2.13363280 1
75  -0.96433690 -2.57850643 1
76   3.92163392 -2.06014725 0
77   1.59818801 -0.63576818 1
78   3.00349543  1.59442979 1
79   3.23508791 -0.22050410 1
80   1.13577108  0.09215872 0
81   1.20171157 -4.13271473 1
82   2.57252769 -0.29584288 1
83   6.72543819 -1.61907907 1
84  -1.81794126 -4.54135160 1
85   4.87822276 -1.09756214 1
86   4.05288152 -2.27497104 0
87   6.36181687  0.53673964 1
88   2.03808597 -3.32689295 0
89   4.17010222 -2.74514862 0
90   3.84464054 -3.60406870 0
91   1.28440103 -2.30675306 0
92   6.81961338 -1.30369517 1
93   6.66951527 -3.26742501 0
94   5.21426998 -0.56175148 1
95   8.01800798 -4.09246077 0
96   4.76608915 -3.81516225 0
97  -1.03693902  0.49615837 0
98   1.18717559 -3.75949942 1
99  -0.87256511 -1.28643887 0
100  1.50297574 -2.66004308 0

CodePudding user response:

Here is a solution with replicate. From the documentation, my emphasis:

replicate is a wrapper for the common use of sapply for repeated evaluation of an expression (which will usually involve random number generation).

set.seed(1)
n <- 99L
dfs <- replicate(n, {
  X1 <- rnorm(100, mean=3, sd=sqrt(10))
  X2 <- rnorm(100, mean=-2, sd=sqrt(3))
  X1sq <- X1^2
  X2sq <- X2^2
  X1X2 <- X1*X2
  a <- exp(0.2*X1sq   0.3*X2sq   0.7*X1X2)
  b <- 1   exp(0.2*X1sq   0.3*X2sq   0.7*X1X2)
  px3 <- a/b
  Y <- rbinom(100, 1, px3)
  data.frame(X1, X2, Y)
}, simplify = FALSE)

Created on 2022-05-21 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related