]> code.communitydata.science - stats_class_2019.git/blob - r_lectures/w06-R_lecture.Rmd
adding a bunch of new files around week 4
[stats_class_2019.git] / r_lectures / w06-R_lecture.Rmd
1 ---
2 title: "Week 6 R Lecture"
3 author: "Jeremy Foote"
4 date: "April 4, 2019"
5 output: html_document
6 ---
7
8 ```{r setup, include=FALSE}
9 knitr::opts_chunk$set(echo = TRUE)
10 ```
11
12 ## Categorical Data
13
14 The goal of this script is to help you think about analyzing categorical data, including proportions, tables, chi-squared tests, and simulation.
15
16 ### Estimating proportions
17
18 If a survey of 50 randomly sampled Chicagoans found that 45% of them thought that Giordano's made the best deep dish pizza, what would be the 95% confidence interval for the true proportion of Chicagoans who prefer Giordano's?
19
20 Can we reject the hypothesis that 50% of Chicagoans prefer Giordano's?
21
22
23 ```{r}
24 est = .45
25 sample_size = 50
26 SE = sqrt(est*(1-est)/sample_size)
27
28 conf_int = c(est - 1.96 * SE, est + 1.96 * SE)
29 conf_int
30 ```
31
32 What if we had the same result but had sampled 500 people?
33
34
35 ```{r}
36 est = .45
37 sample_size = 500
38 SE = sqrt(est*(1-est)/sample_size)
39
40 conf_int = c(est - 1.96 * SE, est + 1.96 * SE)
41 conf_int
42 ```
43
44 ### Tabular Data
45
46 The Iris dataset is composed of measurements of flower dimensions. It comes packaged with R and is often used in examples. Here we make a table of how often each species in the dataset has a sepal width greater than 3.
47
48 ```{r}
49
50 table(iris$Species, iris$Sepal.Width > 3)
51
52 ```
53
54
55 The chi-squared test is a test of how much the frequencies we see in a table differ from what we would expect if there was no difference between the groups.
56
57 ```{r}
58
59 chisq.test(table(iris$Species, iris$Sepal.Width > 3))
60 ```
61
62 The incredibly low p-value means that it is very unlikely that these came from the same distribution and that sepal width differs by species.
63
64
65
66 ## Using Simulation
67
68 When the assumptions of Chi-squared tests aren't met, we can use simulation to approximate how likely a given result is.
69
70 The book uses the example of a medical practitioner who has 3 complications out of 62 procedures, while the typical rate is 10%.
71
72 The null hypothesis is that this practitioner's true rate is also 10%, so we're trying to figure out how rare it would be to have 3 or fewer complications, if the true rate is 10%.
73
74 ```{r}
75 # We write a function that we are going to replicate
76 simulation <- function(rate = .1, n = 62){
77   # Draw n random numbers from a uniform distribution from 0 to 1
78   draws = runif(n)
79   # If rate = .4, on average, .4 of the draws will be less than .4
80   # So, we consider those draws where the value is less than `rate` as complications
81   complication_count = sum(draws < rate)
82   # Then, we return the total count
83   return(complication_count)
84 }
85
86 # The replicate function runs a function many times
87
88 simulated_complications <- replicate(5000, simulation())
89
90 ```
91
92 We can look at our simulated complications
93
94 ```{r}
95
96 hist(simulated_complications)
97 ```
98
99 And determine how many of them are as extreme or more extreme than the value we saw. This is the p-value.
100
101 ```{r}
102
103 sum(simulated_complications <= 3)/length(simulated_complications)
104 ```
105

Community Data Science Collective || Want to submit a patch?