<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>DeclareDesign on R Views</title>
    <link>https://rviews.rstudio.com/tags/declaredesign/</link>
    <description>Recent content in DeclareDesign on R Views</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Mon, 01 Jul 2019 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://rviews.rstudio.com/tags/declaredesign/" rel="self" type="application/rss+xml" />
    
    
    
    
    <item>
      <title>Imagine your Data Before You Collect It</title>
      <link>https://rviews.rstudio.com/2019/07/01/imagine-your-data-before-you-collect-it/</link>
      <pubDate>Mon, 01 Jul 2019 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2019/07/01/imagine-your-data-before-you-collect-it/</guid>
      <description>
        


&lt;p&gt;As data scientists, we are often presented with a dataset and are asked to use it to produce insights. We use R to wrangle, visualize, model, and produce tables and plots for sharing or publication. When we focus on the data in hand in this way, we don’t get to consider where the data came from. The sample size and the set of variables and their scales are fixed. Yet the procedures used to gather or generate them are hugely consequential for how we should analyze the data and also the quality of the insights we can ultimately deliver. Sampling procedures have implications for how the resulting data should be analyzed. For studies that seek to measure causal effects, it matters how some units come to be treated and others left untreated.&lt;/p&gt;
&lt;p&gt;Because these processes are so important, we wanted to make a tool that would help data scientists and other researchers &lt;strong&gt;imagine&lt;/strong&gt; their data &lt;strong&gt;before&lt;/strong&gt; they collect it so that any changes to process can be made before it’s too late.&lt;/p&gt;
&lt;p&gt;When the data is already collected, the tool allows you to imagine your data before you &lt;strong&gt;analyze&lt;/strong&gt; it. When we make data wrangling and modeling decisions based on the results we find under each procedure, or using model fit statistics, we are vulnerable to the unconscious biases labeled the &lt;a href=&#34;http://www.stat.columbia.edu/~gelman/research/unpublished/p_hacking.pdf&#34;&gt;garden of forking paths or p-hacking&lt;/a&gt; that may lead us to select the analysis procedure that produces the best answer. We use the actual data because we don’t have a good substitute: data with the same structure and variables that we have collected.&lt;/p&gt;
&lt;p&gt;This post introduces the &lt;code&gt;fabricatr&lt;/code&gt; package, whose role in the &lt;code&gt;DeclareDesign&lt;/code&gt; suite of packages is to simulate data structure and variables. See this &lt;a href=&#34;https://rviews.rstudio.com/2019/06/04/introducing-declaredesign/&#34;&gt;RViews post introducing &lt;code&gt;DeclareDesign&lt;/code&gt;&lt;/a&gt; and the philosophy behind it. &lt;code&gt;fabricatr&lt;/code&gt; helps you to think about your data before you start analysis or even collection. What are the units? How are they structured? What measurements will you take? What are their ranges and how are they correlated? &lt;code&gt;fabricatr&lt;/code&gt; can help you simulate mock data before you collect the real data, and test out different estimation strategies without worrying about biasing your inferences.&lt;/p&gt;
&lt;div id=&#34;imagining-your-data-structure&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Imagining your data structure&lt;/h1&gt;
&lt;p&gt;Most simply, &lt;code&gt;fabricatr&lt;/code&gt; will create a single-level data structure given a number of units.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(fabricatr)
fabricate(N = 100, temp_fahrenheit = rnorm(N, mean = 80, sd = 20))&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Warning: `is_lang()` is deprecated as of rlang 0.2.0.
## Please use `is_call()` instead.
## This warning is displayed once per session.&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Warning: `lang_name()` is deprecated as of rlang 0.2.0.
## Please use `call_name()` instead.
## This warning is displayed once per session.&lt;/code&gt;&lt;/pre&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr class=&#34;header&#34;&gt;
&lt;th align=&#34;left&#34;&gt;ID&lt;/th&gt;
&lt;th align=&#34;right&#34;&gt;temp_fahrenheit&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td align=&#34;left&#34;&gt;001&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;56.6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td align=&#34;left&#34;&gt;002&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;46.3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td align=&#34;left&#34;&gt;003&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;90.5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td align=&#34;left&#34;&gt;004&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;75.1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td align=&#34;left&#34;&gt;005&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;85.1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td align=&#34;left&#34;&gt;006&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;102.8&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Social science data is often &lt;strong&gt;hierarchical&lt;/strong&gt;. For example, schools have classrooms that have students. &lt;code&gt;fabricatr&lt;/code&gt; shines here with the &lt;code&gt;add_level&lt;/code&gt; command. By default, new levels are nested within the levels above them.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(fabricatr)
fabricate(
  # five schools
  school  = add_level(N = 5,
  n_classrooms = sample(10:15, N, replace = TRUE)),
  # 10 to 15 classrooms per school
  classroom  = add_level(N = n_classrooms),
  # 15 students per classroom
  student = add_level(N = 15)
  )&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Warning: `lang_modify()` is deprecated as of rlang 0.2.0.
