-
Notifications
You must be signed in to change notification settings - Fork 46
/
chapter8.Rmd
225 lines (165 loc) · 5.75 KB
/
chapter8.Rmd
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
---
title: Basic Plots
description: >-
We engage in the basics of plot-making in R, with scatterplots, histograms and
box plots.
---
## Scatterplots
```yaml
type: NormalExercise
key: c535f6f10b
lang: r
xp: 100
skills:
- 1
```
We made a plot of total murders versus population and noted a strong relationship: not surprisingly states with larger populations had more murders. You can run the code in the console to get the plot.
```{r}
library(dslabs)
data(murders)
population_in_millions <- murders$population/10^6
total_gun_murders <- murders$total
plot(population_in_millions, total_gun_murders)
```
Note that many states have populations below 5 million and are bunched up in the plot. We may gain further insights from making this plot in the log scale.
`@instructions`
- Transform the variables using the log base 10 transformation
- Plot the log transformed total murders versus population
`@hint`
Once you define the objects `log10_population` and `log10_total_gun_murders` you plot them against each other using
`plot(log10_population, log10_total_gun_murders)`
```
`@pre_exercise_code`
```{r}
library(dslabs)
data(murders)
```
`@sample_code`
```{r}
# Define some variables (note that the dataset has been loaded for you)
population_in_millions <- murders$population/10^6
total_gun_murders <- murders$total
plot(population_in_millions, total_gun_murders)
# Transform population (the original, unadjusted population, not population in millions) using the log10 transformation and save to object log10_population
# Transform total gun murders using log10 transformation and save to object log10_total_gun_murders
# Create a scatterplot with the log scale transformed population and murders
```
`@solution`
```{r}
# Define some variables (note that the dataset has been loaded for you)
population_in_millions <- murders$population/10^6
total_gun_murders <- murders$total
plot(population_in_millions, total_gun_murders)
# Transform population (the original, unadjusted population, not population in millions) using the log10 transformation and save to object log10_population
log10_population<- log10(murders$population)
# Transform total gun murders using log10 transformation and save to object log10_total_gun_murders
log10_total_gun_murders <- log10(murders$total)
# Create a scatterplot with the log scale transformed population and murders
plot(log10_population, log10_total_gun_murders)
```
`@sct`
```{r}
test_error()
test_object("log10_population", undefined_msg = "Define log10_population first.", incorrect_msg = "Are you using a log10 transformation on the correct variable?")
test_object("log10_total_gun_murders", undefined_msg = "Define log10_total_gun_murders first.", incorrect_msg = "Transform variable using log10.")
test_function("plot",args=c("x","y"), incorrect_msg = "Scatter population on murders.")
success_msg("Doesn't that plot look better!")
```
---
## Histograms
```yaml
type: NormalExercise
key: 6d782c33a3
lang: r
xp: 100
skills:
- 1
```
Now we are going to make a histogram.
`@instructions`
- Compute the population in millions and save it to the object `population_in_millions`
- Create a histogram of the state populations using the function `hist`
`@hint`
`@pre_exercise_code`
```{r}
library(dslabs)
data(murders)
```
`@sample_code`
```{r}
# Store the population in millions and save to population_in_millions
population_in_millions <- murders$population/10^6
# Create a histogram of this variable
```
`@solution`
```{r}
# Store the population in millions (same as previous question)
population_in_millions <- murders$population/10^6
# Create a histogram of this variable
hist(population_in_millions)
```
`@sct`
```{r}
test_error()
test_object("population_in_millions", undefined_msg = "Define the population first.", incorrect_msg = "Check code!")
test_function("hist",args=c("x"), incorrect_msg = "Check code again.")
success_msg("We got a histogram! Awesome!")
```
---
## Boxplots
```yaml
type: NormalExercise
key: 41b5550054
lang: r
xp: 100
skills:
- 1
```
Now we are going to make boxplots. Boxplots are useful when we want a summary of several variables or several strata of the same variables. Making too many histograms can become too cumbersome.
`@instructions`
In one line of code:
- Stratify the state populations by region.
- Generate boxplots for the strata.
Note that you can achieve this using this `population~region` inside `boxplot` to generate the strata and specify the dataset with the `data` argument.
`@hint`
Make sure you specify the dataset. In the `boxplot` function specify the argument `data = murders`.
`@pre_exercise_code`
```{r}
library(dslabs)
```
`@sample_code`
```{r}
# Create a boxplot of state populations by region for the murders dataset
```
`@solution`
```{r}
# Create a boxplot of state populations by region and specify dataset
boxplot(population~region, data = murders)
```
`@sct`
```{r}
test_error()
test_function("boxplot",args=c("formula","data"), incorrect_msg = "Check code. use ~ sign and include dataset.")
success_msg("Great job! Now you've learnt all three basic types of plots in R!")
```
---
## End of Assessment 8
```yaml
type: PureMultipleChoiceExercise
key: bd1aeba6dc
lang: r
xp: 50
skills:
- 1
```
This is the end of the programming assignment for this section. Please DO NOT click through to additional assessments from this page. Please DO answer the question on this page. If you do click through, your scores may NOT be recorded.
Click on "Awesome" to get the "points" for this question and then return to the course on edX.
You can now close this window to go back to the <a href='https://www.edx.org/course/data-science-r-basics-2'>course</a>.
`@hint`
- No hint necessary!
`@possible_answers`
- [Awesome]
- Nope
`@feedback`
- Great! Now navigate back to the course on edX!
- Now navigate back to the course on edX!