bioinfo-jags2

Similar documents
バイオインフォマティクス特論12

バイオインフォマティクス特論4

kubo2015ngt6 p.2 ( ( (MLE 8 y i L(q q log L(q q 0 ˆq log L(q / q = 0 q ˆq = = = * ˆq = 0.46 ( 8 y 0.46 y y y i kubo (ht

12/1 ( ) GLM, R MCMC, WinBUGS 12/2 ( ) WinBUGS WinBUGS 12/2 ( ) : 12/3 ( ) :? ( :51 ) 2/ 71

2014ESJ.key

Microsoft PowerPoint - R-stat-intro_20.ppt [互換モード]

kubostat1g p. MCMC binomial distribution q MCMC : i N i y i p(y i q = ( Ni y i q y i (1 q N i y i, q {y i } q likelihood q L(q {y i } = i=1 p(y i q 1

日心TWS

k2 ( :35 ) ( k2) (GLM) web web 1 :

講義のーと : データ解析のための統計モデリング. 第5回

講義のーと : データ解析のための統計モデリング. 第3回

Stanによるハミルトニアンモンテカルロ法を用いたサンプリングについて

kubostat2018d p.2 :? bod size x and fertilization f change seed number? : a statistical model for this example? i response variable seed number : { i

kubo2017sep16a p.1 ( 1 ) : : :55 kubo ( ( 1 ) / 10

スライド 1

「統 計 数 学 3」

kubostat2015e p.2 how to specify Poisson regression model, a GLM GLM how to specify model, a GLM GLM logistic probability distribution Poisson distrib

講義のーと : データ解析のための統計モデリング. 第2回

DAA04

DAA03

様々なミクロ計量モデル†

Probit , Mixed logit

スライド 1

k3 ( :07 ) 2 (A) k = 1 (B) k = 7 y x x 1 (k2)?? x y (A) GLM (k

統計学 - 社会統計の基礎 - 正規分布 標準正規分布累積分布関数の逆関数 t 分布正規分布に従うサンプルの平均の信頼区間 担当 : 岸 康人 資料ページ :

一般化線形 (混合) モデル (2) - ロジスティック回帰と GLMM

情報工学概論

kubostat2017c p (c) Poisson regression, a generalized linear model (GLM) : :

tnbp59-21_Web:P2/ky132379509610002944

kubostat2017b p.1 agenda I 2017 (b) probability distribution and maximum likelihood estimation :

1 R Windows R 1.1 R The R project web R web Download [CRAN] CRAN Mirrors Japan Download and Install R [Windows 9

/22 R MCMC R R MCMC? 3. Gibbs sampler : kubo/


@i_kiwamu Bayes - -

60 (W30)? 1. ( ) 2. ( ) web site URL ( :41 ) 1/ 77

したがって このモデルではの長さをもつ潜在履歴 latent history が存在し 同様に と指標化して扱うことができる 以下では 潜在的に起こりうる履歴を潜在履歴 latent history 実際にデ ータとして記録された履歴を記録履歴 recorded history ということにする M

¥¤¥ó¥¿¡¼¥Í¥Ã¥È·×¬¤È¥Ç¡¼¥¿²òÀÏ Âè2²ó

kubostat2017e p.1 I 2017 (e) GLM logistic regression : : :02 1 N y count data or

浜松医科大学紀要

DAA02

NLMIXED プロシジャを用いた生存時間解析 伊藤要二アストラゼネカ株式会社臨床統計 プログラミング グループグルプ Survival analysis using PROC NLMIXED Yohji Itoh Clinical Statistics & Programming Group, A

¥¤¥ó¥¿¡¼¥Í¥Ã¥È·×¬¤È¥Ç¡¼¥¿²òÀÏ Âè2²ó

Rによる計量分析:データ解析と可視化 - 第3回 Rの基礎とデータ操作・管理

第101回 日本美容外科学会誌/nbgkp‐01(大扉)

27巻3号/FUJSYU03‐107(プログラム)

パーキンソン病治療ガイドライン2002

本文27/A(CD-ROM

tnbp59-20_Web:P1/ky108679509610002943

Microsoft Word - 計量研修テキスト_第5版).doc

スライド 1

症例数設定? What is sample size estimation? 医療機器臨床試験のコンサルティングで最も相談件数が多いのは 症例数の設定 Many a need of consulting for device clinical trial is sample size estimat

今日の要点 あぶない 時系列データ解析は やめましょう! 統計モデル のあてはめ (危 1) 時系列データの GLM あてはめ (危 2) 時系列Yt 時系列 Xt 各時刻の個体数 気温 とか

スライド 1

ベイズ統計入門

こんにちは由美子です

kubostat2017j p.2 CSV CSV (!) d2.csv d2.csv,, 286,0,A 85,0,B 378,1,A 148,1,B ( :27 ) 10/ 51 kubostat2017j (

<30315F985F95B65F90B490852E696E6464>

(lm) lm AIC 2 / 1

はじめに このドキュメントではftServerに関する障害調査を行う際に 必要となるログ データの取得方法を説明しています ログ データの取得には 初期解析用のデータの取得方法と 詳細な調査を行うときのデータ取得方法があります 特別な理由でOS 側のログが必要となった場合には RHELログの取得につ

Fig. 3 Flow diagram of image processing. Black rectangle in the photo indicates the processing area (128 x 32 pixels).

Microsoft Word doc

Microsoft Word - Meta70_Preferences.doc

Use R

Microsoft PowerPoint - 14回パラメータ推定配布用.pptx

tnbp59-17_Web:プO1/ky079888509610003201

Microsoft PowerPoint - 統計科学研究所_R_重回帰分析_変数選択_2.ppt

& 3 3 ' ' (., (Pixel), (Light Intensity) (Random Variable). (Joint Probability). V., V = {,,, V }. i x i x = (x, x,, x V ) T. x i i (State Variable),

Microsoft PowerPoint - stat-2014-[9] pptx

講義「○○○○」

スライド 1

10

2016 年熊本地震の余震の確率予測 Probability aftershock forecasting of the M6.5 and M7.3 Kumamoto earthquakes of 2016 東京大学生産技術研究所統計数理研究所東京大学地震研究所 Institute of Indus

RとExcelを用いた分布推定の実践例

1 Stata SEM LightStone 4 SEM 4.. Alan C. Acock, Discovering Structural Equation Modeling Using Stata, Revised Edition, Stata Press 3.

untitled

kubostat7f p GLM! logistic regression as usual? N? GLM GLM doesn t work! GLM!! probabilit distribution binomial distribution : : β + β x i link functi


1

生命情報学

Microsoft Word - 第56回日本脂質生化学会プログラムv1.doc


Microsoft Word - Time Series Basic - Modeling.doc

塗装深み感の要因解析


自由集会時系列part2web.key

AD8212: 高電圧の電流シャント・モニタ

日立金属技報 Vol.34

青焼 1章[15-52].indd

JOURNAL OF THE JAPANESE ASSOCIATION FOR PETROLEUM TECHNOLOGY VOL. 66, NO. 6 (Nov., 2001) (Received August 10, 2001; accepted November 9, 2001) Alterna

今回 次回の要点 あぶない 時系列データ解析は やめましょう! 統計モデル のあてはめ Danger!! (危 1) 時系列データの GLM あてはめ (危 2) 時系列Yt 時系列 Xt 各時刻の個体数 気温 とか これは次回)

2 ( ) i

スライド 1

Microsoft PowerPoint - SPECTPETの原理2012.ppt [互換モード]

Ł\”ƒ-2005

r z m ε r ε θ z rθ

C プログラミング演習 1( 再 ) 2 講義では C プログラミングの基本を学び 演習では やや実践的なプログラミングを通して学ぶ

ECCS. ECCS,. ( 2. Mac Do-file Editor. Mac Do-file Editor Windows Do-file Editor Top Do-file e

Introduction Purpose This training course describes the configuration and session features of the High-performance Embedded Workshop (HEW), a key tool

UL 746A 第 6 版の短期特性評価に関する規格について 2016 年 4 月 29 日付で比較トラッキング指数試験 (CTI) 傾斜面トラッキング試験 (IPT) 及びホットワイヤー着火試験 (HWI) について一部改定がありました 以下 参考和訳をご参照ください なお 参考和訳と原文 ( 英

udc-2.dvi

Transcription:

藤 博幸

ベイズ統計で実践モデリング では MCMC による パラメータ推定 と モデル選択 が われている この講義でもその順番で進める

1-1-1. 率を推定する 1-1-2. 率の差 1-1-3. 共通の 率を推論する

ベイズ統計で実践モデリング 3.1 率を推定する 第 3 章 項分布を使った推論

n 回コイントスをして k 回表が出たコインの表が出る割合 θ を求める θ の事後分布を求める

θ コインは θ の割合で表が出る k 回表が観測 k n n 回コイントス

連続値 離散値 観測変数 ( 確率変数 ) 観測変数 依存関係

θ k n θ が与えられた時 n 回のコイントス中 k 回表が出る確率 ( 尤度 ) は 項分布に従う k~b n, θ = n! k! n k! θ* 1 θ,-* θ がどのような値をとるのかの情報がない 様分布を無情報事前分布として いるベータ分布 Beta(1,1) は 様分布になる θ θ~beta(1,1)

beta x; α, β = 9:;< =-9 >;< B(α, β) はベータ関数パラメータ α, β は正の実数? @,A (0 x 1) Rでベータ分布の密度関数をパラメータの値を変えてプロットしてみる code2.1.r

x <- seq(0,1.0, 0.01) alpha <- 5 beta <- 1 y1 <- dbeta(x, alpha, beta) alpha <- 1 beta <- 3 y2 <- dbeta(x, alpha, beta) alpha <- 2 beta <- 2 y3 <- dbeta(x, alpha, beta) alpha<- 1 beta <- 1 y4 <- dbeta(x, alpha, beta) plot(x, y1, ty='l') par(new=t) plot(x, y2, col="red", ty='l', ylim=c(0,2.5), yaxt="n") par(new=t) plot(x, y3, col="yellow", ty='l', ylim=c(0,2.5), yaxt="n") par(new=t) plot(x, y4, col="green", ty='l', ylim=c(0,2.5), yaxt="n")

Jags でのモデルの記述 Rate_1.txt 事前分布 # Inferring a Rate model{ # Prior Distribution for Rate Theta theta ~ dbeta(1,1) # Observed Counts k ~ dbin(theta,n) 尤度 } θ k n

Rate_1_jags.R 変数のクリア # clears workspace: rm(list=ls()) Rate_1.txt と Rate_1_jags.R のあるディレクトリへの移動 # sets working directories: setwd("/users/toh/desktop/code/parameterestimation/binomial") library(r2jags) パッケージの読み込み k <- 5 n <- 10 data <- list("k", "n") # to be passed on to JAGS 観測変数の設定

θ の初期値の設定 myinits <- list( list(theta = 0.1), #chain 1 starting value list(theta = 0.9)) #chain 2 starting value # parameters to be monitored: parameters <- c("theta") モニターするパラメータの設定 # The following command calls JAGS with specific options. # For a detailed description see the R2jags documentation. samples <- jags(data, inits=myinits, parameters, model.file ="Rate_1.txt", n.chains=2, n.iter=20000, n.burnin=1, n.thin=1, DIC=T) Jags を呼び出して θ をサンプリング

samples <- jags(data, inits=myinits, parameters, model.file ="Rate_1.txt", n.chains=2, n.iter=20000, n.burnin=1, n.thin=1, DIC=T) data: 観測値 inits: 初期値 parameters: モニタするパラメータ model.file: Jagsのモデルの指定 n.chains: 2 回 MCMC 異なる初期値からMCMCを行う n.iter:1 回のMCMCのサンプリングの回数 n.burnin: burn inのサイズ n.thin: thiningのサイズ DIC: 逸脱情報量基準 (Deviance Information Criterion) モデル選択の時に用いるが ベイズ統計家でも必ずしも受け入れられている訳ではない TRUEで計算される

# The commands below are useful for a quick overview: print(samples) # a rough summary traceplot(samples) # traceplot (press <enter> repeatedly to see the chains) # Collect posterior samples across all chains: theta <- samples$bugsoutput$sims.list$theta # Now let's plot a histogram for theta. # First, some options to make the plot 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) Nbreaks <- 80 y <- hist(theta, Nbreaks, plot=f) mixing の確認 2 つの chain でサンプルされた θ を つにまとめる θ の事後分布の表 plot(c(y$breaks, max(y$breaks)), c(0,y$density,0), type="s", lwd=2, lty=1, xlim=c(0,1), ylim=c(0,10), xlab="rate", ylab="posterior Density") # NB. ylim=c(0,10) defines the range of the y-axis. Adjust the upper value # in case your posterior distribution falls partly outside this range. max(c(samples$bugsoutput$sims.array[,1,][,2], samples$bugsoutput$sims.array[,2,][1,2])) min(c(samples$bugsoutput$sims.array[,1,][,2], samples$bugsoutput$sims.array[,2,][1,2])) summary(c(samples$bugsoutput$sims.array[,1,][,2], samples$bugsoutput$sims.array[,2,][,2 2 つの chain それぞれについての解析

> print(samples) # a rough summary Inference for Bugs model at "Rate_1.txt", fit using jags, 2 chains, each with 20000 iterations (first 1 discarded) n.sims = 39998 iterations saved mu.vect sd.vect 2.5% 25% 50% 75% 97.5% Rhat n.eff theta 0.500 0.138 0.236 0.403 0.500 0.598 0.764 1.001 40000 deviance 3.658 1.214 2.805 2.890 3.192 3.929 7.115 1.001 18000 For each parameter, n.eff is a crude measure of effective sample size, and Rhat is the potential scale reduction factor (at convergence, Rhat=1). DIC info (using the rule, pd = var(deviance)/2) pd = 0.7 and DIC = 4.4 2 つの chain をまとめた θ の要約

traceplot は R2jags の関数

> summary(theta) V1 Min. :0.05863 1st Qu.:0.40288 Median :0.50008 Mean :0.50032 3rd Qu.:0.59791 Max. :0.94687 chain1 の θ chain2 の θ > max(c(samples$bugsoutput$sims.array[,1,][,2], samples$bugsoutput$sims.array[,2,][1,2])) [1] 0.9468675 > min(c(samples$bugsoutput$sims.array[,1,][,2], samples$bugsoutput$sims.array[,2,][,2])) [1] 0.05862784 > summary(c(samples$bugsoutput$sims.array[,1,][,2], samples$bugsoutput$sims.array[,2,][,2 Min. 1st Qu. Median Mean 3rd Qu. Max. 0.05863 0.40288 0.50008 0.50032 0.59791 0.94687

ベイズ統計で実践モデリング 3.2 2つの 率の差 第 3 章 項分布を使った推論

違うコインでそれぞれコイントス n 1 回トス, k 1 回表が出る n 2 回トス, k 2 回表が出る 表が出る 率 θ 1 表が出る 率 θ 2

δ k = ~B n =, θ = k E ~B n E, θ E 尤度 θ 1 θ 2 θ = ~Beta(1,1) θ E ~Beta(1,1) 事前分布 k 1 k 2 δ θ = θ E 率の差 n 1 n 2 確定変数 (deterministic variable) を表すノード

Rate_2.txt # Difference Between Two Rates model{ # Observed Counts } k1 ~ dbin(theta1,n1) k2 ~ dbin(theta2,n2) # Prior on Rates theta1 ~ dbeta(1,1) theta2 ~ dbeta(1,1) 尤度 事前分布 # Difference Between Rates delta <- theta1-theta2

R2jags による MCMC サンプリング Rate_2_jags.R 変数のクリア # clears workspace: rm(list=ls()) Rate_2.txt と Rate_2_jags.R のあるディレクトリへの移動 # sets working directories: setwd("/users/toh/desktop/code/parameterestimation/binomial") library(r2jags) パッケージの読み込み k1 <- 5 k2 <- 7 n1 <- 10 n2 <- 10 data <- list("k1", "k2", "n1", "n2") # to be passed on to JAGS 観測変数の設定

θ 1 と θ 2 の初期値の設定何故 list(list()) となっているのか? ヒント :n.chains の設定 myinits <- list( list(theta1 = 0.1, theta2 = 0.9)) # parameters to be monitored: parameters <- c("delta", "theta1", "theta2") モニターするパラメータの設定 # The following command calls JAGS with specific options. # For a detailed description see the R2jags documentation. samples <- jags(data, inits=myinits, parameters, model.file ="Rate_2.txt", n.chains=1, n.iter=10000, n.burnin=1, n.thin=1, DIC=T) Jags を呼び出して θ をサンプリング

サンプリングされた δ( 率の差 ) を取り出して その事後分布を作成 delta <- samples$bugsoutput$sims.list$delta # Now let's plot a histogram for delta. # First, some options to make the plot 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) Nbreaks <- 80 y <- hist(delta, Nbreaks, plot=f) plot(c(y$breaks, max(y$breaks)), c(0,y$density,0), type="s", lwd=2, lty=1, xlim=c(-1,1), ylim=c(0,10), xlab="difference in Rates", ylab="posterior Density")

δ の点推定, 区間推定 # mean of delta: mean(delta) # median of delta: median(delta) # mode of delta, estimated from the "density" smoother: density(delta)$x[which(density(delta)$y==max(density(delta)$y))] # 95% credible interval for delta: quantile(delta, c(.025,.975))

> mean(delta) [1] -0.1658759 平均 > median(delta) [1] -0.1681818 メジアン > density(delta)$x[which(density(delta)$y==max(density(delta)$y))] [1] -0.1562157 モード > quantile(delta, c(.025,.975)) 2.5% 97.5% -0.5316203 0.2167374 95% 信 区間

ベイズ統計で実践モデリング 3.3 共通の 率を推論する 第 3 章 項分布を使った推論

同じコインを 2 が独 にコイントス n 1 回トス, k 1 回表が出る n 2 回トス, k 2 回表が出る 表が出る 率 θ

θ k = ~B n =, θ k E ~B n E, θ 尤度 k 1 k 2 θ~beta(1,1) 事前分布 n 1 n 2

θ k i k H ~B n H, θ θ~beta(1,1) 尤度 事前分布 n i i 独 なグラフの繰り返しを閉じた 形で囲んで表す for ループのような表現

Rate_3.txt # Inferring a Common Rate model{ # Observed Counts k1 ~ dbin(theta,n1) 尤度 k2 ~ dbin(theta,n2) # Prior on Single Rate Theta theta ~ dbeta(1,1) 事前分布 }

Rate_3_jags.R 変数のクリア # clears workspace: rm(list=ls()) Rate_2.txt と Rate_2_jags.R のあるディレクトリへの移動 # sets working directories: setwd("/users/toh/desktop/code/parameterestimation/binomial") library(r2jags) パッケージの読み込み k1 <- 5 k2 <- 7 n1 <- 10 n2 <- 10 data <- list("k1", "k2", "n1", "n2") # to be passed on to JAGS 観測変数の設定

θ の初期値の設定 myinits <- list( list(theta = 0.5)) # parameters to be monitored: parameters <- c("theta") モニターするパラメータの設定 # The following command calls JAGS with specific options. # For a detailed description see the R2jags documentation. samples <- jags(data, inits=myinits, parameters, model.file ="Rate_3.txt", n.chains=1, n.iter=1000, n.burnin=1, n.thin=1, DIC=T) Jags を呼び出して θ をサンプリング

samples の中から θ のサンプルを取り出す theta <- samples$bugsoutput$sims.list$theta # Now let's plot a histogram for theta. # First, some options to make the plot 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) Nbreaks <- 80 y <- hist(theta, Nbreaks, plot=f) plot(c(y$breaks, max(y$breaks)), c(0,y$density,0), type="s", lwd=2, lty=1, xlim=c(0,1), ylim=c(0,10), xlab="rate", ylab="posterior Density") θ の事後分布をプロット

> mean(theta) [1] 0.5908549 > median(theta) [1] 0.5866773 > density(theta)$x[which(density(theta)$y==max(density(theta)$y))] [1] 0.5824454 > quantile(theta, c(.025,.975)) 2.5% 97.5% 0.3983661 0.7934511

まとめ 問題からグラフィカルモデルを作る グラフィカルモデルから jags のモデルを記述する R のスクリプトを記述 (1) ワークスペースへの移動 (2) ワークスペースのクリア (3)R2jags の読み込み (4) 観測データの記述 (5) パラメータの初期値の設定 (6) モニターするパラメータの設定 (7)Jags を実 し MCMC によるサンプリングを実 (8) サンプリング結果を可視化 点推定 区間推定