没有合适的资源?快使用搜索试试~ 我知道了~
首页Doing Baysian Data Analysis
资源详情
资源评论
资源推荐

Doing Bayesian Data Analysis: A
Tutorial with R and BUGS
John K. Kruschke
Draft of May 11, 2010. Please do not circulate this preliminary draft. If you report
Bayesian analyses based on this book, please do cite it! ¨⌢
Copyright
c
2010 by John K. Kruschke.

ii
Dedicated to my mother, Marilyn A. Kruschke,
and to the memory of my father, Earl R. Kruschke,
who both brilliantly exemplified and taught sound reasoning.
And, in honor of my father,
who dedicated his first book to his children,
I also dedicate this book to mine:
Claire A. Kruschke and Loren D. Kruschke

Contents
1 This Book’s Organization: Read Me First! 1
1.1 Real people can read this book . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 The organization of this book . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3.1 What are the essential chapters? . . . . . . . . . . . . . . . . . . . 3
1.3.2 Where’s the equivalent of traditional test X in this book? . . . . . . 4
1.4 Gimme feedback (be polite) . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5 Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
I The Basics: Parameters, Probability, Bayes’ Rule, and R 7
2 Introduction: Models we believe in 9
2.1 Models of observations and models of beliefs . . . . . . . . . . . . . . . . 10
2.1.1 Models have parameters . . . . . . . . . . . . . . . . . . . . . . . 11
2.1.2 Prior and posterior beliefs . . . . . . . . . . . . . . . . . . . . . . 13
2.2 Three goals for inference from data . . . . . . . . . . . . . . . . . . . . . . 13
2.2.1 Estimation of parameter values . . . . . . . . . . . . . . . . . . . . 13
2.2.2 Prediction of data values . . . . . . . . . . . . . . . . . . . . . . . 14
2.2.3 Model comparison . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3 The R programming language . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3.1 Getting and installing R . . . . . . . . . . . . . . . . . . . . . . . 15
2.3.2 Invoking R and using the command line . . . . . . . . . . . . . . . 15
2.3.3 A simple example of R in action . . . . . . . . . . . . . . . . . . . 16
2.3.4 Getting help in R . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.3.5 Programming in R . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.3.5.1 Editing programs in R . . . . . . . . . . . . . . . . . . . 18
2.3.5.2 Variable names in R . . . . . . . . . . . . . . . . . . . . 18
2.3.5.3 Running a program . . . . . . . . . . . . . . . . . . . . 19
2.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3 What is this stuff called probability? 21
3.1 The set of all possible events . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.1.1 Coin flips: Why you should care . . . . . . . . . . . . . . . . . . . 22
3.2 Probability: Outside or inside the head . . . . . . . . . . . . . . . . . . . . 23
3.2.1 Outside the head: Long-run relative frequency . . . . . . . . . . . 23
3.2.1.1 Simulating a long-run relative frequency . . . . . . . . . 23
iii

iv
CONTENTS
3.2.1.2 Deriving a long-run relative frequency . . . . . . . . . . 24
3.2.2 Inside the head: Subjective belief . . . . . . . . . . . . . . . . . . 25
3.2.2.1 Calibrating a subjective belief by preferences . . . . . . . 25
3.2.2.2 Describing a subjective belief mathematically . . . . . . 26
3.2.3 Probabilities assign numbers to possibilities . . . . . . . . . . . . . 26
3.3 Probability distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.3.1 Discrete distributions: Probability mass . . . . . . . . . . . . . . . 27
3.3.2 Continuous distributions: Rendezvous with density
†
. . . . . . . . 27
3.3.2.1 Properties of probability density functions . . . . . . . . 29
3.3.2.2 The normal probability density function . . . . . . . . . 30
3.3.3 Mean and variance of a distribution . . . . . . . . . . . . . . . . . 32
3.3.3.1 Mean as minimized variance . . . . . . . . . . . . . . . 33
3.3.4 Variance as uncertainty in beliefs . . . . . . . . . . . . . . . . . . 34
3.3.5 Highest density interval (HDI) . . . . . . . . . . . . . . . . . . . . 34
3.4 Two-way distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.4.1 Marginal probability . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.4.2 Conditional probability . . . . . . . . . . . . . . . . . . . . . . . . 38
3.4.3 Independence of attributes . . . . . . . . . . . . . . . . . . . . . . 39
3.5 R code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
3.5.1 R code for Figure 3.1 . . . . . . . . . . . . . . . . . . . . . . . . . 40
3.5.2 R code for Figure 3.3 . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4 Bayes’ Rule 43
4.1 Bayes’ rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
4.1.1 Derived from definitions of conditional probability . . . . . . . . . 44
4.1.2 Intuited from a two-way discrete table . . . . . . . . . . . . . . . . 45
4.1.3 The denominator as an integral over continuous values . . . . . . . 47
4.2 Applied to models and data . . . . . . . . . . . . . . . . . . . . . . . . . . 47
4.2.1 Data order invariance . . . . . . . . . . . . . . . . . . . . . . . . . 49
4.2.2 An example with coin flipping . . . . . . . . . . . . . . . . . . . . 50
4.2.2.1 p(D|θ) is not θ . . . . . . . . . . . . . . . . . . . . . . . 52
4.3 The three goals of inference . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.3.1 Estimation of parameter values . . . . . . . . . . . . . . . . . . . . 52
4.3.2 Prediction of data values . . . . . . . . . . . . . . . . . . . . . . . 52
4.3.3 Model comparison . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4.3.4 Why Bayesian inference can be difficult . . . . . . . . . . . . . . . 56
4.3.5 Bayesian reasoning in everyday life . . . . . . . . . . . . . . . . . 56
4.3.5.1 Holmesian deduction . . . . . . . . . . . . . . . . . . . 56
4.3.5.2 Judicial exoneration . . . . . . . . . . . . . . . . . . . . 57
4.4 R code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
4.4.1 R code for Figure 4.1 . . . . . . . . . . . . . . . . . . . . . . . . . 57
4.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

