r - Issue adding inputs/calculations in FOR Loop -
r - Issue adding inputs/calculations in FOR Loop -
as title states, have problem adding calculated values empty vector, , in case of loop 1000 iterations, insert same value 1000 times, instead of different values each of 100 iterations. sifted through code, line line, setting , j values forcefulness stop iterations. see below code:
# 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 sample l.sim.chap <- na # temporary list of estimators utilize later # loop: various sample sizes for(i in 1:length(samples.sim)){ # loop: iterations per sample size (j in 1:n.sim){ <- 1:1 j <- 1:2 # grab 1 sim.one <- sample(n, samples.sim, replace=t, prob=null) sim.one <- as.numeric(sim.one) # convert numeric # grab 2 sim.two <- sample(n, samples.sim, replace=t, prob=null) sim.two <- as.numeric(sim.two) # convert numeric # find mutual elements sim.m.two <- intersect(sim.one, sim.two) # amount of mutual 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 loop: iterations per sample size # calculate bias of estimator 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 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 loop: various sample sizes # homecoming 3 columns , create info frame 3 <- (data.frame(n, bias.of.est, sd.of.est)) # list of info frame 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)
basically, on line l.sim.chap[i] <- list(sim.chap), vector l.sim.chap loaded repeated values first sim.chap iteration, not sim.chap values each , subsequent iteration.
removed <-1 , j <- 1. code works now. strange.
# 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 sample l.sim.chap <- na # temporary list of estimators utilize later # loop: various sample sizes for(i in 1:length(samples.sim)){ # loop: iterations per sample size (j in 1:n.sim){ # grab 1 sim.one <- sample(n, samples.sim, replace=t, prob=null) sim.one <- as.numeric(sim.one) # convert numeric # grab 2 sim.two <- sample(n, samples.sim, replace=t, prob=null) sim.two <- as.numeric(sim.two) # convert numeric # find mutual elements sim.m.two <- intersect(sim.one, sim.two) # amount of mutual 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 loop: iterations per sample size # calculate bias of estimator 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 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 loop: various sample sizes # homecoming 3 columns , create info frame 3 <- (data.frame(n, bias.of.est, sd.of.est)) # list of info frame 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)
r for-loop vector
Comments
Post a Comment