正如标题所述,我在将计算值添加到空向量时遇到了问题,对于具有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.chapi <- list( sim.chap )的代码行中,向量l.sim.chap被加载来自第一次sim.chap迭代的重复值,而不是来自每次迭代和后续迭代的sim.chap值。
发布于 2014-11-05 15:14:14
删除了i <-1和j <- 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)https://stackoverflow.com/questions/26751377
复制相似问题