-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path03-STAT_220_QuantitativeVariablesPreLab_tidy.Rmd
More file actions
180 lines (125 loc) · 7.27 KB
/
Copy path03-STAT_220_QuantitativeVariablesPreLab_tidy.Rmd
File metadata and controls
180 lines (125 loc) · 7.27 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
---
title: 'Quantitative variables pre-lab: tidy'
author: "Professor McNamara"
output:
html_document:
toc: true
toc_depth: 3
toc_float: true
---
## Setup and packages
We use three packages in this course: `Lock5Data`, `tidyverse` and `infer`. To load a package, you use the `library()` function, wrapped around the name of a package. I've put the code to load one package into the chunk below. Add the other two you need.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, error = TRUE)
library(Lock5Data)
# put in the other two packages you need here
```
## Loading in data
Let's load the example dataset I have for this course, `GSS_clean.csv`. It should be inside the `data` folder in your RStudio Cloud. We'll use the `read_csv()` function to read in the data. Here is a chunk where we will put the data loading code.
```{r data-load}
# this is where data-loading code goes
```
## Quantitative variables
Quantitative variables are those that encode some sort of continuous numeric range, with a multitude of possible values. We often talk about integer variables (just the whole numbers, like 1, 3, 10, 1000, etc) and more generic numeric variables (which may have long decimals, like 3.141593).
What variables in this dataset are quantitative? How can you tell?
## Visualization for quantitative variables
There are a number of ways to visualize one quantitative variable: histograms, density plots, and boxplots are all common ways. We'll see how to make all three.
We are once again generating graphics in the tidy R syntax. This means we use the main function `ggplot()`, and it needs at least one argument, the name of the dataset. Try running `ggplot(GSS)` and see what the result is.
```{r}
# write code here
```
Just like for quantitative variables, if want an interesting plot, we need to use the `+` operator to add on a "geometric object" (`geom_`) to tell R what kind of plot we want. Let's start with a `geom_histogram()` to make a histogram. Inside the `geom_` function, we use the `aes()` function (short for "aesthetics") and tell R how to map between a variable in our dataset and a variable in a plot. Here's an example:
```{r}
ggplot(GSS) + geom_histogram(aes(x = number_of_hours_worked_last_week))
```
This returns a plot, but also some information from R.
The first is a "message" from R, which tells us that it is using a default of 30 bins in the histogram, but we should pick a better value with binwidth.
The second is a "warning", saying that it removed 967 rows containing non-finite values. That means 967 of the 2348 rows in our dataset didn't have a value for `number_of_hours_worked_last_week`. Maybe we should think more about whether this is a good plot to be making, if almost half the data is missing.
Let's return to that binwidth message, which we can actually address. `binwidth` is another argument to the `geom_histogram()` function, so it goes inside the parentheses of that function, but **outside** the parentheses of the `aes()` function.
```{r}
ggplot(GSS) + geom_histogram(aes(x = number_of_hours_worked_last_week), binwidth = 15)
```
Try adjusting the binwidth. What do you think an appropriate binwidth would be for this data?
```{r}
ggplot(GSS) + geom_histogram(aes(x = number_of_hours_worked_last_week), binwidth = 15) # change binwidth
```
Try making your own histogram of the `number_of_brothers_and_sisters` variable. Pick an appropriate binwidth.
```{r}
# write code here
```
Now, let's try making a density plot instead. What `geom_` do you think we want? You can try typing `geom_` and hitting Tab on your keyboard to see the options.
```{r}
# write code here
```
Finally, let's make a boxplot. The `geom_` is what you might expect-- `geom_boxplot()`!. But, you need to change the way you map the variable to axes. Instead of making `number_of_hours_worked_last_week` the $x$ variable, we need to make it the $y$.
```{r}
ggplot(GSS) + geom_boxplot(aes(y = number_of_hours_worked_last_week))
```
Then, if we want to make side-by-side boxplots, we can add back in an $x$ variable,
```{r}
ggplot(GSS) + geom_boxplot(aes(y = number_of_hours_worked_last_week, x = marital_status))
```
Does this plot make sense?
Note: you can only make side-by-side boxplots if the $x$ variable is categorical. If R thinks your variable is **not** categorical, you may need to wrap the `as_factor()` function around your variable.
```{r}
ggplot(GSS) + geom_boxplot(aes(y = number_of_hours_worked_last_week, x = as_factor(marital_status)))
```
What changed here?
## Summary statistics for quantitative variables
There are many summary statistics we can calculate for quantitative variables. Here are some, and their corresponding R functions:
- mean `mean()`
- median `median()`
- standard deviation `sd()`
- maximum `max()`
- minimum `min()`
- interquartile range `IQR()`
We use these functions inside the `summarize()` command to find summary statistics for an entire quantitative variable (no `group_by()` necessary) or for a quantitative variable broken down by a categorical variable (using `group_by()` in the pipeline).
Let's see how that works.
```{r}
GSS |>
summarize(mean(number_of_hours_worked_last_week))
```
There are a couple weird things here. The first is that the name of the variable in my new, mini dataset is `mean(number_of_hours_worked_last_week)`. The other is that the mean is just `NA`. Let's address these one by one
```{r}
GSS |>
summarize(mean_hours = mean(number_of_hours_worked_last_week))
```
I've now called that variable in my summarized dataset `mean_hours`, which is easier to understand.
Now let's fix the `NA` issue. The mean is being reported as `NA` because of all those missing values we saw in the warning from R earlier when we were making the plot. There are a lot of missing values (encoded as `NA` in R) in the `number_of_hours_worked_last_week` variable. Again, we should consider whether it really makes sense to do summary statistics if half of the data is missing.
There are a couple ways to fix the issue, but perhaps the easiest is to use the `drop_na()` function in our pipeline,
```{r}
GSS |>
drop_na(number_of_hours_worked_last_week) |>
summarize(mean_hours = mean(number_of_hours_worked_last_week))
```
Now, try finding the median `number_of_brothers_and_sisters`.
```{r}
# write code here
```
If we want to compute multiple summary statistics, we can add them to our `summarize()` command, separated with commas.
```{r}
GSS |>
drop_na(number_of_hours_worked_last_week) |>
summarize(
range = max(number_of_hours_worked_last_week) - min(number_of_hours_worked_last_week),
IQR = IQR(number_of_hours_worked_last_week)
)
```
If we wanted to compute a five-number summary, we could code it ourselves,
```{r}
GSS |>
drop_na(number_of_hours_worked_last_week) |>
summarize(
min = min(number_of_hours_worked_last_week),
lower_hinge = quantile(number_of_hours_worked_last_week, .25),
median = median(number_of_hours_worked_last_week),
upper_hinge = quantile(number_of_hours_worked_last_week, .75),
max = max(number_of_hours_worked_last_week)
)
```
Or, we could take the easy route and use the `fivenum()` function. The `fivenum()` function expects a single variable, not a dataset, so we need to add one more verb to our pipeline-- `pull()`.
```{r}
GSS |>
pull(number_of_hours_worked_last_week) |>
fivenum()
```