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

前端 未结 3 1013
借酒劲吻你
借酒劲吻你 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:35

    I think you are describing a simple bootstrap. Eventually, you might want to use the function boot. But until you understand the mechanics, I feel that loops are the way to go. This should get you started:

    test<-function(
        seed=1234,
        sample.size=500,
        resample.number=1000,
        alpha=0.05
        )
        {
    
            #initialize original sample
            original.sample<-runif(sample.size, min=0, max=1)   
    
            #initialize data.frame
            resample.results<-data.frame("Run.Number"=NULL,"mean"=NULL)
            for(counter in 1:resample.number){
                temp<-sample(original.sample, size=length(original.sample), replace = TRUE)
                temp.mean<-mean(temp)
                temp.table.row<-data.frame("Run.Number"=counter,"mean"=temp.mean)
                resample.results<-rbind(resample.results,temp.table.row)
            }
            resample.results<-resample.results[with(resample.results, order(mean)), ]
    
            #for the mean information
            lowerCI.row<-resample.number*alpha/2
            upplerCI.row<-resample.number*(1-(alpha/2))
            median.row<-resample.number/2
    
            #for the mean information
            median<-resample.results$mean[median.row]
            lowerCI<-resample.results$mean[lowerCI.row]
            upperCI<-resample.results$mean[upplerCI.row]
    
            #for the position information
            median.run<-resample.results$Run.Number[median.row]
            lowerCI.run<-resample.results$Run.Number[lowerCI.row]
            upperCI.run<-resample.results$Run.Number[upplerCI.row]
    
            mc.table<-data.frame("median"=NULL,"lowerCI"=NULL,"upperCI"=NULL)
            values<-data.frame(median,lowerCI,upperCI)
            #as.numeric because R doesn't like to mix data types
            runs<-as.numeric(data.frame(median.run,lowerCI.run,upperCI.run))
            mc.table<-rbind(mc.table,values)
            mc.table<-rbind(mc.table,runs)
    
            print(mc.table)
        }
    

    After resampling your data, you find the mean. Then you order all of your resampled means. The middle of that list is the median. And, for example, with 10000 resamples, the 250th ordered resampled mean will be your lower 95% CI. Though I didn't do it here, the min value will just be at position 1, and the max value will be at position 10000. Be careful when you lower the resampling number: the way I calculated positions might become decimal values which will confuse R.

    By the way, I put this in function form. If you like doing things line-by-line, just make sure to run all the lines between function() and the following main {}

提交回复
热议问题