## Please use `call_modify()` instead.
## This warning is displayed once per session.&lt;/code&gt;&lt;/pre&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr class=&#34;header&#34;&gt;
&lt;th align=&#34;left&#34;&gt;school&lt;/th&gt;
&lt;th align=&#34;right&#34;&gt;n_classrooms&lt;/th&gt;
&lt;th align=&#34;left&#34;&gt;classroom&lt;/th&gt;
&lt;th align=&#34;left&#34;&gt;student&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td align=&#34;left&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;12&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;01&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;001&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td align=&#34;left&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;12&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;01&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;002&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td align=&#34;left&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;12&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;01&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;003&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td align=&#34;left&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;12&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;01&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;004&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td align=&#34;left&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;12&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;01&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;005&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td align=&#34;left&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;12&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;01&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;006&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The real world often produces messy, overlapping hierarchies. For example, student data may be collected from middle school and also high school, in which case students are nested in two different schools, but those schools are not nested within each other. Here’s how to make such “cross-classified” data. The &lt;code&gt;rho&lt;/code&gt; parameter governs how correlated &lt;code&gt;primary_rank&lt;/code&gt; and &lt;code&gt;secondary_rank&lt;/code&gt; should be.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dat &amp;lt;- 
fabricate(
  primary_schools = add_level(N = 5, primary_rank = 1:N),
  secondary_schools = add_level(N = 6, secondary_rank = 1:N, nest = FALSE),
  students = link_levels(N = 15, by = join(primary_rank, secondary_rank, rho = 0.9))
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## `link_levels()` calls are faster if the `mvnfast` package is installed.&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat, aes(primary_rank, secondary_rank)) + geom_point(position = position_jitter(width = 0.1, height = 0.1), alpha = 0.5) + theme_bw()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2019-06-21-imagine-your-data-before-you-collect-it_files/figure-html/unnamed-chunk-6-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Similarly, you can create longitudinal data via &lt;code&gt;cross_levels&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;fabricate(
 students = add_level(N = 2),
 years = add_level(N = 20, year = 1981:2000, nest = FALSE),
 student_year = cross_levels(by = join(students, years))
)&lt;/code&gt;&lt;/pre&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr class=&#34;header&#34;&gt;
&lt;th align=&#34;left&#34;&gt;students&lt;/th&gt;
&lt;th align=&#34;left&#34;&gt;years&lt;/th&gt;
&lt;th align=&#34;right&#34;&gt;year&lt;/th&gt;
&lt;th align=&#34;left&#34;&gt;student_year&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td align=&#34;left&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;01&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;1981&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;01&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td align=&#34;left&#34;&gt;2&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;01&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;1981&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;02&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td align=&#34;left&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;02&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;1982&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;03&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td align=&#34;left&#34;&gt;2&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;02&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;1982&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;04&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td align=&#34;left&#34;&gt;1&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;03&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;1983&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;05&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td align=&#34;left&#34;&gt;2&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;03&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;1983&lt;/td&gt;
&lt;td align=&#34;left&#34;&gt;06&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div id=&#34;imagining-your-variables&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Imagining your variables&lt;/h1&gt;
&lt;p&gt;R has lots of great tools for simulating variables. In some cases, though, common kinds of outcome variables are surprisingly tough to simulate. &lt;code&gt;fabricatr&lt;/code&gt; collects a small number of functions to create variable types commonly used by social scientists, with simple syntax. We describe two examples here, but see our
&lt;a href=&#34;https://declaredesign.org/r/fabricatr/articles/common_social.html&#34;&gt;variable creation vignette&lt;/a&gt; for the rest.&lt;/p&gt;
&lt;div id=&#34;variables-with-intra-class-correlation&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Variables with intra-class correlation&lt;/h2&gt;
&lt;p&gt;With the data structure tools described above, you can construct data that has
within-unit and between-unit variation, for example, variation within classrooms
and variation across classrooms in test scores. However, many times you want to
set the level of intra-class correlation (ICC) more precisely. We help with
&lt;code&gt;draw_normal_icc&lt;/code&gt; and &lt;code&gt;draw_binary_icc&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dat &amp;lt;- 
  fabricate(
    N = 1000,
    clusters = sample(LETTERS, N, replace = TRUE),
    Y1 = draw_normal_icc(clusters = clusters, ICC = .2),
    Y2 = draw_binary_icc(clusters = clusters, ICC = .2)
  )
