自助法和水手刀法

R包boot中含有数据集aircondit,里面含有12个观测值用来记录空调设备两次故障之间的小时数。假设鼓掌之间的时间服从指数分布 \(Exp(\lambda)\)。

library(boot)
attach(aircondit)
  1. 给出故障率 \(\lambda\) 的极大似然估计;

注意事项

答:根据计算可知\(\lambda\) 的极大似然估计为\(n/(\sum_i^n x_i)\)

lam_hat=1/mean(hours)
lam_hat
## [1] 0.00925212
  1. 使用自助法对该估计的偏差和标准差进行估计;
lam=function(x,i) 1/mean(x[i])
obj <- boot(data = hours, statistic = lam, R = 200)
sd(obj$t)
## [1] 0.004099043
obj
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = hours, statistic = lam, R = 200)
## 
## 
## Bootstrap Statistics :
##       original     bias    std. error
## t1* 0.00925212 0.00150975 0.004099043
  1. 分别用标准正态、基本、百分位数、自助法t区间和BCa法计算故障间隔平均时间 \(1/\lambda\) 的95%自助法置信区间。

注意事项

(1). 计算观测到得 \(\hat{\theta}\).

(2). 对每个重复, \(b=1, \cdots, B\) :

(a). 从 \(x\) 中有放回的抽样得到第 \(b\) 个样本 \(x^{(b)}=\left(x_{1}^{(b)}, \ldots, x_{n}^{(b)}\right)\).

(b). 由第 \(b\) 个重采样样本计算 \(\hat{\theta}^{(b)}\).

(c). 计算标准差估计 \(\hat{\operatorname{se}}\left(\hat{\theta}^{(b)}\right) \cdot(\) 对每个 Bootstrap 样本 \(x^{(b)}\), 再单独进行一个 Bootstrap 估计).

(d). 计算第 \(b\) 个重复下的" \(t\) " 统计量:$t^{(b)}= $.

(3). 重复样本 \(t^{(1)}, \ldots, t^{(B)}\) 的分布作为推断分布. 找出样 本分位数 \(t_{\alpha / 2}^{*}\) 和 \(t_{1-\alpha / 2}^{*}\).

(4). 计算 \(\hat{s e}(\hat{\theta})\), 即 Bootstrap 重复 \(\left\{\hat{\theta}^{(b)}\right\}\) 的样本标准差.

(5). 计算置信界 \(\left(\hat{\theta}-t_{1-\alpha / 2}^{*} \hat{s e}(\hat{\theta}), \hat{\theta}-t_{\alpha / 2}^{*} \hat{s e}(\hat{\theta})\right)\).

根据计算可知\(1/\lambda\) 的极大似然估计为\(\sum_i^n x_i/n\)

答:根据计算可知\(1/\lambda\) 的极大似然估计为\(\sum_i^n x_i/n\)

B <- 200 # Number of bootstrapping
n <- length(hours) # Sample size
lam=function(x,i) mean(x[i])
lamh <- numeric(B)
lam_hat=1/lam_hat
th=numeric(B)
for(b in 1:B){
i <- sample(1:n, size=n, replace = TRUE)
lamh[b] <- lam(hours,i)
obj <- boot(data = hours[i], statistic = lam, R = 200)
th[b]=(lamh[b]-lam_hat)/sd(obj$t)
}
th.sort=sort(th)
se.lam <- sd(lamh)
#标准正态自助法
print(c(lam_hat-qnorm(p = 0.05/2,lower.tail = FALSE)*se.lam, lam_hat+qnorm(p = 0.05/2,lower.tail = FALSE)*se.lam))
## [1]  36.38176 179.78490
#基本自助法
B=200
lam_sort=sort(lamh)
print(c(2*lam_hat -lam_sort[(1-0.05/2)*B],2*lam_hat- lam_sort[(0.05/2)*B]))
## [1]  29.08333 172.50000
#百分位数自助法
B=200
print(c(lam_sort[0.05/2*B],lam_sort[(1-0.05/2)*B]))
## [1]  43.66667 187.08333
#自助法t区间
print(c(lam_hat-th.sort[(1-0.05/2)*B]*se.lam,lam_hat- th.sort[(0.05/2)*B]*se.lam))
## [1]  51.14366 312.52686
#BCa
z0 <- qnorm(mean(lamh<lam_hat))

