从总体中抽取样本的方法很多,最常用的方法是简单随机抽样。
简单随机抽样:从容量为N的总体中,任意抽取n个单位作为样本,使每个可能的样本被抽中的概率相等的一种抽样方式。
srswr() srswor() sample()
sample可以实现放回随机抽样和不放回随机抽样,也可以对数据进行随机分组。
格式:
sample(x, size, replace=FALS, prob=NULL)
参数说明:
参数 | 说明 |
---|---|
x | 为向量,表示抽样的总体,或者是一个正整数,表示样本总体为1~n; |
size | 为样本容量,即要抽取的样本个数,是一个非负整数; |
replace | 表示是否为有放回的抽样,是一个逻辑值,默认为FALSE,即默认为无放回抽样; |
prob | 为权重向量,即x中元素被抽取到的概率,是一个取值0~1的向量,其长度应该与x的长度相同。 |
从26个大写字母中不放回随机抽取5个
> sample(LETTERS,5)
[1] "E" "W" "L" "X" "Q"
将26个大写字母随机分成2组,第2组和第1组的比例为7:3
> n<-sample(2,26,replace = TRUE,prob=c(0.7,0.3))
> n
[1] 1 1 2 1 1 2 2 2 1 1 2 1 1 1 2 2 1 2 1 2 1 2 1 1 1 1
> sample1<-LETTERS[n==1]
> sample2<-LETTERS[n==2]
> sample1
[1] "A" "B" "D" "E" "I" "J" "L" "M" "N" "Q" "S" "U" "W" "X" "Y" "Z"
> sample2
[1] "C" "F" "G" "H" "K" "O" "P" "R" "T" "V"
将26个大写字母随机分成3组,每组的个数分配比例为0.4,0.4,0.2
> n<-sample(3,26,replace = TRUE,prob = c(0.4,0.4,0.2))
> n
[1] 2 1 2 1 3 2 3 1 3 1 1 1 1 2 1 1 2 3 3 1 2 3 3 2 2 3
> sample1<-LETTERS[n==1]
> sample1
[1] "B" "D" "H" "J" "K" "L" "M" "O" "P" "T"
> sample2<-LETTERS[n==2]
> sample2
[1] "A" "C" "F" "N" "Q" "U" "X" "Y"
> sample3<-LETTERS[n==3]
> sample3
[1] "E" "G" "I" "R" "S" "V" "W" "Z"
有10位学生的学号分别为1,2,…,10,现在要进行毕业答辩,答辩顺序要求 随机产生。请给出代码。
> sample(10)
[1] 7 9 10 6 3 4 1 2 5 8
> x=c(1,3,5,7)
> sample(x,size=20,replace=T, prob=c(0.1,0.2,0.3,0.9))
[1] 5 3 7 7 5 7 7 7 7 5 7 7 7 1 7 3 1 7 1 7
结论:对每一个元素都可以给定一个概率,且每个概率是独立的,即在参数prob中,不一定所有元素的概率加起来等于1,它只代表某元素被抽取的概率而已。
模拟抛硬币游戏,抛10次,看看出现正面H(Heads)和反面T(Tails)的情况。
将抛硬币视为有放回的实验,即将参数replace设置为TRUE。
> sample(c("H","F"),10,replace = TRUE)
[1] "H" "F" "H" "F" "F" "F" "F" "H" "H" "H"
某篮球运动员投篮命中率为70%,模拟10次投篮的命中(S)和未命中(F)情况。
> sample(c("S","F"),10,replace = TRUE,prob = c(0.7,0.3))
[1] "S" "S" "F" "S" "S" "S" "F" "S" "S" "S"
Simple random sampling without replacement
Description:Draws a simple random sampling without replacement of size n (equal probabilities, fixed sample size, without replacement).
Usage:srswor(n,N)
Value:Returns a vector (with elements 0 and 1) of size N, the population size. Each element k of this vector indicates the status of unit k (1, unit k is selected in the sample; 0, otherwise).
> library(sampling)
> s<-srswor(10,26)
> s
[1] 1 0 0 1 0 0 1 0 1 0 0 1 1 0 0 1 1 0 0 0 1 0 1 0 0 0
> obs<-which(s==1)
> obs
[1] 1 4 7 9 12 13 16 17 21 23
> sample<-LETTERS[obs]
> sample
[1] "A" "D" "G" "I" "L" "M" "P" "Q" "U" "W"
在26个中抽取10个,1表示被抽取的状态,0表示没有被抽取状态
Simple random sampling with replacement
Description:Draws a simple random sampling with replacement of size n (equal probabilities, fixed sample size, without replacement).
Usage:srswr(n,N)
Value:Returns a vector of size N, population size. Each element k of this vector indicates the number of replicates for unit k in the sample.
> s<-srswr(10,26)
> s
[1] 1 2 0 0 0 1 2 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 0 0 0 0
> obs<-which(s!=0)
> obs
[1] 1 2 6 7 18 19 20 22
> sample<-LETTERS[obs]
> sample
[1] "A" "B" "F" "G" "R" "S" "T" "V"
分层抽样将分成不同子总体(或称为层)的总体中,按规定的比例从不同层中随机抽取样品(个体)的方法。
这种方法的优点是,样本的代表性比较好,抽样误差比较小。缺点是抽样手续较简单随机抽样还要繁杂些。
R语言sampling包的sampling::strata()可以实现
其命令为:
strata(data, stratanames=NULL, size, method=c(“srswor”,“srswr”,“poisson”,“systematic”), pik,description=FALSE)
其中,x为样本数据, stratanames为分层抽样要使用的变量,size为各层抽取个数,method指的是抽样方法,“srswor”、“srswr”、“poisson”、"systematic"分别指不重置简单抽样、重置简单抽样、泊松抽样、系统抽样,pik指的是各数据包含在样本中的概率,description默认为FALSE,若设置为TRUE则输出样本个数和总体个数。返回值ID_unit(被选单元的标志符)、Stratum(单元层)、Prob(包含单元的概率)
> library(sampling)
> x<-strata(c("Species"),size=c(2,3,4),method="srswor",data=iris)
> x
Species ID_unit Prob Stratum
11 setosa 11 0.04 1
21 setosa 21 0.04 1
68 versicolor 68 0.06 2
83 versicolor 83 0.06 2
98 versicolor 98 0.06 2
102 virginica 102 0.08 3
103 virginica 103 0.08 3
111 virginica 111 0.08 3
112 virginica 112 0.08 3