reportwritingfordatascience - paasp.net · stayintouch!...

120

Upload: others

Post on 25-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo
Page 2: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Report Writing for Data Sciencein R

Roger D. Peng

This book is for sale at http://leanpub.com/reportwriting

This version was published on 2016-01-19

This is a Leanpub book. Leanpub empowers authors andpublishers with the Lean Publishing process. LeanPublishing is the act of publishing an in-progress ebookusing lightweight tools and many iterations to get readerfeedback, pivot until you have the right book and buildtraction once you do.

© 2015 - 2016 Roger D. Peng

Page 3: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Also By Roger D. PengR Programming for Data Science

The Art of Data Science

Exploratory Data Analysis with R

Page 4: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Contents

Stay in Touch! . . . . . . . . . . . . . . . . . . . . . . . 1

Getting Started with R . . . . . . . . . . . . . . . . . . 2

What is Reproducible Reporting? . . . . . . . . . . . 4

The Data Science Pipeline . . . . . . . . . . . . . . . . 11

Literate Statistical Programming . . . . . . . . . . . 15

Organizing a Data Analysis . . . . . . . . . . . . . . . 18

Structure of a Data Analysis: Part 1 . . . . . . . . . . 25

Structure of a Data Analysis: Part 2 . . . . . . . . . . 33

Markdown . . . . . . . . . . . . . . . . . . . . . . . . . 51

Using knitr for Reproducible Reports . . . . . . . . . 58

Communicating Results Over E-mail . . . . . . . . . 80

Reproducibility Check List . . . . . . . . . . . . . . . 85

Evidence-based Data Analysis . . . . . . . . . . . . . 97

Public Reproducibility Resources . . . . . . . . . . . 113

About the Author . . . . . . . . . . . . . . . . . . . . . 116

Page 5: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Stay in Touch!Thanks for purchasing this book. If you are interested inhearing more from me about things that I’m working on(books, data science courses, podcast, etc.), you can do twothings.

First, I encourage you to join my mailing list of LeanpubReaders1.On this list I sendout updates ofmyownactivitiesas well as occasional comments on data science currentevents. I’ll also let you know what my co-conspirators JeffLeek and Brian Caffo are up to because sometimes they doreally cool stuff.

Second, I have a regular podcast called Not So StandardDeviations2 that I co-host with Dr. Hilary Parker, a SeniorData Analyst at Etsy. On this podcast, Hilary and I talkabout the craft of data science and discuss common issuesand problems in analyzing data. We’ll also compare howdata science is approached in both academia and industrycontexts and discuss the latest industry trends.

You can listen to recent episodes on our SoundCloud pageor you can subscribe to it in iTunes3 or your favorite pod-casting app.

Thanks again for purchasing this book and please do stay intouch!

1http://eepurl.com/bAJ3zj2https://soundcloud.com/nssd-podcast3https://itunes.apple.com/us/podcast/not-so-standard-deviations/

id1040614570

1

Page 6: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Getting Started with R

Installation

The first thing you need to do to get started with R is toinstall it on your computer. R works on pretty much everyplatform available, including thewidely availableWindows,Mac OS X, and Linux systems. If you want to watch a step-by-step tutorial on how to install R for Mac or Windows,you can watch these videos:

• Installing R on Windows4

• Installing R on the Mac5

There is also an integrated development environment avail-able for R that is built by RStudio. I really like this IDE—it has a nice editor with syntax highlighting, there is an Robject viewer, and there are a number of other nice featuresthat are integrated. You can see how to install RStudio here

• Installing RStudio6

The RStudio IDE is available from RStudio’s web site7.

4http://youtu.be/Ohnk9hcxf9M5https://youtu.be/uxuuWXU-7UQ6https://youtu.be/bM7Sfz-LADM7http://rstudio.com

2

Page 7: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Getting Started with R 3

Getting started with the R interface

After you install R you will need to launch it and startwriting R code. Before we get to exactly how to write Rcode, it’s useful to get a sense of how the system is organized.In these two videos I talk about where to write code andhow set your working directory, which let’s R know whereto find all of your files.

• Writing code and setting your working directory onthe Mac8

• Writing code and setting your working directory onWindows9

8https://youtu.be/8xT3hmJQskU9https://youtu.be/XBcvH1BpIBo

Page 8: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

What is ReproducibleReporting?Watch a video of this chapter.10

This chapter will be about reproducible reporting, and Iwant to take the opportunity to cover some basic conceptsand ideas that are related to reproducible reporting, just incase you haven’t heard about it or don’t know what it is.

Before we get to reproducibility, we need to cover a littlebackground with respect to how science works (even ifyou’re not a scientist, this is important). The basic idea isthat in science, replication is the most important elementof verifying and validating findings. So if you claim thatX causes Y, or that Vitamin C improves disease, or thatsomething causes a problem, what happens is that otherscientists that are independent of you will try to investigatethat same question and see if they come up with a similarresult. If lots of different people come up with the sameresult and replicate the original finding, then we tend tothink that the original finding was probably true and thatthis is a real relationship or real finding.

The ultimate standard in strengthening scientific evidenceis replication. The goal is to have independent people to doindependent things with different data, different methods,and different laboratories and see if you get the same result.There’s a sense that if a relationship in nature is truly there,then it should be robust to having different people discoverit in different ways. Replication is particularly important

10https://www.youtube.com/watch?v=4rBX6r5emgQ

4

Page 9: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

What is Reproducible Reporting? 5

in areas where findings can have big policy impacts or caninfluence regulatory types of decisions.

What’s Wrong with Replication?

What’swrongwith replication?There’s really nothingwrongwith it. This is what science has been doing for a long time,through hundreds of years. And there’s nothingwrongwithit today. But the problem is that it’s becoming more andmore challenging to do replication or to replicate otherstudies. Part of the reason is because studies are gettingbigger and bigger.

In order to do big studies you need a lot of money and so,well, there’s a lot of money involved! If you want to do tenversions of the same study, you need ten times as muchmoney and there’s not as muchmoney around as there usedto be. Sometimes it’s difficult to replicate a study because ifthe original study took 20 years to do, it’s difficult to waitaround another 20 years for replication. Some studies arejust plain unique, such as studying the impact of a massiveearthquake in a very specific location and time. If you’relooking at a unique situation in time or a unique population,you can’t readily replicate that situation.

There are a lot of good reasons why you can’t replicatea study. If you can’t replicate a study, is the alternativejust to do nothing, just let that study stand by itself? Theidea behind a reproducible reporting is to create a kind ofminimum standard or a middle ground where we won’tbe replicating a study, but maybe we can do somethingin between. The basic problem is that you have the goldstandard, which is replication, and then you have the worststandard which is doing nothing. What can we do that’sin between the gold standard and diong nothing? That is

Page 10: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

What is Reproducible Reporting? 6

where reproducibility comes in. That’s how we can kind ofbridge the gap between replication and nothing.

In non-research settings, often full replication isn’t even thepoint. Often the goal is to preserve something to the pointwhere anybody in an organization can repeat what you did(for example, after you leave the organization). In this case,reproducibility is key tomaintaining the history of a projectand making sure that every step along the way is clear.

Reproducibility to the Rescue

Why do we need this kind of middle ground? I haven’tclearly defined reproducibility yet, but the basic idea is thatyou need to make the data available for the original studyand the computational methods available so that otherpeople can look at your data and run the kind of analysisthat you’ve run, and come to the same findings that youfound.

What reproducible reporting is about is a validation of thedata analysis. Because you’re not collecting independentdata using independent methods, it’s a little bit more dif-ficult to validate the scientific question itself. But if you cantake someone’s data and reproduce their findings, then youcan, in some sense, validate the data analysis. This involveshaving the data and the code because more likely than not,the analysis will have been done on the computer usingsome sort of programming language, like R. So you can taketheir code and their data and reproduce the findings thatthey come up with. Then you can at least have confidencethat the analysiswas done appropriately and that the correctmethods were used.

Recently, there’s been a lot of discussion of reproducibilityin the media and in the scientific literature. The journal

Page 11: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

What is Reproducible Reporting? 7

Science had a special issue on reproducibility and data repli-cation. Other journals of updated policies on publicationto encourage reproducibility. In 2012, a feature on the TVshow60minutes looked at amajor incident atDukeUniver-sity where many results involving a promising cancer testwere found to be not reproducible. This led to a number ofstudies and clinical trials having to be stopped, followed bya investigation which is still ongoing.

Finally, the Institute of Medicine, in response to a lot ofrecent events involving reproducibility of scientific studies,issued a report saying that best practices should be doneto promote and encourage reproducibility, particularly inwhat’s called ‘omics based research, such as genomics, pro-teomics, other similar areas involving high-throughput bi-ological measurements. This was a very important report.Of the many recommendations that the IOMmade, the keyones were that

• Data and metadata need to be made available;

• Computer code should be fully specified, so that peo-ple can examine it to see what was done;

• All the steps of the computational analysis, includingany preprocessing of data, should be fully described sothat people can study it and reproduce it.

From “X” to “Computational X”

What is driving this need for a “reproducibility middleground” between replication anddoingnothing? For starters,there are a lot of new technologies on the scene and inmany different fields of study including, biology, chemistryand environmental science. These technologies allow us tocollect data at a much higher throughput so we end up

Page 12: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

What is Reproducible Reporting? 8

with these very complex and very high dimensional datasets. These datasets can be collected almost instantaneouslycompared to even just ten years ago—the technology hasallowed us to create huge data sets at essentially the touchof a button. Furthermore, we the computing power to takeexisting (already huge) databases and merge them into evenbigger and bigger databases. Finally, the massive increasein computing power has allowed us to implement moresophisticated and complex analysis routines.

The analyses themselves, the models that we fit and thealgorithms that we run, are much much more complicatedthan they used to be. Having a basic understanding of thesealgorithms is difficult, even for a sophisticated person, andit’s almost impossible to describe these algorithms withwords alone. Understanding what someone did in a dataanalysis now requires looking at code and scrutinizing thecomputer programs that people used.

The bottom linewith all these different trends is that for ev-ery field “X”, there is now “Computational X”. There’s com-putational biology, computational astronomy—whatever itis you want, there is a computational version of it.

Air Pollution and Health: A PerfectStorm

One example of an area were reproducibility is importantcomes from research that I’ve conducted in the area of airpollution and health. Air pollution and health is a big fieldand it involves a confluence of features that emphasize theneed for reproducibility.

The first feature is that we’re estimating very small, butvery important, public health effects in the presence of a

Page 13: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

What is Reproducible Reporting? 9

numerous much stronger signals. You can think about airpollution as something that’s perhaps harmful, but even ifit were harmful there are likely many other things that aregoing to be more harmful that you have to worry about.Pollution is going to be at the very top of the list of thingsthat are going to harm you. In other words, there’s aninherently weak signal there.

Second, the results of a lot of air pollution research informsubstantial policy decisions.Many federal air pollution reg-ulations in theUnited States are based on scientific researchin this area and these regulations can affect a lot of stake-holders in government and industry.

Finally, we use a lot of complex statistical methods to dothese studies and these statistical methods are subsequentlysubjected to intense scrutiny. The combination of an inher-ently weak signal, substantial policy impacts, and complexstatistical methods almost require that the research that wedo be reproducible.

One of the things that we’ve done here at Johns Hopkinsis to create what’s called the Internet-based Health and AirPollution Surveillance System11 (iHAPSS). Wemake a lot ofour data available, our statistical methods are distributedin the form of R code and R packages so that they canbe examined, and the data and many of the results fromour papers are available so that they can be reproduced byothers.

Summary

• Replication, whereby scientific questions are exam-ined and verified independently by different scien-tists, is the gold standard for scientific validity.

11http://www.ihapss.jhsph.edu

Page 14: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

What is Reproducible Reporting? 10

• Replication can be difficult and often there are noresources to independently replicate a study.

• Reproducibility, whereby data and code are re-ana-lyzed by independent scientists to obtain the sameresults of the original investigator, is a reasonableminimum standard when replication is not possible.

Page 15: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

The Data Science PipelineWatch a video of this chapter.12

The basic issue is when you read a description of a dataanalysis, such as in an article or a technical report, for themost part, what you get is the report and nothing else. Ofcourse, everyone knows that behind the scenes there’s a lotthat went into this report and that’s what I call the datascience pipeline.

The Data Science Pipeline

In this pipeline, there are two “actors”: the author of thereport/article and the reader. On the left side, the author isgoing from left to right along this pipeline. The reader isgoing from right to left. If you’re the reader you read thearticle and you want to know more about what happened:Where is the data? What was used here? The basic idea

12https://www.youtube.com/watch?v=GXSRP--d3Q4

11

Page 16: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

The Data Science Pipeline 12

behind reproducibility is to focus on the elements in theblue blox: the analytic data and the computational results.With reproducibility the goal is to allow the author of areport and the reader of that report to “meet in the middle”.

Elements of Reproducibility

What do we need for reproducibility? There’s a variety ofways to talk about this, but one basic definition that we’vecome up with is that there are four things that are requiredto make results reproducible:

1. Analytic data. The data thatwere used for the analysisthat was presented should be available for others toaccess. This is different from the raw data because veryoften in a data analysis the raw data are not all usedfor the analysis, but rather some subset is used. It maybe interesting to see the raw data but impractical toactually have it. Analytic data is key to examining thedata analysis.

2. Analytic code. The analytic code is the code that wasapplied to the analytic data to produce the key results.This may be preprocessing code, regression modelingcode, or really any other code used to produce theresults from the analytic data.

3. Documentation. Documentation of that code and thedata is very important.

4. Distribution. Finally, there needs to be some standardmeans of distribution, so all this data in the code iseasily accessible.

Page 17: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

The Data Science Pipeline 13

Authors and Readers

It is important to realize that there are multiple playerswhen you talk about reproducibility–there are differenttypes of parties that have different types of interests. Thereare authors who produce research and they want to maketheir research reproducible. There are also readers of re-search and they want to reproduce that work. Everyoneneeds tools to make their lives easier.

