-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart1a.Rmd
337 lines (212 loc) · 6.18 KB
/
part1a.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
---
title: "Part 1: Template README and Reproducible Practices"
author:
- "Lars Vilhuber"
date: "`r Sys.Date()`"
output:
ioslides_presentation:
incremental: false
self-included: true
widescreen: true
---
```{r, child=c('toc.md')}
```
## Overview
::: {.columns-2}
:::: {.column}
Part 1:
- **Reproducible practices**
- The role of the template README
::::
:::: {.column}
- Ideal directory and data structure
- Some programming practices
::::
:::
# Reproducible practices
## Version code and results
While we are assessing reproducibility of others, our work must be reproducible as well.
- Use versioning. We will use `git`. We will show you how.
- Keep a log of your work
- Git will log some things
- You will keep a high-level log (notes) of your work in the report (`REPLICATION.md`)
- Commit often (at least once a day)
# Part 1 | Ideal structure
## What do we tell researchers?
Here's a few generic guidelines for researchers. You will be on the lookout for these things!
## Generic project setup
![TIER protocol](images/tier-protocol.png)
[TIER Protocol](https://www.projecttier.org/tier-protocol/specifications-3-0/)
## Basic project setup {#user}
::: {.columns-2}
:::: {.column}
**Structure your project**
- Data inputs
- Data outputs
- Code
- Paper/text/etc.
::::
:::: {.column}
**Version your project (`git`)!**
**Track metadata**
- cite articles you reference
- *cite* data sources you use
- We will get back to data citations later!
::::
:::
## Project setup examples {#user}
::: {.columns-2}
:::: {.column}
```
/inputs
/outputs
/code
/paper
```
::::
:::: {.column}
```
/datos/
/brutos
/limpiados
/finales
/codigo
/articulo
```
::::
:::
It doesn't really matter, as long as it is logical. We will get to how this translates to confidential or big data in a moment!
# Computational Empathy
## Consider how the next person will (be able to) compute {#user}
- You don't know who that is
- You don't know what they don't know
- Will not have any of your add-on packages/ libraries/ etc. pre-installed
- Don't force them to do tedious things
**It might be "Future You!"**
## It *IS* you {#user}
The replicator is the first (?) reader of the instructions who will need to reproduce the analysis.
## Streamlining {#user}
- Master script preferred
- Least amount of manual effort
- No manual copying of results
- (dynamic documents!)
- Write out/save tables and figures using packages
- Clear instructions
- No manual install of packages
- Use a script to create all directories, install all necessary packages/requirements/etc.
## Reproducibility {#user}
- No manual manipulation
- “Change the parameter to 0.2, then run the code again”
- Use *functions*, ado files, programs, macros, subroutines
- Use *loops*, parameters, *parameter files* to call those subroutines
- Use *placeholders* (globals, macros, libnames, etc.) for common locations ($CONFDATA, $TABLES, $CODE)
- Compute all numbers in package
- No manual calculation of numbers
- Use cross-platform programming practices
## Cross-platform programming practices 1 {#user}
**Use programming-language specific code as much as possible**
Avoid
```{r, eval=FALSE}
system("unzip C:\data\myfile.zip")
```
or
```{stata, eval=FALSE}
shell unzip "C:\data\myfile.zip"
```
## Cross-platform programming practices 1 {#user}
Most languages have appropriate code:
R:
```{r, eval=FALSE}
unzip(zipfile, files = NULL, list = FALSE, overwrite = TRUE,
junkpaths = FALSE, exdir = ".", unzip = "internal",
setTimes = FALSE)
```
Stata:
```{stata, eval=FALSE}
unzipfile "zipfile.zip" [, replace]
```
## Cross-platform programming practices 2 {#user}
Use neutral pathnames (mostly forward slashes)
::: {.columns-2}
:::: {.column}
**R**: Use functions to combine paths (and/or use forward slashes), packages to make code more portable.
<div class="red2">
```
basepath <- rprojroot::find_root(rprojroot::has_file("README.md"))
data <- read.dta(file.path(basepath,"path","data.dta"))
```
</div>
::::
:::: {.column}
**Stata**: *always* use forward slashes, even on Windows
<div class="blue2">
```
global data "/my/computer"
use "$data/path/data.dta"
```
</div>
::::
:::
# More complex Data structures
## Back to the TIER protocol
![TIER Protocol again](images/tier-protocol-2.png)
## Generic suggested data structure
![Simplified replication package structure](images/tier-protocol-home.png)
## When data are big/in the cloud
![Data are big](images/tier-bigdata.png)
## When data are confidential
![Confidential data](images/tier-confidential.png)
## When data are confidential
![Confidential data in enclaves](images/tier-confidential2.png)
## Project setup examples
::: {.columns-2}
:::: {.column}
This may no longer work:
```
/data/
/raw
/clean
/final
/code
/article
```
::::
:::: {.column}
But this might
```
/project123/
/data/
/raw
/clean
/final
/code
/article
/confidential (read-only)
/taxes (read-only)
/wages (read-only)
```
::::
:::
## Stata configuration files {.smaller}
File structure thus becomes more complex, but fundamentally not so different:
```{stata, eval=FALSE}
global taxdata "/confidential/taxes"
global salarydata "/confidential/wages"
global outputdata "/project/data/clean" // this is where you would write the data you create in this project
global results "/project/article" // All tables for inclusion in your paper go here
global programs "/project/code" // All programs (which you might "include") are to be found here
```
## Stata configuration files {.smaller}
Or even more robust:
```{stata, eval=FALSE}
global basedir "/project123"
global confbase "/data/provided"
global project "$basedir/project"
global taxdata "$confbase/taxes"
global salarydata "$confbase/wages"
global outputdata "$project/data/clean" // this is where you would write the data you create in this project
global results "$project/article" // All tables for inclusion in your paper go here
global programs "$project/code" // All programs (which you might "include") are to be found here
```
# Next up: README
[Template README](part1b.html)