ICC::ICCbare(clusters, Y1, dat)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.09726701&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ICC::ICCbare(clusters, Y2, dat)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.176036&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;ordered-outcomes&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Ordered outcomes&lt;/h2&gt;
&lt;p&gt;We provide a set of tools for discrete random variables (including ordered outcomes). We take a latent variable (i.e., &lt;code&gt;test_ability&lt;/code&gt;) and transform it into an ordered variable (&lt;code&gt;test_score&lt;/code&gt;).&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dat &amp;lt;- 
fabricate(
  N = 100,
  test_ability = rnorm(N),
  test_score = draw_ordered(test_ability, breaks = c(-.5, 0, .5))
)
ggplot(dat, aes(test_ability, test_score)) + geom_point() + theme_bw()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2019-06-21-imagine-your-data-before-you-collect-it_files/figure-html/unnamed-chunk-10-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;fabricatr&lt;/code&gt; is compatible with almost any R variable creation function. We highlight other terrific R packages that help simulate social science-relevant variables &lt;a href=&#34;https://declaredesign.org/r/fabricatr/articles/other_packages.html&#34;&gt;in a vignette here&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;where-to-go-next&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Where to go next&lt;/h1&gt;
&lt;p&gt;This post is a high-level teaser for &lt;code&gt;fabricatr&lt;/code&gt;’s functionality, but for a deeper introduction, check out the &lt;a href=&#34;https://declaredesign.org/r/fabricatr/articles/getting_started.html&#34;&gt;fabricatr getting started vignette&lt;/a&gt;. You can also download and print this &lt;a href=&#34;https://github.com/rstudio/cheatsheets/raw/master/fabricatr.pdf&#34;&gt;cheatsheet&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can install &lt;code&gt;fabricatr&lt;/code&gt; from CRAN:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;install.packages(&amp;quot;fabricatr&amp;quot;)
library(fabricatr)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;&lt;a href=&#34;https://graemeblair.com/&#34;&gt;Graeme Blair&lt;/a&gt; is an Assistant Professor of Political Science at UCLA.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;a href=&#34;http://jasper-cooper.com/&#34;&gt;Jasper Cooper&lt;/a&gt; is a Postdoctoral Research Associate at the Kahneman-Treisman Center for Behavioral Science and Public Policy at Princeton University.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;a href=&#34;https://alexandercoppock.com/&#34;&gt;Alexander Coppock&lt;/a&gt; is an Assistant Professor of Political Science at Yale University.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;a href=&#34;http://www.macartan.nyc/&#34;&gt;Macartan Humphreys&lt;/a&gt; is a Professor of Political Science at Columbia University and a Director of the research group “Institutions and Political Inequality” at the WZB Berlin Social Science Center.&lt;/em&gt;&lt;/p&gt;
&lt;/div&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2019/07/01/imagine-your-data-before-you-collect-it/&#39;;&lt;/script&gt;
      </description>
    </item>
    
    <item>
      <title>Introducing DeclareDesign, a Platform for Research Design</title>
      <link>https://rviews.rstudio.com/2019/06/04/introducing-declaredesign/</link>
      <pubDate>Tue, 04 Jun 2019 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2019/06/04/introducing-declaredesign/</guid>
      <description>
           



