This repository has been archived by the owner on Sep 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
2018-03-07 Linear Models.Rpres
268 lines (185 loc) · 6.45 KB
/
2018-03-07 Linear Models.Rpres
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
Linear Models
========================================================
author: neoglez
date: February 2, 2018
width: 1440
height: 950
McElreath, R. *Statistical Rethinking: A Bayesian Course with Examples in R and
Stan*. (CRC Press/Taylor & Francis Group, 2016).
Motivation
=======================================================
incremental: true
![Ptolemaic Universe](figures/figure_4.1.png)
The model is incredibly wrong, yet makes quite good predictions.
***
![](https://upload.wikimedia.org/wikipedia/commons/thumb/3/33/Geoz_wb_en.svg/1200px-Geoz_wb_en.svg.png)
Why normal distributions are normal
=======================================================
incremental: true
- Normal by addition
- Normal by multiplication
- Normal by log-multiplication
- Using Gaussian distribution as a skeleton for our hypotheses
- Ontological justification -> "the world is full of Gaussians"
- Epistemological justification -> "it represents a particular state of ignorance"
Gaussian distribution
=======================================================
incremental: true
$$P(y|\mu, \sigma) = \frac{1}{{\sqrt {2\pi\sigma^2} }}e^{- \left( {y - \mu } \right)^2 / 2\sigma^2}$$
- This looks monstrous ;)
- but the important bit is just the $(y-\mu)^2$
A language for describing models
=======================================================
incremental: true
$outcome_i\sim Normal(\mu_i, \sigma)$
$\mu_i = \beta * predictor_i$
$\beta\sim Normal(0,10)$
$\sigma\sim HalfCauchy(0, 1)$
1. *Outcome* variables
2. For each outcome variable define likehood distribution
3. Set of measurements we hope to predict -> *predictor* variables
4. Relate the exact shape of the likehood distribution to the predictor variables
5. Priors for all paramerters of the model
>> If that does not make much sense, good. That indicates that you are holding the right textbook ;)
A language for describing models: Re-describing the globe tossing model
=======================================================================
incremental: true
```{r}
w <- 6; n <- 9;
p_grid <- seq(from=0,to=1,length.out=100)
posterior <- dbinom(w,n,p_grid)*dunif(p_grid,0,1)
posterior <- posterior/sum(posterior)
plot( p_grid, posterior, type="b" ,
xlab="probability of water" , ylab="posterior probability" )
mtext( "Grid approximation with 100 points" )
```
<!--- $$P(y|w, n) = \frac{Binomial(w|n, p)Uniform(p|0,1)}{\int_ Binomial(w|n, p)Uniform(p|0,1)}$$ -->
A Gaussian model of height
==========================
incremental: true
- Two parameters: **$\mu, \sigma$**
- Posterior -> distribution of Gaussian distributions
>> Yes, a distribution of distributions. If that doesn’t
make sense yet, then that just means you are being honest with yourself.
A Gaussian model of height: the data
====================================
incremental: true
```{r}
library(rethinking)
data(Howell1)
d <- Howell1
str( d )
```
A Gaussian model of height: the model
=====================================
incremental: true
$h_i\sim Normal(\mu_i, \sigma)$
$\mu_i\sim Normal(178, 20)$
$\sigma\sim Uniform(0, 50)$
Adding a predictor
==================
incremental: true
>> What we’ve done is a Gaussian model of height in a population of adults. But it
doesn’t really have the usual feel of “regression” to it.
<div style="height: 50%; width:50%">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/7e/Botswana_051.jpg/1200px-Botswana_051.jpg">
</div>
***
```{r}
d2 <- d[ d$age >= 18 , ]
plot( d2$height ~ d2$weight )
```
>> How do we take our Gaussian model from the previous section and incorporate predictor variables?
Adding a predictor: the linear model strategy
=============================================
incremental: true
Recall the Guassian model
$h_i\sim Normal(\mu, \sigma)$
$\mu\sim Normal(178, 20)$
$\sigma\sim Uniform(0, 50)$
>> Now how do we get weight into a Gaussian model of height?
***
$h_i\sim Normal(\mu_i, \sigma)$
$\mu_i = \alpha + \beta x_i$
$\alpha\sim Normal(178, 100)$
$\beta\sim Normal(0,10)$
$\sigma\sim Uniform(0, 50)$
Adding a predictor: fitting the model
=====================================
incremental: false
$h_i\sim Normal(\mu_i, \sigma)$
$\mu_i = \alpha + \beta x_i$
$\alpha\sim Normal(178, 100)$
$\beta\sim Normal(0,10)$
$\sigma\sim Uniform(0, 50)$
***
```{r, eval=FALSE}
height ~ dnorm(mu,sigma)
mu <- a + b*weight
a ~ dnorm(156,100)
b ~ dnorm(0,10)
sigma ~ dunif(0,50)
```
Fitting the model: build the MAP model fit
===========================================
incremental: false
```{r, eval=FALSE}
# load data again, since it's a long way back
library(rethinking)
data(Howell1)
d <- Howell1
d2 <- d[ d$age >= 18 , ]
# fit model
m4.3 <- map(
alist(
height ~ dnorm( mu , sigma ) ,
mu <- a + b*weight ,
a ~ dnorm( 156 , 100 ) ,
b ~ dnorm( 0 , 10 ) ,
sigma ~ dunif( 0 , 50 )
) ,
data=d2 )
```
Adding a predictor: Interpreting the model fit
==============================================
incremental: true
>> Tables of estimates
```{r, echo=FALSE}
# load data again, since it's a long way back
library(rethinking)
data(Howell1)
d <- Howell1
d2 <- d[ d$age >= 18 , ]
# fit model
m4.3 <- map(
alist(
height ~ dnorm( mu , sigma ) ,
mu <- a + b*weight ,
a ~ dnorm( 156 , 100 ) ,
b ~ dnorm( 0 , 10 ) ,
sigma ~ dunif( 0 , 50 )
) ,
data=d2 )
```
```{r}
precis( m4.3 )
```
- a person 1 kg heavier is expected to be 0.90 cm taller.
- a person of weight 0 should be 114cm tall. This is nonsense, since real people always have positive weight, yet it is also true -> the value of the intercept is frequently uninterpretable without studying other parameters.
Interpreting the model fit
==============================================
incremental: true
>> Plotting posterior inference against the data
```{r}
plot( height ~ weight , data=d2 )
abline( a=coef(m4.3)["a"] , b=coef(m4.3)["b"] )
```
Summary
==============================================
incremental: true
>> Simple linear regression model, a framework for estimating the association between a predictor variable and an outcome variable.
>> The Gaussian distribution comprises the likelihood in such models, because it counts up the relative numbers of
ways different combinations of means and standard deviations can produce an observation.
>> Fit Gaussian models to data -> the chapter introduced maximum a prior (MAP) **say what? wasn't that a posteriori?** estimation.
>> Procedures for visualizing posterior distributions and posterior predictions.
>> Next chapter -> regression models with more than one predictor variable.