在 R 中,计算 1000 次重复的平均值
In R,Computing average from 1000 replications
我正在尝试进行 1000 次测试,将所有答案相加并除以 1000。
我正在尝试类似的方法,但它不起作用,但它只为我提供了执行 1 次的价值。
for(i in 1:1000) {
x <- sample(1:500, 30, replace = F)
mhat <- stimateM <- 2*mean(x) - 1
y <- abs(mhat-500)
value =+ y
}
根据 coffeinjunky 的建议,这里有一个在循环外定义容器的示例,然后在循环内添加模拟值,最后在循环外对所有值进行求和和除法。
value = list()
for(i in 1:1000){
x=sample(1:500,30,replace=F)
mhat=stimateM=2*mean(x)-1
y=abs(mhat-500)
value[i]=y
}
sumValue = sum(unlist(value))
divValue = sumValue/1000
这里有一个不使用循环的 R
风格的解决方案:
x <- replicate(1000, sample.int(500,30,replace=F))
y <- abs(2*colMeans(x)-501)
sumValue = sum(y)
divValue = mean(y)
replicate
将为我们提供所需的矩阵输出,然后我们可以使用矢量化函数 colMeans
.
随着重复次数的增加,此方法的效果会更好:
loopSoln <- function(N) {
value = list()
for(i in 1:N) {
x=sample(1:500,30,replace=F)
mhat=stimateM=2*mean(x)-1
y=abs(mhat-500)
value[i]=y
}
sumValue = sum(unlist(value))
divValue = sumValue/N
}
replicateSoln <- function(N) {
x <- replicate(1000, sample.int(500,30,replace=F))
y <- abs(2*colMeans(x)-501)
sumValue = sum(y)
divValue = mean(y)
}
(ltimes <- sapply(c(1e2,1e3,1e4,1e5), function(N) system.time(loopSoln(N))[3]))
## elapsed elapsed elapsed elapsed
## 0.002 0.014 0.158 2.009
(rtimes <- sapply(c(1e2,1e3,1e4,1e5), function(N) system.time(replicateSoln(N))[3]))
## elapsed elapsed elapsed elapsed
## 0.007 0.011 0.007 0.010
plot(ltimes~I(2:5), type = 'l', col = 2, xlab = "log_10 number of replicates", ylab = "elapsed time (sec)")
lines(rtimes~I(2:5), col = 4)
legend("topleft", lty = 1, col = c(2,4),
legend = c("Loop", "replicate + rowMeans"))
我正在尝试进行 1000 次测试,将所有答案相加并除以 1000。
我正在尝试类似的方法,但它不起作用,但它只为我提供了执行 1 次的价值。
for(i in 1:1000) {
x <- sample(1:500, 30, replace = F)
mhat <- stimateM <- 2*mean(x) - 1
y <- abs(mhat-500)
value =+ y
}
根据 coffeinjunky 的建议,这里有一个在循环外定义容器的示例,然后在循环内添加模拟值,最后在循环外对所有值进行求和和除法。
value = list()
for(i in 1:1000){
x=sample(1:500,30,replace=F)
mhat=stimateM=2*mean(x)-1
y=abs(mhat-500)
value[i]=y
}
sumValue = sum(unlist(value))
divValue = sumValue/1000
这里有一个不使用循环的 R
风格的解决方案:
x <- replicate(1000, sample.int(500,30,replace=F))
y <- abs(2*colMeans(x)-501)
sumValue = sum(y)
divValue = mean(y)
replicate
将为我们提供所需的矩阵输出,然后我们可以使用矢量化函数 colMeans
.
随着重复次数的增加,此方法的效果会更好:
loopSoln <- function(N) {
value = list()
for(i in 1:N) {
x=sample(1:500,30,replace=F)
mhat=stimateM=2*mean(x)-1
y=abs(mhat-500)
value[i]=y
}
sumValue = sum(unlist(value))
divValue = sumValue/N
}
replicateSoln <- function(N) {
x <- replicate(1000, sample.int(500,30,replace=F))
y <- abs(2*colMeans(x)-501)
sumValue = sum(y)
divValue = mean(y)
}
(ltimes <- sapply(c(1e2,1e3,1e4,1e5), function(N) system.time(loopSoln(N))[3]))
## elapsed elapsed elapsed elapsed
## 0.002 0.014 0.158 2.009
(rtimes <- sapply(c(1e2,1e3,1e4,1e5), function(N) system.time(replicateSoln(N))[3]))
## elapsed elapsed elapsed elapsed
## 0.007 0.011 0.007 0.010
plot(ltimes~I(2:5), type = 'l', col = 2, xlab = "log_10 number of replicates", ylab = "elapsed time (sec)")
lines(rtimes~I(2:5), col = 4)
legend("topleft", lty = 1, col = c(2,4),
legend = c("Loop", "replicate + rowMeans"))