One current challenge is that authors of research have toundergo considerable effort to make their results availableto a wide audience. Publishing data and code today is notnecessarily a trivial task. Although there are a number ofresources available now, that were not available even fiveyears ago, it’s still a bit of a challenge to get things out on theweb (or at least distributedwidely). Resources likeGitHub13

and RPubs14 and various data repositories have made a bigdifference, but there is still a ways to go with respect tobuilding up the public reproducibility infrastructure.

Furthermore, even when data and code are available, read-ers often have to download the data, download the code,and then they have to piece everything together, usually byhand. It’s not always an easy task to put the data and codetogether. Also, readers may not have the same computa-tional resources that the original authors did. If the originalauthors used an enormous computing cluster, for example,to do their analysis, the readers may not have that sameenormous computing cluster at their disposal. It may bedifficult for readers to reproduce the same results.

Generally the toolbox for doing reproducible research issmall, although it’s definitely growing. In practice, authors

13https://github.com14http://rpubs.com

Page 18: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

The Data Science Pipeline 14

often just throw things up on the web. There are jour-nals and supplementary materials, but they are famouslydisorganized. There are only a few central databases thatauthors can take advantage of to post their data and makeit available. So if you’re working in a field that has a centraldatabase that everyone uses, that’s great. If you’re not, thenyou have to assemble your own resources.

Summary

• Theprocess of conducting anddisseminating researchcan be depicted as a “data science pipeline”

• Readers and consumers of data science research aretypically not privy to the details of the data sciencepipeline

• One view of reproducibility is that it gives researchconsumers partial access to the raw pipeline elements.

Page 19: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Literate StatisticalProgrammingWatch a video of this chapter.15

One basic idea to make writing reproducible reports easieris what’s known as literate statistical programing (or some-times called literate statistical practice16). This comes fromthe idea of literate programming17 in the area of writingcomputer programs.

The idea is to think of a report or a publication as a streamof text and code. The text is readable by people and thecode is readable by computers. The analysis is described ina series of text and code chunks. Each kind of code chunkwill do something like load some data or compute someresults. Each text chunk will relay something in a humanreadable language. There might also be presentation codethat formats tables and figures and there’s article text thatexplains what’s going on around all this code. This streamof text and code is a literate statistical program or a literatestatistical analysis.

Weaving and Tangling

Literate programs by themselves are a bit difficult to workwith, but they can be processed in two important ways. Lit-erate programs can beweaved to produce human readable

15https://www.youtube.com/watch?v=bwQWhZQmDuc16http://www.r-project.org/conferences/DSC-2001/Proceedings/Rossini.

pdf17https://en.wikipedia.org/wiki/Literate_programming

15

Page 20: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Literate Statistical Programming 16

documents like PDFs or HTML web pages, and they cantangled to produce machine-readable “documents”, or inother words, machine readable code. The basic idea behindliterate programming in order to generate the differentkinds of output youmight need, you only need a single sourcedocument—you can weave and tangle to get the rist. In orderto use a system like this you need a documentational lan-guage, that’s human readable, and you need a programminglanguage that’s machine readable (or can be compiled/in-terpreted into something that’s machine readable).

Sweave

One of the original literate programming systems in R thatwas designed to do this was called Sweave. Sweave uses adocumentation program called LaTeX and a programminglanguage, which obviously is R. It was originally developedby Fritz Leisch, who is a core member of R, and the codebase is still maintained by R Core. The Sweave systemcomes with a any installation of R.

There are many limitations to the original Sweave system.One of the limitations is that it is focused primarily onLaTeX, which is not a documentation language that manypeople are familiar with. Therefore, it can be difficult tolearn this type of markup language if you’re not already in afield that uses it regularly. Sweave also lacks a lot of featuresthat people find useful like caching, and multiple plots perpage and mixing programming languages.

knitr

One of the alternative that has come up in recent timesis something called knitr. The knitr package for R takes a

Page 21: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Literate Statistical Programming 17

lot of these ideas of literate programming and updates andimproves upon them. knitr still uses R as its programminglanguage, but it allows you to mix other programming lan-guages in. You can also use a variety of documentation lan-guages now, such as LaTeX, markdown and HTML. knitrwas developed by Yihui Xie while hewas a graduate studentat Iowa State and it has become a very popular package forwriting literate statistical programs.

Summary

• Literate statistical programming tools can make iteasier to write up reproducible documents containingdata analyses.

• Sweave was one of the first literate statistical pro-gramming tools, which weaved together a statisticallanguage (R) with a markup language (LaTeX).

• knitr is a package that builds on the work of Sweaveand provides much more powerful functionality, in-cluding the ability to write in Markdown and create avariety of output formats.

Page 22: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Organizing a Data AnalysisIn this chapter, I’m going to give a couple of basic noteson how to organize a data analysis. Of course, there’s nouniversal way to do this that would apply to every singledata analysis performedby anybody in theworld, but I thinkthere’s some useful tips that can be used to help you putthings together in logical places and ultimately ensure thatyour data analysis is reproducible either by yourself or bysomeone else.

Files Files Files…

The kind of key data analysis files that you will retain overthe course of a major project are going to be raw data andprocessed data and you’re probably going to want to savea lot of this in various places. You’ll generate some figuresor tables, for example, and they’re going to be exploratoryfigures that you put together to look at the data to producethis rough cut of what the data analysis might look like.These exploratory figures are not going to be very polished.They’ll be just good enough to get you a sense of what thedata look like. Then theremight be some final figures. Thesefinal figures are going to be useful for you to put in reports.These are the figures you’re going to show to other people,they’re going to be well annotated and nicely organized andput together.

Of course, there’s going to be some code. There might besome R code in the form of both raw and unused scripts.These are kind of things that you code up to seewhat’s going

18

Page 23: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Organizing a Data Analysis 19

on, perhaps in the process of making exploratory figures.There’s going to beR code that you eventually don’t usein the end. So, you’ll have some scripts lying around. Therewill be some final scripts that you use in the final analysis.And these will, hopefully, be easier to read, commented andformatted better. And then you might be running some Rmarkdown files that annotate a data analysis using a literatestatistical programming style.

And finally there’s going to be some text that you write,either in the form of README files that explain what’sgoing on in your project, amore formal report that you havetowrite, or even a paper that you plan on publishing. All thistext is going to integrate everything you’ve done in this dataanalysis,with the data, the figures, the tables, and theR code.

Raw and Processed Data

The raw data will come in any number of different forms.Theymight come, for example, as just records or as format-ted text. You’re going to do something to this raw data tomake it usable for an analysis type of program. For example,you might do some text processing. You might try to parsethe data and if it’s formatted in a special format, you cangenerate something that can be later used for modeling orother types of analysis. You want to store this raw datain your analysis folder if you’re working on a project. Ifthe data were accessed from the web you want to includethings like the URL, where you got the data, what the dataset is, a brief description of what it’s for, the date that youaccessed the URL on, the website, etc. You may want this ina README file so when you look at it later, or if someoneelse looks at it, they know where this data came from andwhat it’s for.

Page 24: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Organizing a Data Analysis 20

Another thing that I like to do, if you’re using git or anequivalent version control system to track things that aregoing on in your project, is to add your data set, your rawdata, if possible. If it’s too big then it’s not really feasible.But you can add your raw data to the repository and, in thelog message, when you add it you can talk about what thewebsite was where you got it, what the URL was, et cetera.That’s a convenient place to put this kind of information.

Processed data is usually cleaner than the raw data. It cancome in a variety of forms. One common format is a table.Your processed data should be named so that you can easilysee what script generated what data. The processing scriptis very important because it shows you how the raw datawere mapped to the processed data. In any README fileor any sort of documentation, it’s important to documentwhat code files were used to transform the raw data into theprocessed data. Finally, the processed data should be tidy18

so that you can use them in subsequentmodeling or analysistypes of functions.

Figures

Exploratory figures are usually very simple figures. Theseare figures that you make in the course of your analysisas you’re getting a look at the data. Typically, your datawill be high dimensional because you’ll be collecting lots ofvariables on lots of subjects or observations. You’re going tobe able to look at pieces of the data at a time. Exploratoryfigures serve the roleof givingyouaquick lookatvariousaspects of your data. They’re not all necessarily going to bepart of your final report or final paper. You’ll tend tomake abunch of these along the way. They don’t need to be pretty,

18http://www.jstatsoft.org/v59/i10/paper

Page 25: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Organizing a Data Analysis 21

but they need to be usable enough so that you understandwhat’s going on in the figure and how to reproduce it.Final figures will generally be much more polished, betterorganized and much more readable.

The final figures usually make a very small subset of theset of exploratory figures that you might generate. Forexample, the typical paper in a journal might have four ormaybe five figures in it because these figures take up a lotof space. You typically don’t want to inundate people witha lot of figures because then the ultimate message of whatyou’re trying to communicate tends to get lost in a pile offigures. It’s important to have these final figures labeledwelland annotated so people understand what’s going on withthe data.

Scripts

As you’re doing a data analysis you’ll probably burn througha lot of different R scripts and code files for various pur-poses. There will be a lot of dead ends that you’ll go down,and therewill bemanyR scripts that don’t play into the finalanalysis. These R scripts are going to be less commentedand contain just code that puts some stuff together. Youmayhave multiple versions of these code files, which, typically,will include analyses that are later discarded.

Final scripts will be much more clearly commented. You’lllikely have bigger comment blocks for whole sections ofcode. There will be a lot of small comments explainingwhat’s going on. Any processing details of any code that isused to process the raw data would be important to include.Basically these final scripts are for any analysis that wouldappear in a final write up of paper.

It’s important when people see a final a product like a

Page 26: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Organizing a Data Analysis 22

paper or a report that they understand what scripts wentinto this report and what processing and analysis scriptsmight have gone into this. Then they can see the chain ofevents that occurred. It’s important, of course, to keep a lotof the other stuff that was not used in the final report justin case someone may want to look at some of the dead endsthat he went down. But that can be placed in a separate partof the project directory.

R Markdown Files

R Markdown files are also very useful (see later chapterfor details). They may not be exactly required, but theycan be very useful to summarize parts of an analysis or anentire analysis. R Markdown files can be used to generatereproducible reports. You can embed code and text into thesame document and then you process the document intosomething readable like a webpage or a PDF file. These arevery easy to create in RStudio and they can be useful as anintermediate step either between just kind of code scripts,code files, data and a polished final report.

If you don’t use R markdown files you may want to haveREADME files that explain what’s going on so you or an-other person can get a sense of the organization of theproject. They could contain step by step instructions forhow the analysis is conducted, what code files are calledfirst, what are used to process the data, what are used to fitmodels, and what are used to kind of generate figures andthings like that.

Page 27: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Organizing a Data Analysis 23

Final Report

Finally, in the end you’ll probably produce a document orreport, maybe a paper or summary of all of the analysis thatyou did. The point of this is to tell the final story ofwhat yougenerated here. Typically you’ll have a title, an introductionthat motivates your problem, the methods that you used torefine, the results and anymeasures of uncertainty, and thenany conclusions that youmight draw from the data analysisthat you did, including any pitfalls or potential problems.The important thing is that you need to tell a coherent story,to take all the analysis that you did and kind of winnow itdown into a final product that has a coherent story to it.

Don’t dump

You definitely should not include every analysis that youperformed through the whole process, so there may bemany analysis that you did but you want to narrow it downto the important parts. That does not mean you need todelete everything that you’ve ever did but the summary re-port should not include everything. And you should alwaysinclude some references for the statistical methods that youuse. That way people know what you used, what softwareyou used, and what implementation was used. This is veryimportant for again for reproducibility by others.

Summary

That’s a quick overview of how to organize your data analy-sis, just some basic tips because every data analysis will haveits specific details. There is no best way to organize a dataanalysis, but there are some useful guidelines that can help

Page 28: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Organizing a Data Analysis 24

you maintain the reproducibility of your work. Most anal-yses move in the direction of “raw” to “final” or “processed”.There are raw data and processed data, exploratory plotsand final plots, raw scripts and final scripts. R Markdownfiles can be useful for bringing all these “final” elementstogether.

Page 29: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a DataAnalysis: Part 1This chapter will talk about the basic process by which dataanalysis will unfold. Of course, not every data analysis isthe same and not every data analysis will require the samecomponents. But I think this will serve as a useful templatefor understanding what the pieces of a data analysis areand how they typically flow together. If one were to writedown the steps in a data analysis, you might come up withsomething along these lines of the following list

• Defining the question

• Defining the ideal dataset

• Determining what data you can access

• Obtaining the data

• Cleaning the data

• Exploratory data analysis

• Statistical prediction/modeling

• Interpretation of results

• Challenging of results

• Synthesis and write up

• Creating reproducible code

There may be little things that you might want to add ordelete, but most data analyses have some subset of thesesteps.

What we’re going to talk about in this chapter, Part 1, isdefining the question that you’re interested in, the ideal

25

Page 30: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 1 26

data set, determining what data you actually can access, andobtaining and cleaning the data. In the next chapter we’lltalk about the remaining topics listed here.

I think the key challenge in pretty much any data analy-sis was well characterized by Dan Meyer, a former highschool mathematics educator. In his TED Talk he said, “Askyourselves what problem you have ever solved, that wasworth solving, where you knew all the given informationin advance, where you didn’t have a surplus of informationand have to filter it out, or you had insufficient informationand had to go find some.” That’s a key element of dataanalysis; typically, you don’t have all the facts or you havetoo much information and you have to go through it. Muchof the process of data analysis is sorting through all of thisstuff. So, the first part of data analysis that you want to startwith is defining a question.

The Question

Not every data analysis starts with a very specific or coher-ent question. But the more effort you can put into comingup with a reasonable question, the less effort you’ll spendhaving to filter through a lot of stuff. Because defininga question is the most powerful dimension reductiontool you can ever employ. If you’re interested in a specificvariable, like height or weight, then you can remove a lotof other variables that don’t really pertain to those criteriaat all. But if you’re interested in a different type of variablethen you can remove another subset. The idea is, if you cannarrowdownyour question as specifically as possible, you’llreduce the kind of noise that you’ll have to deal with whenyou’re going through a potentially very large data set. Now,sometimes youonlywant to look at a data set and seewhat isinside of the data set. Then you’ll have to explore all kinds of

