P <- read.table('house_polls.dat') R <- read.table('house_results.dat') P R T <- R[,2] + R[,3] R[,2] <- 100 * R[,2] / T R[,3] <- 100 * R[,3] / T R <- cbind(R, R[,2]-R[,3]) D <- unique(as.vector(P[,2])) D length(D) i2 <- which(R[,1] == "NC-08") R[i2,] as.vector(R[R[,4] <0,1]) SD <- NULL ## Loop through the races. for (d in D) { ## Get the polls for race d. i1 <- which(P[,2] == d) ## Get the recent polls (roughly three weeks back). i1 <- i1[P[i1,1] > 290] if (length(i1) == 0) { next } ## The standard deviation for the poll average difference. sdev <- 7/(1.96*sqrt(length(i1))) SD <- c(SD, sdev) } SD <- NULL ## Loop through the races. for (d in D) { ## Get the polls for race d. i1 <- which(P[,2] == d) ## Get the recent polls (roughly three weeks back). i1 <- i1[P[i1,1] > 290] if (length(i1) == 0) { next } ## Take the first poll only i1 <- i1[1] ## The standard deviation for the poll average difference. sdev <- 7/(1.96*sqrt(length(i1))) SD <- c(SD, sdev) } k <- 1 n <- 10 theta <- 0.5 xytable <- array( 0, c(n,2) ) while(1) { includeMe <- TRUE X <- rnorm(1) Y <- rnorm(1) if ( X > 1 && 1*(runif(1) < theta) ) { includeMe <- FALSE } if ( includeMe ) { xytable[k,1] <- X xytable[k,2] <- Y k <- k+1 } if( k > n) { break } } xytable