[英]Code for Monte Carlo simulation: generate samples of given size in R
我首先使用以下代碼生成500個均勻分布的0到1之間隨機數的樣本:
set.seed(1234)
X<-runif(500, min=0, max=1)
現在,我需要編寫一個偽代碼,為MC模擬生成10000個N = 500的樣本,計算新創建的X的平均值,並將迭代次數和平均值存儲在結果對象中。 我從未嘗試過這個,到目前為止,我有這個:
n.iter <-(10000*500)
results <- matrix (0, n.iter, 4)
最后,一旦完成,我將運行它,然后獲得應計樣本均值的中位數,平均值和最小值/最大值,並將它們保存到名為MC.table的數據幀中。 (另請注意,上面,我不知道為什么矩陣代碼中有“4” - 我正在處理前面的例子)。 任何建議或幫助將不勝感激。
編輯:我有一個可行的例子,但我真的不明白它是怎么回事,所以請詳細說明它的有效性:
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) } }
如果你甚至可以花時間來解釋這意味着什么,那對我來說還有很長的路要走! 謝謝!
您可以嘗試使用data.table
,這是一個可以使用install.packages("data.table")
安裝的軟件包。 安裝完成后,你會運行像...
> 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中, :
左邊的數字只是行號,而---
表示在顯示中跳過的行。
我想我給出的答案真的取決於你是想學習偽代碼還是想學習“R”這樣做的方法。 對於想要學習如何使用R的人,我會推薦這個答案。
首先,我將制作一個N列和10000行的矩陣。 當我們提前讓空間進入數字時,R贊賞它。
X=matrix(NA,nrow=10000,ncol=500)
您知道如何為一行生成500個隨機變量。
runif(500,0,1)
現在你需要弄清楚如何讓它發生10000次並將每一個分配給X.也許for循環可行。
for(i in 1:10000) X[i,]=runif(500,0,1)
然后你需要弄清楚如何獲得每一行的摘要。 一個可能rowMeans()
功能是rowMeans()
。 查看其幫助頁面,然后嘗試獲取表X的每一行的平均值
獲得每次迭代的方法
rowMeans(X)
然后想知道這些數字是什么樣的,我可能會傾向於
plot(rowMeans(X))
我想你正在描述一個簡單的bootstrap。 最終,您可能希望使用函數boot。 但是在你了解機制之前,我覺得循環是可行的。 這應該讓你開始:
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)
}
重新采樣數據后,您會找到平均值。 然后您訂購所有重新采樣的方法。 該列表的中間是中位數。 而且,例如,對於10000次重采樣,第250次有序重采樣平均值將是您的95%CI較低。 雖然我沒有在這里做,但最小值將在位置1,最大值將在10000位置。當你降低重采樣數時要小心:我計算位置的方式可能會變成十進制值,這會混淆R.
順便說一句,我把它放在功能形式。 如果你喜歡逐行做事,只需確保運行function()和下面的主{}之間的所有行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.