theta.jack <- numeric(n)
for(i in 1:n){
  theta.jack[i] <- lam(hours,-i)
}
bias <- mean(theta.jack) - theta.jack
a <- sum(bias^3)/(6 * sum(bias^2) ^(3/2))

alpha1 <- pnorm(z0 + (z0+qnorm(p = 1-0.05/2,lower.tail = FALSE))/(1-a*(z0+qnorm(p = 1-0.05/2,lower.tail = FALSE))))
alpha2 <- pnorm(z0 + (z0+qnorm(p = 0.05/2,lower.tail = FALSE))/(1-a*(z0+qnorm(p = 0.05/2,lower.tail = FALSE))))

print(quantile(lam_sort, c(alpha1, alpha2)))
## 8.825306% 99.78008% 
##  58.24471 212.94692
boot_time <- function(X, i) {
  my_mean <- mean(X[i])
  n <- length(i)
  my_var <- (n-1) * var(X[i]) / (n^2)
  return(c(my_mean, my_var))
}

# bootstrapping
set.seed(42)
results <- boot(hours, statistic = boot_time, R = 200)
boot.ci(results,type="all")
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 200 bootstrap replicates
## 
## CALL : 
## boot.ci(boot.out = results, type = "all")
## 
## Intervals : 
## Level      Normal              Basic             Studentized     
## 95%   ( 34.3, 174.1 )   ( 22.2, 169.8 )   ( 41.0, 279.3 )  
## 
## Level     Percentile            BCa          
## 95%   ( 46.3, 194.0 )   ( 56.9, 208.9 )  
## Calculations and Intervals on Original Scale
## Some basic intervals may be unstable
## Some studentized intervals may be unstable
## Some percentile intervals may be unstable
## Some BCa intervals may be unstable

置换检验

R函数cor 中提供了三种不同的相关性度量,其中 method="spearman" 提供的是Spearman秩相关性检验统计量。

  1. 产生一组样本量 \(n=20\) 的数据如下(种子设置为 set.seed(123)): \[X_i \sim N(0,1), \quad Y_i =\exp(X_i), \quad i=1,\dots,n.\]
set.seed(123)
n=20
X=rnorm(n)
Y=exp(X)
  1. 用置换检验方法计算上述样本的显著性水平,其中置换次数设置为199。

注意事项

r.obt <- cor(X, Y,method = "spearman")
nreps <- 199
r.random <- numeric(nreps)
for (i in 1:nreps) {
X.new <- sample(X, n, replace = FALSE)
r.random[i] <- cor(X.new,Y,method = "spearman")
}
prob <- length(r.random[r.random >= r.obt])/nreps
cat("Probability randomized r >= r.obt",prob)
## Probability randomized r >= r.obt 0
  1. 比较上述显著性水平和使用 cor.test函数得到的p值,可用表格汇总或者画图展示。

注意事项

nreps=199
reps <- numeric(nreps)
p1 <- vector()
p2 <- vector()
for(i in 1:100){
  set.seed(i)
  x <- rnorm(20)
  y <- exp(x)
  for(j in 1:nreps){
    z <- sample(y, size=20, replace=F)
    reps[j] <- cor(x,z,method="spearman")
  }
  p1[i] <- (length(reps[reps >= cor(x,y,method="spearman")]))/nreps
  p2[i] <- as.numeric(cor.test(x,y,method="spearman")$p.value)
}
plot(p1,p2)