Page 31: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 1 27

things in a large data set. But if you can narrow down yourinterest to a specific type of question, it is extremely usefulfor simplifying your problem.

I encourage you to think about what type of question you’reinterested in answering before you go delving into all thedetails of your data set. The science, generally speaking,will determine what type of question you’re interested inasking. That will lead you to the data. Which may lead youto applied statistics, which you use to analyze the data. Ifyou get really ambitious you might want to think of sometheoretical statistics that will generalize the methods thatyou apply to different types of data. Now, of course, thereare relatively few people who can do that, and so I it wouldnot be expected of everyone.

A key problem is if you randomly apply statistical methodsto datasets to find an interesting answer you will almostcertainly find something interesting, but it may not be re-producible and it may not be really meaningful. A properdata analysis has a scientific context, and at least somegeneral question that we’re trying to investigate which willnarrow down the kind of dimensionality of the problem.Then we’ll apply the appropriate statistical methods to theappropriate data.

Let’s start with the very basic example of a question. Ageneral questionmight be: can I automatically detect emailsthat are spam and those that are not? Of course, this isan important question if you use email and you want toknow which emails you should read, that are important,and which emails are just spam. If you want to turn thatinto a data analysis problem there are many ways to answerthe question. For example, you could hire someone to gothrough your email and figure out what’s spam and what’snot. But that’s not really sustainable, and it’s not particularly

Page 32: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 1 28

efficient. So, if you want to turn this into a data analysisquestion, you have to make the question a little bit moreconcrete and translate it using terms that are specific to dataanalysis tools.

A more concrete version of this question might be: can Iuse quantitative characteristics of the emails themselves toclassify them as spam? Now we can start looking at emailsand try to identify these quantitative characteristics that Iwant to develop so that I can classify them as spam. You’vegot a question, “How do I separate out my email so that Iknow what’s spam and what’s not?” Ultimately, you can getrid of all the spam and just read the real email.

The Ideal Dataset

The first thing you might want to think about is what isthe ideal data set for this problem? If I had all the resourcesin the world, what would I go out to look for? There aredifferent types of data sets that you could potentially collectdepending on the goal and the type of question you’reasking.

If you’re interested in a descriptive problem, you mightthink of a whole population. You don’t need to sample any-thing. Youmightwant to get the entire census or populationthat you’re looking for. So all the emails in the universe,for example. If you just want to explore your question,you might take a random sample with a bunch of variablesmeasured.

If you want to make inference about a problem then youhave to be very careful about the sampling mechanismand the definition of the population that you are samplingfrom. Typically, when you make an inferential statement,you use your smaller sample to make a conclusion about

Page 33: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 1 29

a larger population. Therefore, the sampling mechanismis very important. If you want to make a prediction, youneed something like a training set and a test data set from apopulation that you’re interested in so that you can build amodel and a classifier.

If you want to make a causal statement, such as “if I modifythis component, then something else happens,” you’re goingto need experimental data. One type of experimental datais from something like a randomized trial or a randomizedstudy. If you want to make mechanistic types of statements,you need data about all the different components of thesystem that you’re trying to describe.

For our problem with spam, perhaps you use Gmail. Youknow that all the emails in the Gmail system are going tobe stored on Google’s data centers. Why don’t we just getall the data (emails) in Google’s data centers? That wouldbe a whole population of emails. Then we can just build ourclassifier based on all this data and we wouldn’t have toworry about sampling because we’d have all the data. Thatwould be an example of an ideal data set.

Of course, in the real world, you have to think about whatdata you can actually access, right?Maybe someone atGooglecan actually access all the emails that go through Gmail, buteven in that extreme case, it may be difficult. Furthermore,most people are not going to be able to access that. So,sometimes you have to go for something that is not quitethe ideal data set. You might be able to find free data onthe web. You might need to buy some data from a provider,being sure to respect the terms of use for the data. Anyagreement or contract that you agree to about the data hasto be adhered to. If the data simply does not exist out there,you may need to generate the data yourself in some way.

Page 34: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 1 30

The Real Dataset

Getting all the data fromGoogle will probably not be possi-ble. I’m guessing their data centers have some very high se-curity, sowe’re going to have to gowith something else.Onepossible solution comes from the UCI machine learningrepository, which is the spam data set. This is a collection ofspam in a data set created by people atHewlett Packardwhocollected several thousand spam and regularmessages, thenclassified them appropriately. You can use this database toexplore your problem of how to classify emails into spam.When you obtain the data, the first goal is to try to obtainthe raw data. For example, from the UCI machine learningrepository.

You have to be careful to reference the source, so whereveryou get the data from, you should always reference and keeptrack of where it came from. If you need to get data from apersonor an investigator that you’re not familiarwith, oftena very polite email will go a long way. They may be willingto share that data with you. If you get data from an Internetsource, you should always make sure at the very minimumto record the URL, which is the web site indicator of whereyou got the data, and the time and date that you accessed itso people have a reference of when that data was available.In the future, the website might go down or the URL maychange or may not be available, but at least at the time yougot that data you documented how you got it.

Cleaning the Data

The data set that we’re going to talk about in this example,since we don’t have access to Google’s data centers, is thespam data set which you can get from the kernlab package

Page 35: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 1 31

in R. If you install the kernlab package, you can load thedata set right away. The first thing that you typically needto do with any data set is to clean it a little bit. Raw datatypically needs to be processed in some way to get it intoa form where you can model it or feed it into a modelingprogram.

If the data is already pre-processed, it’s important that youunderstand how it was done. Try to get some documen-tation about what the pre-processing was and how it wasdone. You have to understand kind of where the data comefrom, so for example if it came from a survey, you need toknow how the sampling was done. Was it a conveniencesample, or did the data come from an observational study,or perhaps from experiments? The source of the data is veryimportant. You may need to reformat the data in a certainway to get it to work in a certain type of analysis. If the dataset is extremely large you may want to sub-sample the dataset to make it more manageable.

It is very important that anything you do to clean the datais recorded. Write down these steps in scripts or whateveris most convenient because you or someone else is going tohave to reproduce these steps if theywant to reproduce yourfindings. If you don’t document all these pre-processingsteps, then no one will ever be able to do it again.

Once you have cleaned the data and you have gotten abasic look at it, it is important to determine if the data aregood enough to solve your problems. In some cases, you maydetermine that the data are not good enough. For example,you may not have enough data, you may not have enoughvariables or enough characteristics, or the sampling of thedata may be inappropriate for your question. There may beall kinds of problems that occur to you as you clean the data.

If you determine the data are not good enough for your

Page 36: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 1 32

question, then you’ve got to quit, try again, change the data,or try a different question. It is important to not simply pushon with the data you have, just because that’s all that you’vegot, because that can lead to inappropriate inferences orconclusions.

Here is our cleaned data set from the kernlab package thatwe’re going to use for this example. It’s already been cleanedfor us, in the kernlab package, and I’m just showing you thefirst five variables here. There are many other variables inthe data set but you can see that there are 4601 observationsof the five variables. You can learn more about the datasethere19, where it shows you where the data set came fromand how it’s processed.

library(kernlab)

data(spam)

str(spam[, 1:5])

'data.frame': 4601 obs. of 5 variables:

$ make : num 0 0.21 0.06 0 0 0 0 0 0.15 0.06 ...

$ address: num 0.64 0.28 0 0 0 0 0 0 0 0.12 ...

$ all : num 0.64 0.5 0.71 0 0 0 0 0 0.46 0.77 ...

$ num3d : num 0 0 0 0 0 0 0 0 0 0 ...

$ our : num 0.32 0.14 1.23 0.63 0.63 1.85 1.92 1.8\

8 0.61 0.19 ...

Note if the kernlab package isn’t installed, you can install itfrom CRAN using the install.packages() function.

19http://search.r-project.org/library/kernlab/html/spam.html

Page 37: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a DataAnalysis: Part 2Jumping off from the previous chapter, we can define somefurther steps in a data analysis to be

• Exploratory data analysis

• Statistical prediction/modeling

• Interpretation of results

• Challenging of results

• Synthesis and write up

• Creating reproducible code

Understanding each of these steps and documenting themcarefully through scripts and other tools is essential forreproducibility.

In this chapter, we’re going to continue the data analysisexample that we started in part one. If you recall, we laiddown a list of steps that generally one might take whendoing a data analysis. And previously we talked about thefirst roughly half of these steps. In this chapter, we’re goingto talk about the remaining half. This includes exploratorydata analysis, statistical prediction and modeling, interpre-tation, challenging your results, synthesizing andwriting upthe results, and creating reproducible code.

The basic question was, can I automatically detect emailsthat are SPAMor not? And a slightly more concrete versionof this question that can be used to translate into a Ciscoproblem was, can I use quantitative characteristics of theemails to classify them as SPAM or HAM?

33

Page 38: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 2 34

Splitting the Dataset

Our data set, again, was f=rom this UCI Machine LearningRepository, which had already been cleaned up, and it wasavailable in the current lab package as a data set. This dataset had 4,600 observations or emails that had been kind ofcharacterized along 58 different variables. So, the first thingthat we need to do with this data set if we want to build amodel to classify emails into spam or not, is that we need tosplit the data set into test set and a training set. The ideais that we’re going to use part of the test of the data setto build our model, and then we’re going to use anotherpart of the data set which is independent of the first part toactually determine how good our model is kind of makinga prediction.

library(kernlab)

data(spam)

## Perform the subsampling

set.seed(3435)

trainIndicator = rbinom(4601, size = 1, prob = 0.5)

table(trainIndicator)

trainIndicator

0 1

2314 2287

Here I’m a taking a random half of the data set, so I’mflipping a coin with the rbinom() function, to generate arandom kind of coin flip with probability of half, so that’llseparate the the data set into two pieces. You can see thatroughly 2000, about 2314, are going to be in one half and

Page 39: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 2 35

2287 will be in the other half. And so the training set will beone set and the test set will be another set of data.

trainSpam = spam[trainIndicator == 1, ]

testSpam = spam[trainIndicator == 0, ]

Exploratory Data Analysis

The first thing we’re going to want to do is a little bit ofexploratory data analysis. Given that we have not looked atthis data set yet, it would be useful to look at what are thedata, what did the data look like, what’s the distribution ofthe data, what are the relationships between the variables.We want to look at basic summaries, one dimensional, twodimensional summaries of the data and we want to checkfor is there any missing data, why is there missing data,if there is, create some exploratory plots and do a littleexploratory analyses.

If we look at the training data sets, that’s what we’re goingto focus on right now as we do our exploratory analysis andas we build our model, all that’s going to be done in thetraining data set. And if you look at the columnnames of thedataset, you can see that they’re all just words essentially.

head(names(trainSpam), 20)

Page 40: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 2 36

[1] "make" "address" "all" "num3d" "o\

ur"

[6] "over" "remove" "internet" "order" "m\

ail"

[11] "receive" "will" "people" "report" "a\

ddresses"

[16] "free" "business" "email" "you" "c\

redit"

If you look at the first five rows, we can see that basicallythese are the frequencies at which they occur in a givenemail.

head(trainSpam[, 1:10])

make address all num3d our over remove internet or\

der mail

1 0.00 0.64 0.64 0 0.32 0.00 0.00 0 0\

.00 0.00

7 0.00 0.00 0.00 0 1.92 0.00 0.00 0 0\

.00 0.64

9 0.15 0.00 0.46 0 0.61 0.00 0.30 0 0\

.92 0.76

12 0.00 0.00 0.25 0 0.38 0.25 0.25 0 0\

.00 0.00

14 0.00 0.00 0.00 0 0.90 0.00 0.90 0 0\

.00 0.90

16 0.00 0.42 0.42 0 1.27 0.00 0.42 0 0\

.00 1.27

You can see the word “make” does not appear in that firstemail and, and the word “mail” does not appear. Theseare all basically frequency counts, or frequencies of words

Page 41: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 2 37

within each of the emails. If we look at the training data setand look at the outcome, we see that 906 of the emails arespam, are classified as spam.

table(trainSpam$type)

nonspam spam

1381 906

And the other 1381 are classified as non-spam. This is whatwe’re going to use to build our model for predicting thespam emails. We can make some plots and we can compare,what are the frequencies of certain characteristics betweenthe spam and the non spam emails.

Here we’re looking at a variable called capitalAve, the aver-age number of capital letters.

boxplot(capitalAve ~ type, data = trainSpam)

Page 42: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 2 38

Comparison of ‘capitalAve’ between spam and non-spam emails

And, you can see that it’s difficult to look at this picture,because the data are highly skewed. And so, in these kindsof situations it’s often useful to just look at the log trans-formation of the variable. Here I’m going to to take thebase ten log of the variable, and compare them to spam andnonspam. Since there are a lot of zeros in this particularvariable, taking the log of zero doesn’t really make sense.We’ll just add 1 to that variable, just so we can take the logand get a rough sense of what the data look like. Typically,you wouldn’t want to just add 1 to a variable just because.But since we’re just exploring the data, making exploratory

Page 43: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 2 39

plots, it’s okay to do that in this case.

boxplot(log10(capitalAve + 1) ~ type, data = trainSpam)

Log transformed ‘captialAve’

Here you can see clearlyb that the spam emails have amuch higher rate of these capital letters than the non spamemails, and of course, if you’ve ever seen spamemails, you’reprobably familiar with that phenomenon. And so that’s oneuseful relationship to see there.

We can look at pairwise relationships between the differentvariables in the plots. Here I’ve got a pairs plot of the first

Page 44: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 2 40

four variables, and this is the log transformation of each ofthe variables.

pairs(log10(trainSpam[, 1:4] + 1))

Paris plot of 4 variables

And you can see that some of them are correlated, someof them are not particularly correlated, and that’s useful toknow.

We can explore the predictors space a little bit more bydoing a hierarchical cluster analysis, and so this is a first cut

