我正在从包含许多值的列表的文件中采样,例如:
312313.34
243444
12334.92
321312
353532并使用R从该列表中随机抽样:
list = read.table("data")
out <-sample(list,50,replace=TRUE)
out.mean<-mean(out)
out.mean谁能告诉我如何把这个放到一个循环中,这样我就可以执行这个过程1000次,并取它将产生的1000个平均值的平均值?
非常感谢您的提前!
鲁巴尔
发布于 2012-08-26 20:55:41
另一种解决方案可能是(记住@Tyler Rinker刚才关于replicate的说法)
Data <- read.table(text='
312313.34
243444
12334.92
321312
353532', header=FALSE)
Data <- as.numeric(as.matrix((Data)))
set.seed(007)
Means <- replicate(1000, mean(sample(Data,50,replace=TRUE))) 平均值由大小为50的每个子样本的1000个平均值组成。如果您想要均值,请执行以下操作:
mean(Means) 你想要做的听起来像是自举或类似于减少偏差的重采样技术(我猜)。
发布于 2012-08-26 20:41:00
我会在采样的基础上做一个函数,然后用lapply一遍又一遍地重复这个过程(虽然replicate可能也能工作,但我也有过这种慢得多的经验)。
我建议不要写入名为list的对象,因为这是一个重要的函数。
所以它看起来像这样:
#make a data set that may look like yours
LIST <- rnorm(1000)
#take your code and make a function
mean.find <- function(dat) {
out <-sample(dat, 50,replace=TRUE)
mean(out)
}
#a single use yo check it out
mean.find(LIST)
#repeat it 1000 times with lapply
reps <- unlist(lapply(seq_len(1000), mean.find))
#take the mean of that
mean(reps)https://stackoverflow.com/questions/12130131
复制相似问题