&lt;p&gt;&lt;a href=&#34;https://graemeblair.com/&#34;&gt;Graeme Blair&lt;/a&gt; is an Assistant Professor of Political Science at UCLA.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://jasper-cooper.com/&#34;&gt;Jasper Cooper&lt;/a&gt; is a Postdoctoral Research Associate at the Kahneman-Treisman Center for Behavioral Science and Public Policy at Princeton University.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://alexandercoppock.com/&#34;&gt;Alexander Coppock&lt;/a&gt; is an Assistant Professor of Political Science at Yale University.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.macartan.nyc/&#34;&gt;Macartan Humphreys&lt;/a&gt; is a Professor of Political Science at Columbia University and a Director of the research group “Institutions and Political Inequality” at the WZB Berlin Social Science Center.&lt;/p&gt;
&lt;p&gt;Research design consists of a set of choices about what research &lt;em&gt;procedures&lt;/em&gt; to use. For example, how many subjects to interview, which questions to ask them, and what to do in the analysis phase with the data that results from these choices. We do not have good tools for assessing whether the chosen procedures are good ones. &lt;a href=&#34;https://declaredesign.org/r/declaredesign&#34;&gt;&lt;code&gt;DeclareDesign&lt;/code&gt;&lt;/a&gt; is an R package for learning about, implementing, and communicating research procedures, from data collection to data analysis.&lt;/p&gt;
&lt;p&gt;Today, most data scientists take raw data as the starting point for analysis, and consider how best to import, tidy, transform, visualize, model, and communicate their results. Yet often during this process, we learn that our data can only provide limited answers to our questions. Perhaps the sample size is too small, we do not have enough observations from men and women to estimate gender differences, or we did not collect data on units’ geographic location, so we cannot merge to administrative data. We rely on rules of thumb or expert opinion to navigate the myriad research design choices we face. We select analysis strategies tailored to the data that we have before us rather than the data that could have arisen. In doing so, we risk introducing biases. Our practices can often lead to bad inferences and bad science.&lt;/p&gt;
&lt;p&gt;To learn whether a research procedure is a good one, we need to know what the results &lt;em&gt;would have been&lt;/em&gt; had the data turned out differently. With that knowledge, we can assess our data analysis procedures in terms of whether they will yield answers that are unbiased and precise. &lt;code&gt;DeclareDesign&lt;/code&gt; helps us simulate different data sets that could result from our data collection procedures. With these simulated data, we can ask the question: is our set of procedures likely to yield unbiased, low-variance answers to our research questions? We can assess not only whether we should select difference-in-means or regression as our estimator, but also whether it would have been better to assign half of subjects to treatment in our A/B test, or more than half.&lt;/p&gt;
&lt;p&gt;We’ve built a set of linked tools that help with each step of the research design process. In this post, we’ll talk about using &lt;code&gt;DeclareDesign&lt;/code&gt; to declare and diagnose the properties of a full set of procedures. We’ll also introduce &lt;a href=&#34;https://declaredesign.org/library/&#34;&gt;&lt;code&gt;DesignLibrary&lt;/code&gt;&lt;/a&gt;, which helps you quickly get started learning about your research design for a set of common designs. In the coming weeks, we’ll talk about tools for simulating data (&lt;a href=&#34;https://declaredesign.org/r/fabricatr&#34;&gt;&lt;code&gt;fabricatr&lt;/code&gt;&lt;/a&gt;), sampling cases and assigning A/B tests (&lt;a href=&#34;https://declaredesign.org/r/randomizr&#34;&gt;&lt;code&gt;randomizr&lt;/code&gt;&lt;/a&gt;), and estimating effects (&lt;a href=&#34;https://declaredesign.org/r/estimatr/&#34;&gt;&lt;code&gt;estimatr&lt;/code&gt;&lt;/a&gt;).&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(DeclareDesign)
library(tidyverse)&lt;/code&gt;&lt;/pre&gt;
&lt;div id=&#34;a-grammar-of-research-designs&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;A grammar of research designs&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;DeclareDesign&lt;/code&gt; implements a set of ideas laid out in &lt;a href=&#34;https://declaredesign.org/declare.pdf&#34;&gt;our paper&lt;/a&gt;, forthcoming at the &lt;em&gt;American Political Science Review&lt;/em&gt;. We think of any research design as having four parts:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;A &lt;strong&gt;M&lt;/strong&gt;odel of the world.&lt;/li&gt;
&lt;li&gt;An &lt;strong&gt;I&lt;/strong&gt;nquiry about that model.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;D&lt;/strong&gt;ata strategy according to which data will be collected or brought into existence.&lt;/li&gt;
&lt;li&gt;An &lt;strong&gt;A&lt;/strong&gt;nswer strategy according to which the data will be summarized to generate a guess about the inquiry.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Writing designs down is hard, and &lt;code&gt;DeclareDesign&lt;/code&gt; can make that process simpler, more explicit, and reproducible.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;declaration-steps&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Declaration Steps&lt;/h1&gt;
&lt;p&gt;In &lt;code&gt;DeclareDesign&lt;/code&gt;, you declare each component of a research design using one of the &lt;code&gt;declare_*&lt;/code&gt; functions.&lt;/p&gt;
&lt;table&gt;
&lt;colgroup&gt;
&lt;col width=&#34;33%&#34; /&gt;
&lt;col width=&#34;4%&#34; /&gt;
&lt;col width=&#34;62%&#34; /&gt;
&lt;/colgroup&gt;
&lt;thead&gt;
&lt;tr class=&#34;header&#34;&gt;
&lt;th&gt;&lt;code&gt;declare_*&lt;/code&gt; function&lt;/th&gt;
&lt;th&gt;MIDA&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td&gt;&lt;code&gt;declare_population()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;M&lt;/td&gt;
&lt;td&gt;Pre-treatment covariates&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td&gt;&lt;code&gt;declare_potential_outcomes()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;M&lt;/td&gt;
&lt;td&gt;Relationships between treatments, covariates, and outcomes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td&gt;&lt;code&gt;declare_estimand()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;I&lt;/td&gt;
&lt;td&gt;Quantity of interest&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td&gt;&lt;code&gt;declare_sampling()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;D&lt;/td&gt;
&lt;td&gt;(Possibly random) sampling procedure&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td&gt;&lt;code&gt;declare_assignment()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;D&lt;/td&gt;
&lt;td&gt;(Possibly random) assignment procedure&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td&gt;&lt;code&gt;declare_reveal()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;D&lt;/td&gt;
&lt;td&gt;Function that maps potential outcomes to realized outcomes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td&gt;&lt;code&gt;declare_estimator()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A&lt;/td&gt;
&lt;td&gt;Estimation (and hypothesis testing, if applicable) procedure&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Each &lt;code&gt;declare_*&lt;/code&gt; function is a “function factory”, which means that each of these functions themselves returns a function. Each function takes data as an argument, and returns either data or statistics. Many R users are not used to having functions return functions, but this makes simulation very easy, and is not so odd once you get used to it. In the example below, we define the &lt;code&gt;pop&lt;/code&gt; function using &lt;code&gt;declare_population()&lt;/code&gt;. Each time we call the resulting &lt;code&gt;pop&lt;/code&gt; function, we get a slightly different data set.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;pop &amp;lt;- declare_population(N = 3, X = rnorm(N))
pop()&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;  ID         X
1  1 -2.120337
2  2 -1.156013
3  3  1.703992&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;pop()&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;  ID          X
1  1 -0.6172369
2  2  0.3183334
3  3 -1.3762445&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;chaining-steps-together&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Chaining steps together&lt;/h1&gt;
&lt;p&gt;A research design can be thought of as a series of steps. In &lt;code&gt;DeclareDesign&lt;/code&gt;, we chain steps together using the &lt;code&gt;+&lt;/code&gt; operator, which may be familiar to &lt;code&gt;ggplot2&lt;/code&gt; users. Each step takes the data generated in the step before it, and either returns statistics or passes that data on to the next step, having added new variables or changed the shape of the data. In the next example, we declare a workhorse research strategy used in many scientific fields, the two-arm randomized trial:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;design &amp;lt;-
  
  # M -- Model
  declare_population(N = 100, X = rpois(N, 4), noise = rnorm(N)) +
  declare_potential_outcomes(Y ~ 0.2 * Z + 0.5 * X + noise) +
  
  # I -- Inquiry
  declare_estimand(ATE = mean(Y_Z_1 - Y_Z_0)) +
  
  # D -- Data Strategy
  declare_sampling(n = 50) +
  declare_assignment(m = 25) +
  declare_reveal(Y, Z) +
  
  # A -- Answer Strategy
  declare_estimator(Y ~ Z, model = difference_in_means, estimand = &amp;quot;ATE&amp;quot;, label = &amp;quot;DIM&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Some points to note about this declaration:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;&lt;code&gt;design&lt;/code&gt; is a “design object,” which can then be used to learn all kinds of things about the design. We’ll turn to some common post-declaration functions in a moment.&lt;/li&gt;
&lt;li&gt;“Potential outcomes” is a concept from the causal inference literature. Potential outcomes are the outcomes that each unit would express depending on the counterfactual level of some treatment. The revealed outcome is the &lt;strong&gt;observed&lt;/strong&gt; outcome: treated subjects reveal their treated outcome and untreated subjects reveal their untreated outcome. The potential outcomes model of causality is deep. For users who are new to thinking in terms of potential outcomes, we promise it will get easier with time. &lt;a href=&#34;https://en.wikipedia.org/wiki/Rubin_causal_model&#34;&gt;The Wikipedia article&lt;/a&gt; is OK, but we would suggest &lt;a href=&#34;https://www.amazon.com/Field-Experiments-Design-Analysis-Interpretation/dp/0393979954&#34;&gt;Chapter 2 of this book&lt;/a&gt; if it’s available to you.&lt;/li&gt;
&lt;li&gt;In the code above, the estimand and estimator steps are explicitly linked together – we define the average treatment effect (ATE) in terms of potential outcomes, and then in the estimation step, we tell the estimator that it is shooting at the ATE. This means that a design “knows” what question each estimator is trying to answer.&lt;/li&gt;
&lt;li&gt;These declaration functions depend on the other packages in the &lt;code&gt;DeclareDesign&lt;/code&gt; set of packages, which includes &lt;code&gt;fabricatr&lt;/code&gt;, &lt;code&gt;randomizr&lt;/code&gt;, and &lt;code&gt;estimatr&lt;/code&gt;. We’ll introduce each of these packages in upcoming posts. The population and potential outcomes steps use &lt;code&gt;fabricatr&lt;/code&gt;, the sampling and assignment use &lt;code&gt;randomizr&lt;/code&gt;, and the estimator steps use &lt;code&gt;estimatr&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;div id=&#34;what-can-you-do-with-a-design-object&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;What can you do with a design object?&lt;/h1&gt;
&lt;p&gt;The first thing you might want to do with a design object is &lt;code&gt;draw_data&lt;/code&gt;. Take a look:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dat &amp;lt;- draw_data(design)
head(dat)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;   ID X       noise    Y_Z_0    Y_Z_1 S_inclusion_prob Z Z_cond_prob
1 001 3 -0.09946134 1.400539 1.600539              0.5 1         0.5
2 002 3  0.42156668 1.921567 2.121567              0.5 1         0.5
3 003 3  0.24064949 1.740649 1.940649              0.5 0         0.5
4 005 5 -1.29990991 1.200090 1.400090              0.5 1         0.5
5 007 4  1.38503254 3.385033 3.585033              0.5 1         0.5
6 008 3  0.16181031 1.661810 1.861810              0.5 1         0.5
         Y
1 1.600539
2 2.121567
3 1.740649
4 1.400090
5 3.585033
6 1.861810&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can quickly generate simulated data that (if you did your declaration right!) is the same size and shape as the data your study &lt;strong&gt;will&lt;/strong&gt; generate, once it’s run. Imagining your data ex ante gives you the chance to think about logistical, practical, theoretical, or analytic concerns that might otherwise be put off until far later in the design process.&lt;/p&gt;
&lt;p&gt;Next up, you can take a look a the estimands and estimates produced by one run of your design:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;draw_estimands(design)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;  estimand_label estimand
1            ATE      0.2&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;draw_estimates(design)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;  estimator_label term  estimate std.error statistic   p.value   conf.low
1             DIM    Z 0.1302799 0.3997006 0.3259437 0.7459163 -0.6738506
  conf.high      df outcome estimand_label
1 0.9344104 46.9193       Y            ATE&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These functions give you a chance to ensure that your estimands and estimators make sense and are returning what you expect them to return. If a &lt;code&gt;0.2&lt;/code&gt; average treatment effect is implausible in your research setting, change it!&lt;/p&gt;
&lt;p&gt;You can also add steps to a design. Here, we’re adding another estimator that &lt;strong&gt;also&lt;/strong&gt; shoots at the ATE, but this time adjusts for a pre-treatment covariate, &lt;code&gt;X&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;design &amp;lt;- design + declare_estimator(Y ~ Z + X, model = lm_robust, estimand = &amp;quot;ATE&amp;quot;, label = &amp;quot;OLS&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;simulating-designs&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Simulating designs&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;DeclareDesign&lt;/code&gt; has two functions for conducting simulations: &lt;code&gt;simulate_design&lt;/code&gt; and &lt;code&gt;diagnose_design&lt;/code&gt;. If your answer strategy returns estimates, &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-values, and standard errors, and it is linked to an estimand, &lt;code&gt;diagnose_design&lt;/code&gt; can tell you a lot about your design: its power, its coverage probability, its “true” standard error (the standard deviation of the estimates across repeat samples), the average standard error it returns, its bias, its root mean square error, the probability you get the sign of the estimand wrong, its false positive rate, and more. If you just want to simulate the design, &lt;code&gt;simulate_design&lt;/code&gt; will return a data set in which each row is an estimator-estimand pair. Since we have two estimators, each run of the design produces two rows.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;simulations &amp;lt;- simulate_design(design, sims = 500)
head(simulations)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;  design_label sim_ID estimand_label estimand estimator_label term
1       design      1            ATE      0.2             DIM    Z
2       design      1            ATE      0.2             OLS    Z
3       design      2            ATE      0.2             DIM    Z
4       design      2            ATE      0.2             OLS    Z
5       design      3            ATE      0.2             DIM    Z
6       design      3            ATE      0.2             OLS    Z
     estimate std.error  statistic    p.value     conf.low conf.high
1 -0.73149295 0.4594131 -1.5922335 0.11789917 -1.655212062 0.1922262
2 -0.04367092 0.3435135 -0.1271301 0.89937977 -0.734730962 0.6473891
3  0.23917794 0.4454914  0.5368856 0.59384801 -0.656742583 1.1350985
4  0.62821514 0.3105686  2.0227904 0.04880466  0.003431732 1.2529986
5  0.27936681 0.3567281  0.7831365 0.43758754 -0.438821047 0.9975547
6  0.04149547 0.2894608  0.1433544 0.88662317 -0.540824504 0.6238154
        df outcome
1 47.98565       Y
2 47.00000       Y
3 47.58893       Y
4 47.00000       Y
5 45.69013       Y
6 47.00000       Y&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The simulations data can then be used to summarize the design using &lt;code&gt;ggplot2&lt;/code&gt; and &lt;code&gt;dplyr&lt;/code&gt;. For example, simulation is useful to understand the &lt;strong&gt;distribution&lt;/strong&gt; of effect estimates; are they centered on the estimand? Is the distribution wide or narrow? Are there outliers that might indicate estimation issues?&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;summary_df &amp;lt;-
  simulations %&amp;gt;%
  group_by(estimator_label) %&amp;gt;%
  summarize(`Mean Estimate` = mean(estimate),
            `Mean Estimand` = mean(estimand)) %&amp;gt;%
  gather(key, value, `Mean Estimand`, `Mean Estimate`)

ggplot(simulations, aes(estimate)) +
  geom_histogram(bins = 30) +
  geom_vline(data = summary_df, aes(xintercept = value, color = key)) +
  facet_wrap( ~ estimator_label) + 
  theme_bw() +
  theme(strip.background = element_blank(),
        legend.position = &amp;quot;bottom&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2019-05-19-introducing-declaredesign_files/figure-html/unnamed-chunk-8-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;getting-started-quickly-with-designlibrary&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Getting started quickly with DesignLibrary&lt;/h1&gt;
&lt;p&gt;You can get started by declaring a design in a single line of code with &lt;code&gt;DesignLibrary&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(DesignLibrary)
two_arm_design &amp;lt;- two_arm_designer(N = 1000)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;DesignLibrary&lt;/code&gt; contains 15 common research designs that can be declared in this simple
way. In each case, a set of function arguments are set up to define the core inputs to the design, such as sample size. For example, we can declare a pretest-posttest design in which data is collected before (pre) and after (post) a treatment is assigned. We define the sample size (&lt;code&gt;N&lt;/code&gt;), the average treatment effect (&lt;code&gt;ate&lt;/code&gt;), and the correlation in outcomes between the pre and post periods (&lt;code&gt;rho&lt;/code&gt;):&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;prepost_design &amp;lt;- pretest_posttest_designer(N = 1000, ate = 5, rho = 0.25)&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;using-declaredesign-in-your-data-science-workflow&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Using DeclareDesign in your data science workflow&lt;/h1&gt;
&lt;p&gt;Since the four helper packages can be used to simulate data, sample units, randomize treatments, and analyze data, we see at least seven ways you can get value out of using &lt;code&gt;DeclareDesign&lt;/code&gt; in your everyday data-science workflow.&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;Simulate mock data (&lt;code&gt;draw_data()&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Assess the properties of your design before you run it (&lt;code&gt;diagnose_design()&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Front-load design decisions before data is collected (&lt;code&gt;+&lt;/code&gt; to declare it).&lt;/li&gt;
&lt;li&gt;Compare the properties of several possible designs (&lt;code&gt;compare_designs()&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Write a preanalysis plan or registered report (&lt;code&gt;summary(design)&lt;/code&gt; to get started).&lt;/li&gt;
&lt;li&gt;Describe your design in code in a paper or report (&lt;code&gt;print_code(design)&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Learn about properties of someone else’s design (&lt;code&gt;diagnose_design()&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Finally, you may find the &lt;a href=&#34;https://github.com/rstudio/cheatsheets/blob/master/declaredesign.pdf&#34;&gt;DeclareDesign cheatsheet&lt;/a&gt; hosted by RStudio helpful.&lt;/p&gt;
&lt;/div&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2019/06/04/introducing-declaredesign/&#39;;&lt;/script&gt;
      </description>
    </item>
    
  </channel>
</rss>
