正如标题所述,我在将计算值添加到空向量时遇到问题,并且在具有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]&lt; - list(sim.chap)的行上,向量l.sim.chap加载了第一次sim.chap迭代的重复值,而不是sim.chap来自每次和后续迭代的值。
答案 0 :(得分:0)
删除了i&lt; -1和j&lt; - 1.代码现在可以使用了。太奇怪了。
# 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){
# 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)