Page 45: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 2 41

at trying to do that with the hclust function in R. I plottedthe Dendrogram just to see how what predictors or whatwords or characteristics tend to cluster together.

hCluster = hclust(dist(t(trainSpam[, 1:57])))

plot(hCluster)

Hierarichal cluster analysis

It’s not particularly helpful at this point, although it doesseparate out this one variable, capital total. But if you recall,the clustering algorithms can be sensitive to any skewnessin the distribution of the individual variables, so it may be

Page 46: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 2 42

useful to redo the clustering analysis after a transformationof the predictor space.

hClusterUpdated = hclust(dist(t(log10(trainSpam[, 1:55]\

+ 1))))

plot(hClusterUpdated)

Hierarchical cluster analysis of log-transformed data

Here I’ve taken a log transformation of the predictors inthe training data set, and again, I’ve added one to eachone, just to avoid taking the log of zero. And now youcan see the dendrogram a little bit more interesting. It’s

Page 47: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 2 43

separated out a few clusters and this captialAve is one kindof cluster all by itself. There’s another cluster that includes“you will” or “your”. And then there are a bunch of otherwords that lump more ambiguously together. And so thismay be something worth exploring a little bit further, if yousee some particular characteristics that are interesting.

Once we’ve done exploratory data analysis, we’ve looked atsome univariate and bivariate plots, we did a little clusteranalysis, we can move on to doing a more sophisticatedstatistical model and some predictionmodeling. And so anystatistical modeling that you engage in should be informedby questions that you’re interested in, of course, and theresults of any exploratory analysis. The exact methods thatyou employ will depend on the question of interest. Andwhen you do a statistical model, you should account for thefact that the data have been processed or transformed, ifthey have, in fact, been so. As you do statistical modeling,you should always think about, what are the measures ofuncertainty? What are the sources of uncertainty in yourdata set?

Statistical Modeling

Here we’re going to just do a very basic statistical model.What we’re going to do is we’re going to go through eachof the variables in the data set and try to fit a generalizelinear model, in this case a logistic regression, to see if wecan predict if an email is spam or not by using just a singlevariable.

Here, using the reformulate function to create a formulathat includes the response, which is just the type of emailand one of the variables of the data set, and we’re justgoing to cycle through all the variables in this data set

Page 48: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 2 44

using this for-loop to build a logistic regression model, andthen subsequently calculate the cross validated error rate ofpredicting spam emails from a single variable.

trainSpam$numType = as.numeric(trainSpam$type) - 1

costFunction = function(x, y) sum(x != (y > 0.5))

cvError = rep(NA, 55)

library(boot)

for (i in 1:55) {

lmFormula = reformulate(names(trainSpam)[i], respon\

se = "numType")

glmFit = glm(lmFormula, family = "binomial", data =\

trainSpam)

cvError[i] = cv.glm(trainSpam, glmFit, costFunction\

, 2)$delta[2]

}

## Which predictor has minimum cross-validated error?

names(trainSpam)[which.min(cvError)]

[1] "charDollar"

Oncewe’ve done this, we’re going to try to figure out whichof the individual variables has theminimum cross validatederror rate. And so we can just go, and you can take thisvector of results, this CV error, and just figure out whichone is the minimum.

It turns out that the predictor that has the minimum crossvalidated error rate is this variable called charDollar. This isan indicator of the number of dollar signs in the email. Keepin mind this is a very simple model. Each of these modelsthat we fit only have a single predictor in it. Of course we

Page 49: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 2 45

could think of something more complicated, but this maybe an interesting place to start.

If we take this best model from this set of 55 predictors, thischarDollar variable, and I’ll just re-fit the model again righthere. This is a logistic regression model. We can actuallymake predictions now from the model on the test data.Recall that we split the data set into two parts and built thetraining model on the training data set. Nowwe’re going topredict the outcome on the test data set to see how well wedo.

In a logistic regression we don’t get specific 0/1 classifica-tions of each of the messages, we get a probability that amessage is going to be spam or not. Then we have to takethis continuous probability, which ranges between 0 and 1,and determine at what point, at what cutoff, do we thinkthat the email is spam. We’re just going to draw the cut offhere at 0.5, so if the probability is above 50%, we’re justgoing to call it a spam email.

## Use the best model from the group

