-------------------------------- SW text Sec 3.2, Random Sampling ---------------------------------- > #ex 3.1 text, draw random uniform, discrete > #Ex3.1 SW, use 'sample' to get 15 obs from discreteU[1,2500] no replacement > k = 1:2500 > sample(k, size = 15, p = rep(1:1, 2500)/2500) [1] 1403 2008 1467 1883 454 369 1551 1174 1881 2348 31 1752 1545 2441 484 > sample(k, size = 15, p = rep(1:1, 2500)/2500) [1] 1480 501 723 675 1372 941 1740 12 2457 1282 1549 1166 1558 1191 251 > sample(k, size = 15, p = rep(1:1, 2500)/2500) [1] 2298 1752 307 1574 175 1921 2225 882 1908 1328 996 2074 2326 381 1128 ------------------------- Illustrate Rectangular Distribution ------------------------ > stem(sample(k, size = 1500, p = rep(1:1, 2500)/2500, replace = T)) output deleted > #each bin/stem has about 60 > hist(sample(k, size = 1500, p = rep(1:1, 2500)/2500, replace = T)) ----------------------------------------------------- > #sampling exercise, number of mutants, p.77-78 > k = 0:99 > sample(k, size = 5, p = rep(1:1, 100)/100) [1] 54 49 42 31 67 > sample(k, size = 5, p = rep(1:1, 100)/100) [1] 5 64 82 58 43 > sample(k, size = 5, p = rep(1:1, 100)/100) [1] 23 33 6 88 29 > sample(k, size = 5, p = rep(1:1, 100)/100) [1] 1 52 39 15 9 > sample(k, size = 5, p = rep(1:1, 100)/100) [1] 62 94 13 43 68 > #42 and 67 mutants in my first try > help("sample") Description: 'sample' takes a sample of the specified size from the elements of 'x' using either with or without replacement. Usage: sample(x, size, replace = FALSE, prob = NULL) > help("rep") >Description: 'rep' replicates the values in 'x'. Usage: rep(x, times, ...) > rep(1:5,2) [1] 1 2 3 4 5 1 2 3 4 5 probability examples SW Text, Sec 3.3-3.5 > # verzani ex 5.1 number heads in 2 tosses > k = 0:2 > p = c(1,2,1)/4 > sample(k, size=10, prob = p, replace = TRUE) [1] 2 1 0 1 2 0 1 0 0 2 > try1 = sample(k, size=100, prob = p, replace = TRUE) > table(try1)/100 try1 0 1 2 0.26 0.48 0.26 > try1 = sample(k, size=1000, prob = p, replace = TRUE) > table(try1)/1000 try1 0 1 2 0.234 0.518 0.248 > try1 = sample(k, size=1000, prob = p, replace = TRUE) > table(try1)/1000 try1 0 1 2 0.252 0.498 0.250