CONTENTS
v
II All the Fundamentals Applied to Inferring a Binomial Proportion 63
5 Inferring a Binomial Proportion via Exact Mathematical Analysis 65
5.1 The likelihood function: Bernoulli distribution . . . . . . . . . . . . . . . . 66
5.2 A description of beliefs: The beta distribution . . . . . . . . . . . . . . . . 67
5.2.1 Specifying a beta prior . . . . . . . . . . . . . . . . . . . . . . . . 68
5.2.2 The posterior beta . . . . . . . . . . . . . . . . . . . . . . . . . . 70
5.3 Three inferential goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
5.3.1 Estimating the binomial proportion . . . . . . . . . . . . . . . . . 71
5.3.2 Predicting data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
5.3.3 Model comparison . . . . . . . . . . . . . . . . . . . . . . . . . . 73
5.3.3.1 Is the best model a good model? . . . . . . . . . . . . . 75
5.4 Summary: How to do Bayesian inference . . . . . . . . . . . . . . . . . . 75
5.5 R code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
5.5.1 R code for Figure 5.2 . . . . . . . . . . . . . . . . . . . . . . . . . 76
5.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
6 Inferring a Binomial Proportion via Grid Approximation 83
6.1 Bayes’ rule for discrete values of θ . . . . . . . . . . . . . . . . . . . . . . 84
6.2 Discretizing a continuous prior density . . . . . . . . . . . . . . . . . . . . 84
6.2.1 Examples using discretized priors . . . . . . . . . . . . . . . . . . 85
6.3 Estimation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
6.4 Prediction of subsequent data . . . . . . . . . . . . . . . . . . . . . . . . . 88
6.5 Model comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
6.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
6.7 R code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
6.7.1 R code for Figure 6.2 etc. . . . . . . . . . . . . . . . . . . . . . . . 90
6.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
7 Inferring a Binomial Proportion via the Metropolis Algorithm 97
7.1 A simple case of the Metropolis algorithm . . . . . . . . . . . . . . . . . . 98
7.1.1 A politician stumbles upon the Metropolis algorithm . . . . . . . . 99
7.1.2 A random walk . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
7.1.3 General properties of a random walk . . . . . . . . . . . . . . . . . 101
7.1.4 Why we care . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
7.1.5 Why it works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
7.2 The Metropolis algorithm more generally . . . . . . . . . . . . . . . . . . 107
7.2.1 “Burn-in,” efficiency, and convergence . . . . . . . . . . . . . . . . 108
7.2.2 Terminology: Markov chain Monte Carlo . . . . . . . . . . . . . . 109
7.3 From the sampled posterior to the three goals . . . . . . . . . . . . . . . . 110
7.3.1 Estimation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
7.3.1.1 Highest density intervals from random samples . . . . . . 111
7.3.1.2 Using a sample to estimate an integral . . . . . . . . . . 112
7.3.2 Prediction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
7.3.3 Model comparison: Estimation of p(D) . . . . . . . . . . . . . . . 113
7.4 MCMC in BUGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
7.4.1 Parameter estimation with BUGS . . . . . . . . . . . . . . . . . . 116
7.4.2 BUGS for prediction . . . . . . . . . . . . . . . . . . . . . . . . . 118
剩余540页未读,继续阅读


















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0