-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathlesson3_student.rmd
363 lines (252 loc) · 6.7 KB
/
lesson3_student.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
Lesson 3
========================================================
***
### What to Do First?
Notes:
***
### Pseudo-Facebook User Data
Notes:
```{r Pseudo-Facebook User Data}
setwd("~/Repos/data-analysis-with-r/")
pf <- read.csv("./data/pseudo_facebook.tsv", sep="\t")
names(pf)
```
***
### Histogram of Users' Birthdays
Notes:
```{r Histogram of Users\' Birthdays}
library(ggplot2)
library(ggthemes)
qplot(x = dob_day, data = pf ) +
scale_x_discrete(breaks = 1:31)
```
***
#### What are some things that you notice about this histogram?
Response:
***
### Moira's Investigation
Notes:
***
### Estimating Your Audience Size
Notes:
***
#### Think about a time when you posted a specific message or shared a photo on Facebook. What was it?
Response:
#### How many of your friends do you think saw that post?
Response:
50
#### Think about what percent of your friends on Facebook see any posts or comments that you make in a month. What percent do you think that is?
Response:
20%
***
### Perceived Audience Size
Notes:
***
### Faceting
Notes:
```{r Faceting}
```
#### Let’s take another look at our plot. What stands out to you here?
Response:
***
### Be Skeptical - Outliers and Anomalies
Notes:
***
### Moira's Outlier
Notes:
#### Which case do you think applies to Moira’s outlier?
Response:
***
### Friend Count
Notes:
#### What code would you enter to create a histogram of friend counts?
```{r Friend Count}
names(pf)
qplot(x = friend_count, data = pf )
```
#### How is this plot similar to Moira's first plot?
Response:
***
### Limiting the Axes
Notes:
```{r Limiting the Axes}
qplot(x = friend_count, data = pf, xlim = c(0,1000))
qplot(x = friend_count, data = pf ) +
scale_x_continuous(limits = c(0,1000))
```
### Exploring with Bin Width
Notes:
***
### Adjusting the Bin Width
Notes:
### Faceting Friend Count
```{r Faceting Friend Count}
# What code would you add to create a facet the histogram by gender?
# Add it to the code below.
qplot(x = friend_count, data = pf, binwidth = 10) +
scale_x_continuous(limits = c(0, 1000),
breaks = seq(0, 1000, 50))
```
***
### Omitting NA Values
Notes:
```{r Omitting NA Values}
qplot(x = friend_count, data = subset(pf, !is.na(gender)), binwidth = 10) +
scale_x_continuous(limits = c(0, 1000),
breaks = seq(0, 1000, 50))
```
***
### Statistics 'by' Gender
Notes:
```{r Statistics \'by\' Gender}
qplot(x = friend_count, data = subset(pf,!is.na(gender)), binwidth = 25, fill = gender) +
scale_x_continuous(limits = c(0, 1000), breaks = seq(0, 1000, 50)) +
facet_grid(. ~ gender)
by(pf$friend_count,pf$gender,summary)
```
#### Who on average has more friends: men or women?
Response:
Male
#### What's the difference between the median friend count for women and men?
Response:
#### Why would the median be a better measure than the mean?
Response:
***
### Tenure
Notes:
```{r Tenure}
str(pf)
qplot(x = tenure, data = pf, binwidth = 30,
color=I('black'), fill=I("#099DD9"))
```
***
#### How would you create a histogram of tenure by year?
```{r Tenure Histogram by Year}
qplot(x = tenure/365, data = pf, binwidth = 0.25,
color=I('black'), fill=I("#099DD9")) +
scale_x_continuous(breaks = seq(1, 7, 1), limits = c(0, 7))
```
***
### Labeling Plots
Notes:
```{r Labeling Plots}
qplot(x = tenure/365, data = pf,
xlab = "Number of years using Facebook",
ylab = "Number of users in sample",
color=I('black'), fill=I("#099DD9")) +
scale_x_continuous(breaks = seq(1, 7, 1), limits = c(0, 7))
```
***
### User Ages
Notes:
```{r User Ages}
qplot(x=age, data=subset(pf,!is.na(gender)), binwidth = 1,
color = I("black"), fill = I("#099DD9")) +
scale_x_discrete(breaks = seq(0, 113, 10))
```
#### What do you notice?
Response:
***
### The Spread of Memes
Notes:
***
### Lada's Money Bag Meme
Notes:
***
### Transforming Data
Notes:
***
### Add a Scaling Layer
Notes:
```{r Add a Scaling Layer}
library(gridExtra)
p1 <- qplot(x = friend_count, data = pf)
p2 <- p1 + scale_x_log10()
p3 <- p1 + scale_x_sqrt()
grid.arrange(p1,p2,p3,ncol = 1)
h1 <- qplot(x = friend_count, data = pf)
h2 <- qplot(x = log10(friend_count), data = pf)
h3 <- qplot(x = sqrt(friend_count), data = pf)
grid.arrange(h1,h2,h3,ncol = 1)
```
***
### Frequency Polygons
```{r Frequency Polygons}
ggplot(data = subset(pf, !is.na(gender))) +
geom_freqpoly(aes(x = friend_count, y = ..count../sum(..count..),color = gender)) +
scale_x_continuous(limits = c(0, 1000), breaks = seq(0, 1000, 50)) +
xlab('Friend Count') +
ylab('Percentage of users with that friend count')
```
***
### Likes on the Web
Notes:
```{r Likes on the Web}
ggplot(data = subset(pf, !is.na(gender))) +
geom_freqpoly(aes(x = www_likes, color = gender)) +
scale_x_continuous(limits = c(1, 1000), breaks = seq(1, 1000, 50)) +
scale_x_log10()
by(pf$www_likes,pf$gender,sum)
```
***
### Box Plots
Notes:
```{r Box Plots}
qplot(x=gender,y=friend_count,
data = subset(pf, !is.na(gender)), geom = "boxplot")
```
#### Adjust the code to focus on users who have friend counts between 0 and 1000.
```{r}
q1 <- qplot(x=gender,y=friend_count,
data = subset(pf, !is.na(gender) & friend_count > 0 & friend_count < 1000 ),
geom = "boxplot")
q2 <- qplot(x=gender,y=friend_count,
data = subset(pf, !is.na(gender)), geom = "boxplot",
ylim = c(0,1000))
q3 <- qplot(x=gender,y=friend_count,
data = subset(pf, !is.na(gender)), geom = "boxplot") +
coord_cartesian(ylim = c(0,1000))
grid.arrange(q1,q2,q3,ncol = 3)
```
***
### Box Plots, Quartiles, and Friendships
Notes:
```{r Box Plots, Quartiles, and Friendships}
qplot(x = gender, y = friend_count,
data = subset(pf, !is.na(gender)),
geom = "boxplot") +
coord_cartesian(ylim = c(0,250))
by(pf$friend_count,pf$gender,summary)
```
#### On average, who initiated more friendships in our sample: men or women?
Response:
#### Write about some ways that you can verify your answer.
Response:
```{r Friend Requests by Gender}
qplot(x = gender, y = friendships_initiated,
data = subset(pf, !is.na(gender)),
geom = "boxplot") +
coord_cartesian(ylim = c(0,250))
by(pf$friendships_initiated,pf$gender,summary)
```
Response:
***
### Getting Logical
Notes:
```{r Getting Logical}
summary(pf$mobile_likes)
summary(pf$mobile_likes > 0)
mobile_check_in <- NA
pf$mobile_check_in <- ifelse(pf$mobile_likes > 0, 1, 0)
pf$mobile_check_in <- factor(pf$mobile_check_in)
summary(pf$mobile_check_in)
sum(pf$mobile_check_in == 1) / length(pf$mobile_check_in)
table(pf$mobile_likes > 0) / length(pf$mobile_likes)
```
Response:
***
### Analyzing One Variable
Reflection:
***
Click **KnitHTML** to see all of your hard work and to have an html
page of this lesson, your answers, and your notes!