2015-04-08 12 views
0

假设下面的简单的自举过程:查看选定的样品用于引导环路中的每个复制

x <-  c(20,54,18,65,87,49,45,94,22,15,16,15,84,55,44,13,16,65,48,98,74,56,97,11,25,43,32,74,45,19,56,874,3,56,89,12,28,71,93) 
n <- length(x) 

nBoot <- 5; mn <- numeric(nBoot) 
for(boots in 1:nBoot){ 
set.seed(830219+boots) 
repl <- sample(x,n,replace=TRUE) 
mn[boots] <- mean(repl) 
} 

是否有我可以查看重采样数据集“REPL”对于每个重复5次的方法吗?

非常感谢您的答复。提前感谢

编辑

我试过如下:

x <-  c(20,54,18,65,87,49,45,94,22,15,16,15,84,55,44,13,16,65,48,98,74,56,97,11,25,43,32,74,45, 19,56,874,3,56,89,12,28,71,93) 
n <- length(x) 

nBoot <- 5; mn <- numeric(nBoot) 
for(boots in 1:nBoot){ 
set.seed(830219+boots) 
repl <- sample(x,n,replace=TRUE) 
print(repl) 
mn[boots] <- mean(repl) 
} 

这样我就可以查看每个5个采样数据集,但不允许我与每个数据集seperately工作如REPL [1],REPL [2],...

EDIT2

我尝试以下:

x <-  c(20,54,18,65,87,49,45,94,22,15,16,15,84,55,44,13,16,65,48,98,74,56,97,11,25,43,32,74,45,19,56,874,3,56,89,12,28,71,93) 
n <- length(x) 

nBoot <-3; mn <- numeric(nBoot); repl <- x 
for(boots in 1:nBoot){ 
set.seed(830219+boots) 
repl[boots] <- sample(x, n, replace=TRUE) 
pr <- print(repl) 
mn[boots] <- mean(repl) 
} 

然后我然而得到5个警告消息:“在REPL [靴] < - 样品(X,N,替换= TRUE): 数项替换的不是的倍数更换长度”

,并呼吁REPL [1]只给了我一个号码

+1

如果我理解正确,它似乎是一个简单的'R'编程问题。如果您需要在每次迭代中查看重新采样的数据集,为什么不在'sample()'调用之后使用适当的函数,如'print(repl)'或类似的函数? –

+0

感谢您的评论。在循环内使用打印选项确实可以查看每个重采样数据集。但是,似乎这不允许我使用每个重采样数据集进行分析。是不是有可能将重采样数据集输出为某种列表,这样我就可以像repl [1],repl [2]一样单独处理每个数据集?我appoligize我的可怜的R知识,并再次感谢你的帮助。 – user3387899

+0

非常欢迎你,不用道歉。关于使用每个重采样数据集,您当然可以单独引用每个集合(起初我不清楚),但为此您需要将'repl'定义为一个向量并初始化该向量的相应元素在每次迭代中:在for循环之前:repl < - x(为了简单起见);在'seed()'后面:'repl [boots] < - sample(x,n,replace = TRUE)'。然后,可以在循环之后将每个重采样数据集称为“repl [1]”等。 –

回答

0

根据您的意见,我已经固定的代码。这是我测试的版本,它似乎工作:

x <- c(20,54,18,65,87,49,45,94,22,15,16,15,84,55,44,13,16,65,48,98,74,56,97,11,25,43,32,74,45,19,56,874,3,56,89,12,28,71,93) 
n <- length(x) 

nBoot <-3; mn <- numeric(nBoot) 
repl <- matrix(x, nrow=nBoot, ncol=length(x)) 

for (boots in 1:nBoot) { 
    set.seed(830219+boots) 
    repl[boots, ] <- sample(x, n, replace=TRUE) 
    pr <- print(repl) 
    mn[boots] <- mean(repl) 
} 
相关问题