【发布时间】:2015-01-01 07:13:51
【问题描述】:
正如标题所述,我在将计算值添加到空向量中时遇到问题,对于具有 1000 次迭代的 FOR 循环,我只需将相同的值插入 1000 次,而不是从 100 次中的每一个中插入不同的值迭代。我逐行筛选代码,将 i 和 j 设置为某些值以强制停止迭代。我的代码见下文:
# Population Size
N <- 100000
# Iterations per Sample Size
n.sim <- 1000
# Various Sample Sizes
samples.sim <- seq(from=100, to=5000, by=50)
cap.recap.partdeux <- function(N, n.sim, samples.sim){
n <- NA # sample size
bias.of.est <- NA # bias of estimator
sd.of.est <- NA # standard deviation of estimators by sample
l.sim.chap <- NA # temporary list of estimators to use for later
# For Loop: Various Sample Sizes
for(i in 1:length(samples.sim)){
# For Loop: Iterations per Sample Size
for (j in 1:n.sim){
i <- 1:1
j <- 1:2
# Catch One
sim.one <- sample(N, samples.sim, replace=T, prob=NULL)
sim.one <- as.numeric(sim.one) # Convert to numeric
# Catch Two
sim.two <- sample(N, samples.sim, replace=T, prob=NULL)
sim.two <- as.numeric(sim.two) # Convert to numeric
# Find Common Elements
sim.m.two <- intersect(sim.one, sim.two)
# Amount of Common Elements
sim.l.m.two <- length(sim.m.two)
# Calculate Chapman Estimator
sim.chap <- ((samples.sim[i]+1)*(samples.sim[i]+1)/(sim.l.m.two+1))-1
l.sim.chap[j] <- list(sim.chap)
} # End For Loop: Iterations per Sample Size
# Calculate bias of estimator for each sample
sum.b.est <- sum(unlist(l.sim.chap), na.rm=T)
bias.est <- (sum.b.est/n.sim)-N
bias.of.est[i] <- bias.est
# Calculate standard deviation of estimator for each sample
sd.est <- sd(unlist(l.sim.chap), na.rm = TRUE)
sd.of.est[i] <- sd.est
# Sample Size
n[i] <- samples.sim[i]
} # End For Loop: Various Sample Sizes
# Return Three Columns and make Data Frame
Three <- (data.frame(n, bias.of.est, sd.of.est))
# List of Data Frame with True Population
Output <- (list(Three, "POP"=N))
return(Output)
} # End Function
Output <- cap.recap.partdeux(N=100000, n.sim=1000, samples.sim)
Test <- data.frame(Output)
基本上,根据 l.sim.chap[i]
【问题讨论】:
-
你进入循环后为什么还要
i = 1:1和j = 1:2? -
@RichardScriven,我设置 i
-
@RichardScriven,我相信你说得有道理。傻我>.