駆け足で読む Bayesian Computation with R

Bayesian Computation with R (Use R)

Bayesian Computation with R (Use R)

  • 1. An Introduction to R
    • Rの関数、コマンド
    • #file読み込み
      • d=read.table("datafile.txt",sep="\t",header=TRUE)
    • #dにデータアクセス
      • data(d)
    • #attachコマンドで、テーブルのカラム名に直接アクセス
      • attach(d)
    • #1サンプルのレコード表示
      • d[1,]
    • #カテゴリ別カウント
      • table(Var1)
    • #棒グラフ
      • barplot(table(Var1))
    • #記述統計量
      • summary(Var1)
    • #カテゴリ別はこひげ図
      • boxplot(Var2~Var1)
    • #ヒストグラム
      • hist(Var2)
    • #線形回帰
      • fit=lm(Var2~Var3)
    • #回帰直線の追加
      • plot(Var2,Var3)
      • k1=fit1[1]
      • k2=fit1[2]
      • abline(k1,k2)
    • #正規分布
      • rnorm(N,mean=m,sd=s)
    • #要素数
      • length(x)
    • #平均
      • mean(x)
    • #t統計量
      • tstatistic(x,y)
    • #t分布
    • #のクオンタイル対応値
      • qt(p,df)
    • #からの乱数
      • rt(N,df)
    • #指数分布からの乱数
      • rexp(N,rate=r)
    • #plotとline追加、ポイントの追加
      • plot(A,B)
      • lines(A,C)
      • points(X,Y)
  • 2. Introduction to Bayesian Thinking
    • #均等区間シーケンス
      • seq(start,end, by=interval)
    • #事前分布の作り方(離散)
      • pri=c(1,3,5,2,1)
      • pri=pri/sum(pri)
    • #2項データからの事後分布の作り方(離散)
      • data=c(x,y)
      • post=pdisc(p,pri,data)
    • #同長ベクトルをつないで、テーブルにする
      • cbind(vector1,vector2,vector3)
    • #ベータ分布事前分布
      • pri=dbeta(x,y,z)
    • #そのクオンタイル点
      • qbeta(c(0.05,0.95),a,b)
    • #ヒストグラム事前分布
      • histprior(p,mp,risanpri)
    • #サンプリング.離散的確率密度からリプレースありでサンプリング
      • sample(p,N,replace=TRUE,prob)
    • #離散分布からの予測 LearnBayesパッケージ. N個のサンプリングで的中する回数ごとの確率
      • pdiscp(p,pri,N,0:N)
    • #ベータ分布からの予測
    • #ksはベータ分布の係数
      • pbetap(ks,N,0:N)
    • #信頼区間を満足する範囲(離散の場合)
      • distribution=cbind(range,prob)
      • coverage=0.95
      • discint(distribution,coverage)
  • 3. Single-Parameter Models
    • #まれな事象、ポワソン分布→共役事前分布のガンマ分布を使う。観測データから、最尤推定量を初期事前分布のパラメタに指定する
    • #最尤推定
      • dpois
      • dgamma
    • #分子、分母のdgammaの引数のshapeとrateは、分子が事前分布そのまま、分母が、事前分布のそれに、観測データの総数と事象の観測回数を加えた値とすることで、ベイズの定理を満足させる
      • post=dpois(xxx)*dgamma(xxx)/dgamma(xxx)
    • #ベルヌーイ事象、2項分布→共役事前分布のベータ分布を使う。
    • #処理フレームは同じ
    • #dpoisの代わりに
      • dbinom
    • #dgammaの代わりに
      • dbeta
  • 4. Multiparameter Models
    • #ディリクレ分布からの乱数発生
      • rdirichret(N,c(a1,a2,...,ak))
    • #データに正規分布を仮定して、そのパラメタ(平均と分散)について、事後分布(のログ)を2次元プロット。いずれもLeanrBayesの中
      • normchi2post
      • mycontour(normchi2post,c(x1,x2,y1,y2),data)
    • #ロジスティック回帰では、切片と傾きについて同様に
      • logisticpost
      • mycontour(logisticpost,c(x1,x2,y1,y2),data)
    • #事後分布からランダムに発生させる関数
      • simcontour(logisticpost,c(x1,x2,y1,y2),data,N)
    • #2集団の比率の比較にはHoward prior(2x2表)
      • howardprior
        • 以下にて、2x2表の比率の違い別の2群の比率の事後確率分布が描ける
library(LearnBayes)
twobytwo=c(3,15,7,5)#2x2分割表の4つの値
sigma=c(2,1,0.5,0.25)#2群の比率の違いの程度
par(mfrow=c(2,2))
for(i in 1:length(sigma))
{
mycontour(howardprior,c(0.0001,0.9999,0.0001,0.9999),c(1+twobytwo[1],1+twobytwo[2],1+twobytwo[3],1+twobytwo[4],sigma[i]))
lines(c(0,1),c(0,1))
}
      • sampled=simcontour(hawardprior,c(0.0001,0.9999,0.0001,0.9999),c(1+twobytwo[1],1+twobytwo[2],1+twobytwo[3],1+twobytwo[4],sigma[i]),Nototalsample)
        • により、この仮定のもとでのサンプリングができるから、得られたサンプルのうち、ある仮説(片方の群の比率がもう片方の群の比率より大きくなった率は
      • sum(sampled$x>sampled$y)/Nototalsample
        • にて算出できて、それが一番大きくなるようなsigma[i]がもっともそれらしいモデルということを示す
  • 5. Introduction to Bayesian Computation
    • Beta-Binomial Model for Overdispersion
      • 素の値
      • ログをとる
    • 事後分布を多変量正規分布に近似する
      • それにより、解析的積分が可能になる
    • Monte Carlo Method for Computing Integrals
    • Rejection sampling
      • 事後分布からのランダムサンプリングの汎用手法
    • Importance sampling
      • 積分するのに重要な場合に重みをつけてサンプリングする
    • Sampling Importance Resampling
  • 6. Markov Chain Monte Carlo Methods
    • Discrete Markov Chains
    • Metropolis-Hasitng Algorithm
    • Gibbs Sampling
    • MCMC output analysis
  • 7. Hierarchical Modeling
    • 事前分布に階層構造を入れる
  • 8. Model Comparison
    • 片側検定、両側検定
    • モデル間の比較
    • 偶現表の独立性検定
  • 9. 回帰モデル
  • 10. Gibbs Sampling
  • 11. WinBUGSでRによるベイズをする
    • WinBUGSはGibbsサンプラーを使って事後分布からサンプリングするプログラム