蒙特卡洛模拟代码:在R中生成给定大小的样本 [英] Code for Monte Carlo simulation: generate samples of given size in R
问题描述
我首先使用以下代码生成了一个0到1之间的500个均匀分布的随机数样本:
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)
现在,我需要编写一个伪代码,以生成10000个N = 500的样本用于MC模拟,计算我新创建的X的平均值,并将迭代次数和平均值存储在结果对象中.我从来没有尝试过,到目前为止,我有这个:
Now, I need to write a psuedocode that generates 10000 samples of N=500 for a MC simulation, compute the mean of my newly created X, and store the iteration number and mean value in a result object. I have never attempted this, and so far I have this:
n.iter <-(10000*500)
results <- matrix (0, n.iter, 4)
最后,一旦完成,我要运行它,然后获取应计样本均值的中值,均值和最小值/最大值,并将其保存到名为MC.table的数据帧中. (还要注意,上面我不知道为什么矩阵代码中有一个"4"-我正在处理前面的示例).任何建议或帮助将不胜感激.
Finally, once this is accomplished, I'm to run it, then obtain median, mean, and min/max of the accrued sample means and save them to a data frame called MC.table. (Also note, above, I have no idea why there's a "4" in the matrix code --- I'm working off of previous examples). Any advice or help would be greatly appreciated.
我有一个可能可行的示例,但我真的不了解它所发生的情况,因此请详细说明其有效性:
I have an example that may work, but I don't really understand what's going on with it, so please elaborate on its validity for this:
Ni <- 10000
n <- 500
c <- 0
for (i in n){
for (j in 1:Ni){
c <- c+ 1
d <- data.frame (x= , y= )
results [c,1] <- c
results [c,2] <- j
results [c,3] <- i
results [c,4] <- something( d$x, d$y)
rm (d) } }
如果您甚至可以花时间解释这意味着什么,那么对我有很大帮助!谢谢!
If you could even take the time to explain what that means, that'd go a long way to helping me! Thanks!
推荐答案
您可以尝试使用data.table
,这是可以使用install.packages("data.table")
安装的软件包.安装后,您将运行类似...
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
我认为语法非常简单.您可能需要使用?
(例如,?rep
)查找某些功能.以#开头的行仅显示生成的对象.在data.tables中,:
左侧的数字只是行号,而---
表示在显示中跳过的行.
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.
这篇关于蒙特卡洛模拟代码:在R中生成给定大小的样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!