バイオインフォマティクス特論
1 パラメータ推定
1-3. データ解析の例
1-3. データ解析の例
1-3-1. ピアソン相関係数
1-3-2. ⼀致性のカッパ係数
1-3-1. ピアソン相関係数
「ベイズ統計で実践モデリング」
第5章 データ解析の例
5.1 ピアソン係数
データはnペアの独⽴な観測値の対
例:特定の
薬剤の投与量
と投与からt 時間後の注⽬する
遺伝⼦の発現量
2つの変数間の線形の関係性は
ピアソンの積率相関係数r
で表現される
薬剤の投与量 t時間後の 注⽬する 遺伝⼦の 発現量通常は相関係数rとその有意性(無相関の検定)
が記述される。
相関係数の事後分布を求めることで、⼀つの
数字で関係性を現すだけでなく、相関のレベル
がどの程度あるのか(信⽤区間)を求めること
ができる。
問題をグラフィカルモデルで表現
x
i
i番⽬のデータ
μ
r
σ
σ
μ
11, σ
, μ
22~ InvGamma(0.001, 0.001)
~ Gaussian(0, 0.001)
r ~ Uniform(-1, 1)
x
i~
MvGaussian
𝜇
",𝜇
$,
𝜎
"$𝑟𝜎
"𝜎
$𝑟𝜎
"𝜎
$𝜎
$$ '"尤度
i番⽬の投与量x
i[1]と、t時間後の発現量x
i[2]
x
i= (x
i[1], x
i[2])
t平均ベクトル μ = (μ
1, μ
2)
t分散共分散行列
Σ
=
𝜎
"
$
𝑟𝜎
"
𝜎
$
𝑟𝜎
"
𝜎
$
𝜎
$
$
(
1
2𝜋
$
Σ
exp −
1
2
𝑥
2
− 𝜇
3
Σ
'"
𝑥
2
− 𝜇
4
25"
事前分布
μ
1, μ
2~ Gaussian(0, 0.001)
σ
1, σ
2~ InvGamma(0.001, 0.001)
r ~ Uniform(-1, 1)
後者は「7⼈の科学者」問題と同様、
λ=1/σ
2が
ガンマ分布
に
従うと仮定することに相当する。
Jagsでのモデルの記述
Correlation1.txt
# Pearson Correlation
model{
# Data
for (i in 1:n){
x[i,1:2] ~
dmnorm
(mu[],TI[,])
}
# Priors
mu[1] ~ dnorm(0,.001)
mu[2] ~ dnorm(0,.001)
lambda[1] ~ dgamma(.001,.001)
lambda[2] ~ dgamma(.001,.001)
r ~ dunif(-1,1)
# Reparameterization
sigma[1] <- 1/sqrt(lambda[1])
sigma[2] <- 1/sqrt(lambda[2])
T[1,1] <- 1/lambda[1]
T[1,2] <- r*sigma[1]*sigma[2]
T[2,1] <- r*sigma[1]*sigma[2]
T[2,2] <- 1/lambda[2]
TI[1:2,1:2] <- inverse(T[1:2,1:2])
}
R2jagsによるMCMCサンプリング
Correlation_1_jags.R
# clears workspace:rm(list=ls())
# sets working directories:
setwd("C:/Users/EJ/Dropbox/EJ/temp/BayesBook/test/ParameterEstimation/DataAnalysis") library(R2jags) # Choose a dataset: dataset <- 1 # The datasets: if (dataset == 1) { x <- matrix(c(.8,102, 1,98, .5,100, 0.9,105, .7,103, 0.4,110, 1.2,99, 1.4,87, 0.6,113, 1.1,89, 1.3,93), nrow=11,ncol=2,byrow=T) } if (dataset == 2) { x <- matrix(c(.8,102, 1,98, .5,100, 0.9,105, .7,103, 0.4,110, 1.2,99, 1.4,87, 0.6,113, 1.1,89, 1.3,93, .8,102, 1,98, .5,100, 0.9,105, .7,103, 0.4,110, 1.2,99, 1.4,87, 0.6,113, 1.1,89, 1.3,93), nrow=22,ncol=2,byrow=T) }
2種の観測データ
の⼀⽅を選択
n <- nrow(x) # number of people/units measured data <- list("x", "n") # to be passed on to JAGS myinits <- list(
list(r = 0, mu = c(0,0), lambda = c(1,1))) # parameters to be monitored:
parameters <- c("r", "mu", "sigma")
# The following command calls JAGS with specific options. # For a detailed description see the R2jags documentation. samples = jags(data, inits=myinits, parameters,
model.file ="Correlation_1.txt", n.chains=1, n.iter=5000, n.burnin=1, n.thin=1, DIC=T)
# Now the values for the monitored parameters are in the "samples" object, # ready for inspection.
#Frequentist point-estimate of r:
freq.r <- cor(x[,1],x[,2])
layout(matrix(c(1,2),1,2))
layout.show(2)
#some plotting options to make things look better:
par(cex.main = 1.5, mar = c(5, 6, 4, 5) + 0.1, mgp = c(3.5, 1, 0),
cex.lab = 1.5,
font.lab = 2, cex.axis = 1.3, bty = "n", las=1)
# data panel:
plot(x[,1],x[,2], type="p", pch=19, cex=1)
# correlation panel:
plot(density(r, from=-1,to=1), main="", ylab="Posterior Density",
xlab="Correlation", lwd=2)
> print(freq.r)
[1] -0.8109671
> summary(r)
V1
Min. :-0.9759
1st Qu.:-0.8184
Median :-0.7379
Mean :-0.7057
3rd Qu.:-0.6272
Max. : 0.1336
1-3-2. 一致性のカッパ係数
「ベイズ統計で実践モデリング」
第5章 データ解析の例
5.2 ⼀致性のカッパ係数
カッパ係数
2つの⽅法が、どの程度同じ判断を与えるか
ただし判断は2値(1 or 0)
Poehling, Griffin and Dittus (2002)
233名の⼦供に対してインフルエンザの臨床検査
⽅法1と⽅法2の両⽅で感染しているとされたもの
14名 (1, 1)
⽅法1では感染していることとされたが⽅法2では診断できなかったもの
4名 (1, 0)
⽅法1では感染していないとされたが、⽅法2では感染しているとされたもの
5名 (0, 1)
⽅法1と⽅法2の両⽅で感染していないとされたもの
210名 (0, 0)
⽅法1は⾼価で、⽅法2は安価であるような場合に⽐較する(1, 1)の数を a (1, 0)の数を b (0, 1)の数を c (0, 0)の数を d 観測された⼀致度
𝑝
7
=
89:
4
偶然のみから期待される⼀致度𝑝
;
=
89< 89= 9 <9: =9:
4
> ※ (a + b)/n : ⽅法1が1である頻度 (a + c)/n : ⽅法2が 1である頻度 (b + d)/n : ⽅法2が0である頻度 (c + d)/n : ⽅法1が0である頻度 a + b + c + d = nカッパ係数 𝜅 =
A
B'A
C"'A
C -1 (a=d=0, b=c=n/2)から1(a+d=n, b=c=0)までの値をとる経験的には0.4未満を偶然に⽐べて芳しくなく、0.4〜0.75は偶然よりもまあまあ良い、 0.75以上は偶然よりも優れた⼀致度されてきた
問題をグラフィカルモデルで表現
ξ
κ
ψ
π
aπ
bπ
cπ
dy
α
β
γ
yは観測データa, b, c, d
α :⽅法1が1と判定する⽐率
(1-α):⽅法1が 0と判定する⽐率
β :⽅法1が1と判定した時に
⽅法2も1と判定する⽐率
γ :⽅法1が0と判定する時に
⽅法2も0と判定する⽐率
問題をグラフィカルモデルで表現
ξ
κ
ψ
π
aπ
bπ
cπ
dy
α
β
γ
yは観測データa, b, c, d
α :⽅法1が1と判定する⽐率
(1-α):⽅法1が 0と判定する⽐率
β :⽅法1が1と判定した時に
⽅法2も1と判定する⽐率
γ :⽅法1が0と判定する時に
⽅法2も0と判定する⽐率
π
a=αβ、π
b=α(1ーβ)
π
c=(1-α)(1-γ)、π
d=(1-α)γ
問題をグラフィカルモデルで表現
ξ
κ
ψ
π
aπ
bπ
cπ
dy
α
β
γ
yは観測データa, b, c, d
α :⽅法1が1と判定する⽐率
(1-α):⽅法1が 0と判定する⽐率
β :⽅法1が1と判定した時に
⽅法2も1と判定する⽐率
γ :⽅法1が0と判定する時に
⽅法2も0と判定する⽐率
ξ=αβ+(1-α)γ ψ=D
E9D
FD
E9D
G+
D
F9D
HD
G9D
H𝜅 =
𝜉 − 𝜓
1 − 𝜓
尤度
多項分布で尤度は表現される
𝑛!
事前分布
α, β, γ ~ Beta(1,1)
x <- seq(0, 1, 0.01)
Jagsでのモデルの記述
Kappa.txt
# Kappa Coefficient of Agreement
model{
# Underlying Rates
# Rate Objective Method Decides "one"
alpha ~ dbeta(1,1)
# Rate Surrogate Method Decides "one" When Objective Method Decides "one"
beta ~ dbeta(1,1)
# Rate Surrogate Method Decides "zero" When Objective Method Decides "zero"
gamma ~ dbeta(1,1)
# Probabilities For Each Count
pi[1] <- alpha*beta
pi[2] <- alpha*(1-beta)
pi[3] <- (1-alpha)*(1-gamma)
pi[4] <- (1-alpha)*gamma
# Count Data
y[1:4] ~ dmulti(pi[],n)
# Derived Measures
# Rate Surrogate Method Agrees With the Objective Method
xi <- alpha*beta+(1-alpha)*gamma
# Rate of Chance Agreement
psi <- (pi[1]+pi[2])*(pi[1]+pi[3])+(pi[2]+pi[4])*(pi[3]+pi[4])
# Chance-Corrected Agreement
kappa <- (xi-psi)/(1-psi)
}
R2jagsによるMCMCサンプリング
Kappa_jags.R
# clears workspace: rm(list=ls())
# sets working directories:
setwd("C:/Users/EJ/Dropbox/EJ/temp/BayesBook/test/ParameterEstimation/DataA nalysis")
library(R2jags)
# choose a data set: # Influenza
y <- c(14, 4, 5, 210)
n <- sum(y) # number of people/units measured data <- list("y", "n") # to be passed on to JAGS
myinits <- list(
list(alpha = 0.5, beta = 0.5, gamma = 0.5)) # parameters to be monitored:
parameters <- c("kappa","xi","psi","alpha","beta","gamma","pi") # The following command calls JAGS with specific options.
# For a detailed description see the R2jags documentation. samples <- jags(data, inits=myinits, parameters,
model.file ="Kappa.txt", n.chains=1, n.iter=2000, n.burnin=1, n.thin=1, DIC=T)
# Now the values for the monitored parameters are in the "samples" object, # ready for inspection.
kappa <- samples$BUGSoutput$sims.list$kappa plot(kappa[,1],ty='l') summary(kappa) V1 Min. :0.3758 1st Qu.:0.6485 Median :0.7061 Mean :0.6998 3rd Qu.:0.7608 Max. :0.9171 quantile(kappa, c(0.025, 0.975)) 2.5% 97.5% 0.5125542 0.8481686
Cohenのκの点推定値
p0 <- (y[1]+y[4])/npe <- (((y[1]+y[2]) * (y[1]+y[3])) + ((y[2]+y[4]) * (y[3]+y[4]))) / n^2 kappa.Cohen <- (p0-pe) / (1-pe)
kappa.Cohen [1] 0.7357944
pi <- samples$BUGSoutput$sims.list$pi summary(pi[,1])
Min. 1st Qu. Median Mean 3rd Qu. Max. 0.02277 0.05033 0.05986 0.06094 0.07028 0.13221 summary(pi[,2])
Min. 1st Qu. Median Mean 3rd Qu. Max. 0.002252 0.013389 0.018521 0.019999 0.024689 0.075621 summary(pi[,3])
Min. 1st Qu. Median Mean 3rd Qu. Max. 0.003851 0.017819 0.024143 0.025250 0.031550 0.072737 summary(pi[,4])
Min. 1st Qu. Median Mean 3rd Qu. Max. 0.8075 0.8811 0.8947 0.8938 0.9077 0.9544
π
a, π
b, π
c, π
dの⽐較
14/(14+4+5+210) [1] 0.06008584 4/(14+4+5+210) [1] 0.01716738 5/(14+4+5+210) [1] 0.02145923 210/(14+4+5+210) [1] 0.90128761-3-3. 時系列データにおける変化検出
「ベイズ統計で実践モデリング」
第5章 データ解析の例
5.3 時系列データにおける変化検出
時系列データにおける変化検出
近⾚外線分光データ
注意⽋陥多動性障害(ADHD)の成⼈の注意課題時の前頭葉の活動を酸化ヘモグロビンのカウ
ントによって測定
注意課題によってカウントの時系列に変化が⽣じる
この
変化点を同定
したい
1178点の時系列データ
観測データは時刻t
iでのカウントc
iである。
観測されていない変数τは変化が起こる時点
カウントは、ガウス分布に従う。分散は同じだが、
時点τで平均がμ
1からμ
2に変化する
問題をグラフィカルモデルで表現
i番⽬のサンプル
c
i
t
i
τ
μ
1λ
μ
2𝜇
𝜆 ~ Gamma(0.001, 0.001)
", 𝜇
$~ 𝐺𝑎𝑢𝑠𝑠𝑖𝑎𝑛 0,0.001
𝜏 ~ 𝑈𝑛𝑖𝑓𝑜𝑟𝑚(0, 𝑡
b8c)
𝑐
2~e
𝐺𝑎𝑢𝑠𝑠𝑖𝑎𝑛 𝜇
",𝜆 𝑖𝑓 𝑡
2< 𝜏
𝐺𝑎𝑢𝑠𝑠𝑖𝑎𝑛 𝜇
$,𝜆 𝑖𝑓 𝑡
2≥ 𝜏
尤度
( 𝑑𝑛𝑜𝑟𝑚 𝑥
2
, 𝜇
"
, 𝜆
3
hij
( 𝑑𝑛𝑜𝑟𝑚 𝑥
2
, 𝜇
$
, 𝜆
事前分布
𝜇
"
, 𝜇
$
~ 𝐺𝑎𝑢𝑠𝑠𝑖𝑎𝑛 0, 0.001
𝜆 ~ Gamma(0.001, 0.001)
𝜏 ~ 𝑈𝑛𝑖𝑓𝑜𝑟𝑚(0, 𝑡
b8c
)
Jagsでのモデルの記述
ChangeDetection.txt
model{
# Data Come From A Gaussian
for (i in 1:n){
c[i] ~ dnorm(mu[z1[i]],lambda)
}
# Group Means
mu[1] ~ dnorm(0,.001)
mu[2] ~ dnorm(0,.001)
# Common Precision
lambda ~ dgamma(.001,.001)
sigma <- 1/sqrt(lambda)
# Which Side is Time of Change Point?
for (i in 1:n){
z[i] <-
step(t[i]-tau
)
z1[i] <- z[i]+1
}
# Prior On Change Point
tau ~ dunif(0,n)
}
step関数
引数が負ならば0
0以上であれば1を返す関数
配列zは0か1が⼊る
配列z1でこれに1を⾜して1か2が⼊る形にし、
これはデータが従うガウス分布の平均の添字にして、
平均の時点τでの切り替えを表現している
R2jagsによるMCMCサンプリング
ChangeDetection_jags.R
# clears workspace:
rm(list=ls())
# sets working directories:
setwd("/Users/toh/Desktop/Code/ParameterEstimation/DataAnalysis")
library(R2jags)
c <- scan("changepointdata.txt")
n <- length(c)
t <- 1:n
data <- list("c", "n", "t") # to be passed on to JAGS
myinits <- list(
# parameters to be monitored:
parameters <- c("mu","sigma","tau")
# The following command calls JAGS with specific options. # For a detailed description see the R2jags documentation. samples <- jags(data, inits=myinits, parameters,
model.file ="ChangeDetection.txt", n.chains=1, n.iter=1000, n.burnin=1, n.thin=1, DIC=T)
# Now the values for the monitored parameters are in the "samples" object, # ready for inspection.
最初の200ステップは burn inとして捨てるべき
samples <- jags(data, inits=myinits, parameters,
model.file ="ChangeDetection.txt", n.chains=1, n.iter=1000, n.burnin=1, n.thin=1, DIC=T)
samples <- jags(data, inits=myinits, parameters,
model.file ="ChangeDetection.txt", n.chains=1, n.iter=1000, n.burnin=200, n.thin=1, DIC=T)
tau <- samples$BUGSoutput$sims.list$tau
summary(tau)
V1
Min. :726.2
1st Qu.:731.0
Median :731.6
Mean :732.3
3rd Qu.:733.7
Max. :743.1
時点732あたりで切り替わっている
mu <- samples$BUGSoutput$sims.list$mu
summary(mu[,1])
Min. 1st Qu. Median Mean 3rd Qu. Max.
37.20 37.72 37.89 37.89 38.10 38.66
summary(mu[,2])
Min. 1st Qu. Median Mean 3rd Qu. Max.
29.68 30.38 30.59 30.59 30.79 31.51
τ=732の前後のカウント数でt検定
t.test(c[1:731],c[732:1178],var.equal=T) Two Sample t-test
data: c[1:731] and c[732:1178]
t = 17.861, df = 1176, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval:
6.528340 8.139576 sample estimates: mean of x mean of y