-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy path11-why-model-stata.Rmd
200 lines (158 loc) · 3.52 KB
/
11-why-model-stata.Rmd
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
# (PART\*) Stata code{-}
# 11. Why model: Stata{-}
```{r, results='hide', message=FALSE, warning=FALSE}
library(Statamarkdown)
```
```{stata}
do dependency
```
```
/***************************************************************
Stata code for Causal Inference: What If by Miguel Hernan & Jamie Robins
Date: 10/10/2019
Author: Eleanor Murray
For errors contact: [email protected]
***************************************************************/
```
## Program 11.1
- Figures 11.1, 11.2, and 11.3
- Sample averages by treatment level
```{stata}
clear
**Figure 11.1**
*create the dataset*
input A Y
1 200
1 150
1 220
1 110
1 50
1 180
1 90
1 170
0 170
0 30
0 70
0 110
0 80
0 50
0 10
0 20
end
*Save the data*
qui save ./data/fig1, replace
*Build the scatterplot*
scatter Y A, ylab(0(50)250) xlab(0 1) xscale(range(-0.5 1.5))
qui gr export figs/stata-fig-11-1.png, replace
*Output the mean values for Y in each level of A*
bysort A: sum Y
```
```{r, echo=FALSE, out.width='85%', fig.align='center'}
knitr::include_graphics("figs/stata-fig-11-1.png")
```
```{stata}
*Clear the workspace to be able to use a new dataset*
clear
**Figure 11.2**
input A Y
1 110
1 80
1 50
1 40
2 170
2 30
2 70
2 50
3 110
3 50
3 180
3 130
4 200
4 150
4 220
4 210
end
qui save ./data/fig2, replace
scatter Y A, ylab(0(50)250) xlab(0(1)4) xscale(range(0 4.5))
qui gr export figs/stata-fig-11-2.png, replace
bysort A: sum Y
```
```{r, echo=FALSE, out.width='85%', fig.align='center'}
knitr::include_graphics("figs/stata-fig-11-2.png")
```
```{stata}
clear
**Figure 11.3**
input A Y
3 21
11 54
17 33
23 101
29 85
37 65
41 157
53 120
67 111
79 200
83 140
97 220
60 230
71 217
15 11
45 190
end
qui save ./data/fig3, replace
scatter Y A, ylab(0(50)250) xlab(0(10)100) xscale(range(0 100))
qui gr export figs/stata-fig-11-3.png, replace
```
```{r, echo=FALSE, out.width="85%", fig.align='center'}
knitr::include_graphics("figs/stata-fig-11-3.png")
```
## Program 11.2
- 2-parameter linear model
- Creates Figure 11.4, parameter estimates with 95% confidence intervals from Section 11.2, and parameter estimates with 95% confidence intervals from Section 11.3
```{stata}
**Section 11.2: parametric estimators**
*Reload data
use ./data/fig3, clear
*Plot the data*
scatter Y A, ylab(0(50)250) xlab(0(10)100) xscale(range(0 100))
*Fit the regression model*
regress Y A, noheader cformat(%5.2f)
*Output the estimated mean Y value when A = 90*
lincom _b[_cons] + 90*_b[A]
*Plot the data with the regression line: Fig 11.4*
scatter Y A, ylab(0(50)250) xlab(0(10)100) xscale(range(0 100)) || lfit Y A
qui gr export figs/stata-fig-11-4.png, replace
```
```{r, echo=FALSE, out.width="85%", fig.align='center'}
knitr::include_graphics("figs/stata-fig-11-4.png")
```
```{stata}
**Section 11.3: non-parametric estimation*
* Reload the data
use ./data/fig1, clear
*Fit the regression model*
regress Y A, noheader cformat(%5.2f)
*E[Y|A=1]*
di 67.50 + 78.75
```
## Program 11.3
- 3-parameter linear model
- Creates Figure 11.5 and Parameter estimates for Section 11.4
```{stata}
* Reload the data
use ./data/fig3, clear
*Create the product term*
gen Asq = A*A
*Fit the regression model*
regress Y A Asq, noheader cformat(%5.2f)
*Output the estimated mean Y value when A = 90*
lincom _b[_cons] + 90*_b[A] + 90*90*_b[Asq]
*Plot the data with the regression line: Fig 11.5*
scatter Y A, ylab(0(50)250) xlab(0(10)100) xscale(range(0 100)) || qfit Y A
qui gr export figs/stata-fig-11-5.png, replace
```
```{r, echo=FALSE, out.width="85%", fig.align='center'}
knitr::include_graphics("figs/stata-fig-11-5.png")
```