R小推车

《Modern Statistics for Modern Bi

2019-03-07  本文已影响1人  热衷组培的二货潜

《Modern Statistics for Modern Biology》Chapter 一: 离散数据模型的预测(1.1 - 1.3)

《Modern Statistics for Modern Biology》Chapter 一: 离散数据模型的预测(1.4 - 1.5)

《Modern Statistics for Modern Biology》Chapter 二: 统计建模(2.1-2.3)

《Modern Statistics for Modern Biology》Chapter 二: 统计建模(2.4-2.5)

《Modern Statistics for Modern Biology》Chapter 二 统计建模(2.6 - 2.7)

从这章开始最开始记录一些markdown等小知识
$\hat{p}=\frac{1}{12}$\hat{p}=\frac{1}{12}
掌握R语言中的apply函数族
卡方检验
Hardy-Weinberg equilibrium( 哈迪-温伯格平衡 )
带你理解beta分布
简单介绍一下R中的几种统计分布及常用模型

  • 统计分布每一种分布有四个函数:d――density(密度函数),p――分布函数,q――分位数函数,r――随机数函数。比如,正态分布的这四个函数为dnorm,pnorm,qnorm,rnorm。下面我们列出各分布后缀,前面加前缀d、p、q或r就构成函数名:norm:正态t:t分布f:F分布chisq:卡方(包括非中心) unif:均匀exp:指数,weibull:威布尔,gamma:伽玛beta:贝塔 lnorm:对数正态,logis:逻辑分布,cauchy:柯西binom:二项分布geom:几何分布hyper:超几何nbinom:负二项pois:泊松 signrank:符号秩,wilcox:秩和tukey:学生化极差

2.8 序列依赖关系建模:马尔可夫链(Modeling sequential dependencies: Markov chains)

2.9 贝叶斯思维

单倍型

2.9.1 示例:单体型频率

> setwd("e://compute language/NGS/Modern Statistics for Modern Biology/data/")
> haplo6=read.table("haplotype6.txt",header = TRUE)
> haplo6
  Individual DYS19 DXYS156Y DYS389m DYS389n DYS389p
1         H1    14       12       4      12       3
2         H3    15       13       4      13       3
3         H4    15       11       5      11       3
4         H5    17       13       4      11       3
5         H7    13       12       5      12       3
6         H8    16       11       5      12       3

这表明单倍型H1DYS19位置有14个重复,在DXYS156Y位置有12个重复。通过使用这些Y-STR图谱创建的单倍型在同一宗法血统的男性之间共享。由于这些原因,两个不同的男人可能有相同的侧写。我们需要在感兴趣的人群中找到感兴趣的单倍型的潜在比例θ。我们将使用收集到的观测数据,将单倍型的出现视为二项分布中的“成功”.

2.9.2 二项式贝叶斯模型的模拟研究 (Simulation study of the Bayesian paradigm for the binomial)

Y的分布

> rtheta = rbeta(100000, 50, 350)
> y = vapply(rtheta, function(th) {
+   rbinom(1, prob = th, size = 300)
+ }, numeric(1))
> hist(y, breaks = 50, col = "orange", main = "", xlab = "")

问题

rtheta_rep <- rbinom(length(rtheta), rtheta, size = 300)
hist(rtheta_rep, breaks = 50, col = "blue", main = "rbinom", xlab = "")
除了颜色。几乎一毛一样

Histogram of all the thetas such that Y = 40 : the posterior distribution

> thetas = seq(0, 1, by = 0.001)
> thetaPostEmp = rtheta[ y == 40 ]
> hist(thetaPostEmp, breaks = 40, col = "chartreuse4", main = "",
+   probability = TRUE, xlab = expression("posterior"~theta))
> densPostTheory  =  dbeta(thetas, 90, 610)
> lines(thetas, densPostTheory, type="l", lwd = 3)
图 2.19
> mean(thetaPostEmp)
[1] 0.1287488
> dtheta = thetas[2]-thetas[1]
> sum(thetas * densPostTheory * dtheta)
[1] 0.1285714
> thetaPostMC = rbeta(n = 1e6, 90, 610)
> mean(thetaPostMC)
[1] 0.1285824
> qqplot(thetaPostMC, thetaPostEmp, type = "l", asp = 1)
> abline(a = 0, b = 1, col = "blue")
图 2.20

问题

后验分布也是β分布 (Posterior distribution is also a beta)

Suppose we had a second series of data
在看到我们以前的数据后,我们现在有了一个新的先前版本,beta(90,610)

现在,我们收集了一组新的数据,其中n=150次观测,y=25次成功,即125次失败
那么我们对θ的最佳猜测是什么呢?

> thetas = seq(0, 1, by = 0.001)
> densPost2 = dbeta(thetas, 115, 735)
> mcPost2   = rbeta(1e6, 115, 735)
> sum(thetas * densPost2 * dtheta)  # mean, by numeric integration
[1] 0.1352941
> mean(mcPost2)                     # mean, by MC
[1] 0.1352917
> thetas[which.max(densPost2)]      # MAP estimate
[1] 0.134

问题

比例参数的置信度声明

> quantile(mcPost2, c(0.025, 0.975))
     2.5%     97.5% 
0.1131668 0.1590122 

又是一章头大的统计。

上一篇 下一篇

猜你喜欢

热点阅读