predictionModel = glm(numType ~ charDollar, family = "b\

inomial", data = trainSpam)

## Get predictions on the test set

predictionTest = predict(predictionModel, testSpam)

predictedSpam = rep("nonspam", dim(testSpam)[1])

## Classify as `spam' for those with prob > 0.5

predictedSpam[predictionModel$fitted > 0.5] = "spam"

Once we’ve created our classification, we can take a lookat the predicted values from our model, and then comparethem with the actual values from the test data set, becausewe know which was spam, and which was not. Here’s the

Page 50: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 2 46

classification table that we get from the predicted and thethe real values.

table(predictedSpam, testSpam$type)

predictedSpam nonspam spam

nonspam 1346 458

spam 61 449

Now we can just calculate the error rate. The mistakes thatwe made are on the off diagonal elements of this table, so61 and 458. So, 61 were classified as spam that were notactually spam, and 458 were classified as non spam butactually were spam. So we calculate this error rate as about22%.

## Error rate

(61 + 458)/(1346 + 458 + 61 + 449)

[1] 0.2242869

Interpreting Results

So far we’ve done the analysis, calculated some results,calculated our bestmodel, and looked at the error rate that’sproducedby thatmodel.Nowweneed to interpret our find-ings and it’s important when you interpret your findingsto use appropriate language and to not use language thatgoes beyond the analysis that you actually did. If you’re inthis type of application where we’re just looking at somedata and building a predictivemodel, youwant to usewords

Page 51: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 2 47

like, “prediction” or “it correlates with” or “certain variablesmay be associated with the outcome” or “the analysis isdescriptive”. Think carefully about what kind of languageyou use to interpret your results. It’s also good to givean explanation for why certain models predict better thanothers, if possible.

If there are coefficients in the model that you need tointerpret, you can do that here. And in particular it’s usefulto bring in measures of uncertainty, to calibrate your inter-pretationof the final results. In this example,wemight thinkof stating that the fraction of characters that are dollar signscan be used to predict if an email is spam. Maybe we decidethat anything with more than 6.6% dollar signs is classifiedas spam. More dollar signs always means more spam underour predictionmodel. And for ourmodel in the test data set,the error rate was 22.4%.

Challenge the Findings

Once you’ve done your analysis and you’ve developed yourinterpretation, it’s important that you, yourself, challengeall the results that you’ve found. Because if you don’t do it,someone else is going to do it once they see your analysis,and so you might as well get one step ahead of everyone bydoing it yourself first. It’s good to challenge everything, thewhole process by which you’ve gone through this problem.Is the question even a valid question to ask? Where did thedata come from? How did you get the data? How did youprocess the data? How did you do the analysis and draw anyconclusions?

If you have measures of uncertainty, are those the appro-priate measures of uncertainty? And if you built models,why is your model the best model? Why is it an appropriate

Page 52: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 2 48

model for this problem? How do you choose the thingsto include in your model? All these things are questionsthat you should ask yourself and should have a reasonableanswer to, so that when someone else asks you, you canrespond in kind.

It’s also useful to think of potential alternative analyses thatmight be useful. It doesn’t mean that you have to do thosealternative analyses, in the sense that you might stick toyour original just because of other reasons. But it may beuseful to try alternative analyses just in case they may beuseful in different ways or may produce better predictions.

Synthesizing Results

Once you’ve interpreted your results, you’ve done the anal-ysis, you’ve interpreted your results, you’ve drawn someconclusions, and you’ve challenged all your findings, you’regoing to need to synthesize the results and write them up.Synthesis is very important because typically in any dataanalysis, there are going to be many, many, many thingsthat you did. Andwhen you present them to another personor to a group you’re going to want to have winnowed itdown to the most important aspects to tell a coherent story.Typically you want to lead with the question that you weretrying to address. If people understand the question thenthey can draw up a context in their mind, and have a betterunderstanding of the framework inwhich you’re operating.That will lead to what kinds of data are necessary, areappropriate for this question, what kinds of analyses wouldbe appropriate.

You can summarize the analyses as you’re telling the story.It’s important that you don’t include every analysis thatyou ever did, but only if its needed for telling a coherent

Page 53: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 2 49

story. It’s useful to sometimes keep these analyses in yourback pocket though, even if you don’t talk about it, becausesomeone may challenge what you’ve done and it’s usefulto say, “Well, you know we did do that analysis but it wasproblematic because of” whatever the reason may be.

It’s usually not that useful to talk about the analyses thatyou did chronologically, or the order in which you didthem, because the order in which you did them is often veryscattered and doesn’t make sense in retrospect. Talk aboutthe analyses of your data set in the order that’s appropriatefor the story you’re trying to tell. When you’re telling thestory or you’re presenting to someone or to your group it’suseful to include very well done figures so that people canunderstand what you’re trying to say in one picture or two.

In our example, the basic question was “Can we use quanti-tative characteristics of the emails to classify them as spamor ham?” Our approach was rather than try to get theideal data set from all Google servers, we collected somedata from the UCImachine learning repository and createdtraining and test sets from this data set. We explored somerelationships between the various predictors. We decidedto use a logistic regression model on the training set andchose our single variable predictor by using cross valida-tion. When we applied this model to the test set it was78% accurate. The interpretation of our results was thatbasically, more dollar signs seemed to indicate an email wasmore likely to be spam, and this seems reasonable. We’veall seen emails with lots of dollar signs in them trying to sellyou something. This is both reasonable and understandable.Of course, the results were not particularly great as 78% testset accuracy is not that good formost prediction algorithms.We probably could do much better if we included morevariables or if we included a more sophisticated model,maybe a non-linear model. These are the kinds of things

Page 54: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Structure of a Data Analysis: Part 2 50

that you want to outline to people as you go through dataanalysis and present it to other people.

Reproducible Code

Finally, the thing that you want to make sure of is that youdocument your analysis as you go. You can use things liketools like RMarkdown and knitr and RStudio to documentyour analyses as you do them (more about this in laterchapters). You can preserve the R code as well as any kindof a written summary of your analysis in a single documentusing knitr. And so then to make sure that all of what youdo is reproducible by either yourself or by other people,because ultimately that’s the standard by which most bigdata analysis will be judged. If someone cannot reproduce itthen the conclusions that you draw will be not as worthy asan analysis where the results are reproducible. So try to stayorganized. Try to use the tools for reproducible research tokeep things organized and reproducible. And that will makeyour evidence for your conclusions much more powerful.

Page 55: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

MarkdownMarkdown is a tool for writing for the web. In the words ofits creator John Gruber20

“Markdown is a text-to-HTML conversion toolfor web writers. Markdown allows you to writeusing an easy-to-read, easy-to-write plain textformat, then convert it to structurally validXHTML(or HTML).”

Since its inception, Markdown has evolved to be widelyadopted for a variety of purposes, far beyondwriting for theweb. In particular, the book you are reading was written inMarkdown.

The name “Markdown” is a play on the names of of many“markup” languages. Its key feature is that it can be read as-is, without any special formatting.Manymarkup languages,like HTML or LaTeX, require that numerous formatting el-ements (tags) be included in the text to instruct the programthatwill be formatting the text for reading onhow todisplaythe text.While this gives great control over the look and feelof the text to the writer, it can greatly obscure the sourcetext itself, making the text difficult to read and edit. WithHTML, some of the tag clutter was removed with the use ofcascading style sheets (CSS), but there is still quite a bit ofclutter in most HTML documents.

The benefit of Markdown for writers is that it allows oneto focus on writing as opposed to formatting. It has simple

20http://daringfireball.net/

51

Page 56: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Markdown 52

and minimal yet intuitive formatting elements and can beeasily converted to valid HTML (and other formats) usingexisting tools.

Complete information about Markdown is available at theMarkdown web site21 maintained by John Gruber. You’llnotice that the web page is short. There’s not much to de-scribe! There’s also some background information22 aboutMarkdown at Gruber’s site.

What is R Markdown?

R markdown is the integration of R code with Markdown.Documents written in R Markdown have R coded nestedinside of them, which allows one to to create documentscontaining “live” R code. R Markdown files cannot be eval-uated using standard Markdown tools. Rather, R code isevaluated as part of the processing of theMarkdown beforethe traditional Markdown tools are called.

Rmarkdown can be converted to standardmarkdownusingthe knitr23 package in R. Results from R code are insertedintoMarkdown document.Markdown can subsequently beconverted to HTML using the markdown24 package in R,but you seldom have to call this package explicitly.

The use of R Markdown to create reproducible reportsis now a core tool in literate statistical programming andhas largely replaced previous tools. The key reason forits usefulness is the simplicity of Markdown as the maindocumentation language, the powerful support provided by

21http://daringfireball.net/projects/markdown/22http://daringfireball.net/2004/03/dive_into_markdown23http://yihui.name/knitr/24https://github.com/rstudio/markdown

Page 57: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Markdown 53

the knitr package, and the integration of RMarkdown intowidely used products like RStudio.

One co-benefit of using R Markdown is that any basic texteditor can be used to create a document. No special editingtools are needed (the editor that comeswithRStudio, for ex-ample, is quite reasonable). The RMarkdown toMarkdowntoHTMLwork flowcanbe easilymanagedusingRStudio25

(but is not required). Lastly, R Markdown documents havethe benefit that they are purely textual documents that canplay well with version control systems that are primarilydesigned to track changes in text files.

Markdown Syntax

Markdown has a very simple syntax that we will brieflyreview here.

Headings

# This is a primary heading (think <h1> in HTML)

## This is a secondary heading

### This is a tertiary heading

Italics

*This text will appear italicized!*

_so will this text_

This text will appear italicized!so will this text

Bold

25http://rstudio.org

Page 58: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Markdown 54

**This text will appear bold!**

__as will this text__

This text will appear bold!as will this text

Unordered Lists

- first item in list

- second item in list

- third item in list

• first item in list

• second item in list

• third item in list

Ordered Lists

1. first item in list

2. second item in list

3. third item in list

1. first item in list

2. second item in list

3. third item in list

Note that you don’t have to worry about the specific order-ing of the numbers in the ordered list. As long as there arenumbers there, Markdown will put them in the right order.

The following Markdown source

Page 59: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Markdown 55

1. first item in list

2. second item in list

4. Whoops! Forgot one

3. third item in list

will get processed as

1. first item in list

2. second item in list

3. Whoops! Forgot one

4. third item in list

Links

Inline links can be formatted with the following syntax.

The number one school of the public health is the [John\

s Hopkins Bloomberg School of Public Health](http://www\

.jhsph.edu/).

The first thing you always want to do is [Download R](h\

ttp://www.r-project.org/).

The second thing you want to do is get [RStudio](http:/\

/www.rstudio.com/).

The number one school of the public health is the JohnsHopkins Bloomberg School of Public Health26.

The first thing you always want to do is Download R27.

The second thing you want to do is get RStudio28.

26http://www.jhsph.edu/27http://www.r-project.org/28http://www.rstudio.com/

Page 60: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Markdown 56

Occasionally, have to put so many links into a documentthat the document starts getting difficult to read because ofall the URLs. In that case, there is an alternate approach tolinking that is akin to inserting footnotes.

I spend so much time reading [R bloggers][1] and [Simpl\

y Statistics][2]!

[1]: http://www.r-bloggers.com/ "R bloggers"

[2]: http://simplystatistics.org/ "Simply Statistics"

I spend so much time reading R bloggers29 and SimplyStatistics30!

In the above text, the full links can be placed at the bottomofa document for convenience. The resulting formatted textis identical using both approaches, however, the source textwill be easier to read because it is less cluttered.

Newlines

Newlines require a double space after the end of a line.

If you just hit “enter/return” at the end of a line this is whatyou get.

First line

Second line

First line Second line

However, if you put two spaces after the end of the first line,this is what youget.

29http://www.r-bloggers.com/30http://simplystatistics.org/

Page 61: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Markdown 57

First line

Second line

First lineSecond line

Markdown Resources

• The Offical Markdown Documentation31

• Github’s Markdown Guide32

31http://daringfireball.net/projects/markdown/basics32https://help.github.com/articles/github-flavored-markdown

Page 62: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr forReproducible ReportsThere are a number of challenges to making reproduciblereport writing a reality. Authors must undertake consid-erable effort to put data and results on the web or at leastto make them available to a wide audience. Readers mustdownload those data and results individually and piece to-gether which data go with which code sections. Often bothauthors and readers must manually interact with websites,in a highly non-automated process. Finally, there is oftenno single document to integrate data analysis with textualrepresentations, so the data, code, and text are not linkedtogether in a coherent manner.

A general solution that attempts to address some of thesechallenges is literate statistical programming, a concept weintroduced earlier in this book. For those of you who areskipping around, the idea of literate programming comesfrom Donald Knuth, who envisioned computer code andthe documentation for that code existing together in a singledocument.

For literate statistical programming, the idea is that a reportis viewed as a stream of text and code. Analysis code isdivided into code chunks with text surrounding the codechunks explainingwhat is going on. In general, literate pro-grams are weaved to produce human-readable documentsand tangled to produce machine- readable documents

The requirements for writing literate programs are a docu-mentation language and aprogramming language. Knuth’s

58

Page 63: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 59

original WEB system used TeX as the documentation lan-guage and Pascal as the programming language. Fritz Leischoriginally developed the Sweave system for R, which usedLaTeX as the documentation language and R as the pro-gramming language. This system was very powerful, andquite useful for those mathematically and statistically in-clinedusers forwhomLaTeXwas a commonlyusedmarkuplanguage. However, the requirement to learn LaTeX was ahigh barrier for mamy people, higher even than learning R,if only because people saw few benefits to learning LaTeXexcept to use a system like Sweave. Sweave still comeswith every R installation and is maintained by the R Coremembers.

Literate Statistical Programming: Prosand Cons

Before we go on to the details of using knitr for repro-ducible reports, we should note that there are both advan-tages and disadvantages to using a literate programmingsystem for writing reports. Some of the advantages are that

• Text and code are all in one place and placed in a log-ical order as dictated by the flow of the data analysis

• Results are automatically updated to reflect externalchanges to data, code, etc.

• Code is live; this helps to serve as a built in regressiontest when building a document—if you can’t run thecode, you can’t build the document.

On the other hand, there are some disadvantages:

• Text and code all in one place! This can sometimesmake documents difficult to read, especially if there

Page 64: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 60

is a lot of code. It might make sense to place code in aseparate file, but they you are introducing more com-plexity and more chances for non-reproducibility.

• The processing of documents can be very slow, espe-cially if the document is long. However, there are nowtools that can help with that (somewhat). In general,literate statistical programming tools are awkward touse for very long or very complex documents. In thosecases, it’s better to use literate programming tools inconjunction with other tools, like Makefiles.

My general feeling is that knitr is ideal for

• Manuals

• Short/medium-length technical documents

• Tutorials

• Reports, especially if they will be generated periodi-cally with updated data

• Data preprocessing documents and summaries

I feel that knitr is NOT particularly well-suited for thefollowing:

• Very long research articles

• Documenting very complex and time-consuming com-putations

• Documents that require precise formatting (althoughthis is more an issue with using Markdown than it iswith using knitr)

Page 65: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 61

The knitr Packages

The knitr package was developed by Yihui Xie33, while hewas a graduate student at Iowa State University. The knitr

package many of the ideas that had been developed in sepa-rate packages since the introduction of Sweave and com-bined them all together into one logical package/frame-work. The package supports a variety of documentationlanguages (Markdown, LaTeX, and HTML) and a variety ofprogramming languages. The package also supports weav-ing to PDF and HTML formats (in RStudio you can alsoweave to Microsoft Word docx format). For the purposesof this book, we will focus on using Markdown as the doc-umentation language and R as the programming languageand assume you are exporting to HTML format.

The knitr package is available from your local CRAN mir-ror and can be installed with the install.packages() func-tion in R.One key feature of the RStudio IDE (amongmany)is that knitr is built right into the GUI so that you canwrite documents with knitr without having to write at theconsole.

To use the knitr package, you need

• A recent version of R

• A text editor for writing RMarkdown documents (theone that comes with RStudio is fine)

• Some support packages installed (also available onCRAN)

• Some knowledge of Markdown, LaTeX, or HTML

As noted above, we will focus on using Markdown here.

33http://yihui.name

Page 66: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 62

My First knitr Document

Here’s an example of creating a simple R Markdown docu-ment in RStudio. To create a new document, you can clickon the menu in the upper left hand side of the GUI.

Open an RMarkdown Document

RStudio will prompt you with a dialog box to set some ofthe metadata for the document. You can feel free to eitherfill this out now or just clickOK. Either way, you can alwaysmake changes to this information later.

Page 67: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 63

Initial Configuration Options

Initially, RStudio creates the R Markdown document withsome filler text.Don’tworry about this text, you can feel freeto delete later it when you’re ready to write your document.However, if you’re new to RMarkdown, you might want totake a look at it to get a feel of howRMarkdown documentsare written.

Page 68: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 64

Initial Document with Boilerplate Text

Processing an R Markdown Document

When you are ready to process and view your RMarkdowndocument the easiest thing to do is click on the Knit HTML

button that appears at the top of the editor window. Fornow, just leave the boiler

Page 69: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 65

Use the “Knit HTML” Button

If you’re not using RStudio, you can still process R Mark-down documentswith the knitr package directly. Assumingyour R Markdown document is in your current workingdirectory (as determined by getwd()) you can run

library(knitr)

knit2html("document.Rmd")

browseURL("document.html")

where document.Rmd and document.html should be substitutedwith whatever your file name happens to be.

HTML Output

When you click the Knit HTML button (or run the knit2html()function) the result you get should look something like this.

Page 70: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 66

Initial HTMLOutput

Note here that the the code is echoed in the document in agrey background box and the output is shown just below itin a white background box. This is the default theme for theoutput in RStudio. For better or for worse, knitr produces(by default) a standalone HTML document with the stylinginformation built in to the document.

Notice also that the output is prepended with two poundsymbols. This is there so that if you happen to be copyingcode from the HTML output and pasting it into the R con-sole, you just copy and paste the code and don’t accidentlypaste the output too (the output will be commented out inthe R console). You can turn this behavior off by setting

knitr::opts_chunk$set(comment = NA)

at the top of your document somewhere (see below forsetting global options).

Page 71: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 67

A Few Notes

There are a couple of thingsworth noting at this point. First,as you already saw, opening a new RMarkdown documentin RStudio will fill a new document with filler text. Justdelete the filler; it’s just there to demonstrate the basics of RMarkdown documents.

Code chunks begin with ```{r} and end with just ```. AnyR code that you include in a document must be containedwithin these delimiters, unless you have inline code. Codechunks can have names, which is useful when we startincorporating graphics (graphics file names are based on thecode chunk name). Finally, by default, code in a code chunkis echoed back out to the document, as will the results of thecomputation (if there are results to print).

When you process a knitr document here’s what happensunder the hood:

1. You write the RMarkdown document (with a .Rmd

extension)

2. knitr produces a Markdown document (with a .md

extension)

3. knitr converts the Markdown document into HTML(by default)

The process is .Rmd to .md to .html; in principle there shouldbe three documentswhen the process is complete, but RStu-dio deletes the intermediary .md file by default (you can turnthis behavior off if you need the .md file)

You should NOT edit (or even save) the .md or .html doc-uments until you are finished; these documents will beoverwritten the next time you knit the .Rmd file.

Page 72: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 68

Another Example

Here’s an example of an R Markdown document where wemodify some of the chunk-level arguments to override thedefaults. In this case, we choose to NOT echo the code inthe resulting document. This sometimes useful if there’s justtoomuch code and you don’t want to overwhelm the readerwith a print out of the code.

Another RMarkdown Example

And here is the output.

Page 73: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 69

Hiding Source Code

Notice that now the code is no longer in the document.

Hiding Results

In addition to hiding the code, you can hide the resultsof computation. This is sometimes useful if you are, forexample, making a plot and you want the reader to see thefinal plot, not the various results of the computations thatcame before it. Setting the option results = "hide" achievesthis.

Page 74: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 70

Hiding Results

The following output now hides both the code and theresults of the code.

Output With Hiding Results

Page 75: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 71

Inline Computations

Occasionally, it’s useful to put some computations withina sentence. For example, you may be quoting a specificnumerical results in the middle of a paragraph. Rather thantry to copy and paste the result into the paragraph, it’s betterto just do the computation right there in the text.

In this example I’m quoting the current time and generatinga random Normal variate as my “favorite” number.

Inline Computation

Here is the output from the above source. Notice that thecurrent time is inserted into the text, as well as a randomnumber.

Inline Computation Output

Page 76: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 72

Incorporating Graphics

No report of a data analysis would be complete withoutsome sort of graphic or plot, right? Of course, incorporat-ing data graphics with knitr is straightforward. By default,nothing special needs to be done. You can just put theplotting code in a code chunk and the plot will be insertedinto the resulting HTML document.

Creating Plots

In the example above, I decided to change the figure heightto make it a bit shorter than the default. You can change thewidth of the plot with the fig.width argument in the code

Page 77: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 73

chunk.

When knitr creates the HTML document, by default, itembeds the figure into the HTML document as base 64encoded string. You can see that here in the HTML below.

Generated HTML

This is useful sometimes because it makes the resultingHTML document stand alone. You can email it to a col-league and they will be able to view it without having toworry about any external dependencies like image files.However, it does make the resulting HTML much largerthan it needs to be (and make sit totally unreadable). Thatsaid, given that the HTML is not meant to be read directlyand is just auto-generated by knitr, it’s not clear that thereare many downsides to this approach.

Here’s the actual output that gets rendered in the HTMLviewer (or web browser).

Page 78: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 74

Creating Plots

Making Tables With xtable

Tables can be made in R Markdown documents with thehelp of the xtable package. Tables can be notoriously non-reproducible if they are made by hand. Luckly, the xtable

package can take tabular information and format it nicely ineither HTML or LaTeX. Here, we will create HTML tables.

Page 79: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 75

In the code below, we fit a multiple linear regression modeland then create a table of the regression coefficients. Essen-tially, this is the output that is generated by the summary()

function when called on lm objects.

Making Tables with xtable

Notice that in the chunk options I specify results="asis".This is because xtable generates its own HTML output, soit doesn’t have to be subsequently converted into HTML byknitr.

The output document is below.

Page 80: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 76

Table Output from xtable

This is just one simple example of how xtable can be used togenerate HTML tables. There are many other options thatcan be configured in the print() method for xtable objectsand the help page is worth exploring.

Setting Global Options

Sometimes you want to set an option that applies to allchunks in your document. This can be done by modifyingthe opts_chunk variable in knitr, which is a global variablein the knitr package. Usually, this is done by creating a codechunk somewhere in the beginning of the document (beforeany other code chunks in the document) and modifying theopts_chunk object.

For example, if we wanted the default to be that all chunksdo NOT echo their code and always hide their results, wecould set

Page 81: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 77

knitr::opts_chunk$set(echo = FALSE, results = "hide")

Any other valid chunk options can be set in the embeddedset() function in the opts_chunk object.

Global options can always be overridden by any specificoptions that are set in at the chunk level. For example, whenusing the xtable package, I want to override the defaultresults argument.

Setting Global Options

Here is the corresponding output.

Page 82: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 78

Global Options

Caching Computations

Earlier in this chapter, I mentioned that knitr is perhaps notso useful if you are writing a very long document or oneinvolving complex computations. However, there is onetool that can be useful and may help you circumvent someof these problems.

The basic issue is that if you have a long document or oneinvolving lengthy computations, then every time you wantto view your document in the pretty formatted version, youneed to re-compile the document, meaning you need to re-run all the computations. If your code chunks are quick andnot too numerous, the having to knit the document everytime is not such a burden. But otherwise, it can be painfulto have to sit there and wait for every computation to runevery single time.

Chunk caching is one way to avoid these lengthy compu-tations. By setting the cache = TRUE chunk option, what

Page 83: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Using knitr for Reproducible Reports 79

knitr does is run the chunk once, then store the outputin a database in your working directory. Then, when youre-knit the document, instead of running the code in thatparticular chunk, knitr simply re-loads the stored outputfrom the database. It’s kind of like a chunk-level version ofmemoization. However, if the code in the chunk changes atall, knitr can detect this and will re-run the code (and storethe reuslts again).

There some caveats to caching. In particular, by defaultdependencies between chunks are not checked. If the resultsof a cached chunk depend on a previous chunk that hasbeenmodified, those changeswill not necessarily propogatedown to later cached chunks. Also, chunks with significantside effects, such as those writing output to files or interact-ing with the external environment in any way, may not becacheable.

Summary

Literate statistical programming can be a useful way to puttext, code, data, and output all in one document. Doingthis with R and Markdown, in conjuncdtion with the knitr

package and RStudio, is an very easy and yet powerful toolfor integrating code and text in a simple document format.

Page 84: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Communicating ResultsOver E-mailA substantial and important part of any good data anal-ysis involves communicating the results to others. Whilecommunication may involve writing a report, publishing apaper, or making an oral presentation, in many settings theinitial “presentation” is through e-mail or something simi-lar. In those cases, it’s often useful to structure the pre-sentation as a hierarchy, with key high-level information upfront and supporting details or evidence in the background.This chapter talks about ways in which you can organizepresentations so that various types of people (e.g. managers,colleagues) can extract as much useful information out aspossible.

This chapter is not so much about reproducibility per se. It’smore about howyou can communicate your results in awaythat people can understand them and can check or chal-lenge your results, if they need to. Particularly, when you’reworking on a large data analysis, or a complex project,and you do your work in a reproducible manner, you’regoing to generate a lot of content, files, output, and data.These outputs cannot all be presented at once without anystructure.

When you present your findings to another person there’sa hierarchy of information that you’re going to want topresent, ranging from the least specific tomost specific. Thebottom line is that most people these days are very busy34.

34http://simplystatistics.org/2011/09/23/getting-email-responses-from-busy-people/

80

Page 85: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Communicating Results Over E-mail 81

People like managers and organization leaders are going tobe very busy, so they are more likely to accept and digestcontent that you present in a streamlined and hierarchicalorder.

Many timeswhen you present a data analysis, it will be in anoral form like a presentation or in ameeting.Often the earlyresults or intermediate results are presented via a mediumlike e-mail. It’s useful to break down the results of an anal-ysis into different levels of specificity or granularity. Thebasic idea is that you don’twant to blast a ton of informationto someone in one go, because it’ll be overwhelming andpeople will be less likely to read what you have to say.

Hierarchy of a Research Paper

If you think about what a typical research paper might looklike, it’s a hierarchy of information built into a researchpaper.

1. Title and Author list: Typically the first level is goingto be the title, which is descriptive of what the paper’sabout. Hopefully it’s interesting, it tells you a little bitabout what’s going on, but there’s really no detail ina title, just the the topic that’s covered. Also, we canlearn who is responsible for the work from the authorlist.

2. Abstract: The next will be the abstract, usually a cou-ple hundredwords, describingwhat the paper’s about,what motivated the problem and what was done tosolve the problem.

3. BodyandResults: Then, of course, youhave the paperitself. The body of the paper will have the methodsand more details about what you really did. The more

Page 86: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Communicating Results Over E-mail 82

detailed results will include any sensitivity analysesand a much longer discussion of the implications ofthe results.

4. Supplementary materials (gory details): Of course,even the written paper, for a complex data analysis,doesn’t specify all the details that are needed to re-produce the findings. Typically, you might have somesupplementary materials which have a lot more of thedetails about what was done.

5. Code and data (really gory details): If you really wantto do what was done to a level of precision that isuseful, you probably need to get the code, the data andall the gory details of what happened.

That’s the range of things that you might be able to presentfrom least specific to most specific in a typical researchpaper.

Hierarchy of an E-mail

Not everyone is writing a research paper, but there is ananalog: e-mail. If you’re going to be e-mailing results eithera colleague or a manager, then it’s a good idea to follow abasic structure:

1. Subject Line and Sender Information: First line ofinformation is the subject of the e-mail, which be-comes like the title. It should be concise, you wantto be descriptive, and at a minimum, you want tohave one. So don’t send an e-mail without a subject,it’s not specific as to what the e-mail is about. If youcan summarize what you’ve done in one sentence, itmay be useful put that in the subject. That way people

Page 87: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Communicating Results Over E-mail 83

reading the subject can get a sense ofwhat’s happeningand maybe even make a decision based on that alone.

2. E-mail Body: The next level of information is the e-mail body. Even though there’s no technical limit onthe size of the e-mail body, you don’t want to go toocrazy with this. You need to provide a brief descrip-tion of the problem, because the person, if they’reworking onmany different things at once,may not re-member what the problem is or precisely what you’reworking on. Give them a little context, give thema little description. Recall if there was a dsicussionpreviously. Talk about what was proposed and whatyou actually did. Summarize some of your findingsand your results for one to two paragraphs withinthe e-mail body. If you need the person to take somesort of action based on the results of this presentation,try to suggest some options that and make them asconcrete as possible. If there are questions that need tobe addressed, it’s best to make them yes/no questions,or as simple as possible.

3. Attachments: After the e-mail body you might attachsomething that’s quite a bit longer, a PDF file or an-other type of document. This can be a report con-taining more detailed analyses, figures, tables, thingslike that. This can be derived from an R markdownfile. You can use something you create with knitr.Even in the report like this where you may be allowedacouple of pages to present things, you still want tostay concise, you don’t want to spit out pages andpages of code and tables and results. We know thatyour code is available because if you use somethinglike knitr then obviously, you have to put the codein with the results, but you don’t necessarily have topresent it all in the report.

Page 88: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Communicating Results Over E-mail 84

4. SupplementaryMaterials: If someone reallywants tolook at what you did precisely, you can give them alink to a repository, likeGitHub, a project website thatwould have all of the details, or all your code files, allyour data sets, your software environment, things likethat. Those are the different levels of detail that youmight want to present to someone.

There may bemany different kinds of people with differentlevels of interest in your work who are reading the e-mailyou send out. People who are truly interested in everythingthat you did or really want to know the details, they mightgo to your GitHub repository and start pulling your codeand start looking at the detailed results. Conversely, some-onewhowants top line summarieswill read the e-mail, lookat the subject, read the brief description, and maybe flipthrough the report.

Youwant to present peoplewith these different levels of de-tail so they can choose the level that they aremost interestedin. This is just a generic template for how youmight presentan analysis or a data, or a project that you’veworked on.Notevery presentation will need all these different levels but Ifind this to be a useful breakdown of the different types ofpresentations that you can make.

Page 89: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Reproducibility Check ListReproducibility can be more or less easy to achieve de-pending on the context, the scientific area, the complexityof a data analysis, and a variety of other factors. However,over time, I’ve developed a few rules of thumb that I thinkare useful for at least encouraging reproducibility, if notguaranteeing it. In this chapter, I put together a simple“check list” of ideas that I’ve developed in my experiencedoing data analysis and computational research.

Start With Good Science

Good science, generally speaking, or a good question, is thekey to any worthwhile investigation. The general rule of“garbage in, garbage out” applies here. If you do not startwith ameaningful question, thenno amount of data analysisor statistical machinery will be able to make the resultsinteresting to you. If the question and the results are notinteresting to you or your colleagues, therewill be relativelylittle motivation to make the results reproducible. This is aproblem.

Having a coherent, focused question simplifies many prob-lems and will make it easier to determine whether you areon the right track or if an error has occurred. Vague andbroadly defined questions can fit many different scenariosand are more likely to encourage sloppiness and unclearthinking.

Related toworking on a problem that interests you is work-ing with good collaborators. Collaborators that you work

85

Page 90: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Reproducibility Check List 86

well with will reinforce good practices and will encourageyou to do the best work. Ultimately, if you are uncom-fortable with the people you are working with, or moreseriously, if you do not completely trust the people you areworking with, then there will be breakdowns in communi-cation and things will get lost. If you don’t feel comfortable(politely) challenging a colleague’s work when needed, thenbad work will make it through, which can lead to non-reproducible results. Working with the right people is animportant, but often unmentioned, aspect of making workreproducible.

Don’t Do Things By Hand

If this chapter could be boiled down to one rule, it might be“Don’t do things by hand”.What do Imean by that? Here area few examples that are common, but are bad practice:

• Editing spreadsheets of data to “clean it up”. Oftenthis is doen to remove outliers, do quality assuranceor quality control checks (QA/QC), or validating in-dividual data entries

• Editing tables or figures (e.g. rounding, formatting) tomake then look better

• Downloadingdata fromaweb site using awebbrowser

• Moving data around your computer

• Splitting or reformatting data files

Often, the motiviation for doing all of the above things isthat “We’re just going to do this once.” The thinking is thatif the activity is only going to be done once, it doesn’t needto be automated (i.e. programmed into a computer).

Page 91: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Reproducibility Check List 87

But programming a procedure into a computer is not nec-essarily about automation. It is also about documentation.The problem with things that are done by hand, is thatthings done by hand need to be precisely documented (thisis harder than it sounds). Often, it can very difficult tocommunicate to someone what was done after the fact. Itcan be easy tomiss a step that “isn’t important” when in factit is.

Don’t Point And Click

Pointing and clicking is obviously related to doing things byhand. Most modern operating systems have a windowinginterface that allow you to click on menus that can leadto automated built-in routines. Many data processing andstatistical analysis packages have graphical user interfaces(GUIs) that simplify the use of the program, but the actionsyou take with a GUI can be difficult for others to reproducebecause there’s not necessarily a log of what was clicked.

Some GUIs for statistical analysis packages produce a logfile or script which includes equivalent commands for re-producing the behavior of the GUI, but this is by no meansthe standard. In general, be careful with data analysis soft-ware that is highly interactive. There is often a trade-offbetween the ease of use of a software package and thetendency to lead to non-reproducible results.

Of course, this doesn’t mean that all interactive software.Some software has to be interactive, like text editors orword processors, and that’s fine. It’s just when the softwaremust be used to conduct data analysis, youmust be careful notto be seduced by the ease-of-use of an interactive interface.

Page 92: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Reproducibility Check List 88

Teach a Computer

Theopposite of doing things byhand is teaching a computerto do something. Computers need very precise instructionsto accomplish a task so there’s no room for ambiguity. Thisis a Good Thing if your goal is to make your proceduresand processes reproducible. If something needs to be doneas part of your analysis or investigation, try to teach yourcomputer to do it, even if you only need to do it once. In the end,teaching a computer to do something almost guaranteesreproducibilty.

Example: Downloading data

Downloadling datasets is something data scientists are con-stantly doing. But if you’re using a web browser to down-load data, you’re probably not downloading data in a re-producible way. Suppose you wanted to obtain a dataset toanalyze from the UCI Machine Learning Repository. Oneway to do that by hand would be to

1. Go to theUCIMachineLearningRepository at http://archive.ics.uci.edu/ml/

2. Download the Bike Sharing Dataset35 by clicking onthe link to theData Folder, then clicking on the link tothe zip file of dataset, and choosing “Save Linked FileAs…” and then saving it to a folder on your computer

But this involves doing things by hand! Normally, the inter-active nature of the web browser is a great feature, but notwhen you need to download a dataset that is important toyour analysis

Another way to accomplish this task is to teach your com-puter to do the same thing using R:

35http://archive.ics.uci.edu/ml/datasets/Bike+Sharing+Dataset

Page 93: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Reproducibility Check List 89

> download.file("http://archive.ics.uci.edu/ml/machine-\

learning-databases/00275/Bike-Sharing-Dataset.zip",

+ "ProjectData/Bike-Sharing-Dataset.zip")

Notice here that

• The full URL to the dataset file is specified (no clickingthrough a series of links)

• The name of the file saved to your local computer isspecified (“Bike-Sharing-Dataset.zip”)

• The directory in which the file was saved is specified(“ProjectData”)

• The code can always be executed in R (as long as linkis available)

Now that you’ve taught a computer to do this task, it is farmore reproducible than writing down a series of instruc-tions directing someone to use a web browser. In fact, the Rcode is a far more compact representation of this task.

Use Some Version Control

Version control systems is not something we’ve explicitlycovered in this book so far, so I won’t go into great detailhere. Briefly, version control systems are software systemsdesigned to help you keep track of changes to a set of codefiles in a given project. They are primarily designed forsoftware projects where code files are typically reasonablysmall text files, but they can also be applied to data analysisprojects. Examples of popular version control systems thesedays are git36, subversion37 (svn), and mercurial38 (hg).

36http://git-scm.com37http://subversion.apache.org38https://mercurial.selenic.com

Page 94: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Reproducibility Check List 90

If there’s one reason for using a version control system totrack the changes to your data analysis project, it is that theversion control system can help to slow things down. Inmany instances with data analyses, it’s tempting to zoomahead and start plowing into the data to find somethinginteresting. This excitement is good, of course, but not at theexpense of keeping track of what’s happening. Version con-trol systems can be helpful for reminding you that changesneed to be tracked and notes need to be taken, if only toremind yourself of what happened a little be later (much lessfor communicating to team members).

Version control systems have many benefits, such as be-ing able to track snapshots of a project and to mark/tagmajor milestones. They also allow for simple collaborationacross networks (internal or external) and for publishingyour work. With complementary web sites like GitHub39,BitBucket40, and SourceForge41, it is now straightforwardto publish your projects so that anyone can view yourwork.Most of these sites have some free tier that allows you tohost your projects without any cost to you.

Keep Track of Your SoftwareEnvironment

If you work on a complex project involving many toolsand datasets, the software and computing environment canplay a critical role in determining whether your analysis isreproducible. In the extreme case, if your analysis dependson some custom proprietary software or hardware thatonly you possess, then obviously no one else will be able

39https://github.com40https://bitbucket.org41http://sourceforge.net

Page 95: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Reproducibility Check List 91

to reproduce your analysis. However, there are many casesshort of that extreme one where the software and hardwareenvironment in which a data analysis was conducted can beimportant for reproducibility.

Here are a few things that you should keep in mind as youkeep track of your environment.

• Computer architecture:What kindofCPUdoes yourcomputer use? Intel, AMD, ARM, etc.? And are youusing graphical processing units (GPUs)?

• Operating system: Are you using Windows, Mac OS,Linux / Unix, something else? Themore obscure youroperating system, the more difficult it might be toreproduce your work unless you do things in a cross-platform manner.

• Software toolchain: This includes things like com-pilers, interpreters, the command shell, programminglanguages (C, Perl, Python, etc.), database backends,and any data analysis software.

• Supporting software and infrastructure: Softwarelibraries, R packages, software dependencies

• External dependencies: Your data analysis is likelyto depend on things outside of your computer, likeweb sites, data repositories, remote databases, andsoftware repositories.

• Version numbers: Ideally, you should keep track ofthe version numbers for everything you use, if pos-sible. This is particularly important for software andlibraries because often certain versions of softwaredo not work with other versions of software, so amismatch in version numbers may prevent anotherperson from reproducible your work. Communicat-ing the appropriate version numbers to others can

Page 96: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Reproducibility Check List 92

improve the chances of them reproducingwhat you’vedone.

One important function in R that can be useful for docu-menting your R environment is the sessionInfo() function.This function displays various details about yourR environ-ment like the search path, which packages are loaded, theversion number of R, the locale, and the operating systemof your computer. For example, here’s what it outputs formy environment.

> sessionInfo()

R version 3.2.2 RC (2015-08-08 r68921)

Platform: x86_64-apple-darwin14.4.0 (64-bit)

Running under: OS X 10.10.4 (Yosemite)

locale:

[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/e\

n_US.UTF-8

attached base packages:

[1] stats graphics grDevices utils datasets b\

ase

other attached packages:

[1] knitr_1.10.5

loaded via a namespace (and not attached):

[1] magrittr_1.5 formatR_1.2 tools_3.2.2 stringi_0\

.5-5 stringr_1.0.0

[6] evaluate_0.7

Page 97: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Reproducibility Check List 93

Including a call to sessionInfo() at the end of each reportwritten inR (perhapswithmarkdownor knitr) can be usefulfor communicating to the reader what type of environmentis needed to reproduce the contents of the report (itmay notbe necessary but it’s likely sufficient for simple analyses).

Don’t Save Output

Saving output from various stages in a data analysis mayseem like a responsible thing to do (what if the computercrashes), but it should be avoided if possible. The reason isthat output files are often undocumented and themanner inwhich they were constructed can be difficult to reproduce.Better to save the inputs and code that were used to create agiven piece of output rather than save the output itself. Thatway, if changes need to bemade (or if output is lost), you cansimply re-run the code with the appropriate input.

Outputs that you should avoid saving are things like tables,figures, summaries, and processed data. The one exceptionhere is if it took a very long time to create that output.Then it might make sense to temporarily save some outputfor efficiency purposes. But in those cases, it’s important todocument carefully how the output was generated, perhapsvia a version control system. Ultimately, if an output filecannot be easily connected with the means by which it wascreated, then it is not reproducible.

Set Your Seed

This is a niche issue thatmaynot be generally applicable, butis often the source of non-reproducible results in statisticaloutput or simulations. Many sophisticated statistical rou-tines these days depend on the generation of random num-bers. Think Markov chain Monte Carlo, random forests,

Page 98: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Reproducibility Check List 94

and bootstrapping. Any procedure that depends on ran-domness will not generate the exact same output if you runit twice (the very definition of non-reproducibility). How-ever, on a computer, randomnumbers are not truly random,rather they are pseudo-random. Therefore, it is possible toexactly reconstruct a sequence of pseudo-random numbersif you have the initial seed.

In R you can use the set.seed() function to set the randomnumber generator seed and to specify which random num-ber generator to use (see ?set.seed for details). Setting theseed allows for the stream of random numbers to be exactlyreproducible at a later date.Whenever you generate randomnumbers for a non-trivial purpose, always set the seed atthe beginning of your code.

Here’s an example of some random numbers.

> rnorm(5)

[1] 2.22414093 0.09524444 -1.16593756 0.59730725 1.\

34369099

There is now no way for me to go back (via code) and re-generate those numbers because I didn’t set the seed. Thenext time I call rnorm() it will generate different numbers.

> rnorm(5)

[1] -1.9432379 0.6078967 1.8811491 -1.0447159 0.3690\

495

However, if I set the seed first, I can always re-generate thesame numbers if needed.

Page 99: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Reproducibility Check List 95

> set.seed(10)

> rnorm(5)

[1] 0.01874617 -0.18425254 -1.37133055 -0.59916772 0.\

29454513

And again.

> set.seed(10)

> rnorm(5)

[1] 0.01874617 -0.18425254 -1.37133055 -0.59916772 0.\

29454513

Think About the Entire Pipeline

Data analysis is a lengthy process, starting from obtainingdata all the way to generating results and communicatingoutput. It is not just fitting a few prediction models orcreating tables, figures, and reports. Typically, there willbe raw data, processed or analytic data, analysis results,and then a final report. In addition to all that there will becode files that correspond to each of those transitions. Theykey thing to remember is that how you got the end is just asimportant as the end itself. Themore of the entire data analysispipeline you canmake reproducible, the better for everyone(most importantly, yourself).

Page 100: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Reproducibility Check List 96

Summary

Here is the basic reproducibility check list:

• Are we doing good science?

• Was any part of this analysis done by hand?If so, arethose parts precisely document? Does the documenta-tion match reality?

• Have we taught a computer to do as much as possible(i.e. coded)?

• Are we using a version control system?

• Have we documented our software environment?

• Have we saved any output that we cannot reconstructfrom original data + code?

• How far back in the analysis pipeline canwe go beforeour results are no longer (automatically) reproducible?

Page 101: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Evidence-based DataAnalysisNOTE: Parts of this chapter were previously published onthe Simply Statistics42 blog.

Watch a video of this chapter: Part 143 Part 244 Part 345 Part446 Part 547

Discussions about reproducibility in scientific researchhavebeen on the rise lately. The rise in frequency of these discus-sions was in part a driver for me to write this book. Thereare many underlying trends that have produced this in-creased interest in reproducibility: larger and larger studiesbeing harder to replicate independently, cheaper data col-lection technologies andmethods producing larger datasets,cheaper computing power allowing for more sophisticatedanalyses (even for small datasets), and the rise of generalcomputational science (for every “X” we now have “Com-putational X”).

For those who are skipping around this book, here’s a briefreview of what I mean when I say “reproducibility”. For themost part in science,we focus onwhat I and someothers call“replication”. The purpose of replication is to address thevalidity of a scientific claim. If I conduct a study and con-clude that “X is related to Y”, then othersmay be encouraged

42http://simplystatistics.org43https://www.youtube.com/watch?v=Z792eZcBH3E44https://www.youtube.com/watch?v=_gVgNkHRqu045https://www.youtube.com/watch?v=mzfVddVcYG046https://www.youtube.com/watch?v=1r98iJqlJtQ47https://www.youtube.com/watch?v=jFDWhqKjk-o

97

Page 102: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Evidence-based Data Analysis 98

to replicate my study–with independent investigators, datacollection, instruments, methods, and analysis–in order todetermine whether my claim of “X is related to Y” is infact true. If many scientists replicate the study and come tothe same conclusion, then there’s evidence in favor of theclaim’s validity. If other scientists cannot replicate the samefinding, then onemight conclude that the original claimwasfalse. In either case, this is how science has always workedand how it will continue to work.

Reproducibility, on the other hand, focuses on the validityof the data analysis. In the past, when datasets were smalland the analyses were fairly straightforward, the idea ofbeing able to reproduce a data analysis was perhaps not thatinteresting. But now, with computational science and bigdata, where data analyses can be extraodinarily complicated(and much of the value comes from the analysis), there’sgreat interest in whether certain data analyses can in fact bereproduced. By this I mean is it possible to take someone’sdataset and come to the same numerical/graphical/what-ever output that they came to.While this seems theoreticallytrivial, in practice it’s very complicated because a givendata analysis, which typically will involve a long pipelineof analytic operations, may be difficult to keep track ofwithout proper organization, training, or software.

What Problem Does ReproducibilitySolve?

Reproducibility cannot really address the validity of a sci-entific claim as well as replication. Of course, if a givenanalysis is not reproducible, that may call into questionany conclusions drawn from the analysis. However, if ananalysis is reproducible, that says practically nothing aboutthe validity of the conclusion or of the analysis itself.

Page 103: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Evidence-based Data Analysis 99

In fact, there are numerous examples in the literature ofanalyses that were reproducible but just wrong. Perhapsthe most nefarious recent example is the Potti scandal atDuke48. Given the amount of effort (somewhere close to2000 hours) Keith Baggerly and his colleagues had to putinto figuring out what Potti and others did, I think it’sreasonable to say that their work was not reproducible.But in the end, Baggerly was able to reproduce some ofthe results–this was how he was able to figure out that theanalysis were incorrect. If the Potti analysis had not beenreproducible from the start, it would have been impossiblefor Baggerly to come up with the laundry list of errors thatthey made.

TheReinhart-Rogoff kerfuffle49 is another example of anal-ysis that ultimately was reproducible but nevertheless ques-tionable. While Thomas Herndon did have to do a littlereverse engineering to figure out the original analysis, itwas nowhere near the years-long effort of Baggerly andcolleagues. However, it was Reinhart-Rogoff’s unconven-tional weighting scheme (fully reproducible, mind you) thatdrew all of the negative attention and strongly influencedthe analysis.

I think the key question we want to answer when seeingthe results of any data analysis is “Can I trust this analysis?”It’s not possible to go into every data analysis and checkeverything, even if all the data and code were available. Inmost cases, we want to have a sense that the analysis wasdone appropriately (if not optimally). I would argue thatrequiring that analyses be reproducible does not addressthis key question.

With reproducibility you get a number of important bene-

48http://simplystatistics.org/2012/02/27/the-duke-saga-starter-set/49http://simplystatistics.org/2013/04/19/podcast-7-reinhart-rogoff-

reproducibility/

Page 104: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Evidence-based Data Analysis 100

fits: transparency, data and code for others to analyze, andan increased rate of transfer of knowledge. These are allvery important things. Data sharing in particular may beimportant independent of the need to reproduce a study ifothers want to aggregate datasets or do combined analyses.But reproducibility does not guarantee validity or cor-rectness of the analysis.

Prevention vs. Medication

One key problem with the notion of reproducibility is thepoint in the research process at which we can apply itas an intervention. Reproducibility plays a role only inthe most downstream aspect of the research process–post-publication. Only after a paper is published (and after anyquestionable analyses have been conducted) can we checkto see if an analysis was reproducible or conducted in error.

At this point it may be difficult to correct any mistakes ifthey are identified. Grad students have graduated, postdocshave left, people have left the team. In the Potti case, lettersto the journal editors were ignored. While it may be betterto check the research process at the end rather than tonever check it, intervening at the post-publication phase isarguably the most expensive place to do it. At this phaseof the research process, you are merely “medicating” theproblem, to draw an analogy with chronic diseases. Butfundamental data analytic damage may have already beendone.

This medication aspect of reproducibility reminds me of afamous quotation from R. A. Fisher:

To consult the statistician after an experiment isfinished is often merely to ask him to conduct

Page 105: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Evidence-based Data Analysis 101

a post mortem examination. He can perhaps saywhat the experiment died of.

Reproducibility allows for the statistician to conduct thepost mortem of a data analysis. But wouldn’t it have beenbetter to have prevented the analysis from dying in the firstplace?

Moving Upstream

Reproducibility in the Publication Process?

There has already been much discussion of changing therole of reproducibility in the publication/disseminationpro-cess. What if a paper had to be deemed reproducible beforeit was published? The question here is who will reproducethe analysis? We can’t trust the authors to do it so wehave to get an independent third party. What about peerreviewers? I would argue that this is a pretty big burdento place on a peer reviewer who is already working forfree. How about one of the Editors? Well, at the journalBiostatistics, that’s exactly what we do. However, our policyis voluntary and only plays a role after a paper has beenaccepted through the usual peer review process. At any rate,

Page 106: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Evidence-based Data Analysis 102

from a business perspective, most journal owners will bereluctant to implement any policy that might reduce thenumber of submissions to the journal.

What Then?

To summarize, I believe reproducibility of computationalresearch is very important, primarily to increase trans-parency and to improve knowledge sharing. However, Idon’t think reproducibility in and of itself addresses thefundamental question of “Can I trust this analysis?”. Fur-thermore, reproducibility plays a role at the most down-streampart of the researchprocess (post-publication)whereit is costliest to fix any mistakes that may be discovered.Ultimately, we need to think beyond reproducibility andto consider developing ways to ensure the quality of dataanalysis from the start.

How can we address the key problem concerning the valid-ity of a data analysis?

Towards Evidence-based Data Analysis

Now, it’s not true that reproducibility has no value. That’sdefinitely not true and I’m hoping I can clarify my think-ing in this followup post. Having code and data available,importantly, makes it possible to discover problems in ananalysis, but only after the fact. I think this results in twoissues: (1) It may take a while to figure out what exactly theproblems are (even with code/data) and how to fix them;and (2) the problems in the analysismay have already causedsome sort of harm.

Page 107: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Evidence-based Data Analysis 103

Open Source Science?

For the first problem, I think a reasonable analogy for re-producible research is open source software. There the ideais that source code is available for all computer programs sothat we can inspect and modify how a program runs. Withopen source software “all bugs are shallow”. But the key hereis that as long as all programmers have the requisite tools,they canmodify the source code on their own, publish theircorrected version (if they are fixing a bug), others can reviewit and accept or modify, and on and on. All programmersare more or less on the same footing, as long as they havethe ability to hack the code. With distributed source codemanagement systems like git, people don’t even need per-mission to modify the source tree. In this environment, thebest idea wins.

The analogy with open source software breaks down a bitwith scientific research because not all players are on thesame footing. Typically, the original investigator is muchbetter equipped to modify the “source code”, in this casethe data analysis, and to fix any problems. Some types ofanalyses may require tremendous resources that are notavailable to all researchers. Also, it might take a long timefor others who were not involved in the research, to fullyunderstand what is going on and how to make reasonablemodifications. That may involve, for example, learning thescience in the first place, or learning how to program acomputer for that matter. So I think making changes to adata analysis and having them accepted is a slow processin science, much more so than with open source software.There are definitely things we can do to improve our abilityto make rapid changes/updates, but the implementation ofthose changes are only just getting started.

Page 108: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Evidence-based Data Analysis 104

First Do No Harm

The second problem, that some sort of harm may havealready occurred before an analysis can be fully examinedis an important one. As I mentioned in the previous post,merely stating that an analysis is reproducible doesn’t saya whole lot about whether it was done correctly. In orderto verify that, someone knowledgeable has to go into thedetails andmuck around to seewhat is going on. If someoneis not available to do this, then we may never know whatactually happened. Meanwhile, the science still stands andothers may build off of it.

In the Duke saga, one of the most concerning aspects of thewhole story was that some of Potti’s research was going tobe used to guide therapy in a clinical trial. The fact that aseries of flawed data analyses was going to be used as thebasis of choosingwhat cancer treatments peoplewere goingto get was very troubling. In particular, one of these flawedanalyses reversed the labeling of the cancer and controlcases!

To me, it seems that waiting around for someone like KeithBaggerly to come around and spend close to 2,000 hoursreproducing, inspecting, and understanding a series of anal-yses is not an efficient system. In particular, when actualhuman lives may be affected, it would be preferable if theanalyses were done right in the first place, without the“statistics police” having to come in and check that every-thing was done properly.

Evidence-based Data Analysis

What I think the statistical community needs to invest timeand energy into is what I call “evidence-based data analysis”.

Page 109: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Evidence-based Data Analysis 105

What do I mean by this? Most data analyses are not thesimple classroom exercises that we’ve all done involvinglinear regression or two-sample t-tests. Most of the time,you have to obtain the data, clean that data, remove outliers,impute missing values, transform variables and on and on,even before you fit any sort of model. Then there’s modelselection,model fitting, diagnostics, sensitivity analysis, andmore. So a data analysis is really pipeline of operationswhere the output of one stage becomes the input of another.

The basic idea behind evidence-based data analysis is thatfor each stage of that pipeline, we should be using the bestmethod, justified by appropriate statistical research thatprovides evidence favoring one method over another. If wecannot reasonable agree on a best method for a given stagein the pipeline, then we have a gap that needs to be filled. Sowe fill it!

Just to clarify things before moving on too far, here’s asimple example.

Evidence-based Histograms

Consider the following simple histogram.

Page 110: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Evidence-based Data Analysis 106

plot of chunk unnamed-chunk-2

The histogram was created in R by calling hist(x) on someNormal random deviates. Now, we all know that a his-togram is a kind of smoother, and with any smoother,the critical parameter is the smoothing parameter or thebandwidth. Here, it’s the size of the bin or the number ofbins.

Notice that when I call hist() I don’t actually specify thenumber of bins. Why not? Because in R, the default is to useSturges’ formula for the number of bins. Where does thatcome from?Well, there is a paper in the Journal of theAmer-ican Statistical Association in 1926 by H. A. Sturges that

Page 111: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Evidence-based Data Analysis 107

justifies why such a formula is reasonable for a histogram(it is a very short paper, those were the days). R providesother choices for choosing the number of bins. For exam-ple, David Scott wrote a paper in Biometrika that justifiedbandwith/bin size based in integrated mean squared errorcriteria.

The point is that R doesn’t just choose the default numberof binswilly-nilly, there’s actual research behind that choiceand evidence supporting why it’s a good choice. Now, wemay not all agree that this default is the best choice at alltimes, but personally I rarely modify the default numberof bins. Usually I just want to get a sense of what thedistribution looks like and the default is fine. If there’s aproblem, transforming the variable somehow often is moreproductive than modifying the number of bins. What’s thebest transformation? Well, it turns out there’s research onthat too.

Evidence-based Reproducible Research

Now why can’t we extend the idea behind the histogrambandwidth to all data analysis? I think we can. For everystage of a given data analysis pipeline, we can have the“best practices” and back up those practices with statisticalresearch. Of course it’s possible that such best practiceshave not yet been developed. This is common in emergingareas like genomics where the data collection technology isconstantly changing. That’s fine, but in more mature areas,I think it’s possible for the community to agree on a seriesof practices that work, say, 90% of the time.

There are a few advantages to evidence-based reproducibleresearch.

Page 112: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Evidence-based Data Analysis 108

• It reduces the “researcher degrees of freedom”. Re-searchers would be disincentivized from choosing themethod that produces the “best” results if there isalready a generally agreed upon approach. If a givendata analysis required a different approach, the bur-den would be on the analyst to justify why a deviationfrom the generally accepted approach was made.

• The methodology would be transparent because theapproach would have been vetted by the community.I call this “transparent box” analysis, as opposed toblack box analysis. The analysis would be transparentso you would know exactly what is going on, but itwould “locked in a box” so that you couldn’t tinkerwith it to game the results.

• You would not have the lonely data analyst coming upwith their own magical method to analyze the data.If a researcher claimed to have conducted an analysisusing an evidence-based pipeline, you could at leasthave a sense that something reasonable was done. Youwould still need reproducibility to ensure that theresearcher was not misrepresenting him/herself, butnow we would have two checks on the analysis, notjust one.

• Most importantly, evidence-based reproducible re-search attacks the furthest upstream aspect of theresearch, which is the analysis itself. It guarantees thatgenerally accepted approaches are used to analyze thedata from the very beginning and hopefully preventsproblems from occurring rather than letting thempropagate through the system.

Page 113: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Evidence-based Data Analysis 109

Evidence-based Data Analysis inPractice

How can we implement evidence-based data analysis inpractice? Depending on your favorite software system youcould imagine a number of ways to do this. If the pipelinewere implemented in R, you could imagine it as an R pack-age. The precise platform is not critical at this point; I wouldimagine most complex pipelines would involve multipledifferent software systems tied together.

Below is a rough diagram of how I think the various piecesof an evidence-based data analysis pipeline would fit to-gether.

Deterministic Statistical Machine

There are a few key elements of this diagram that I’d like tostress:

• Inputs are minimal. You don’t want to allow for alot of inputs or arguments that can be fiddled with.

Page 114: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Evidence-based Data Analysis 110

This reduces the number of degrees of freedom andhopefully reduces the amount of hacking. Basically,youwant to be able to input the data andperhaps somemetadata.

• Analysis comes in stages. There are multiple stages inany analysis, not just the part where you fit a model.Everything is important and every stage should usethe best available method.

• The stuff in the red box does not involvemanual inter-vention. The point is to not allow tweaking, fudging,and fiddling. Once the data goes in, we just wait forsomething to come out the other end.

• Methods should be benchmarked. For each stage ofthe analysis, there is a set of methods that are ap-plied. Thesemethods should, at aminimum, be bench-marked via a standard group of datasets. That way, ifanother method comes a long, we have an objectiveway to evaluatewhether the newmethod is better thanthe older methods. Newmethods that improve on thebenchmarks can replace the existing methods in thepipeline.

• Output includes a human-readable report. This reportsummarizes what the analysis was and what the re-sults were (including results of any sensitivity anal-ysis). The material in this report could be includedin the “Methods” section of a paper and perhaps inthe “Results” or “Supplementary Materials”. The goalwould be to allow someonewhowas not intimately fa-miliar with the all of the methods used in the pipelineto be able to walk away with a report that he/shecould understand and interpret. At a minimum, thisperson could take the report and share it with theirlocal statistician for help with interpretation.

• There is a defined set of output parameters. Each anal-

Page 115: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Evidence-based Data Analysis 111

ysis pipeline should, in a sense, have an “API” so thatwe knowwhat outputs to expect (not the exact values,of course, but what kinds of values). For example, if apipeline fits a regression model at the end the regres-sion parameters are the key objects of interest, thenthe output could be defined as a vector of regressionparameters. There are two reasons to have this: (1) theoutputs, if the pipeline is deterministic, could be usedfor regression testing in case the pipeline is modified;and (2) the outputs could serve as inputs into anotherpipeline or algorithm.

Clearly, one pipeline is not enough. We need many of themfor different problems. So what do we do with all of them?

I think we could organize them in a central location (kindof a specialized GitHub) where people could search for,download, create, and contribute to existing data analy-sis pipelines. An analogy (but not exactly a model) is theCochrane Collaboration which serves as a repository forevidence-based medicine. There are already a number ofinitiatives along these lines, such as the Galaxy Project forbioinformatics. I don’t know whether it’d be ideal to haveeverything in one place or have a number of sub-projectsfor specialized areas.

Each pipeline would have a leader (or “friendly dictator”)whowouldorganize the contributions anddeterminewhichcomponents would go where. This could obviously be con-tentious, more some in some areas than in others, but Idon’t think any more contentious than your average opensource project (check the archives of the Linus Kernel orGit mailing lists and you’ll see what I mean).

Page 116: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Evidence-based Data Analysis 112

Summary

To summarize, I thinkwe need to organize lots of evidence-based data analysis pipelines and make them widely avail-able. If I were writing this 5 or 6 years ago, I’d be complain-ing about a lack of infrastructure out there to support this.But nowadays, I think we have pretty much everything weneed in terms of infrastructure. So what are wewaiting for?

Page 117: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Public ReproducibilityResourcesThis chapter provides a list of resources that you may finduseful when making your code and data available to others.The list is certainly incomplete, but provides a sense whatwhat’s out there. Keep inmind that all of these resources areprimarily designed aroundmaking things publicly available.If you do not want the public to access your material, youmay need to seek other resources (or pay a fee).

Code Repositories

• GitHub50: this site hosts git repositories for free if theyare public and for a fee if they are private. GitHuboffers a lot of features for collaborating with otherssuch as the ability to track issues and for others tomerge changes into your repository. Hosting a repos-itory here typically obviates the need for a separatemailing list as many of the communication aspects arebuilt in.

• Sourceforge51: An older site that similarly hosts coderepositories including those based on Mercurial, git,or subversion. Features also include issue tracking anddiscussion forums.

• Bitbucket52: similar to other sites described above,Bitbucket hosts repositories based onMercurial or git

50https://github.com51http://sourceforge.net52https://bitbucket.org

113

Page 118: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Public Reproducibility Resources 114

and allows for simple collaboration between projectteam members. Pricing is based on the number ofusers collaborating on a repository. They also have aspecial plan for academics (if you have a .edu emailaddress).

• repo.or.cz53: this site was one of the original repos-itories for hosting git repositories. It is free, and isstill running, but has largely been overtaken by othersites like GitHub, which offer far more in the way offeatures.

• CRAN54: this is the central repository for R packages.If you develop code for R, in addition to hosting yoursource code on a site like GitHub, it’s probably wiseto put a package on CRAN so that others can easilyinstall it.

It may be logical to host data in a code repository, if thedatasets are small and manageable (typically “toy” datasetsor demonstration datasets). Large datasets are usually notsuitable for hosting in a code repository.

Data Repositories

• NCBI55: The National Center for Biotechnology In-formation hosts a number of databases to which youcan upload data for scientific publications and fromwhich you can obtain publicly available data. It is anenormous resource for biological data and the go-to place for biological researchers looking to deposittheir data.

53http://repo.or.cz54http://cran.r-project.org55http://www.ncbi.nlm.nih.gov

Page 119: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

Public Reproducibility Resources 115

• Dataverse56: This site is run out of Harvard’s Insti-tute for Quantitative and Social Science as a placewhere researchers can host their data with their pa-pers. Datasets get a permanent unique ID that can bereferenced elsewhere.

• ICPSR57: The Inter-university Consortium for Po-litical and Social Research is another repository fordatasets, typically in the social sciences.

Publication Repositories

• R Pubs58: this is a fairly simple web site hosted byRStudio59 that allows for the simple publication ofHTML documents created using the RStudio devel-opment environment. If you are creating an R mark-down document in RStudio, you can, with the click ofa button, publish the HTML version of that documentto RPubs and immediately make it publicly available.RPubs will provide you with a permanent unique linkthat you can share with others.

• GitHub60: Although GitHub is a source code reposi-tory, it can be quite useful for hosting documents too.In particular, the web site will automatically interpretmarkdown documents and prettify them so that peo-ple can view the directly from the repository in theirweb browser.

56http://dataverse.org57https://www.icpsr.umich.edu/icpsrweb/landing.jsp58http://rpubs.com59https://www.rstudio.com60https://github.com

Page 120: ReportWritingforDataScience - paasp.net · StayinTouch! Thanksforpurchasingthisbook.Ifyouareinterestedin hearingmorefrommeaboutthingsthatI’mworkingon (books,datasciencecourses,podcast,etc.),youcandotwo

About the AuthorRoger D. Peng is an Associate Professor of Biostatistics atthe JohnsHopkinsBloomberg School of PublicHealth.He isalso a Co-Founder of the Johns Hopkins Data Science Spe-cialization61, which has enrolled over 1.5 million students,the Johns Hopkins Executive Data Science Specialization62,the Simply Statistics blog63 where he writes about statisticsand data science for the general public, and theNot So Stan-dard Deviations64 podcast. Roger can be found on Twitterand GitHub under the user name @rdpeng65.

61http://www.coursera.org/specialization/jhudatascience/162https://www.coursera.org/specializations/executive-data-science63http://simplystatistics.org/64https://soundcloud.com/nssd-podcast65https://twitter.com/rdpeng

116