forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPA1_template.Rmd
More file actions
662 lines (474 loc) · 27.9 KB
/
Copy pathPA1_template.Rmd
File metadata and controls
662 lines (474 loc) · 27.9 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
---
title: "Reproducible Research - Peer Assessment 1"
header-includes: \usepackage{caption}
output:
html_document:
keep_md: true
fig_caption: no
---
### Some initial R settings, packages and formatting
```{r Settings, echo = TRUE, eval = TRUE, results = "hide", warning = FALSE, message = FALSE}
#___0.1___Clear workspace
rm(list=ls())
#___0.2___Tables
# \captionsetup[table]{labelformat=empty}
#___0.3___Load packages
# Packages loaded
pck_loaded <- (.packages())
# Packages to load
pck_toload <- c('ggplot2', 'mvtnorm', 'data.table', 'sqldf', 'stargazer', 'xtable')
# Load packages
for(i in 1:length(pck_toload)) {
if (!pck_toload[i] %in% pck_loaded)
print(pck_toload[i])
library(pck_toload[i], character.only = TRUE)
}
remove(i)
#___0.4___ SOme common standards for charts using ggplot2
form_xlab1 <- element_text(face = "italic", color = "blue")
form_title1 <- element_text(face = "bold",lineheight=.8)
```
## Part 1 - Loading and preprocessing the data
### 1.1 - Load the data
The data is loaded into a data table using **read.table** and **data.table**.
The following chunk shows how it's done.
```{r Part 1, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE}
#___1.1___Load the data
setwd("C:/repos_github/coursera/repres/data")
dt_initial <- data.table(read.table("activity.csv", sep = ",", header = TRUE))
head(dt_initial,5)
```
The table above displays the first five observations from the initial data set
### 1.2 - Process/transform the data into a format suitable for the analysis
Aside from removing missing values, no further processing of the data seems necessary at this point.
All observations are of the same format in the different columns, and column names describe the contents well. All further data munging and imputing is done step wise in the assignment.
```{r Part 1.2, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE}
#___1.2___Process/transform
# Remove all observations with missing data
# The prefix dt indicates that the variable is a datatable
dt_initialNaNo <- dt_initial[complete.cases(dt_initial),]
# Rows with missing data removed
naRemoved <- nrow(dt_initial) - nrow(dt_initialNaNo)
```
Here's a table showing the first five observations from the initial data set with all missing values removed.
```{r Part 1.1.2, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE}
head(dt_initialNaNo,5)
```
## 2 - What is the mean total number of steps taken per day?
### 2.1 - Calculate the total number of steps taken per day
For the total number of steps per day, and the corresponding histogram,it is convenient to transform the data into showing unique dates in the date column.This way, total steps for all time intervals are displayed for each day in the data table dt_dailysteps.
There are several ways you can do this.The package data.table has several methods, but mostly I use an sql package for R called sqldf.
```{r Part 2.1, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}
#___2.1___Calculate the total number of steps taken per day
# Number of steps per day using data.table
dt_dailystepsDT <- dt_initialNaNo[,.(steps.sum = sum(steps)),by=date]
# Number of steps per day using sqldf
dt_dailysteps <- sqldf("SELECT sum(steps) as stepsum, date
FROM dt_initialNaNo
Group by date")
dt_dailysteps <- data.table(dt_dailysteps)
grandtotalsteps <- sum(dt_dailysteps$stepsum)
grandtotalobs <- nrow(dt_initialNaNo)
rows_dt_dailysteps <- nrow(dt_dailysteps)
remove(dt_dailystepsDT)
head(dt_dailysteps, 5)
```
There's a total of **`r grandtotalsteps`** steps recorded accross **`r grandtotalobs`** observations in the original dataset after accounting for missing values. Ordering the sum of steps for all intervals per day results in a table with **`r rows_dt_dailysteps`** records
The previous table displays the first 5 rows with the total number of steps per day displayed in the column named stepsum. This table is the basis for the histogram that follows in Figure 1.
### 2.2 - Make a histogram of the total number of steps taken each day
```{r Part 2.2 - Figure 1 Histogram, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}
#___2.2___Make a histogram of number of steps taken each day
h1 <- ggplot(data=dt_dailysteps, aes(dt_dailysteps$stepsum))
h1 <- h1 + geom_histogram(colour = "blue", fill = "grey")
h1 <- h1 + theme_classic()
h1 <- h1 + ggtitle("Figure 1 - Histogram of total steps per day") + xlab("steps")
h1 <- h1 + theme(plot.title = form_title1 )
h1 <- h1 + theme(axis.title = form_xlab1)
h1 <- h1 + ylim(0, 14)
#plot(h1)
#setwd("C:/repos_github/coursera/repres")
#ggsave(filename = "Histogram Number of Steps.pdf", plot = h1)
plot(h1)
```
### 2.3 - Calculate and report the mean and median of the total number of steps taken per day
```{r Part 2.3 - Average and median, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}
#___2.3___Calculate and report the mean and median
nsteps_NaNo <- sum(dt_dailysteps$steps)
avgsteps_NaNo <- mean(dt_dailysteps$stepsum)
medsteps_NaNo <- median(dt_dailysteps$stepsum)
# h1 <- h1 + geom_vline(xintercept = avgsteps, labels = "mean", show_guide = TRUE, color = "green")
# h1 <- h1 + geom_vline(xintercept = medsteps, labels = "mean", show_guide = TRUE, color = "red")
# h1
#Data table of the results
Method <- c("NA removed", "NA Imputed")
Steptotal <- c(format(round(nsteps_NaNo, 0), nsmall = 0),"")
Averages <- c(format(round(avgsteps_NaNo, 2), nsmall = 2),"")
Medians <- c(format(round(medsteps_NaNo, 0), nsmall = 0),"")
tb1_methods <- data.table(Method, Steptotal, Averages, Medians)
```
When accumulating all intervals per day, which is what the assignment asks for,
the mean and median number of steps are **10766.19** and **10765**, respectively.
These findings are displayed in the table below, which will be filled with more findings along the way.
So far we've calculated the mean and median after ignoring missing values. Later, we'll calculate the same values using other methods for handling missing values.
```{r Part 2.3.1 - Average and median, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}
tb1 <- data.table(Method, Averages, Medians)
tb1_methods
```
## 3 - What is the average daily pattern?
### 3.1 - Make a time series plot of the 5 minute intervals averaged accross all days.
Again, it is convenient to transform the original data. Here, the data is stored as an average for each interval accross all observed days in the data table dt_dailypattern.
```{r Part 3.1 - Time Series, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}
#___3.1___Make a time series plot
# Data transformation
dt_dailypattern <- (sqldf("SELECT interval, avg(steps) as steps
FROM dt_initialNaNo
Group by interval"))
dt_dailypattern <- data.table(dt_dailypattern)
# Using the time intervals on a continuos x-axis results in an uneven
# time series due to the jumps between, for example, 955 and 1000.
idx1 <- 1:nrow(dt_dailypattern)
dt_dailypattern <- data.table(dt_dailypattern)
numberofintervals <- nrow(dt_dailypattern)
# The plot, step 1
p1 <- ggplot(dt_dailypattern, aes((interval), steps))
p1 <- p1 + geom_line(colour = "blue") + theme_classic() +
ggtitle("Daily pattern by 5 minute interval") + xlab("interval")
p1 <- p1 + theme(plot.title = form_title1 )
# Find interval with max steps
maxsteps = max(dt_dailypattern$steps)
# Look up the corresponding interval for the max number of steps
maxint <- sqldf("SELECT interval, max(steps)
FROM dt_dailypattern")
dailymax <- maxint[1,2]
# The plot step 2 - Add a point in the plot indicating the interval
# with max number of steps and plot it.
p1 <- p1 + geom_point(data = subset(dt_dailypattern, interval == maxint$interval[1]),
colour = "red")
p1 <- p1 + geom_text(data = subset(dt_dailypattern, interval == maxint$interval[1]),
aes(x = interval,y = steps, hjust = -0.01
, label = paste("max = ",dailymax, " steps for interval",
maxint[1,1])))
# Plot the plot
p1 <- p1 + theme(plot.title = element_text(lineheight=.8, face="bold"))
p1 <- p1 + ggtitle("Figure 2 - Daily steps for each interval") + xlab("steps")
p1 <- p1 + theme(axis.title = form_xlab1)
p1 <- p1 + ylim(0, 260)
# plot(p1)
```
The table below shows the results for the data munging in this part of the assignment. Here, we have taken the average for each interval accross all observed days with no missing values. There's a total of `r numberofintervals` intervals. The first and last 5 intervals are shown in the table.
```{r Part 3.1 Table, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}
dt_dailypattern
```
The following figure shows a plot from the previous table.
```{r Part 3.1 - Plot Daily Steps, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}
plot(p1)
```
### 3.2 Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
```{r Part 3.2, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}
#___3.2___5 minute interval that contains the maximum number of steps?
# Find interval with max steps
maxsteps = max(dt_dailypattern$steps)
# Look up the corresponding interval for the max number of steps
maxint <- sqldf("SELECT interval, max(steps)
FROM dt_dailypattern")
```
The 5-minute interval which contains the maximum average steps per day, is **`r maxint$interval[1]`**. The average number of steps taken per day during this interval is **206**.
## 4 - Imputing Missing values
### 4.1 - Calculate and report the total number of missing values
```{r Part 4.1, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}
#___4.1___Calculate and report the total number of missing values in the dataset
# First check for missing values in each column
missingColumnwise <- sapply(dt_initial, function(x) sum(is.na(x)))
# Total number of missing values
missingTotal <- sum(sapply(dt_initial, function(x) sum(is.na(x))))
```
Checking for missing values in each column using is.na() and sapply reveals that there are **no missing observations** for **date** and **interval**, but **2304** missing observations in the **steps** column. Of course, the total number of missing values in the dataset is **2304**.
### 4.2 - Devise a strategy for filling in all of the missing values in the dataset
```{r PArt 4.2, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}
#___4.2___ Devise a strategy for filling in all of the missing values in the dataset
# Make a data table in which missing values will be raplaced.
dt_imputed <- dt_initial
```
It would be pretty straight forward to replace all missing values with the average or median step for each day. It's a bit more challenging to do the same for each and every interval, so this is what I'm going to do. There are surely many ways to do this, perhaps most efficiently in dplyr. I'm going to brute force the whole process using a nested For Loop. The following code loops through the table **dt_imputed**. For each missing value, the code loops through the table **avgsteps**, finds the corresponding step count, and writes it to the table **dt_imputed**.
### 4.3 - Create a new dataset that is equal to the original dataset but with the missing data filled in.
```{r Part4.3, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}
#___4.3___Create a new dataset that is equal to the original dataset
# Make a data table that contains daily averages for all intervals
dt_avgsteps <- (sqldf("SELECT interval, avg(steps) as steps
FROM dt_initialNaNo
Group by interval"))
dt_avgsteps <- data.table(dt_avgsteps)
# Brute Force For Loop, replacing NAs in dt_imputed with an average step count.
i <- 1
j <- 1
nmiss <- 0
fakesteps <- 0
for(i in 1:length(dt_initial$steps)) {
#print(dt_avgsteps$steps[i])
if (is.na(dt_initial$steps[i])) {
#print(paste(i, "missing", sep = " - "))
nmiss <- nmiss + 1
for(j in 1:length(dt_avgsteps$steps)) {
#print(paste(nmiss,j, "missing", sep = " - "))
if (dt_initial$interval[i] == dt_avgsteps$interval[j]) {
# print(paste(i,j, "missing", sep = " - "))
dt_imputed$steps[i] <- dt_avgsteps$steps[j]
fakesteps <- fakesteps + dt_avgsteps$steps[j]
break
}
}
}
}
#i
# nmiss
# themissing <- nrow(subset(dx1, is.na(dt_nonmissing$steps)))
# themissing
# fakesteps
# missmix <- data.table(dt_missing, dt_nonmissing$steps)
# Look at the data
# head(dt_imputed)
setwd("C:/repos_github/coursera/repres/data")
write.table(dt_imputed, "imputedData.csv", row.names = FALSE)
#remove(nonmissing)
#nonmissing <- data.table(read.table("imputedData.csv", sep = " ", header = TRUE))
```
The following table displays the first and last five observations of the imputed data set.
```{r Part4.3.1 Imputed data, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}
dt_imputed
```
### 4.4 - Make a histogram of steps taken each day and find the mean and median.
```{r Part 4.4, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}
#___4.4___Make a histogram of the total number of steps taken each day, and report mean and median.
# Order sum of steps per day for all intervals
dt_dailysteps2 <- (sqldf("SELECT sum(steps) as stepsum, date
FROM dt_imputed
Group by date"))
dt_dailysteps2 <- data.table(dt_dailysteps2)
# Look at the data
# head(dt_dailysteps)
# head(dt_dailysteps2)
# Number of steps per day, calculations
nstepsNaNo <- sum(dt_dailysteps$steps)
avgstepsNaNo <- mean(dt_dailysteps$steps)
medstepsNaNo <- median(dt_dailysteps$steps)
# Update tb1 where averages and meand
# Histogram of number of steps per day
h2 <- ggplot(data=dt_dailysteps2, aes(dt_dailysteps2$steps))
h2 <- h2 + geom_histogram(colour = "blue", fill = "grey")
h2 <- h2 + theme_classic()
h2 <- h2 + ggtitle(" Figure 3 - Total number of steps per day") + xlab("steps")
h2 <- h2 + theme(axis.title = form_xlab1)
h2 <- h2 + ylim(0, 14)
#plot(h2)
#setwd("C:/repos_github/coursera/repres")
#ggsave(filename = "Histogram Number of Steps no missing values.pdf", plot = h1)
# Calculate mean and median
nsteps_NaImp <- sum(dt_dailysteps2$stepsum)
avgsteps_NaImp <- mean(dt_dailysteps2$stepsum)
medsteps_NaImp <- median(dt_dailysteps2$stepsum)
#Data table of the results
Method <- c("NA removed", "NA Imputed")
Steptotal <- c(format(round(nstepsNaNo, 0), nsmall = 0),nsteps_NaImp)
Averages <- c(format(round(avgstepsNaNo, 2), nsmall = 2),format(round(avgsteps_NaImp, 2), nsmall = 2))
Medians <- c(format(round(medstepsNaNo, 0), nsmall = 0),medsteps_NaImp)
tb1_methods <- data.table(Method, Steptotal, Averages, Medians)
impdiff_avg <- format(round(avgsteps_NaImp - avgstepsNaNo, 0), nsmall = 0)
impdiff_med <- format(round(medsteps_NaImp - medstepsNaNo, 0), nsmall = 0)
tb1_methods
```
The sum of steps added to the original data after the imputing process is **`r format(round(fakesteps, 0), nsmall = 0)`**.
This appears as the difference between the Steptotal column for NA removed and NA Imputed in the table above.
We can also see that the average and median number of steps decreases respectively `r impdiff_avg` and `r impdiff_med`
Below is a histogram showing the distribution of steps after missing values have been imputed.
```{r Part 4.4.1 Histogram Number of Steps Imputed, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}
plot(h2)
```
Comparing the histogram in figure 3 with the histogram in figure 1 shows that the imputed data set leads to an increased number of observations at the center of the distribution. Let us take a quick look at why this happens by taking the difference between each observation in the imputed data set and another table where all missing values are replaced by zero.
### 4.4.2 - Imputing Missing values, a closer look at the implications.
```{r Part 4.4.2, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}
# Make a table of the origninal dataset where all missing values for steps are replaced by 0.
dt_initialNa0 <- dt_initial
dt_initialNa0[is.na(dt_initialNa0)] <- 0
dt_diff <- (dt_imputed - dt_initialNa0)
dt_diff$date <- dt_initial$date
dt_diff$interval <- dt_initial$interval
# sum(dt_diff$steps)
dt_diffdays <- data.table(sqldf("SELECT sum(steps) as stepsum, date
FROM dt_diff
Group by date"))
# sum(dt_diffdays$stepsum)
dt_diffdays <- (sqldf("SELECT sum(steps) as stepsum, date
FROM dt_diff
Group by date"))
dt_diffdays <- data.table(dt_diffdays)
# sum(dt_diffdays$stepsum)
# Make datatable of dt_diffdays with all zeros removed
# In other words, alld days with no effects from the imputing process are disregarded.
dt_diffdaysNo0 <- sqldf("SELECT stepsum, date
FROM dt_diffdays
Where stepsum <> 0
Group by date")
dt_diffdaysNo0 <- data.table(dt_diffdaysNo0)
# UNION (dt_dailysteps + category = nonimputed) and (dt_dailysteps3 + category = imputed)
FactImpNot <- 1:nrow(dt_dailysteps)
FactImp <- 1:nrow(dt_dailysteps2)
FactImp <- rep("NaImputed",length(FactImp))
FactImpNot <- rep("NaIgnored",length(FactImpNot))
NaTreatment <- FactImpNot
dt_dailyStepsCat <- data.table(dt_dailysteps, NaTreatment)
NaTreatment <- FactImp
dt_dailySteps2Cat <- data.table(dt_dailysteps2, NaTreatment)
dt_dailyUnion <- (sqldf("SELECT * from dt_dailyStepsCat
UNION ALL
SELECT * from dt_dailySteps2Cat
ORDER BY date"))
dt_dailyUnion <- data.table(dt_dailyUnion)
# sum(dt_dailyStepsCat$stepsum)
# sum(dt_dailySteps2Cat$stepsum)
# Histogram of number of steps per day
h3 <- ggplot(data=dt_dailyUnion, aes(dt_dailyUnion$steps, fill = NaTreatment))
h3 <- h3 + geom_histogram(alpha = 0.3, colour = "blue", position="identity")
h3 <- h3 + scale_fill_manual(values = c("black", "grey"))
h3 <- h3 + theme_classic()
h3 <- h3 + ggtitle("Figure 3 - Total number of steps per day") + xlab("steps")
h3 <- h3 + theme(axis.title = form_xlab1)
h3 <- h3 + ylim(0, 14)
h3 <- h3 + theme(plot.title = element_text(lineheight=.8, face="bold"))
h3 <- h3 + ggtitle(expression(atop(bold("Figure 3 - Total number of steps per day"), atop(italic("Distinguished by method for missing values"), ""))))
#plot(h3)
#setwd("C:/repos_github/coursera/repres")
#ggsave(filename = "Histogram Number of Steps no missing values.pdf", plot = h1)
dt_diffdaysNo0
```
The previous table shows the sum of the steps that have been added to the data set due to missing values.The fact that the sum of **10641** is equal for all days reveals an interesting detail. It turns out that all dates with missing values, have missing values for all intervals, and that there is no missing interval for all dates contained in the data set. This means that it would not matter much if the imputing process replaced missing values for each and every interval, or for each date only.
It also means that it's not possible to add any particular value to the analysis by replacing missing values in this example. It would have made much more sense if the original dataset was missing observations for **some intervals** for some days. In this case, it would be possible to add to the value of the analysis, particularly if the same intervals were not missing accross very many days.
This can be further illustrated by combining the two previous histograms in Figure 1 and Figure 2 into a new histogram in Figure 4. Notice that the number of days in the previous table is 8, and that the sum of steps for each day is 10641. The histogram in Figure 3 shows a light grey area that illustrate an increase ofeight counts in the bin where 10641 is located. The only thing you get by adding more days with the average number of steps only, is an increased stepcount around the center of the distribution of steps. The value added to the analysis by including more dates with no initially no observations, I would say is pretty close to zero.
```{r Part 4.4.2 Figure 3 - Total number of steps per day, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}
plot(h3)
```
## 5 - Are there differences in activity patterns between weekdays and weekends?
### 5.1 - Create a new factor variable in the dataset with two levels
```{r Part 5.1, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}
#___5.1___Create a new factor variable in the dataset with two levels -
# "weekday" and "weekend" indicating whether a given date
# is a weekday or weekend day.
# Make variables for factor categories
dayofweek <- 1:nrow(dt_imputed)
# Factor categories
wday <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
# Change regional settings to match the English language.
Sys.setlocale("LC_TIME", "English")
# Distribute factor categories
for(i in 1:length(dt_imputed$date)) {
cday <- weekdays(as.Date(dt_imputed$date[i]))
if (cday %in% wday) {
dayofweek[i] <- "weekday"
}else{
dayofweek[i] <- "weekend"
}
}
# Add the factor variables to dt_avgsteps in a new table
dt_imputed2 <- data.table(dt_imputed, dayofweek)
# Take a look at the nwe data
head(dt_imputed2, 5)
```
The table above shows how the new variable is added to the imputed data set..
### 5.2 - Make a panel plot
```{r Part 5.2 Panel Plot, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}
# Reset regional settings
# Sys.setlocale("LC_TIME", "Norwegian")
#_________Calculations on weekdays
# Average steps per interval for each weekday
dt_weekdaypattern <- data.table(sqldf("SELECT interval, avg(steps) as steps, dayofweek
FROM dt_imputed2
WHERE dayofweek = 'weekday'
Group by interval"))
# Find max interval with corresponding value for weekdays
maxsteps_wday = max(dt_weekdaypattern$steps)
# Look up the corresponding interval for the max number of steps for weekdays
maxint_wday <- sqldf("SELECT interval, max(steps)
FROM dt_weekdaypattern")
# Make new variables for dt_weekdaypattern in dt_weekdaypattern, indicating interval with max value
remove(intvmax)
stepmax <- 1:nrow(dt_weekdaypattern)
intvmax <- 1:(length(stepmax))
maxintv <- maxint_wday$interval[1]
for(i in 1:nrow(dt_weekdaypattern)) {
if (dt_weekdaypattern$steps[i] == maxsteps_wday) {
stepmax[i] <- paste("Max = ", maxsteps_wday, " steps at interval ", maxintv, sep = "")
intvmax[i] <- maxintv
}else{
stepmax[i] <- NA
intvmax[i] <- NA
}
}
dt_weekdaypattern <- data.table(dt_weekdaypattern, intvmax, stepmax)
#_________Calculations on weekends
# Average steps per interval for each weekend
dt_weekendpattern <- (sqldf("SELECT interval, avg(steps) as steps, dayofweek
FROM dt_imputed2
WHERE dayofweek = 'weekend'
Group by interval"))
dt_weekendpattern <- data.table(dt_weekendpattern)
# Find max interval with corresponding value for weekends
maxsteps_wend = max(dt_weekendpattern$steps)
# Look up the corresponding interval for the max number of steps for weekends
maxint_wend <- sqldf("SELECT interval, max(steps)
FROM dt_weekendpattern")
# Make new variables for dt_weekendpattern in dt_weekendpattern, indicating interval with max value
remove(intvmax)
stepmax <- 1:nrow(dt_weekendpattern)
intvmax <- 1:(length(stepmax))
maxintv <- maxint_wend$interval[1]
for(i in 1:nrow(dt_weekendpattern)) {
if (dt_weekendpattern$steps[i] == maxsteps_wend) {
stepmax[i] <- paste("Max = ", maxsteps_wend, " steps at interval ", maxintv, sep = "")
intvmax[i] <- maxintv
}else{
stepmax[i] <- NA
intvmax[i] <- NA
}
}
dt_weekendpattern <- data.table(dt_weekendpattern, intvmax, stepmax)
# Union dt_weekdaypattern and dt_weekendpattern into dt_weekpatterns
# dt_weekendpattern <- data.table(dt_weekendpattern, stepmax)
dt_weekpatterns <- (sqldf("SELECT * from dt_weekdaypattern
UNION ALL
SELECT * from dt_weekendpattern
ORDER BY interval"))
dt_weekpatterns <- data.table(dt_weekpatterns)
# Panel plot for average steps per interval, distiingueshed by weekday or weekend
p3 <- ggplot(dt_weekpatterns, aes(interval, steps)) + geom_line(colour = "blue")
p3 <- p3 + theme_classic()
p3 <- p3 + facet_grid(dayofweek ~ .)
p3 <- p3 + geom_text(aes(x = interval, y = steps, label = stepmax, group=NULL),data=dt_weekpatterns, hjust = -0.2)
p3 <- p3 + geom_point(data=subset(dt_weekpatterns, !is.na(stepmax)), colour = "red")
p3 <- p3 + ggtitle("Figure 4 - Daily pattern by 5 minute interval - Weekdays vs Weekend")
p3 <- p3 + ggtitle(expression(atop(bold("Figure 4 - Daily pattern by 5 minute interval"), atop(italic("Weekdays Vs. Weekends"), ""))))
p3 <- p3 + theme(axis.title = form_xlab1)
p3 <- p3 + theme(plot.title = element_text(lineheight=.8, face="bold"))
p3 <- p3 + ylim(0, 260)
plot(p3)
```
Comparing the pattern for average number of steps for each interval for weekdays versus weekends shows that the maximum number of steps is quite a bit lower and appears later in the day on weekends. In other words, you tend to get up later in the weekends and run around less. Sounds sweet. It is also apparent that the tendency to get moving earlier in the day is stronger on weekdays, with a sharp jump in activity around 5 o'clock.
Another interesting thing is that the weekday pattern flattens after the peak at 835, and that the following intervals have a lower step count than compared to the weekends. This becomes more apparent when the two lines are plotted against a common y axis in the figure that follows. Briefly put, the graph will show that, compared to weekends, you get up earlier in the morning and run around a lot more just after getting up. Then, you sit down and more or less stay in the chair before you get up and go to bed. In the weekends, you get up later, move less around just after getting up, but move around a lot more during the day. Which also sounds nice. Have a nice weekend!
### 5.2.1 Extra - Combine panel plot in a single plot
```{r Part 5.2.1 Extra Combine panel plot in a single plot, echo = TRUE, eval = TRUE, results = "show", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8}
# Common plot for average steps per interval, distingueshed by day of week
p4 <- ggplot(dt_weekpatterns, aes(interval, steps, colour = dayofweek )) + geom_line()
p4 <- p4 + theme_classic()
# p4 <- p4 + facet_grid(dayofweek ~ .)
p4 <- p4 + geom_text(aes(x = interval, y = steps, label = stepmax, group=NULL), data=dt_weekpatterns, hjust = -0.2)
p4 <- p4 + geom_point(data=subset(dt_weekpatterns, !is.na(stepmax)), colour = "red")
p4 <- p4 + ggtitle("Daily pattern by 5 minute interval - Weekdays vs Weekend")
p4 <- p4 + theme(axis.title = form_xlab1)
p4 <- p4 + theme(plot.title = element_text(lineheight=.8, face="bold"))
p4 <- p4 + scale_color_manual(values=c("blue", "grey"))
p4 <- p4 + ylim(0, 260)
plot(p4)
#___1.0___ Make md file
#setwd("C:/repos_github/coursera/RepData_PeerAssessment1")
#getwd()
#library(knitr)
#knit('PA1_template.Rmd')
```