Likan Zhan

第 06 章. 抽样和抽样分布

侃侃迩行 · 2017-04-20

6.1. 抽样

6.1.1. 简单随机抽样

  • combn():在非替换条件下,从一个容量为N有限总体中,抽取容量为n的所有样本。
data <- c(2, 5, 8, 12, 13)
combn(x = data, m = 3)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,]    2    2    2    2    2    2    5    5    5     8
## [2,]    5    5    5    8    8   12    8    8   12    12
## [3,]    8   12   13   12   13   13   12   13   13    13
  • 软件包PASWR2中的srs()有类似的功能
library(PASWR2, quietly=TRUE)
t(srs(data, 3))
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,]    2    2    2    2    2    2    5    5    5     8
## [2,]    5    5    5    8    8   12    8    8   12    12
## [3,]    8   12   13   12   13   13   12   13   13    13
  • sample() 用于从整体中选取一个样本
set.seed(13)
sample(x = 1 : 180,
       size = 5,
       replace = FALSE,
       prob = rep(1/180, 180)
)
set.seed(13)
sample(x = 1 : 20,
       size = 5,
       replace = FALSE,
       prob = c(
         rep(1/26, 18),
         rep(4/26, 2)
       )
)
## [1] 129  46  72  18 175
## [1] 14 20  9 19  1

6.1.2. 分层抽样

6.1.3. 系统抽样

set.seed(13)
seq(sample(1:100, 1), 1000, 100)
seq(sample(1:50, 1), 1000, 50)
##  [1]  72 172 272 372 472 572 672 772 872 972
##  [1]  13  63 113 163 213 263 313 363 413 463 513 563 613 663 713 763 813
## [18] 863 913 963

6.1.4. 群聚抽样

6.2. 参数

6.3. 估计

6.4. 样本平均值的分布

N <- 6
n <- 2
pop <- 1:N
rs <- expand.grid(
   Draw1 = pop,
   Draw2 = pop
)

xbarN <- apply(rs, 1, mean)
s2N <- apply(rs, 1, var)
RSV <- cbind(rs, xbarN = xbarN, s2N = s2N)

library(MASS)
fractions(xtabs(~ xbarN) / 36) # Sampling dist of xbar (random sampling)
fractions(xtabs( ~ s2N) / 36) # Sampling dist of S2 (random sampling)
## xbarN
##    1  1.5    2  2.5    3  3.5    4  4.5    5  5.5    6 
## 1/36 1/18 1/12  1/9 5/36  1/6 5/36  1/9 1/12 1/18 1/36 
## s2N
##    0  0.5    2  4.5    8 12.5 
##  1/6 5/18  2/9  1/6  1/9 1/18
T1 <- fractions(xtabs( ~ xbarN) / 36)
T2 <- fractions(xtabs( ~ s2N) / 36)
XBAR <- as.numeric(names(T1))
S2 <- as.numeric(names(T2))
MU_xbarN <- sum(XBAR * T1)
MU_xbarN
VAR_xbarN <- sum((XBAR - MU_xbarN) ^ 2 * T1) # Var xbarN
VAR_xbarN
MU_s2N <- sum(S2*T2)
s2N > MU_s2N
## [1] 7/2
## [1] 35/24
##  [1] 0 0 0 1 1 1 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 1 1 1 0 0
## [36] 0