class: center, middle, inverse, title-slide # POL90: Exercise 01 ### ### 2022-01-19 --- ## Today's Agenda .mlarge[ 1. Survey: https://forms.gle/AXc92KK4bTs5DNwc9 2. Why literate programming: `R` + `Markdown` + LaTeX + `knitr`? - Merits and challenges <!-- + Useful resources --> 3. Install R, RStudio, LaTeX 4. Install packages 5. Exercise: Play with StatKey, Run your own randomization <!-- 6. R Extras (for fun) --> <!-- 6. Coding exercise --> <!-- - Randomization distributions --> <!-- - Introduce `infer` --> ] --- ## Literate Programming .large[ “Let us change our traditional attitude to the construction of programs: Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do.” — Donald Knuth ] --- ## Why Use `R` + `R Markdown` + LaTeX + `knitr`? .mlarge[ 1. Management - braids together writing, code, output (e.g., tables, plots) in a single document 2. Reproducibility - Dramatically simplifies recreating results in document, a bedrock of good science 3. Elegance - With a little work, much better looking tables, plots, etc. 4. Up-to-date - Changes to data can be immediately reflected in prose, tables, plots. ] --- ## What Does `R Markdown` Look Like? RMarkdown template <img src="images/rmarkdown_screenshot.png" width="75%" style="display: block; margin: auto;" /> --- ## Why 'Literate Programming'? <img src="images/rmarkdown_wizards.png" width="100%" style="display: block; margin: auto;" /> .footnote[Artwork by @allison_horst] --- ## Challenges .mlarge[ 1. Multilingual - Four different languages in one document: `R`, `R Markdown`, LaTeX, `knitr` (glue connecting everything) 2. LaTeX - An advanced formatting language that's standard in science but can be complicated. 3. Debugging - More languages means more opportunities for bugs; we can help! 4. Control - At first you give up some control over formatting. ] --- ## Have you `knit` a `Markdown` document? .mlarge[ - If not, follow the *setup installation* instructions at: http://appliedstats.org/setup_instructions.html - To test that it is working, create a new `R Markdown` document in RStudio, and see if can `knit` - If you can't complete installation, follow-up with instructors ] --- ## Install course packages: - Copy and paste the following command to your R Console: - source("http://appliedstats.org/POL90_2022_packages.R") - Can also copy from: http://appliedstats.org/setup_instructions.html - If prompted to restart `R`, click `No` - If prompted with 'Do you want to install from sources the package which needs compilation?' type `n` and hit `return` --- ## Exercise: Randomization Distribution with StatKey + Go to http://www.lock5stat.com/StatKey - You can just search for StatKey on Google + Click on "Test for Difference in Means" + Click on "Leniency and Smiles" for Pop-up menu - Select "Mosquitos (Beer vs Water) + Play with randomizing assignment to the two conditions --- ## StatKey: Mosquitos (Beer vs Water) <img src="images/statkey_mosquitos_water_beer01.png" width="85%" /> --- ## StatKey: Mosquitos (Beer vs Water) <img src="images/statkey_mosquitos_water_beer02.png" width="85%" /> --- ## StatKey: Mosquitos (Beer vs Water) <img src="images/statkey_mosquitos_water_beer03.png" width="85%" /> --- ## Chapter 1: Creativity study .mlarge[ - Research questions - Do grading systems promote creativity in students? - Do ranking systems and incentive awards increase productivity among employees? - Do rewards and praise stimulate children to learn? ] --- ## Chapter 1: Creativity study .mlarge[ - Teresa Amabile ran an experiment on effects of intrinsic and extrinsic motivation on creativity - Subjects with creative writing experience randomly assigned: 24 to "Intrinsic," 23 to "Extrinsic" - After questionnaire, subjects asked to write a Haiku about "laughter" - Poems submitted to 12 poets, who rated them on 40-point scale of creativity - Score is average of 12 judges ] --- ## Idea behind randomization distribution .mlarge[ - If treatment had no effect, then observed outcomes are unrelated to whether subject was assigned to treatment or control group - Under assumption of "null hypothesis" or that treatment had no effect, we could shuffle all treatment and control assignments and recalculate difference-in-means - These simulated results allow us to see if our observed result is extreme compared to other plausible treatment and control groups - Each randomization is like a possible parallel universe (under assumption of no effect of treatment) - Questions? ] --- ## Coding exercise: load packages & data ```r # load packages suppressMessages(library(dplyr)) library(janitor) ``` ``` ## ## Attaching package: 'janitor' ``` ``` ## The following objects are masked from 'package:stats': ## ## chisq.test, fisher.test ``` ```r library(infer) # load data creativity <- Sleuth2::case0101 %>% # chapter 01, case 01 clean_names() # view data creativity %>% head(2) ``` ``` ## score treatment ## 1 5.0 Extrinsic ## 2 5.4 Extrinsic ``` --- ## Coding exercise: statistical tests .mlarge[ 1. With Base R `t.test()`, calculate whether there is a significant difference-in-means 2. By hand, randomize treatment assignment and calculate a new difference-in-means. See [http://appliedstats.org/chapter1.html](http://appliedstats.org/chapter1.html) 3. Now, repeat Step 2 ten times. How do these results compare to observed difference-in-means? 4. Randomize treatment assignment and calculate difference-in-means 1,000 times, plot histogram of differences 5. With `infer` calculate randomization distribution and plot histogram of differences ] --- ## Have you customized `R` and `RStudio`? (Optional) .mlarge[ - See R Hacks: http://appliedstats.org/r_keyboard_shortcuts.html ] --- ## Why pipes? (Optional) <img src="images/andrew_heiss_pipes_example.jpg" width="100%" /> --- ## What is `clean_names()` and `snake_case`? <img src="images/coding_cases.png" width="100%" style="display: block; margin: auto;" /> .footnote[Artwork by @allison_horst]