BenjaminLind

SamplingLesson20140128Commands

Jan 27th, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.02 KB | None | 0 0
  1. source("http://pastebin.com/raw.php?i=BU2tycrK")
  2.  
  3. pop.param <- mean(studentdat$commties) #Measures population
  4. pop.param <- round(pop.param, digits = 3)
  5. first.five <- which(studentdat$timerank %in% 1:5) #Samples
  6. convien.param <- mean(studentdat$commties[first.five]) #Measures sample
  7. cat("\n  Population parameter = ", pop.param, ", Sample parameter = ", convien.param, "\n")
  8.  
  9. m <- sort(studentdat$timerank[which(studentdat$male)])[1] #First male times
  10. f <- sort(studentdat$timerank[which(!studentdat$male)])[1:4] #First four females times
  11. quota.samp <- which(studentdat$timerank %in% c(m, f)) #Samples
  12. quota.param <- mean(studentdat$commties[quota.samp]) #Measures sample
  13. cat("\n  Population parameter = ", pop.param, ", Sample parameter = ", quota.param, "\n")
  14.  
  15. set.seed(666) #Standardizes random number generator
  16. sb.samp <- function(n, rr = .5, nseeds = 1, net = studentel){
  17.   recruit.wave <- rep(NA, times=vcount(studentel))
  18.   orig.seeds <- sample.int(nrow(studentdat), nseeds)
  19.   new.seeds <- orig.seeds
  20.   ncount <- nseeds
  21.   wave <- 1
  22.   recruit.wave[orig.seeds] <- wave
  23.   deadend <- FALSE
  24.   while ((ncount < n) & !deadend){
  25.     wave <- wave + 1
  26.     new.seeds <- unlist(sapply(new.seeds, neighbors, graph = net))
  27.     if (all(new.seeds %in% orig.seeds))
  28.       deadend <- TRUE
  29.     else{
  30.       new.seeds <- new.seeds[which(new.seeds %in% orig.seeds == FALSE)]
  31.       recruited <- which(rbinom(length(new.seeds), 1, rr)==1)
  32.       new.seeds <- new.seeds[recruited]
  33.       if ((length(recruited) == 0) | (length(new.seeds) == 0))
  34.         deadend <- TRUE
  35.       else{
  36.         orig.seeds <- c(orig.seeds, new.seeds)
  37.         recruit.wave[new.seeds] <- wave
  38.         ncount <- length(orig.seeds)
  39.         }
  40.       }
  41.     }
  42.   return(recruit.wave)
  43.   }
  44.  
  45. #General plot of "population" sociogram
  46. library(RColorBrewer)
  47. plot.wrap <- function(...)
  48.   plot(studentel, edge.width=7, vertex.label.family="sans", vertex.label.cex=1.25, vertex.label.dist=.85, vertex.label.degree=-1.9*pi/4, ...)
  49. plot.wrap.sb <- function(sb.recruited){
  50.   sb.param <- mean(degree(studentel)[which(!is.na(sb.recruited))])
  51.   sb.param <- round(sb.param, digits = 3)
  52.   sb.maintitle <- "Snowball Sample Simulation"
  53.   sb.sub <- paste("Population Parameter = ", pop.param, ", Sample parameter = ", sb.param, sep="")
  54.   sb.recruits.color <- rev(brewer.pal(max(sb.recruited, na.rm=TRUE), "Blues"))[sb.recruited]
  55.   sb.recruits.color[is.na(sb.recruited)] <- "white"
  56.   plot.wrap(vertex.color = sb.recruits.color, main = sb.maintitle, sub = sb.sub)  
  57.   }
  58. plot.wrap.sb(sb.samp(5)) #Sample size of 5, response rate of .5, 1 seed
  59. plot.wrap.sb(sb.samp(10, rr = .3, nseeds = 2)) #Sample size of 10, response rate of .3, 2 seeds
  60. plot.wrap.sb(sb.samp(10, rr = .8, nseeds = 2)) #Same as above, yet with a higher response rate
  61.  
  62. set.seed(666)
  63. random.samp <- sample.int(1000, 10)
  64. random.samp
  65. random.samp.meanfun <- function(ss)
  66.   return(mean(studentdat$commties[sample.int(vcount(studentel), ss)]))
  67. cat("\n  Population parameter = ", pop.param, ", Sample parameter = ", round(random.samp.meanfun(5), digits = 3), "\n")
  68.  
  69. #To demonstrate the central limit theorem
  70. random.mean.5samp.1k <- replicate(1000, random.samp.meanfun(5))
  71. mean.across.randomsamples <- mean(random.mean.5samp.1k)
  72. mean.across.randomsamples <- round(mean.across.randomsamples, digits = 3)
  73. hist(random.mean.5samp.1k, main = "Frequency of Mean Communication Ties\nin 1000 Random Samples of 5", sub = paste("Population Parameter = ", pop.param, ", Mean Across Distributions = ", mean.across.randomsamples, sep=""), xlab = "Mean Number of Communication Partners")
  74.  
  75. #To demonstrate systematic samples
  76. start.point <- sample.int(vcount(studentel), 1)
  77. k <- 4
  78. first.half <- seq(from = 1, to = start.point, by = k)
  79. second.half <- seq(from = start.point, to = vcount(studentel), by = k)
  80. syst.samp <- unique(c(first.half, second.half))
  81. syst.samp
  82. syst.samp.mean <- mean(studentdat$commties[syst.samp])
  83. syst.samp.mean <- round(syst.samp.mean, digits = 3)
  84. cat("\n  Population parameter = ", pop.param, ", Sample parameter = ", syst.samp.mean, "\n")
Advertisement
Add Comment
Please, Sign In to add comment