forked from behrman/ros
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolls_tv.Rmd
More file actions
78 lines (64 loc) · 1.87 KB
/
polls_tv.Rmd
File metadata and controls
78 lines (64 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
---
title: "Regression and Other Stories: Death penalty poll"
author: "Andrew Gelman, Jennifer Hill, Aki Vehtari"
date: "`r Sys.Date()`"
output:
github_document:
toc: true
---
Tidyverse version by Bill Behrman.
Death penalty poll - Proportion of American adults supporting the death
penalty. See Chapter 4 in Regression and Other Stories.
-------------
```{r, message=FALSE}
# Packages
library(tidyverse)
# Parameters
# Gallup death penalty poll data
file_death_penalty <- here::here("Death/data/polls.dat")
# Common code
file_common <- here::here("_common.R")
#===============================================================================
# Run common code
source(file_common)
```
# 4 Statistical inference
## 4.2 Estimates, standard errors, and confidence intervals
### Comparisons, visual and numerical
Data
```{r, message=FALSE}
death_penalty <-
file_death_penalty %>%
scan() %>%
matrix(ncol = 5, byrow = TRUE) %>%
as_tibble(
.name_repair = ~ c("year", "month", "favor", "not_in_favor", "no_opinion")
) %>%
transmute(
date = lubridate::make_date(year = year, month = month),
favor = favor / (favor + not_in_favor),
favor_sd = sqrt(favor * (1 - favor) / 1000)
)
```
Are you in favor of the death penalty for a person convicted of murder?
```{r}
death_penalty %>%
ggplot(aes(date, favor)) +
geom_pointrange(
aes(ymin = favor - favor_sd, ymax = favor + favor_sd),
size = 0.2
) +
scale_x_date(
breaks = lubridate::make_date(year = seq(1940, 2000, 10)),
minor_breaks = lubridate::make_date(year = seq(1936, 2004, 2)),
date_labels = "%Y"
) +
scale_y_continuous(labels = scales::label_percent(accuracy = 1)) +
labs(
title =
"Are you in favor of the death penalty for a person convicted of murder?",
x = "Year",
y = "Percentage in favor of those with an opinion",
caption = "Source: Gallup"
)
```