设为首页收藏本站获取积分奖励

R语言中文网

 找回密码
 立即注册
查看: 7897|回复: 6

[实例投稿提交] 自助法示例

[复制链接]
发表于 2014-6-29 16:03:41 | 显示全部楼层 |阅读模式
  1. n <- 1000 #样本数据集数量
  2. B <- 1000 # boot.samples
  3. alpha <- 0.05 #置信概率
  4. df <- 6
  5. true.mu <- 0 # 真是均值
  6. true.sd <- 1 # 真实标准差

  7. # 模拟数据生成
  8. x <- rnorm(n,mean = true.mu, sd = true.sd)

  9. sample.mean.x <- mean(x) #样本均值
  10. sample.sd.x <- sd(x) #样本标准差
  11. sample.se.x <- sample.sd.x/sqrt(n) #样本标准误
  12. sample.conf.x <- sample.mean.x + c(-1,1)*qnorm(alpha/2,lower.tail=F)*sample.se.x #样本95%置信区间

  13. # 为自助法预留缓存向量
  14. boot.x <- vector(mode="numeric",length=B)
  15. boot.mean <- vector(mode="numeric",length=B)
  16. boot.sd <- vector(mode="numeric",length=B)
  17. boot.t <- vector(mode="numeric",length=B)

  18. for(i in 1:B)
  19. {
  20.   boot.x <- sample(x, size=n, replace=T)
  21.   boot.mean[i] <- mean(boot.x)
  22.   boot.sd[i] <- sd(boot.x)
  23.   boot.t[i] <- (sample.mean.x-boot.mean[i])/(boot.sd[i]/sqrt(n))
  24. }

  25. # bootstrap 统计量
  26. boot.mean.x <- mean(boot.mean)
  27. boot.sd.x <- mean(boot.sd)
  28. boot.se.x <- boot.sd.x/sqrt(B)
  29. boot.t.quantile <- quantile(boot.t,probs = c(alpha/2,1-alpha/2))
  30. boot.mean.quantile <- quantile(boot.mean,probs = c(alpha/2,1-alpha/2))

  31. boot.conf.x <- boot.mean.x + c(-1,1)*qt(alpha/2,df=B-1,lower.tail=F)*boot.se.x
  32. boot.conf.x2 <- boot.mean.x + boot.t.quantile*boot.se.x

  33. true.conf.x <- 2*sample.mean.x - c(boot.mean.quantile[2],boot.mean.quantile[1])

  34. #boot.mean.x
  35. #boot.sd.x
  36. #boot.se.x
  37. #boot.conf.x
  38. #boot.conf.x2

  39. #true.conf.x

  40. #sample.mean.x
  41. #sample.sd.x
  42. #sample.se.x
  43. #sample.conf.x


  44. hist(boot.mean, col = "lightblue", border = "pink",freq = F, main="Normal distribution")
  45. lines(density(boot.mean),col="blue", lwd=2)
  46. abline(v=0, col="red",lwd=2)

  47. message("boot.mean.x = ", format(boot.mean.x,digits = 4),"           sample.mean.x = ", format(sample.mean.x,digits = 4),"\n",
  48.         "boot.sd.x = ", format(boot.sd.x,digits = 4),"               sample.sd.x = ", format(sample.sd.x,digits = 4),"\n",
  49.         "boot.conf.x = ", format(boot.conf.x,digits = 4),"      sample.conf.x = ", format(sample.conf.x,digits = 4),"\n",
  50.         "boot.conf.x2 = ", format(boot.conf.x2,digits = 4),"      true.conf.x = ", format(true.conf.x,digits = 4),"\n")

复制代码
这段代码主要简单得阐述了bootstrap技术,并对比了在bootstrap统计量和样本统计量以及(真实)统计量的差异。
回复

使用道具 举报

发表于 2014-8-20 08:54:51 | 显示全部楼层
不错哦 ,么么哒
回复 支持 反对

使用道具 举报

发表于 2015-1-24 12:46:13 | 显示全部楼层
回复 支持 反对

使用道具 举报

发表于 2015-5-15 13:47:17 | 显示全部楼层
bootstrap和蒙特卡洛间的区别大不大?
回复 支持 反对

使用道具 举报

发表于 2016-3-17 21:22:13 | 显示全部楼层
这个的确不错啊,把统计学知识和机器学习结合起来,值得学习哈。
回复 支持 反对

使用道具 举报

发表于 2017-11-21 00:42:59 | 显示全部楼层
您好~~~这个用于对比的truemean应该是sample的mean还是总体population的mean呀?用不用先rnorm一个总体,在从总体里抽样出来作为样本(sample)?正在做一项相关的作业,这里感到很困惑。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|Archiver|R语言中文网    

GMT+8, 2024-5-1 10:52 , Processed in 0.024098 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表