├── .gitignore ├── Eubank_DefensiveProgramming.pdf ├── Eubank_DefensiveProgramming.tex ├── README.md ├── exercise_builder ├── polity.xls ├── prep_data.py └── wdi.csv ├── exercise_solutions ├── For Class.ipynb ├── polity.dta ├── resource_curse_regressions.tex ├── solutions_latex.pdf ├── solutions_latex.tex ├── solutions_r.r ├── solutions_stata.do └── wdi.dta └── exercises ├── polity.dta ├── starter_code.R ├── starter_code.do ├── starter_latex.tex └── wdi.dta /.gitignore: -------------------------------------------------------------------------------- 1 | ## Core latex/pdflatex auxiliary files: 2 | *.aux 3 | *.lof 4 | *.log 5 | *.lot 6 | *.fls 7 | *.out 8 | *.toc 9 | *.fmt 10 | *.fot 11 | *.cb 12 | *.cb2 13 | 14 | ## Intermediate documents: 15 | *.dvi 16 | *-converted-to.* 17 | # these rules might exclude image files for figures etc. 18 | # *.ps 19 | # *.eps 20 | # *.pdf 21 | 22 | ## Generated if empty string is given at "Please type another file name for output:" 23 | .pdf 24 | 25 | ## Bibliography auxiliary files (bibtex/biblatex/biber): 26 | *.bbl 27 | *.bcf 28 | *.blg 29 | *-blx.aux 30 | *-blx.bib 31 | *.run.xml 32 | 33 | ## Build tool auxiliary files: 34 | *.fdb_latexmk 35 | *.synctex 36 | *.synctex(busy) 37 | *.synctex.gz 38 | *.synctex.gz(busy) 39 | *.pdfsync 40 | 41 | ## Auxiliary and intermediate files from other packages: 42 | # algorithms 43 | *.alg 44 | *.loa 45 | 46 | # achemso 47 | acs-*.bib 48 | 49 | # amsthm 50 | *.thm 51 | 52 | # beamer 53 | *.nav 54 | *.pre 55 | *.snm 56 | *.vrb 57 | 58 | # changes 59 | *.soc 60 | 61 | # cprotect 62 | *.cpt 63 | 64 | # elsarticle (documentclass of Elsevier journals) 65 | *.spl 66 | 67 | # endnotes 68 | *.ent 69 | 70 | # fixme 71 | *.lox 72 | 73 | # feynmf/feynmp 74 | *.mf 75 | *.mp 76 | *.t[1-9] 77 | *.t[1-9][0-9] 78 | *.tfm 79 | 80 | #(r)(e)ledmac/(r)(e)ledpar 81 | *.end 82 | *.?end 83 | *.[1-9] 84 | *.[1-9][0-9] 85 | *.[1-9][0-9][0-9] 86 | *.[1-9]R 87 | *.[1-9][0-9]R 88 | *.[1-9][0-9][0-9]R 89 | *.eledsec[1-9] 90 | *.eledsec[1-9]R 91 | *.eledsec[1-9][0-9] 92 | *.eledsec[1-9][0-9]R 93 | *.eledsec[1-9][0-9][0-9] 94 | *.eledsec[1-9][0-9][0-9]R 95 | 96 | # glossaries 97 | *.acn 98 | *.acr 99 | *.glg 100 | *.glo 101 | *.gls 102 | *.glsdefs 103 | 104 | # gnuplottex 105 | *-gnuplottex-* 106 | 107 | # gregoriotex 108 | *.gaux 109 | *.gtex 110 | 111 | # hyperref 112 | *.brf 113 | 114 | # knitr 115 | *-concordance.tex 116 | # TODO Comment the next line if you want to keep your tikz graphics files 117 | *.tikz 118 | *-tikzDictionary 119 | 120 | # listings 121 | *.lol 122 | 123 | # makeidx 124 | *.idx 125 | *.ilg 126 | *.ind 127 | *.ist 128 | 129 | # minitoc 130 | *.maf 131 | *.mlf 132 | *.mlt 133 | *.mtc[0-9]* 134 | *.slf[0-9]* 135 | *.slt[0-9]* 136 | *.stc[0-9]* 137 | 138 | # minted 139 | _minted* 140 | *.pyg 141 | 142 | # morewrites 143 | *.mw 144 | 145 | # nomencl 146 | *.nlo 147 | 148 | # pax 149 | *.pax 150 | 151 | # pdfpcnotes 152 | *.pdfpc 153 | 154 | # sagetex 155 | *.sagetex.sage 156 | *.sagetex.py 157 | *.sagetex.scmd 158 | 159 | # scrwfile 160 | *.wrt 161 | 162 | # sympy 163 | *.sout 164 | *.sympy 165 | sympy-plots-for-*.tex/ 166 | 167 | # pdfcomment 168 | *.upa 169 | *.upb 170 | 171 | # pythontex 172 | *.pytxcode 173 | pythontex-files-*/ 174 | 175 | # thmtools 176 | *.loe 177 | 178 | # TikZ & PGF 179 | *.dpth 180 | *.md5 181 | *.auxlock 182 | 183 | # todonotes 184 | *.tdo 185 | 186 | # easy-todo 187 | *.lod 188 | 189 | # xindy 190 | *.xdy 191 | 192 | # xypic precompiled matrices 193 | *.xyc 194 | 195 | # endfloat 196 | *.ttt 197 | *.fff 198 | 199 | # Latexian 200 | TSWLatexianTemp* 201 | 202 | ## Editors: 203 | # WinEdt 204 | *.bak 205 | *.sav 206 | 207 | # Texpad 208 | .texpadtmp 209 | 210 | # Kile 211 | *.backup 212 | 213 | # KBibTeX 214 | *~[0-9]* 215 | 216 | # auto folder when using emacs and auctex 217 | /auto/* 218 | 219 | # expex forward references with \gathertags 220 | *-tags.tex 221 | 222 | _minted* 223 | *.Rhistory 224 | *.ipynb_checkpoints 225 | */.ipynb_checkpoints/* 226 | -------------------------------------------------------------------------------- /Eubank_DefensiveProgramming.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickeubank/DefensiveProgramming/49e64ef9b885c09742feac66ad6c06c5a081a864/Eubank_DefensiveProgramming.pdf -------------------------------------------------------------------------------- /Eubank_DefensiveProgramming.tex: -------------------------------------------------------------------------------- 1 | % !TEX options=--shell-escape 2 | 3 | \documentclass[11pt]{beamer} 4 | \usetheme[sectionpage=none, numbering=none]{metropolis} % Use metropolis theme 5 | % Kill page numbers with [numbering=none] in next line: 6 | % To do printouts, add ", handout" after aspectratio. 7 | \usepackage{makecell} 8 | \usepackage{booktabs} 9 | %\usepackage{graphicx} 10 | \usepackage{color} 11 | \usepackage{minted} 12 | 13 | 14 | \title{Defensive Programming} 15 | \author{ \small Nick Eubank \\ 16 | \scriptsize{CSDI, Vanderbilt University}\vspace*{.15in}} 17 | \date{\vspace*{.3in} \today} 18 | 19 | 20 | % This is the beginning of a real document! 21 | \begin{document} 22 | 23 | 24 | \begin{frame} 25 | \maketitle 26 | \end{frame} 27 | 28 | 29 | \begin{frame}[c]{Goals} 30 | \begin{enumerate} 31 | \pause \item Introduce principles of \emph{Defensive Programming}\\\vspace{0.1cm} 32 | \pause \item Learn four specific ``best practices'' 33 | \begin{itemize} 34 | \item Use tests 35 | \item Don't duplicate information 36 | \item Don't transcribe, export 37 | \item Use good style 38 | \end{itemize} 39 | \end{enumerate} 40 | \end{frame} 41 | 42 | \begin{frame}[c]{Defensive programming} 43 | \pause Philosophy of writing code \pause motivated by the simple proposition: \\ 44 | 45 | \begin{center} 46 | \pause \alert{People are bad at writing code} 47 | \end{center} 48 | \pause If we want to avoid errors, \pause not enough to \alert{``just be careful.''}\\ 49 | \pause \vspace{0.2cm}\\$\Rightarrow$ Need strategies take take our fallibility into account 50 | \end{frame} 51 | 52 | 53 | \begin{frame}[c]{Defensive programming} 54 | Set of best practices designed to: 55 | \begin{enumerate} 56 | \pause \item Minimize opportunities for errors to enter code 57 | \pause \item Maximize the probability that \emph{when} we commit errors, we catch them quickly 58 | \end{enumerate} 59 | \end{frame} 60 | 61 | 62 | \begin{frame}\frametitle{Do I need defensive programming?} 63 | \begin{center} 64 | \pause \LARGE \alert{YES.} 65 | \end{center} 66 | \end{frame} 67 | 68 | \begin{frame}\frametitle{Do I need defensive programming?} 69 | \begin{center} 70 | \alert{``To Err is Human''} 71 | \end{center} 72 | \begin{itemize} 73 | \pause \item \emph{Among professional programmers}, average error rate is 10 - 50 bugs per 1,000 lines of delivered code \\ 74 | {\color{gray}{Steve McConnell, 1993}} 75 | \end{itemize} 76 | ``Bugs'' $\nRightarrow$ syntax errors 77 | \end{frame} 78 | 79 | \begin{frame}\frametitle{Do I need defensive programming?} 80 | QJPS Replication Review: 81 | \begin{itemize} 82 | \item Before publication, test whether replication packages run and generate results in the paper. 83 | \end{itemize} 84 | 85 | \pause From 2012 - 2016: 86 | \begin{itemize} 87 | \pause \item 4 packages passed without modifications 88 | \pause \item \alert{\emph{58\%}} of packages generated results that were different from those in the paper. 89 | \end{itemize} 90 | \end{frame} 91 | 92 | \begin{frame}[t]\frametitle{Do I need defensive programming?} 93 | \begin{enumerate} 94 | \pause \item If a correlation exists between sophistication of analysis and likelihood of errors, it is if anything positive. 95 | \begin{itemize} 96 | \item Senior, junior, fancy, basic: all had problems! 97 | \end{itemize} 98 | \pause \item Even if you trust yourself, do you trust your coauthors? 99 | \begin{itemize} 100 | \pause \item (Do you trust the version of you that wrote that code at 3am?)) 101 | \end{itemize} 102 | \pause \item Do you trust the people who made the dataset you're using? 103 | \begin{itemize} 104 | \pause \item If you estimate the share of a population that's female, and someone left a \texttt{7} in the \texttt{female} variable, if you don't catch it, that means your answer is \emph{wrong}. 105 | \end{itemize} 106 | \end{enumerate} 107 | \end{frame} 108 | 109 | \begin{frame}[c]{Defensive programming} 110 | Set of best practices designed to: 111 | \begin{enumerate} 112 | \item Minimize opportunities for errors to enter code 113 | \item Maximize the probability that \emph{when} we commit errors, we catch them quickly 114 | \end{enumerate} 115 | \end{frame} 116 | 117 | 118 | 119 | \begin{frame}[c]{Four Skills} 120 | \tableofcontents 121 | \end{frame} 122 | 123 | 124 | 125 | \section{Write tests} 126 | 127 | \begin{frame}[c]{Four Skills} 128 | \tableofcontents[current] 129 | \end{frame} 130 | 131 | \begin{frame}[c, fragile]{Tests} 132 | Lines of code that assert something about the data 133 | \begin{itemize} 134 | \item Evaluate to \texttt{True} or \texttt{False} 135 | \end{itemize} 136 | \vspace{0.5cm}\\ 137 | \pause e.g. 138 | \begin{minted}{r} 139 | df = read.csv('state_populations.csv') 140 | stopifnot( nrow(df) == 50 ) 141 | \end{minted} 142 | \end{frame} 143 | 144 | \begin{frame}[c]{But I ``test'' informally...} 145 | Value of tests: 146 | \begin{enumerate} 147 | \pause \item Explicit form of checking data is doing what you expect 148 | \pause \item Unlike just looking at result interactively, executes \alert{every time you run your code} 149 | \begin{itemize} 150 | \pause \item If you or co-author change upstream data or code and introduce a mistake, tests will catch. 151 | \end{itemize} 152 | \end{enumerate} 153 | \end{frame} 154 | 155 | 156 | \begin{frame}[fragile]\frametitle{Writing tests: R} 157 | Is age always positive? 158 | 159 | \vspace{0.5cm} 160 | This will pass (do nothing): 161 | \begin{minted}{r} 162 | age = c(42, 20, 31, 18) 163 | # Make sure age is positive: 164 | stopifnot( age > 0 ) 165 | \end{minted} 166 | \pause 167 | \vspace{1cm} 168 | But if, for example, ``missing'' was coded as -99, this would throw an error: 169 | \begin{minted}{r} 170 | age = c(42, 20, 31, -99) 171 | stopifnot( age > 0 ) 172 | \end{minted} 173 | \end{frame} 174 | 175 | \begin{frame}[fragile]\frametitle{Writing tests: R} 176 | For vectors, \texttt{stopifnot} checks if ALL values are TRUE. 177 | \vspace{1cm} 178 | 179 | This will fail: 180 | \begin{minted}{r} 181 | # Are all values True? 182 | v = c(1, 2, 3) 183 | stopifnot( v == 2 ) 184 | \end{minted} 185 | \vspace{1cm} 186 | This will pass: 187 | \begin{minted}{r} 188 | stopifnot( v > 0 ) 189 | \end{minted} 190 | \end{frame} 191 | 192 | \begin{frame}[fragile]\frametitle{Writing tests: R} 193 | If you want to see if test holds for at least \emph{some} observations, use \texttt{any}. 194 | \vspace{1cm} 195 | 196 | \begin{minted}{r} 197 | # Are there at least some values that are 2? 198 | v = c(1, 2, 3) 199 | stopifnot( any(v == 2) ) 200 | \end{minted} 201 | This will pass. 202 | \end{frame} 203 | 204 | \begin{frame}[fragile]\frametitle{Writing tests} 205 | 206 | Can combine with functions: 207 | \vspace{1cm} 208 | \begin{minted}{r} 209 | stopifnot( length(VECTOR) == 100 ) 210 | \end{minted} 211 | \end{frame} 212 | 213 | 214 | \begin{frame}[fragile]\frametitle{Writing tests: Stata} 215 | Is age always positive? 216 | 217 | \vspace{0.5cm} 218 | \begin{minted}{stata} 219 | assert age > 0 220 | \end{minted} 221 | \pause 222 | 50 States in data? 223 | \begin{minted}{stata} 224 | count 225 | assert r(N) == 50 226 | \end{minted} 227 | \end{frame} 228 | 229 | 230 | \begin{frame}\frametitle{How to write tests} 231 | Go to: 232 | \end{frame} 233 | 234 | 235 | 236 | 237 | \begin{frame}\frametitle{Tests: Exercise 1} 238 | Setup: 239 | \begin{enumerate} 240 | \pause \item Download \texttt{exercises} folder 241 | \pause \item Open \texttt{starter\_code.R} or \texttt{starter\_code.do}, set the working directory to the \texttt{exercises} folder. 242 | \end{enumerate} 243 | 244 | Exercises: 245 | \begin{enumerate} 246 | \pause \item The World Development Indicator (WDI) data has duplicate entries. \alert{Write a test that fails because there shouldn't be duplicates.} 247 | \pause \item The countries in Polity should be a perfect subset of the countries in the WDI dataset, but they are not. \alert{Write a test that fails because of this.} 248 | \end{enumerate} 249 | \end{frame} 250 | 251 | 252 | \begin{frame}\frametitle{Tests: When to write them} 253 | 254 | \begin{itemize} 255 | \pause \item \textbf{After merges} No where are problems with data made more clear then in a merge. ALWAYS add tests after a merge! 256 | \pause \item \textbf{After complicated manipulations} If you had to think about it, you should test to do it. 257 | \pause \item \textbf{Before dropping observations} 258 | \end{itemize} 259 | 260 | \vspace{1cm} 261 | \pause Most of use check things interactively to make sure we did it right. A good rule of thumb is that when you catch yourself checking something interactively, stop and write it as a test. 262 | 263 | \end{frame} 264 | 265 | 266 | \section{Don't duplicate information} 267 | 268 | 269 | \begin{frame}[c]{Four Skills} 270 | \tableofcontents[currentsection] 271 | \end{frame} 272 | 273 | 274 | \begin{frame}[fragile]{Don't duplicate information} 275 | 276 | \begin{itemize} 277 | \pause \item If information is represented in many places, when you make changes you have to find all those places. 278 | \pause \item If information is represented once, and everything else points back to that representation, one change will \emph{always} change everything. 279 | \end{itemize} 280 | 281 | \end{frame} 282 | 283 | \begin{frame}[fragile]\frametitle{Don't duplicate information} 284 | 285 | Duplicated information: 286 | \vspace{0.5cm} 287 | \begin{minted}{r} 288 | df$var1 <- gsub("armadillo", "Mr. Armadillo", 289 | df$var1) 290 | df$var2 <- gsub("armadillo", "Mr. Armadillo", 291 | df$var2) 292 | 293 | ... 294 | [Other manipulations] 295 | ... 296 | 297 | df$var7 <- gsub("armadillo", "Mr. Armadillo", 298 | df$var7) 299 | \end{minted} 300 | \end{frame} 301 | 302 | \begin{frame}[fragile]\frametitle{Don't duplicate information} 303 | One representation: 304 | \vspace{0.5cm} 305 | \begin{minted}{r} 306 | pre_change_name <- "armadillo" 307 | replacement_name <- "Mr. Armadillo" 308 | 309 | df$var1 <- gsub(pre_change_name, 310 | replacement_name, df$var1) 311 | df$var2 <- gsub(pre_change_name, 312 | replacement_name, df$var2) 313 | df$var7 <- gsub(pre_change_name, 314 | replacement_name, df$var7) 315 | \end{minted} 316 | \end{frame} 317 | 318 | 319 | \begin{frame}[fragile]\frametitle{Don't duplicate information} 320 | One representation: 321 | \vspace{1cm} 322 | \begin{minted}{r} 323 | pre_change_name <- "armadillo" 324 | replacement_name <- "Dr. Armadillo" 325 | 326 | df$var1 <- gsub(pre_change_name, 327 | replacement_name, df$var1) 328 | df$var2 <- gsub(pre_change_name, 329 | replacement_name, df$var2) 330 | df$var7 <- gsub(pre_change_name, 331 | replacement_name, df$var7) 332 | \end{minted} 333 | \end{frame} 334 | 335 | 336 | \begin{frame}[fragile]\frametitle{Don't duplicate information} 337 | \begin{minted}{stata} 338 | replace var1 = "Mr. Armadillo" /// 339 | if var1 == "armadillo" 340 | replace var2 = "Mr. Armadillo" /// 341 | if var2 == "armadillo" 342 | replace var7 = "Mr. Armadillo" /// 343 | if var3 == "armadillo" 344 | \end{minted} 345 | \end{frame} 346 | 347 | \begin{frame}[fragile]\frametitle{Don't duplicate information} 348 | \begin{minted}{stata} 349 | local pre_change_name = "armadillo" 350 | local replacement_name = "Mr. Armadillo" 351 | replace var1 = "`replacement_name'" /// 352 | if var1 == "`pre_change_name'" 353 | replace var2 = "`replacement_name'" /// 354 | if var2 == "`pre_change_name'" 355 | replace var7 = "`replacement_name'" /// 356 | if var3 == "`pre_change_name'" 357 | \end{minted} 358 | \pause (Or write as loop) 359 | \end{frame} 360 | 361 | 362 | 363 | \begin{frame}\frametitle{Don't Duplicate: Exercise 2} 364 | Exercises: 365 | \begin{itemize} 366 | \item All the regressions in the code you have use the same base specification. Consolidate the representation of that base specification. 367 | \item Now add \texttt{population} as a control to all you regressions. You should only have to add it once! 368 | \end{itemize} 369 | \end{frame} 370 | 371 | \section{Don't transcribe, export} 372 | 373 | \begin{frame}[c]{Four Skills} 374 | \tableofcontents[currentsection] 375 | \end{frame} 376 | 377 | 378 | 379 | \begin{frame}\frametitle{Don't transcribe results!} 380 | 381 | Number one reason papers don't match real results at QJPS. 382 | \vspace{0.5cm} 383 | 384 | R: 385 | \begin{itemize} 386 | \item \texttt{stargazer} 387 | \item Tutorial: \url{http://jakeruss.com/cheatsheets/stargazer.html} 388 | \item Custom: \url{http://stanford.edu/~ejdemyr/r-tutorials/tables-in-r/} 389 | \end{itemize} 390 | 391 | Stata: 392 | \begin{itemize} 393 | \item Summary: \url{http://www.nickeubank.com/exporting-results-stata-latex/} 394 | \end{itemize} 395 | 396 | \textbf{Use for numbers in your text as well!} 397 | \end{frame} 398 | 399 | \begin{frame}[fragile]\frametitle{Don't transcribe results: Exercise 3} 400 | Oh man, our tests found all these problems. Ugh, why did we put all those old results in by hand?! Now we have to copy them again. Or... we could make the automatically updating! 401 | 402 | 1) Export the regression table at the end of \texttt{starter\_code.R} using stargazer. 403 | 404 | 2) Open our latex analysis file (\texttt{start\_latex.tex}). 405 | 406 | 3) Import it into your latex document using the \texttt{\\input\{\}} command. 407 | \end{frame} 408 | 409 | \begin{frame}[c, fragile]{Don't transcribe results: Exercise 3} 410 | Make tables: 411 | \begin{minted}{r} 412 | stargazer(list(model1, model2, model3, model4), 413 | title="TITLE HERE", 414 | type="latex", 415 | out='FILE_NAME.tex' ) 416 | \end{minted} 417 | \hline 418 | \begin{minted}{r} 419 | eststo clear 420 | eststo: reg polity gdp_per_cap 421 | eststo: reg polity gdp_per_cap under5_mortality 422 | esttab using your_file_name.tex, replace /// 423 | title("My regressions!") 424 | \end{minted} 425 | \hline 426 | Latex Import: \texttt{\\input\{your\_file\_name.tex\}} 427 | \end{frame} 428 | 429 | 430 | \begin{frame}[fragile]\frametitle{Don't transcribe: Exercise 2 (Part 2)} 431 | Now, in the text, we say that households have an average size of 8, but we know that's wrong. Can you export the average size of the household from R and import it into LaTeX? Hint: Here's how you write a number to a file as text: 432 | 433 | \begin{minted}{r} 434 | x = 1/3 435 | x_as_string = format(x, digits=2) 436 | write(x_as_string, "my_file.tex") 437 | \end{minted} 438 | \end{frame} 439 | 440 | 441 | 442 | \section{Use good style} 443 | \begin{frame}[c]{Four Skills} 444 | \tableofcontents[currentsection] 445 | \end{frame} 446 | 447 | \begin{frame}[c]{Style} 448 | Style isn't just about aesthetics; it's about making code \alert{readable} so errors are easy to see. 449 | \begin{itemize} 450 | \item Whitespace is free; use it. 451 | \item Use informative variable names (not x, y) 452 | \item COMMENT 453 | \end{itemize} 454 | \end{frame} 455 | 456 | \begin{frame}[c, fragile]{Style} 457 | Bad: 458 | \begin{minted}{r} 459 | df=read.csv('file_90823409.csv') 460 | df$var07=df$var07+11 461 | df=df[df$var07>65] 462 | lm("pid~var09") 463 | \end{minted} 464 | \end{frame} 465 | 466 | \begin{frame}[c, fragile]{Style} 467 | \begin{minted}{r} 468 | # Load voter survey data from 2007 469 | voters = read.csv('file_90823409.csv') 470 | 471 | # var07 is age in 2007. Want age today (2018) 472 | voters$age = voters$var07 + 11 473 | 474 | # Let's see how income predicts voterid for 475 | # people over 65 today, so subset to 476 | # people over 65 today and regress. 477 | 478 | voters = voters[df$age>65] 479 | voters$income = df$var09 480 | 481 | lm("pid ~ income") 482 | \end{minted} 483 | \end{frame} 484 | 485 | \begin{frame}[c]{Thanks!} 486 | Resources: 487 | \url{www.nickeubank.com/replication} 488 | \end{frame} 489 | \end{document} 490 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DefensiveProgramming 2 | Defensive Programming For Social Scientists Workshop Materials 3 | 4 | Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. 5 | -------------------------------------------------------------------------------- /exercise_builder/polity.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickeubank/DefensiveProgramming/49e64ef9b885c09742feac66ad6c06c5a081a864/exercise_builder/polity.xls -------------------------------------------------------------------------------- /exercise_builder/prep_data.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Created on Wed Jan 24 13:11:18 2018 5 | 6 | @author: Nick 7 | """ 8 | import pandas as pd 9 | import os 10 | os.chdir('/users/nick/github/instructionalmaterials/defensiveprogramming/') 11 | polity = pd.read_excel('exercise_builder/polity.xls') 12 | wdi = pd.read_csv('exercise_builder/WDI.csv', na_values='..') 13 | 14 | # Thin out 15 | polity = polity.query("year == 2010")[['scode', 'country', 'democ', 'autoc', 'polity']] 16 | polity = polity[polity.country != "Taiwan"] 17 | polity = polity[pd.notnull(polity.country)] 18 | 19 | replaces = [{"old": "Bosnia", "new": "Bosnia and Herzegovina"}, 20 | {"old": "Cape Verde", "new": "Cabo Verde"}, 21 | {"old": "Congo Brazzaville", "new": "Congo, Rep."}, 22 | {"old": "Congo Kinshasa", "new": "Congo, Dem. Rep."}, 23 | {"old": "East Timor", "new": "Timor-Leste"}, 24 | {"old": "Egypt", "new": "Egypt, Arab Rep."}, 25 | {"old": "Gambia", "new": "Gambia, The"}, 26 | {"old": "Iran", "new": "Iran, Islamic Rep."}, 27 | {"old": "Ivory Coast", "new": "Cote d'Ivoire"}, 28 | {"old": "Korea North", "new": "Korea, Dem. People's Rep."}, 29 | {"old": "Korea South", "new": "Korea, Rep."}, 30 | {"old": "Kyrgyzstan", "new": "Kyrgyz Republic"}, 31 | {"old": "Laos", "new": "Lao PDR"}, 32 | {"old": "Macedonia", "new": "Macedonia, FYR"}, 33 | {"old": "Syria", "new": "Syrian Arab Republic"}, 34 | {"old": "UAE", "new": "United Arab Emirates"}, 35 | {"old": "Venezuela", "new": "Venezuela, RB"}, 36 | {"old": "Yemen", "new": "Yemen, Rep."}] 37 | 38 | for r in replaces: 39 | polity['country'] = polity.country.str.replace(r['old'], r['new']) 40 | 41 | renames = {'Country Name': 'country_name', 42 | 'Country Code': 'country_code', 43 | 'GDP per capita, PPP (constant 2011 international $) [NY.GDP.PCAP.PP.KD]': 'gdp_per_cap', 44 | 'Literacy rate, adult total (% of people ages 15 and above) [SE.ADT.LITR.ZS]': 'literacy_rate', 45 | 'Total natural resources rents (% of GDP) [NY.GDP.TOTL.RT.ZS]': 'natural_resources_pct_gdp', 46 | 'Population, total [SP.POP.TOTL]': 'population', 47 | 'Urban population (% of total) [SP.URB.TOTL.IN.ZS]': 'urban_pct', 48 | 'Life expectancy at birth, total (years) [SP.DYN.LE00.IN]': 'life_expectancy', 49 | 'Lifetime risk of maternal death (%) [SH.MMR.RISK.ZS]': 'maternal_death_risk', 50 | 'Mortality rate, under-5 (per 1,000 live births) [SH.DYN.MORT]': 'under5_mortality'} 51 | 52 | wdi = wdi.rename(renames, axis = 'columns') 53 | 54 | wdi = wdi[list(renames.values())] 55 | wdi = wdi[pd.notnull(wdi.country_name)] 56 | wdi['country_name'] = wdi.country_name.str.replace(u"(\u2018|\u2019)", "'") 57 | 58 | # Add duplicate rows to WDI as bug to be caught with test. 59 | start = len(wdi) 60 | wdi = pd.concat([wdi, wdi.sample(5)]) 61 | assert len(wdi) == start + 5 62 | 63 | wdi.to_stata('exercises/wdi.dta') 64 | polity.to_stata('exercises/polity.dta') 65 | 66 | a = pd.merge(wdi, polity, how='outer', left_on='country_name', right_on='country', 67 | indicator=True, validate='m:1') 68 | 69 | # Russia and Myanmar have deliberately different spellings 70 | assert not (a._merge != "right_only").all() 71 | -------------------------------------------------------------------------------- /exercise_builder/wdi.csv: -------------------------------------------------------------------------------- 1 | Time,Time Code,Country Name,Country Code,"GDP per capita, PPP (constant 2011 international $) [NY.GDP.PCAP.PP.KD]","Literacy rate, adult total (% of people ages 15 and above) [SE.ADT.LITR.ZS]",Total natural resources rents (% of GDP) [NY.GDP.TOTL.RT.ZS],"Population, total [SP.POP.TOTL]",Urban population (% of total) [SP.URB.TOTL.IN.ZS],"Life expectancy at birth, total (years) [SP.DYN.LE00.IN]",Lifetime risk of maternal death (%) [SH.MMR.RISK.ZS],"Mortality rate, under-5 (per 1,000 live births) [SH.DYN.MORT]" 2 | 2010,YR2010,Afghanistan,AFG,1614.25500126115,..,0.727903283215587,28803167,24.689,61.2354634146342,3.5431435670919,90.2 3 | 2010,YR2010,Albania,ALB,9927.11957619658,..,2.06355206829376,2913021,52.163,76.7479024390244,0.0496072945024301,16.6 4 | 2010,YR2010,Algeria,DZA,12870.6026985154,..,19.997522747888,36117637,67.526,74.6697804878049,0.43950134057935,27.3 5 | 2010,YR2010,American Samoa,ASM,..,..,0,55637,87.594,..,..,.. 6 | 2010,YR2010,Andorra,AND,..,..,0,84449,87.817,..,..,3.3 7 | 2010,YR2010,Angola,AGO,5895.11408790181,..,39.0314457416062,23369131,40.097,58.1411219512195,3.8151284503933,119.4 8 | 2010,YR2010,Antigua and Barbuda,ATG,19212.7201307541,..,0,94661,26.239,75.3132926829268,..,10.3 9 | 2010,YR2010,Argentina,ARG,18712.0630773436,..,3.13817619530761,41223889,90.966,75.4920243902439,0.145415024369927,14.5 10 | 2010,YR2010,Armenia,ARM,6702.84800627941,..,2.99209032217788,2877311,63.58,73.1092926829268,0.0622657611176616,18.1 11 | 2010,YR2010,Aruba,ABW,..,96.82264,0.00228741179007636,101669,43.059,74.9106341463415,..,.. 12 | 2010,YR2010,Australia,AUS,41384.9235523277,..,9.58182220697495,22031750,88.733,81.6951219512195,0.0126174296368835,4.8 13 | 2010,YR2010,Austria,AUT,43384.5880101396,..,0.216120111597594,8363404,65.852,80.5804878048781,0.00537818084184961,4.3 14 | 2010,YR2010,Azerbaijan,AZE,15950.2574578784,99.77114,30.4655902074112,9054332,53.401,70.9186097560976,0.0681644237892897,39.2 15 | 2010,YR2010,"Bahamas, The",BHS,22895.0657376506,..,0.0195222002123309,360832,82.549,74.5969024390244,0.160256763588384,13.8 16 | 2010,YR2010,Bahrain,BHR,40570.5356928307,94.55679,4.83211645139003,1240862,88.535,76.1553170731707,0.0367620845185728,8.6 17 | 2010,YR2010,Bangladesh,BGD,2442.72888765696,..,1.2086738903886,152149102,30.462,70.2420487804878,0.64349646928125,49.4 18 | 2010,YR2010,Barbados,BRB,16457.9159049246,..,0.353703338163156,279569,32.06,74.8488048780488,0.0555685519294063,14.2 19 | 2010,YR2010,Belarus,BLR,16235.1714534334,..,1.40663703040682,9490583,74.615,70.4048780487805,0.00781451850657493,5.5 20 | 2010,YR2010,Belgium,BEL,41085.9189401366,..,0.0191998043477151,10895586,97.641,80.1829268292683,0.0134397698473588,4.5 21 | 2010,YR2010,Belize,BLZ,7875.25594808055,..,5.35256603452255,321608,44.963,69.7620243902439,0.114749092977381,18.8 22 | 2010,YR2010,Benin,BEN,1818.77846075906,..,4.68713924062084,9199259,41.854,59.2592682926829,2.3427501236372,111.1 23 | 2010,YR2010,Bermuda,BMU,56395.0078003539,..,0,65124,100,79.2885365853658,..,.. 24 | 2010,YR2010,Bhutan,BTN,6419.67559908811,..,5.15305950984865,727641,34.793,67.7742926829268,0.526939559962854,42.7 25 | 2010,YR2010,Bolivia,BOL,5407.47022023569,..,9.61290530060865,9918242,66.426,66.4058048780488,0.869412020467234,46.6 26 | 2010,YR2010,Bosnia and Herzegovina,BIH,9720.2776202025,..,2.64496801605492,3722084,39.226,75.7937317073171,0.0167159638403555,6.9 27 | 2010,YR2010,Botswana,BWA,13334.1987970857,..,4.93646727109819,2014866,56.235,59.7809268292683,0.4960369056624,52.5 28 | 2010,YR2010,Brazil,BRA,14539.0845345652,90.37918,4.45122322030381,196796269,84.335,73.7695609756098,0.125366106811309,19.8 29 | 2010,YR2010,British Virgin Islands,VGB,..,..,..,27224,44.645,..,..,.. 30 | 2010,YR2010,Brunei Darussalam,BRN,80552.8644713138,..,22.766972619551,388662,75.51,76.7541951219512,0.0572189070203679,10.5 31 | 2010,YR2010,Bulgaria,BGR,15283.1797819496,..,2.5157070094982,7395599,72.302,73.5121951219512,0.0165270259149641,10.8 32 | 2010,YR2010,Burkina Faso,BFA,1423.37770598198,..,14.9275495493892,15605217,25.665,57.0146829268293,2.4920320034704,116.4 33 | 2010,YR2010,Burundi,BDI,763.831930213843,..,24.1343217454441,8766930,10.641,54.837,4.78298674217764,93.6 34 | 2010,YR2010,Cabo Verde,CPV,5828.24158804338,..,0.488298294710238,502384,61.833,71.7745365853659,0.142992690841025,26.5 35 | 2010,YR2010,Cambodia,KHM,2522.92682475402,..,2.69595661546956,14308740,19.81,66.4196829268293,0.630705457203524,44.4 36 | 2010,YR2010,Cameroon,CMR,2929.82911281962,71.29051,8.39342055283269,19970495,51.516,55.4011463414634,3.48520850439913,108.3 37 | 2010,YR2010,Canada,CAN,40699.3551293841,..,2.7608544271818,34005274,80.937,81.1975609756098,0.0128891691517098,5.6 38 | 2010,YR2010,Cayman Islands,CYM,..,..,..,55507,100,..,..,.. 39 | 2010,YR2010,Central African Republic,CAF,888.007249982842,36.75261,8.56965501999418,4448525,38.828,47.5327073170732,3.94408094109584,148.9 40 | 2010,YR2010,Chad,TCD,1925.19511802507,..,24.4981731188238,11887202,21.983,50.2063414634146,7.12030197790908,149.7 41 | 2010,YR2010,Channel Islands,CHI,..,..,..,159581,31.054,80.0713414634146,..,.. 42 | 2010,YR2010,Chile,CHL,19442.0503154635,..,18.1861844351289,16993354,88.586,78.3089268292683,0.0476251553131913,8.7 43 | 2010,YR2010,China,CHN,9525.81848339164,95.12448,6.25452674143635,1337705000,49.226,75.2660975609756,0.0532600650185461,15.7 44 | 2010,YR2010,Colombia,COL,10900.5052821616,93.37233,6.26613994024701,45918097,75.036,73.2829024390244,0.152028253941455,18.6 45 | 2010,YR2010,Comoros,COM,1412.50268663997,..,2.7278650643883,689692,27.918,61.8470731707317,1.8627129669199,88.2 46 | 2010,YR2010,"Congo, Dem. Rep.",COD,609.404402826866,..,35.3245081050662,64523263,39.937,56.8701707317073,4.93748574804147,115.6 47 | 2010,YR2010,"Congo, Rep.",COG,5186.34478845194,..,49.1368884757658,4386693,63.228,60.4526097560976,2.61675091914732,63.9 48 | 2010,YR2010,Costa Rica,CRI,12999.9921931442,..,1.6468530916139,4545280,71.734,78.7326341463415,0.0583828286636828,10.2 49 | 2010,YR2010,Cote d'Ivoire,CIV,2690.25800198952,..,6.43512973148971,20401331,50.557,50.4507073170732,3.73343782889393,110.5 50 | 2010,YR2010,Croatia,HRV,20118.0859812851,..,0.617489625706863,4417781,57.537,76.4756097560976,0.0156333314313574,5.5 51 | 2010,YR2010,Cuba,CUB,..,..,2.9638432930086,11333051,76.597,78.9366585365854,0.0628089945102309,6.1 52 | 2010,YR2010,Curacao,CUW,..,..,..,148703,89.905,..,..,.. 53 | 2010,YR2010,Cyprus,CYP,33941.1380158355,..,0.0606877241543736,1112607,67.551,79.3849512195122,0.011893698765099,3.6 54 | 2010,YR2010,Czech Republic,CZE,28352.9493964403,..,0.900667779695603,10474410,73.255,77.4243902439025,0.00701139594556261,3.4 55 | 2010,YR2010,Denmark,DNK,43998.4366716285,..,1.30137414113286,5547683,86.795,79.1,0.0125157524892357,4.1 56 | 2010,YR2010,Djibouti,DJI,2634.71324617292,..,0.838156180876962,851146,76.996,60.3667317073171,0.894517250494067,76.5 57 | 2010,YR2010,Dominica,DMA,10198.2735161701,..,0.0491550311178363,71440,68.094,..,..,22.1 58 | 2010,YR2010,Dominican Republic,DOM,11123.5309648031,89.53873,0.192817175006333,9897985,73.752,72.7448780487805,0.219310708727117,34.4 59 | 2010,YR2010,Ecuador,ECU,9352.34423681593,91.85404,10.8674129343069,14934690,62.69,75.0333902439024,0.215264713015088,25.1 60 | 2010,YR2010,"Egypt, Arab Rep.",EGY,9857.46828190066,72.04785,8.70000760378051,84107606,43.019,70.3414146341464,0.146491915577567,29.1 61 | 2010,YR2010,El Salvador,SLV,7299.87131216445,84.49272,0.886071866392796,6164626,64.286,71.6701951219512,0.132550710592382,19.2 62 | 2010,YR2010,Equatorial Guinea,GNQ,33723.3658030152,..,32.044444094691,951104,39.223,55.9589512195122,1.84767164575083,110.6 63 | 2010,YR2010,Eritrea,ERI,1416.36580154238,..,1.93089796391949,4390840,20.572,62.1758536585366,2.87213818218707,55.2 64 | 2010,YR2010,Estonia,EST,22740.9720038708,..,1.07610105569546,1331475,68.094,75.4292682926829,0.0136689150712805,4.5 65 | 2010,YR2010,Ethiopia,ETH,1073.82629506882,..,16.1464362840417,87702670,17.319,61.6018536585366,2.57957787658975,80.9 66 | 2010,YR2010,Faroe Islands,FRO,..,..,0,48550,40.926,80.6390243902439,..,.. 67 | 2010,YR2010,Fiji,FJI,7352.08836002372,..,2.9443487933594,859950,51.828,69.3908536585366,0.0959269000985384,24.1 68 | 2010,YR2010,Finland,FIN,39848.1344978775,..,0.629935438949641,5363352,83.558,79.8707317073171,0.00563579439421283,3 69 | 2010,YR2010,France,FRA,36872.2252545845,..,0.055387523403456,65027512,78.345,81.6634146341463,0.0175349219610044,4.2 70 | 2010,YR2010,French Polynesia,PYF,..,..,..,267820,56.479,75.7074634146342,..,.. 71 | 2010,YR2010,Gabon,GAB,15355.7931982373,..,33.4739995224163,1640210,85.697,62.8565365853659,1.36751379403912,63.7 72 | 2010,YR2010,"Gambia, The",GMB,1644.22861355625,..,4.64534159645475,1692149,56.297,59.6457317073171,4.49753165891238,80.3 73 | 2010,YR2010,Georgia,GEO,6733.78376130988,..,1.84689109839834,3926000,52.869,72.4980487804878,0.0724799993028785,16.5 74 | 2010,YR2010,Germany,DEU,40428.7210307617,..,0.15882328495153,81776930,74.291,79.9878048780488,0.00874751908844809,4.2 75 | 2010,YR2010,Ghana,GHA,3059.4136985984,71.49707,12.4526277301014,24512104,50.713,60.8937317073171,1.40614287281402,74.7 76 | 2010,YR2010,Gibraltar,GIB,..,..,..,33189,100,..,..,.. 77 | 2010,YR2010,Greece,GRC,28726.0792836873,..,0.319341036775021,11121341,76.292,80.3878048780488,0.00465295037622427,3.9 78 | 2010,YR2010,Greenland,GRL,..,..,0,56905,84.383,70.8570731707317,..,.. 79 | 2010,YR2010,Grenada,GRD,11177.9997155594,..,0,104677,35.681,72.572,0.0657143000344511,14.6 80 | 2010,YR2010,Guam,GUM,..,..,0,159444,94.099,78.1828536585366,..,.. 81 | 2010,YR2010,Guatemala,GTM,6714.06359554651,..,2.56791297320424,14630417,49.323,71.3862195121951,0.415223670367532,35.4 82 | 2010,YR2010,Guinea,GIN,1575.38231056336,25.30774,25.3098754121828,10794170,34.856,56.743243902439,3.93317126518011,109.4 83 | 2010,YR2010,Guinea-Bissau,GNB,1400.36271908519,..,14.9952030484571,1555880,45.221,55.0005365853659,2.91770453827323,114 84 | 2010,YR2010,Guyana,GUY,5847.85725499679,..,23.3507982149709,746556,28.239,66.0264146341463,0.620354537445157,37.6 85 | 2010,YR2010,Haiti,HTI,1502.03271141224,..,1.19217646335721,9999617,52.016,61.2531951219512,1.10042272473878,208 86 | 2010,YR2010,Honduras,HND,3971.18299352727,84.75536,2.74079141590315,8194778,51.696,72.3950487804878,0.468260349045002,23.4 87 | 2010,YR2010,"Hong Kong SAR, China",HKG,48107.7222260692,..,0.00143899522167942,7024200,100,82.9780487804878,..,.. 88 | 2010,YR2010,Hungary,HUN,22404.2391969471,..,0.45008805396801,10000023,68.859,74.2073170731707,0.0210294733417512,6 89 | 2010,YR2010,Iceland,ISL,38815.2756502659,..,0,318041,93.624,81.8975609756098,0.00772259775639771,2.6 90 | 2010,YR2010,India,IND,4404.69700530452,..,4.65324116752377,1230980691,30.93,66.6399268292683,0.615066291933603,58.8 91 | 2010,YR2010,Indonesia,IDN,8433.49735678452,..,6.79061535639821,242524123,49.924,68.1527317073171,0.433617648387686,33.3 92 | 2010,YR2010,"Iran, Islamic Rep.",IRN,17942.8328426148,..,22.6287932942169,74567511,70.626,73.9561951219512,0.056345325181891,19.3 93 | 2010,YR2010,Iraq,IRQ,12717.5975271684,..,40.8701874497255,30762701,69.034,68.4821707317073,0.263525328126497,36.9 94 | 2010,YR2010,Ireland,IRL,44030.3786080134,..,0.132394839364518,4560155,61.84,80.7439024390244,0.0155001309815943,4.2 95 | 2010,YR2010,Isle of Man,IMN,..,..,0,80072,51.994,..,..,.. 96 | 2010,YR2010,Israel,ISR,29743.0058727916,..,0.139876012840069,7623600,91.824,81.6024390243903,0.017914485426704,4.6 97 | 2010,YR2010,Italy,ITA,36201.1615672134,..,0.099121878005061,59277417,68.327,82.0365853658537,0.0055044434086203,4 98 | 2010,YR2010,Jamaica,JAM,7996.01948795873,..,1.17053847438014,2817210,53.743,74.8464878048781,0.214490262402002,18.1 99 | 2010,YR2010,Japan,JPN,35749.7712106096,..,0.0178600840402967,128070000,90.522,82.8426829268293,0.00780589688003526,3.2 100 | 2010,YR2010,Jordan,JOR,9472.76832624669,92.55104,1.6722944392729,7182390,82.473,73.4383902439024,0.225321410285137,21.1 101 | 2010,YR2010,Kazakhstan,KAZ,20096.9180664411,99.78163,21.0293700421972,16321581,53.732,68.2953658536585,0.0558173216540696,21.5 102 | 2010,YR2010,Kenya,KEN,2475.84353728418,..,3.10298088331619,41350152,23.571,62.8881463414634,2.93638985602935,62.2 103 | 2010,YR2010,Kiribati,KIR,1731.53862650466,..,0.063495205418365,102652,43.773,65.2775609756098,0.41324557792366,62.7 104 | 2010,YR2010,"Korea, Dem. People’s Rep.",PRK,..,..,..,24591599,60.21,69.3273170731707,0.178399786239203,29.5 105 | 2010,YR2010,"Korea, Rep.",KOR,30352.1048190303,..,0.0318395143480391,49554112,81.936,80.5512195121951,0.0181105474570493,4.1 106 | 2010,YR2010,Kosovo,XKX,7875.93115673265,..,4.10759284715746,1775680,..,69.9,..,.. 107 | 2010,YR2010,Kuwait,KWT,75204.145116617,94.46901,48.4015054465816,2998083,98.263,74.1328536585366,0.0127389253674844,10.8 108 | 2010,YR2010,Kyrgyz Republic,KGZ,2790.17066394311,..,11.278479089317,5447900,35.303,69.3,0.269139036060874,29.6 109 | 2010,YR2010,Lao PDR,LAO,3983.5021426628,..,17.5689326184019,6246274,33.123,64.3032926829268,1.07056821484715,78.9 110 | 2010,YR2010,Latvia,LVA,18251.8238805559,..,1.22907527798356,2097555,67.692,73.4829268292683,0.0276923314808182,7.8 111 | 2010,YR2010,Lebanon,LBN,16451.7598336606,..,0.0015507153287506,4337141,87.183,78.5382682926829,0.0360757250441746,10.3 112 | 2010,YR2010,Lesotho,LSO,2365.68346109084,..,3.67549643906119,2040551,24.753,50.6612195121951,1.99754150088158,99.7 113 | 2010,YR2010,Liberia,LBR,699.653721510599,..,29.1473802049564,3948125,47.801,59.605512195122,4.20654642040136,89.3 114 | 2010,YR2010,Libya,LBY,29630.2221484622,..,54.9834017422433,6169140,77.642,71.7369756097561,0.0231057752199354,16.6 115 | 2010,YR2010,Liechtenstein,LIE,..,..,0.00924063538229507,36003,14.464,81.8414634146342,..,.. 116 | 2010,YR2010,Lithuania,LTU,21069.2853787989,..,0.526711320034481,3097282,66.757,73.2682926829268,0.0129020924675852,6.1 117 | 2010,YR2010,Luxembourg,LUX,91743.2938905974,..,0.0704735209096316,506953,88.547,80.6317073170732,0.0179832995605045,2.8 118 | 2010,YR2010,"Macao SAR, China",MAC,98184.3763086752,..,0.000909141683636676,536969,100,82.5735609756098,..,.. 119 | 2010,YR2010,"Macedonia, FYR",MKD,11355.2684675862,..,4.72525512210799,2070739,56.992,74.6040975609756,0.011947170367219,10.4 120 | 2010,YR2010,Madagascar,MDG,1385.68507168039,..,6.850760849933,21151640,31.929,63.3578048780488,2.16862145285469,62.2 121 | 2010,YR2010,Malawi,MWI,1032.9628806199,61.30972,5.76901543442764,15167095,15.54,57.156243902439,3.60644817119267,90.5 122 | 2010,YR2010,Malaysia,MYS,21107.2679372523,93.11789,8.44976515044475,28112289,70.912,74.2868536585366,0.099886826840775,7.9 123 | 2010,YR2010,Maldives,MDV,11924.0830709081,..,0.00998770122601358,367000,39.984,76.1569024390244,0.220232511605884,13.2 124 | 2010,YR2010,Mali,MLI,1871.98474385098,31.09975,12.2344720298932,15075085,35.996,55.229756097561,4.2222915339428,136.5 125 | 2010,YR2010,Malta,MLT,28329.7049010996,..,0,414508,94.665,81.3975609756098,0.0152937677920812,6.8 126 | 2010,YR2010,Marshall Islands,MHL,3479.15530340683,..,0,52425,71.343,..,..,39.2 127 | 2010,YR2010,Mauritania,MRT,3316.97478042488,..,48.7646837420208,3609543,56.682,61.9513658536585,3.51796686905004,97.4 128 | 2010,YR2010,Mauritius,MUS,15938.4192089782,..,0.00683374042283496,1250400,40.579,72.9673170731707,0.0930451778368066,15 129 | 2010,YR2010,Mexico,MEX,15534.9456319503,93.06894,4.85468095108734,117318941,77.825,76.0338536585366,0.115998784343087,17.3 130 | 2010,YR2010,"Micronesia, Fed. Sts.",FSM,3297.65693360804,..,0.0275354181258015,103616,22.298,68.5505609756098,0.387188724379149,40 131 | 2010,YR2010,Moldova,MDA,3910.86053324527,..,0.109995411729961,3562045,44.886,69.5106585365854,0.0464124310489103,17.2 132 | 2010,YR2010,Monaco,MCO,..,..,0,37094,100,..,..,4 133 | 2010,YR2010,Mongolia,MNG,7708.59999413593,98.257,38.611251712621,2712650,67.567,67.4455609756098,0.172413737638596,26.1 134 | 2010,YR2010,Montenegro,MNE,14034.5076558934,..,1.03128015237844,619428,63.096,75.2035365853659,0.0143648405251169,6.8 135 | 2010,YR2010,Morocco,MAR,6443.25361319481,..,2.71107381368626,32409639,57.684,73.9410731707317,0.39677231309049,33.5 136 | 2010,YR2010,Mozambique,MOZ,917.763832040531,..,9.86424652270358,24221405,30.955,54.601756097561,3.3441427840629,100.8 137 | 2010,YR2010,Myanmar,MMR,3721.21144009279,..,4.94123949053922,50155896,31.405,65.1041707317073,0.493720290369707,64.4 138 | 2010,YR2010,Namibia,NAM,8460.84736812378,..,2.89865195836173,2173170,41.616,58.0746097560976,1.19213606432239,56 139 | 2010,YR2010,Nauru,NRU,6592.43683574598,..,..,10025,100,..,..,39.4 140 | 2010,YR2010,Nepal,NPL,1985.98034060169,..,1.47069217400499,27023137,16.822,67.9101707317073,1.04208503137061,47 141 | 2010,YR2010,Netherlands,NLD,45524.6621425733,..,0.479104160659833,16615394,87.061,80.7024390243902,0.0124042162359281,4.5 142 | 2010,YR2010,New Caledonia,NCL,..,..,..,250000,67.273,77.4731707317073,..,.. 143 | 2010,YR2010,New Zealand,NZL,32252.9188480016,..,2.01301500469197,4350700,86.165,80.7024390243902,0.025735751655278,6.2 144 | 2010,YR2010,Nicaragua,NIC,4028.71208738128,..,3.94358331287412,5737723,57.255,73.5923414634146,0.460656331130529,24.6 145 | 2010,YR2010,Niger,NER,813.977388000367,..,10.0627523005313,16425578,17.559,56.8454146341463,5.20117418087686,123.3 146 | 2010,YR2010,Nigeria,NGA,5150.15881822763,..,13.7946276203046,158578261,43.48,50.8372682926829,4.94207765986988,129.6 147 | 2010,YR2010,Northern Mariana Islands,MNP,..,..,0,54424,89.474,..,..,.. 148 | 2010,YR2010,Norway,NOR,62350.4107068751,..,7.68670810151464,4889252,79.102,80.9975609756098,0.0103750733170321,3.2 149 | 2010,YR2010,Oman,OMN,45334.8294308969,86.939,37.1975695592929,3041460,75.161,75.9700731707317,0.062447993047174,11.7 150 | 2010,YR2010,Pakistan,PAK,4283.60731163635,55.37519,2.15982681334308,170560182,36.598,65.1500487804878,0.864325092412845,92.1 151 | 2010,YR2010,Palau,PLW,12411.9647719852,..,0,20470,83.358,..,..,19.3 152 | 2010,YR2010,Panama,PAN,15419.341616565,94.09412,0.257356101135916,3643222,65.115,76.8551951219512,0.271614006176065,19.7 153 | 2010,YR2010,Papua New Guinea,PNG,3191.57914928813,..,43.5244909276789,7108239,13.019,64.640243902439,0.975312391222675,65.2 154 | 2010,YR2010,Paraguay,PRY,7289.51391005233,93.87092,2.63899154726928,6209877,58.487,72.298243902439,0.421975814372818,24.1 155 | 2010,YR2010,Peru,PER,9956.55663668307,..,11.1018860775945,29373646,76.915,73.6513658536585,0.245721906281757,20.3 156 | 2010,YR2010,Philippines,PHL,5596.8372879546,..,2.90210242848751,93726624,45.255,68.3582682926829,0.422316792969563,31.9 157 | 2010,YR2010,Poland,POL,21770.6444359772,..,1.58636734693208,38042794,60.892,76.2463414634146,0.00535810114388046,6 158 | 2010,YR2010,Portugal,PRT,27238.4606724422,..,0.259236249272664,10573100,60.567,79.0268292682927,0.0146433578852986,3.9 159 | 2010,YR2010,Puerto Rico,PRI,34453.7684264135,91.96523,0,3721525,93.825,78.4081707317073,0.0273502467138672,.. 160 | 2010,YR2010,Qatar,QAT,125140.838348001,96.28374,17.8928637022579,1779676,98.655,77.6404878048781,0.0419527886229003,9 161 | 2010,YR2010,Romania,ROU,17817.9695126628,..,1.485966475227,20246871,53.829,73.4585365853659,0.0438939596111201,11.5 162 | 2010,YR2010,Russian Federation,RUS,23107.7852212822,99.68427,13.9012640776876,142849449,73.687,68.8412195121951,0.0455146765735215,10 163 | 2010,YR2010,Rwanda,RWA,1353.15407202718,65.85227,6.70165034478188,10246842,23.952,63.0655609756098,1.72604254844893,63.6 164 | 2010,YR2010,Samoa,WSM,5399.89309631838,..,0.425260194040711,186205,20.078,73.2300243902439,0.270141023141134,18.9 165 | 2010,YR2010,San Marino,SMR,73862.3374374501,..,..,31110,94.086,83.1593791574279,..,3.4 166 | 2010,YR2010,Sao Tome and Principe,STP,2641.51394857495,..,3.01216130009387,174776,61.907,65.8086097560976,0.819613605279002,45.4 167 | 2010,YR2010,Saudi Arabia,SAU,45421.2284797446,..,42.254709564031,27425676,82.084,73.7001463414634,0.0426532982111869,15.7 168 | 2010,YR2010,Senegal,SEN,2183.99505141766,..,4.16200653838546,12916229,42.23,64.0836341463415,2.01716466561538,66.8 169 | 2010,YR2010,Serbia,SRB,12688.0349691633,..,2.28231842611919,7291436,55.208,74.3365853658537,0.0239058369891909,7.6 170 | 2010,YR2010,Seychelles,SYC,20365.1189331513,93.95423,0.147076426024177,89770,52.319,73.1975609756098,..,14.4 171 | 2010,YR2010,Sierra Leone,SLE,1217.21923498169,..,10.3114337656825,6458720,38.241,48.212,7.98155790916999,159.7 172 | 2010,YR2010,Singapore,SGP,72105.4139202794,95.85733,0.00045970964203152,5076732,100,81.5414634146342,0.0140646216274598,2.8 173 | 2010,YR2010,Sint Maarten (Dutch part),SXM,..,..,..,35474,100,..,..,.. 174 | 2010,YR2010,Slovak Republic,SVK,25159.0775487579,..,0.331711644728117,5391428,54.685,75.1121951219512,0.00799345009224338,7 175 | 2010,YR2010,Slovenia,SVN,28678.3726303687,..,0.276108866727978,2048583,50.04,79.4219512195122,0.0139111287228482,3.2 176 | 2010,YR2010,Solomon Islands,SLB,1782.82935002354,..,21.2459186032906,527790,20.048,68.6228292682927,0.610405944054011,28.1 177 | 2010,YR2010,Somalia,SOM,..,..,..,12053223,37.259,53.9911951219512,5.43496474747067,159.2 178 | 2010,YR2010,South Africa,ZAF,12028.9339762531,92.87732,7.66313493460826,50979432.3622277,62.218,55.8696097560976,0.380548923279963,53.7 179 | 2010,YR2010,South Sudan,SSD,3789.92344190339,..,41.7299093714052,10067192,17.855,53.6319756097561,4.54304494346724,112.5 180 | 2010,YR2010,Spain,ESP,32507.0939660629,97.7489,0.0650459062557332,46576897,78.442,81.6268292682927,0.00673895695451097,3.8 181 | 2010,YR2010,Sri Lanka,LKA,8563.21243914991,91.18136,0.180674017820145,20119000,18.321,74.3130243902439,0.0799683134807594,11.2 182 | 2010,YR2010,St. Kitts and Nevis,KNA,21412.201453108,..,0,51445,31.806,..,..,12 183 | 2010,YR2010,St. Lucia,LCA,11798.4257915794,..,0.0256122712364086,172580,18.45,74.4506585365854,0.110870946746659,15.4 184 | 2010,YR2010,St. Martin (French part),MAF,..,..,..,30235,..,78.7219512195122,..,.. 185 | 2010,YR2010,St. Vincent and the Grenadines,VCT,9915.57761891195,..,0.0352867084603901,109315,48.785,72.334243902439,0.10881371326731,20.7 186 | 2010,YR2010,Sudan,SDN,3366.09626107549,..,13.8451253468617,34385963,33.08,62.600756097561,1.6830401935313,76.7 187 | 2010,YR2010,Suriname,SUR,14211.8775163255,94.67575,24.3149942292226,526103,66.344,70.364243902439,0.426363375058518,24.1 188 | 2010,YR2010,Swaziland,SWZ,7296.51111860929,83.09829,2.29083193657083,1202843,21.492,51.4481951219512,1.53050593869213,97.4 189 | 2010,YR2010,Sweden,SWE,42942.5625032962,..,1.01348957113932,9378126,85.056,81.4512195121951,0.00797651053344222,3 190 | 2010,YR2010,Switzerland,CHE,55866.3050707537,..,0.0154286608848631,7824909,73.663,82.2463414634147,0.00851980691319242,4.5 191 | 2010,YR2010,Syrian Arab Republic,SYR,..,..,..,21018834,55.677,72.3398292682927,0.154197548401651,16.3 192 | 2010,YR2010,Tajikistan,TJK,2106.33875580206,..,1.32942618844532,7641630,26.516,69.8023658536585,0.13861591867205,52.4 193 | 2010,YR2010,Tanzania,TZA,2090.58322467574,67.8007,7.47328811319093,46098591,28.114,60.8556829268293,2.9039869189468,71.9 194 | 2010,YR2010,Thailand,THA,13486.5624149548,96.43091,2.31298268874176,67208808,44.08,73.8941951219512,0.0341975732927822,14.9 195 | 2010,YR2010,Timor-Leste,TLS,1702.23271594834,58.30898,..,1109591,29.507,67.3004634146342,1.91054615855088,62.6 196 | 2010,YR2010,Togo,TGO,1208.2812098328,..,17.4772678475936,6502952,37.533,57.442512195122,1.93918188612011,90.3 197 | 2010,YR2010,Tonga,TON,4984.19362951029,..,0.0481668272496717,104137,23.389,72.1481951219512,0.495210554311673,17.4 198 | 2010,YR2010,Trinidad and Tobago,TTO,31260.9070471763,..,12.4604532394579,1328100,9.092,69.7925365853659,0.122089872793156,22.4 199 | 2010,YR2010,Tunisia,TUN,10436.3655998628,79.13058,5.90184696901596,10639931,65.934,74.8112682926829,0.145173283993364,17.4 200 | 2010,YR2010,Turkey,TUR,17959.2591065745,92.6606,0.56299495140787,72326914,70.715,74.0940487804878,0.0530417563194452,19.1 201 | 2010,YR2010,Turkmenistan,TKM,9942.4269559212,..,19.0749331900952,5087210,48.402,66.6053902439024,0.119649494283018,62 202 | 2010,YR2010,Turks and Caicos Islands,TCA,..,..,..,30994,90.228,..,..,.. 203 | 2010,YR2010,Tuvalu,TUV,3006.53679917393,..,0,10531,54.796,..,..,30.7 204 | 2010,YR2010,Uganda,UGA,1515.8665813571,73.21188,10.7082594150591,33915133,14.492,57.0776341463415,2.7221073632811,81 205 | 2010,YR2010,Ukraine,UKR,7824.49845171438,..,8.4312099805582,45870700,68.686,70.2653658536586,0.0374774066570103,11.7 206 | 2010,YR2010,United Arab Emirates,ARE,57579.8351654688,..,20.8352445753649,8270684,84.055,76.6948780487805,0.012545213960632,8.6 207 | 2010,YR2010,United Kingdom,GBR,36366.9831782522,..,0.880358346248581,62766365,81.302,80.4024390243902,0.0184357590072691,5.2 208 | 2010,YR2010,United States,USA,49372.6192042539,..,0.982306918899239,309348193,80.772,78.5414634146342,0.0274820388767194,7.3 209 | 2010,YR2010,Uruguay,URY,17082.4046549884,98.07271,2.03231910489815,3374415,94.414,76.3961219512195,0.0391652654243209,10.6 210 | 2010,YR2010,Uzbekistan,UZB,4239.70220839215,..,14.3931138511203,28562400,36.191,69.9684390243902,0.10962345225979,36.3 211 | 2010,YR2010,Vanuatu,VUT,2948.03293729183,..,0.79665333033355,236295,24.589,70.8031219512195,0.352968476284299,29.2 212 | 2010,YR2010,"Venezuela, RB",VEN,16544.9720552598,..,10.2057190790167,29028033,88.769,73.6751463414634,0.262236404403123,17.1 213 | 2010,YR2010,Vietnam,VNM,4486.25945896288,..,8.20637827153666,86932500,30.392,74.9704390243903,0.122849248151756,23.3 214 | 2010,YR2010,Virgin Islands (U.S.),VIR,..,..,0,106267,94.594,79.1731707317073,..,.. 215 | 2010,YR2010,West Bank and Gaza,PSE,..,94.93201,0,3811102,74.136,72.447,0.26062436335277,23 216 | 2010,YR2010,"Yemen, Rep.",YEM,4478.7436902936,..,21.29077790011,23606779,31.732,63.498243902439,2.00400284187901,56.3 217 | 2010,YR2010,Zambia,ZMB,3279.27716144476,83.00767,21.3874433595246,13850033,38.725,56.5244878048781,1.53012492689212,83.1 218 | 2010,YR2010,Zimbabwe,ZWE,1474.87713733128,..,9.87171374597581,14086317,33.196,52.8778048780488,1.91772041544519,89.9 219 | ,,,,,,,,,,, 220 | ,,,,,,,,,,, 221 | ,,,,,,,,,,, 222 | Data from database: World Development Indicators,,,,,,,,,,, 223 | Last Updated: 01/19/2018,,,,,,,,,,, 224 | -------------------------------------------------------------------------------- /exercise_solutions/For Class.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Set your working directory here\n", 10 | "setwd(\"/users/nick/github/InstructionalMaterials/DefensiveProgramming/exercises/\")" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 5, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "# Load data. Need foreign packages. Can be\n", 20 | "# installed with command `install_package(\"foreign\")` if\n", 21 | "# you don't already have it!\n", 22 | "library(foreign)\n", 23 | "\n", 24 | "# Load world development indicators\n", 25 | "wdi = read.dta(\"wdi.dta\")\n", 26 | "\n", 27 | "# Load polity scores\n", 28 | "polity = read.dta(\"polity.dta\")\n" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 6, 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [ 37 | "# Bring two datasets together\n", 38 | "merged = merge(polity, wdi, by.x=\"country\", by.y=\"country_name\", all.x=TRUE, all.y=FALSE)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 8, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "# Basic specification\n", 48 | "model1 = lm('polity ~ natural_resources_pct_gdp + gdp_per_cap' , merged)\n", 49 | "\n", 50 | "# Does life expectancy matter?\n", 51 | "model2 = lm('polity ~ natural_resources_pct_gdp + gdp_per_cap + life_expectancy', merged)\n", 52 | "\n", 53 | "# What about under 5 mortality?\n", 54 | "model3 = lm('polity ~ natural_resources_pct_gdp + gdp_per_cap + under5_mortality', merged)\n", 55 | "\n", 56 | "# Maybe maternal dealth risk\n", 57 | "model4 = lm('polity ~ natural_resources_pct_gdp + gdp_per_cap + maternal_death_risk', merged)" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": null, 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [] 66 | } 67 | ], 68 | "metadata": { 69 | "kernelspec": { 70 | "display_name": "R", 71 | "language": "R", 72 | "name": "ir" 73 | }, 74 | "language_info": { 75 | "codemirror_mode": "r", 76 | "file_extension": ".r", 77 | "mimetype": "text/x-r-source", 78 | "name": "R", 79 | "pygments_lexer": "r", 80 | "version": "3.3.2" 81 | } 82 | }, 83 | "nbformat": 4, 84 | "nbformat_minor": 2 85 | } 86 | -------------------------------------------------------------------------------- /exercise_solutions/polity.dta: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickeubank/DefensiveProgramming/49e64ef9b885c09742feac66ad6c06c5a081a864/exercise_solutions/polity.dta -------------------------------------------------------------------------------- /exercise_solutions/resource_curse_regressions.tex: -------------------------------------------------------------------------------- 1 | 2 | % Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu 3 | % Date and time: Fri, Jan 26, 2018 - 22:26:09 4 | \begin{table}[!htbp] \centering 5 | \caption{Resource curse and public health} 6 | \label{} 7 | \begin{tabular}{@{\extracolsep{5pt}}lcccc} 8 | \\[-1.8ex]\hline 9 | \hline \\[-1.8ex] 10 | & \multicolumn{4}{c}{Polity Score} \\ 11 | \cline{2-5} 12 | \\[-1.8ex] & (1) & (2) & (3) & (4)\\ 13 | \hline \\[-1.8ex] 14 | natural\_resources\_pct\_gdp & $-$0.153 & $-$0.064 & $-$0.046 & $-$0.123 \\ 15 | & (0.098) & (0.106) & (0.101) & (0.103) \\ 16 | & & & & \\ 17 | gdp\_per\_cap & 0.0001$^{*}$ & $-$0.00000 & $-$0.00003 & 0.0001 \\ 18 | & (0.0001) & (0.0001) & (0.0001) & (0.0001) \\ 19 | & & & & \\ 20 | life\_expectancy & & 0.383$^{**}$ & & \\ 21 | & & (0.186) & & \\ 22 | & & & & \\ 23 | under5\_mortality & & & $-$0.113$^{***}$ & \\ 24 | & & & (0.035) & \\ 25 | & & & & \\ 26 | maternal\_death\_risk & & & & $-$0.824 \\ 27 | & & & & (0.916) \\ 28 | & & & & \\ 29 | Constant & 1.300 & $-$24.307$^{*}$ & 7.084$^{***}$ & 2.232 \\ 30 | & (1.839) & (12.555) & (2.560) & (2.163) \\ 31 | & & & & \\ 32 | \hline \\[-1.8ex] 33 | Observations & 160 & 160 & 159 & 159 \\ 34 | R$^{2}$ & 0.034 & 0.059 & 0.094 & 0.039 \\ 35 | Adjusted R$^{2}$ & 0.021 & 0.041 & 0.077 & 0.020 \\ 36 | Residual Std. Error & 14.955 (df = 157) & 14.802 (df = 156) & 14.564 (df = 155) & 15.003 (df = 155) \\ 37 | F Statistic & 2.728$^{*}$ (df = 2; 157) & 3.273$^{**}$ (df = 3; 156) & 5.365$^{***}$ (df = 3; 155) & 2.079 (df = 3; 155) \\ 38 | \hline 39 | \hline \\[-1.8ex] 40 | \textit{Note:} & \multicolumn{4}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\ 41 | \end{tabular} 42 | \end{table} 43 | -------------------------------------------------------------------------------- /exercise_solutions/solutions_latex.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickeubank/DefensiveProgramming/49e64ef9b885c09742feac66ad6c06c5a081a864/exercise_solutions/solutions_latex.pdf -------------------------------------------------------------------------------- /exercise_solutions/solutions_latex.tex: -------------------------------------------------------------------------------- 1 | \documentclass[12pt]{article} 2 | 3 | \title{The Resource Curse: Controlling for Health Outcomes} 4 | \author{Your name here!\footnote{University of Awesome}} 5 | \date{\today} 6 | 7 | \begin{document} 8 | \maketitle 9 | 10 | 11 | For no justifiable theoretically reason whatsoever, this paper examines whether we find support for the resource curse persists in a cross-sectional analysis after we control for different measures of public health! 12 | 13 | To test, we run a series of regressions, presented in Table~\ref{resourcecurseregressions} below. 14 | 15 | \input{resource_curse_regressions.tex} 16 | 17 | \end{document} 18 | -------------------------------------------------------------------------------- /exercise_solutions/solutions_r.r: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Set your working directory here 4 | setwd("/users/nick/github/InstructionalMaterials/DefensiveProgramming/exercise_solutions/") 5 | 6 | # Load data. Need foreign packages. Can be 7 | # installed with command `install_package("foreign")` if 8 | # you don't already have it! 9 | library(foreign) 10 | 11 | # Load world development indicators 12 | wdi = read.dta("wdi.dta") 13 | 14 | # In this case duplicate entries are perfect duplicates, 15 | # so I can just drop 16 | wdi = wdi[ !duplicated(wdi$country_name), ] 17 | 18 | # Load polity scores 19 | polity = read.dta("polity.dta") 20 | 21 | ##### 22 | # Bring two datasets together 23 | ##### 24 | # Check for duplicates 25 | stopifnot( !anyDuplicated(polity$country) ) 26 | stopifnot( !anyDuplicated(wdi$country_name) ) 27 | 28 | # Fix a few names 29 | polity[polity$country == "Myanmar (Burma)", 'country'] = "Myanmar" 30 | polity[polity$country == "Russia", 'country'] = "Russian Federation" 31 | 32 | wdi$temp = 1 33 | merged = merge(polity, wdi, by.x="country", by.y="country_name", all.x=TRUE, all.y=FALSE) 34 | 35 | # Make sure all polities merged with a wdi entry 36 | stopifnot( !any( is.na(merged$temp) ) ) 37 | 38 | # Run some regressions! Let's see if resource curse is dependent on various 39 | # measures of public health! 40 | 41 | base_specification = 'polity ~ natural_resources_pct_gdp + gdp_per_cap' 42 | 43 | # Basic specification 44 | model1 = lm( paste(base_specification) , merged) 45 | 46 | # Does life expectancy matter? 47 | model2 = lm( paste(base_specification, ' + life_expectancy'), merged) 48 | 49 | # What about under 5 mortality? 50 | model3 = lm(paste(base_specification, ' + under5_mortality'), merged) 51 | 52 | # Maybe maternal dealth risk 53 | model4 = lm(paste(base_specification, ' + maternal_death_risk'), merged) 54 | 55 | library(stargazer) 56 | stargazer(list(model1, model2, model3, model4), title="Resource curse and public health", 57 | type="latex", dep.var.caption="Polity Score", 58 | dep.var.labels.include=FALSE, 59 | out='resource_curse_regressions.tex' ) 60 | -------------------------------------------------------------------------------- /exercise_solutions/solutions_stata.do: -------------------------------------------------------------------------------- 1 | * Set working directory here 2 | cd /users/nick/github/instructionalmaterials/defensiveprogramming/exercise_solutions 3 | 4 | * Load WDI data. 5 | * Provides data on all possibly included countries. 6 | use wdi, clear 7 | 8 | * Have to get rid of duplicates! 9 | * They're prefect duplicates, so 10 | * doesn't require force option. 11 | duplicates drop 12 | 13 | 14 | * Merge with polity. 15 | * Should be a perfect subset of WDI data. 16 | rename country_name country 17 | 18 | * Fix imperfect merge values 19 | replace country = "Myanmar (Burma)" if country == "Myanmar" 20 | replace country = "Russia" if country == "Russian Federation" 21 | 22 | merge 1:1 country using polity.dta 23 | 24 | * Since polity is a perfect subset, then all should merge! 25 | assert _m != 2 26 | 27 | ********** 28 | * Let's run some regressions! 29 | ********** 30 | 31 | * Put common controls into a macro to prevent duplication 32 | local base_specification = "polity natural_resources_pct gdp_per_cap" 33 | 34 | 35 | * Export via esttab! 36 | set more off 37 | eststo clear 38 | 39 | * Basic specification 40 | eststo: reg `base_specification' 41 | 42 | * Does life expectancy matter? 43 | eststo: reg `base_specification' life_expectancy 44 | 45 | * What about under 5 mortality? 46 | eststo: reg `base_specification' under5_mortality 47 | 48 | * Maybe maternal dealth risk 49 | eststo: reg `base_specification' maternal_death_risk 50 | 51 | esttab 52 | 53 | esttab using resource_curse_regressions.tex, label replace /// 54 | title("Resource Curse and Public Health! \label{resourcecurseregressions}") 55 | -------------------------------------------------------------------------------- /exercise_solutions/wdi.dta: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickeubank/DefensiveProgramming/49e64ef9b885c09742feac66ad6c06c5a081a864/exercise_solutions/wdi.dta -------------------------------------------------------------------------------- /exercises/polity.dta: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickeubank/DefensiveProgramming/49e64ef9b885c09742feac66ad6c06c5a081a864/exercises/polity.dta -------------------------------------------------------------------------------- /exercises/starter_code.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Set your working directory here 4 | setwd("/users/nick/github/InstructionalMaterials/DefensiveProgramming/exercises/") 5 | 6 | # Load data. Need foreign packages. Can be 7 | # installed with command `install_package("foreign")` if 8 | # you don't already have it! 9 | library(foreign) 10 | 11 | # Load world development indicators 12 | wdi = read.dta("wdi.dta") 13 | 14 | # Load polity scores 15 | polity = read.dta("polity.dta") 16 | 17 | # Bring two datasets together 18 | merged = merge(polity, wdi, by.x="country", by.y="country_name", all.x=TRUE, all.y=FALSE) 19 | 20 | # Run some regressions! Let's see if resource curse is dependent on various 21 | # measures of public health! 22 | 23 | # Basic specification 24 | model1 = lm('polity ~ natural_resources_pct_gdp + gdp_per_cap' , merged) 25 | 26 | # Does life expectancy matter? 27 | model2 = lm('polity ~ natural_resources_pct_gdp + gdp_per_cap + life_expectancy', merged) 28 | 29 | # What about under 5 mortality? 30 | model3 = lm('polity ~ natural_resources_pct_gdp + gdp_per_cap + under5_mortality', merged) 31 | 32 | # Maybe maternal dealth risk 33 | model4 = lm('polity ~ natural_resources_pct_gdp + gdp_per_cap + maternal_death_risk + ', merged) 34 | -------------------------------------------------------------------------------- /exercises/starter_code.do: -------------------------------------------------------------------------------- 1 | * Set working directory here 2 | cd /users/nick/github/instructionalmaterials/defensiveprogramming/exercises 3 | 4 | * Load WDI data. 5 | * Provides data on all possibly included countries. 6 | use wdi, clear 7 | 8 | * Merge with polity. 9 | * Should be a perfect subset of WDI data. 10 | rename country_name country 11 | merge 1:1 country using polity.dta 12 | 13 | ********** 14 | * Let's run some regressions! 15 | ********** 16 | 17 | * Basic specification 18 | reg polity natural_resources_pct_gdp gdp_per_cap 19 | 20 | * Does life expectancy matter? 21 | reg polity natural_resources_pct_gdp gdp_per_cap life_expectancy 22 | 23 | * What about under 5 mortality? 24 | reg polity natural_resources_pct_gdp gdp_per_cap under5_mortality 25 | 26 | * Maybe maternal dealth risk 27 | reg polity natural_resources_pct_gdp gdp_per_cap maternal_death_risk 28 | -------------------------------------------------------------------------------- /exercises/starter_latex.tex: -------------------------------------------------------------------------------- 1 | \documentclass[12pt]{article} 2 | 3 | \title{The Resource Curse: Controlling for Health Outcomes} 4 | \author{Your name here!\footnote{University of Awesome}} 5 | \date{\today} 6 | 7 | \begin{document} 8 | \maketitle 9 | 10 | 11 | For no justifiable theoretically reason whatsoever, this paper examines whether we find support for the resource curse persists in a cross-sectional analysis after we control for different measures of public health! 12 | 13 | To test, we run a series of regressions, presented in Table~\ref{resourcecurseregressions} below. 14 | 15 | % Put table here! 16 | 17 | \end{document} 18 | -------------------------------------------------------------------------------- /exercises/wdi.dta: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickeubank/DefensiveProgramming/49e64ef9b885c09742feac66ad6c06c5a081a864/exercises/wdi.dta --------------------------------------------------------------------------------