Code for Monte Carlo simulation: generate samples of given size in R

前端 未结 3 1014
借酒劲吻你
借酒劲吻你 2021-02-06 18:02

I started by generating a sample of 500 uniformly-distributed random numbers between 0 and 1 using the code below:

set.seed(1234)
X<-runif(500, min=0, max=1)
         


        
3条回答
  •  野的像风
    2021-02-06 18:37

    You could try using data.table, a package that can be installed using install.packages("data.table"). With that installed, you would run something like...

    > require(data.table)
    > dt <- data.table(x=runif(500*10000),iter=rep(1:500,each=10000))
                      # x iter
          # 1: 0.48293196    1
          # 2: 0.61935416    1
          # 3: 0.99831614    1
          # 4: 0.26944687    1
          # 5: 0.38027524    1
         # ---                
    # 4999996: 0.11314160  500
    # 4999997: 0.07958396  500
    # 4999998: 0.97690312  500
    # 4999999: 0.81670765  500
    # 5000000: 0.62934609  500
    > summaries <- dt[,list(mean=mean(x),median=median(x)),by=iter]
         # iter      mean    median
      # 1:    1 0.5005310 0.5026592
      # 2:    2 0.4971551 0.4950034
      # 3:    3 0.4977677 0.4985360
      # 4:    4 0.5034727 0.5052344
      # 5:    5 0.4999848 0.4971214
     # ---                         
    # 496:  496 0.5013314 0.5048186
    # 497:  497 0.4955447 0.4941715
    # 498:  498 0.4983971 0.4910115
    # 499:  499 0.5000382 0.4997024
    # 500:  500 0.5009614 0.4988237
    > min_o_means <- min(summaries$mean)
    # [1] 0.4914826
    

    I think the syntax is fairly straightforward. You may want to look up some of the functions using ? (e.g., ?rep). The lines starting with # are just displaying the generated objects. In data.tables, the number to the left of the : is just the row number and --- indicates rows that are skipped in the display.

提交回复
热议问题