forked from TuomoNieminen/Helsinki-Open-Data-Science
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter1.Rmd
1043 lines (726 loc) · 31.5 KB
/
chapter1.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
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: 'Regression and model validation'
description: 'Data wrangling, simple regression, multiple regression, regression diagnostics'
---
## Reading data from the web
```yaml
type: NormalExercise
key: 6e77718b66
lang: r
xp: 100
skills: 1
```
Welcome to **Helsinki Open Data Science**: Regression and Model validation.
The first step of data analysis with R is reading data into R. This is done with a function. Which function and function arguments to use to do this, depends on the original format of the data.
Conveniently in R, the same functions for reading data can usually be used weather the data is saved locally on your computer or is located behind a web URL.
After the correct function has been identified and data read into R, the data will usually be in R's `data.frame` format. A data frame's dimensions are ($n$,$d$), where $n$ is the number of rows (the observations) and $d$ the number of columns (the variables).
`@instructions`
- Read the `lrn14` data frame to memory with `read.table()`. There is information related to the data [here](http://www.helsinki.fi/~kvehkala/JYTmooc/JYTOPKYS3-meta.txt)
- Use `dim()` on the data frame to look at the dimensions of the data. How many rows and colums does the data have?
- Look at the structure of the data with `str()`.
`@hint`
- For both functions you can pass a data frame as the first (unnamed) argument.
`@pre_exercise_code`
```{r}
```
`@sample_code`
```{r}
# read the data into memory
lrn14 <- read.table("http://www.helsinki.fi/~kvehkala/JYTmooc/JYTOPKYS3-data.txt", sep="\t", header=TRUE)
# Look at the dimensions of the data
# Look at the structure of the data
```
`@solution`
```{r}
# read the data into memory
lrn14 <- read.table("http://www.helsinki.fi/~kvehkala/JYTmooc/JYTOPKYS3-data.txt", sep="\t", header=TRUE)
# Look at the dimensions of the data
dim(lrn14)
# Look at the structure of the data
str(lrn14)
```
`@sct`
```{r}
test_function("dim", args = "x",incorrect_msg = "Please use the function dim() to look at the dimensions of the data")
test_function("str", args = "object", incorrect_msg = "Please use the function str() to look at the structure of the data")
test_error()
success_msg("Good work!")
```
---
## Scaling variables
```yaml
type: NormalExercise
key: 3efc4528fc
lang: r
xp: 100
skills: 1
```
The next step is [wrangling the data](https://en.wikipedia.org/wiki/Data_wrangling) into a format that is easy to analyze. We will wrangle our data for the next few exercises.
A neat thing about R is that may operations are *vectorized*. It means that a single operation can affect all elements of a vector. This is often convenient.
The column `Attitude` in `lrn14` is a sum of 10 questions related to students attitude towards statistics, each measured on the [Likert scale](https://en.wikipedia.org/wiki/Likert_scale) (1-5). Here we'll scale the combination variable back to the 1-5 scale.
`@instructions`
- Execute the example codes to see how vectorized division works
- Use vector division to create a new column `attitude` in the `lrn14` data frame, where each observation of `Attitude` is scaled back to the original scale of the questions, by dividing it with the number of questions.
`@hint`
- Assign 'Attitude divided by 10' to the new column 'attitude.
`@pre_exercise_code`
```{r}
# read the data into memory
lrn14 <- read.table("http://www.helsinki.fi/~kvehkala/JYTmooc/JYTOPKYS3-data.txt", sep="\t", header=TRUE)
```
`@sample_code`
```{r}
#lrn14 is available
# divide each number in a vector
c(1,2,3,4,5) / 2
# print the "Attitude" column vector of the lrn14 data
lrn14$Attitude
# divide each number in the column vector
lrn14$Attitude / 10
# create column 'attitude' by scaling the column "Attitude"
lrn14$attitude <- "change me!"
```
`@solution`
```{r}
#lrn14 is available
# divide each number in a vector
c(1,2,3,4,5) / 2
# print the "Attitude" column vector of the lrn14 data
lrn14$Attitude
# divide each number in the column vector
lrn14$Attitude / 10
# create column "attitude" by scaling the column "Attitude"
lrn14$attitude <- lrn14$Attitude / 10
```
`@sct`
```{r}
test_object("lrn14", incorrect_msg = "Please create the column 'attitude in the data frame 'lrn14' by scaling the column 'Attitude'")
test_error()
success_msg("Well done. Excellent wrangling!")
```
---
## Combining variables
```yaml
type: NormalExercise
key: 9a390d843f
lang: r
xp: 100
skills: 1
```
Our data includes many questions that can be thought to measure the same *dimension*. You can read more about the data and the variables [here](http://www.helsinki.fi/~kvehkala/JYTmooc/JYTOPKYS3-meta.txt). Here we'll combine multiple questions into combination variables. Useful functions for summation with data frames in R are
function | description
------------- | ----------
`colSums(df)` | returns a sum of each column in `df`
`rowSums(df)` | returns a sum of each row in `df`
`colMeans(df)`| returns the mean of each column in `df`
`rowMeans(df)`| return the mean of each row in `df`
We'll combine the use of `rowMeans()`with the `select()` function from the **dplyr** library to average the answers of selected questions. See how it is done from the example codes.
`@instructions`
- Access the **dplyr** library
- Execute the example codes to create the combination variables 'deep' and 'surf' as columns in `lrn14`
- Select the columns related to strategic learning from `lrn14`
- Create the combination variable 'stra' as a column in `lrn14`
`@hint`
- Columns related to strategic learning are in the object `strategic_questions`. Use it for selecting the correct columns.
- Use the function `rowMeans()` identically to the examples
`@pre_exercise_code`
```{r}
# read the data into memory
lrn14 <- read.table("http://www.helsinki.fi/~kvehkala/JYTmooc/JYTOPKYS3-data.txt", sep="\t", header=TRUE)
lrn14$attitude <- lrn14$Attitude / 10
```
`@sample_code`
```{r}
# lrn14 is available
# Access the dplyr library
library(dplyr)
# questions related to deep, surface and strategic learning
deep_questions <- c("D03", "D11", "D19", "D27", "D07", "D14", "D22", "D30","D06", "D15", "D23", "D31")
surface_questions <- c("SU02","SU10","SU18","SU26", "SU05","SU13","SU21","SU29","SU08","SU16","SU24","SU32")
strategic_questions <- c("ST01","ST09","ST17","ST25","ST04","ST12","ST20","ST28")
# select the columns related to deep learning and create column 'deep' by averaging
deep_columns <- select(lrn14, one_of(deep_questions))
lrn14$deep <- rowMeans(deep_columns)
# select the columns related to surface learning and create column 'surf' by averaging
surface_columns <- select(lrn14, one_of(surface_questions))
lrn14$surf <- rowMeans(surface_columns)
# select the columns related to strategic learning and create column 'stra' by averaging
strategic_columns <- "change me!"
```
`@solution`
```{r}
# lrn14 is available
# Access the dplyr library
library(dplyr)
# questions related to deep, surface and strategic learning
deep_questions <- c("D03", "D11", "D19", "D27", "D07", "D14", "D22", "D30", "D06", "D15", "D23", "D31")
surface_questions <- c("SU02","SU10","SU18","SU26", "SU05","SU13","SU21","SU29","SU08","SU16","SU24","SU32")
strategic_questions <- c("ST01","ST09","ST17","ST25","ST04","ST12","ST20","ST28")
# select the columns related to deep learning and create column 'deep' by averaging
deep_columns <- select(lrn14, one_of(deep_questions))
lrn14$deep <- rowMeans(deep_columns)
# select the columns related to surface learning and create column 'surf' by averaging
surface_columns <- select(lrn14, one_of(surface_questions))
lrn14$surf <- rowMeans(surface_columns)
# select the columns related to strategic learning and create column 'stra' by averaging
strategic_columns <- select(lrn14, one_of(strategic_questions))
lrn14$stra <- rowMeans(strategic_columns)
```
`@sct`
```{r}
test_object("lrn14", incorrect_msg = "Please create the columns 'deep', 'surf' and 'stra' in the data frame 'lrn14' by averaging the answers to selected questions")
test_error()
success_msg("Yes! You are clearly a combination of intelligence and enthusiasm!")
```
---
## Selecting columns
```yaml
type: NormalExercise
key: 6acf3bcc8b
lang: r
xp: 100
skills: 1
```
Often it is convenient to work with only a certain column or a subset of columns of a bigger data frame. There are many ways to select columns of data frame in R and you saw one of them in the previous exercise: `select()` from **dplyr***.
**dplyr** is a popular library for *data wrangling*. There is also a convenient [data wrangling cheatsheet by RStudio](https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf) to help you get started.
`@instructions`
- Access the **dplyr** library
- Create object `keep_columns`
- Use `select()` (possibly together with `one_of()`) to create a new data frame `learning2014` with the columns named in `keep_columns`.
- Look at the structure of the new dataset
`@hint`
- See the previous exercise or the data wrangling cheatsheet for help on how to select columns
`@pre_exercise_code`
```{r}
lrn14 <- read.table("http://www.helsinki.fi/~kvehkala/JYTmooc/JYTOPKYS3-data.txt", sep="\t", header=TRUE)
lrn14$attitude <- lrn14$Attitude / 10
deep_questions <- c("D03", "D11", "D19", "D27", "D07", "D14", "D22", "D30","D06", "D15", "D23", "D31")
lrn14$deep <- rowMeans(lrn14[, deep_questions])
surface_questions <- c("SU02","SU10","SU18","SU26", "SU05","SU13","SU21","SU29","SU08","SU16","SU24","SU32")
lrn14$surf <- rowMeans(lrn14[, surface_questions])
strategic_questions <- c("ST01","ST09","ST17","ST25","ST04","ST12","ST20","ST28")
lrn14$stra <- rowMeans(lrn14[, strategic_questions])
```
`@sample_code`
```{r}
# lrn14 is available
# access the dplyr library
library(dplyr)
# choose a handful of columns to keep
keep_columns <- c("gender","Age","attitude", "deep", "stra", "surf", "Points")
# select the 'keep_columns' to create a new dataset
learning2014 <- "change me!"
# see the stucture of the new dataset
```
`@solution`
```{r}
# lrn14 is available
# access the dplyr library
library(dplyr)
# choose a handful of columns to keep
keep_columns <- c("gender","Age","attitude", "deep", "stra", "surf", "Points")
# select the 'keep_columns' to create a new dataset
learning2014 <- select(lrn14, one_of(keep_columns))
# see the structure of the new dataset
str(learning2014)
```
`@sct`
```{r}
test_object("learning2014", incorrect_msg = "Please use the selected columns to create the learning2014 dataset")
test_function("str", args = "object", incorrect_msg = "Please use the `str()` function to see the structure of the learnign2014 dataset")
test_error()
success_msg("Great work! I choose you!")
```
---
## Modifying column names
```yaml
type: NormalExercise
key: b020be3ce5
lang: r
xp: 100
skills: 1
```
Sometimes you want to rename your colums. You could do this by creating copies of the columns with new names, but you can also directly get and set the column names of a data frame, using the function `colnames()`.
The **dplyr** library has a `rename()` function, which can also be used. Remember [the cheatsheet](https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf).
`@instructions`
- Print out the column names of `learning2014`
- Change the name of the second column to 'age'
- Change the name of 'Points' to 'points'
- Print out the column names again to see the changes
`@hint`
- You can use `colnames()` similarily to the example. Which index matches the column 'Points'?
`@pre_exercise_code`
```{r}
lrn14 <- read.table("http://www.helsinki.fi/~kvehkala/JYTmooc/JYTOPKYS3-data.txt", sep="\t", header=TRUE)
lrn14$attitude <- lrn14$Attitude / 10
deep_questions <- c("D03", "D11", "D19", "D27", "D07", "D14", "D22", "D30","D06", "D15", "D23", "D31")
lrn14$deep <- rowMeans(lrn14[, deep_questions])
surface_questions <- c("SU02","SU10","SU18","SU26", "SU05","SU13","SU21","SU29","SU08","SU16","SU24","SU32")
lrn14$surf <- rowMeans(lrn14[, surface_questions])
strategic_questions <- c("ST01","ST09","ST17","ST25","ST04","ST12","ST20","ST28")
lrn14$stra <- rowMeans(lrn14[, strategic_questions])
learning2014 <- lrn14[, c("gender","Age","attitude", "deep", "stra", "surf", "Points")]
```
`@sample_code`
```{r}
# learning2014 is available
# print out the column names of the data
colnames(learning2014)
# change the name of the second column
colnames(learning2014)[2] <- "age"
# change the name of "Points" to "points"
# print out the new column names of the data
```
`@solution`
```{r}
# learning2014 is available
# print out the column names of the data
colnames(learning2014)
# change the name of the second column
colnames(learning2014)[2] <- "age"
# change the name of "Points" to "points"
colnames(learning2014)[7] <- "points"
# print out the new column names of the data
colnames(learning2014)
```
`@sct`
```{r}
test_output_contains('c("gender","age","attitude", "deep", "stra", "surf", "points")', incorrect_msg = "Please change the name of the 'Age' column to 'age' and 'Points' column to 'points' and print out the new column names")
test_error()
success_msg("Well done!")
```
---
## Excluding observations
```yaml
type: NormalExercise
key: 7955b0f30e
lang: r
xp: 100
skills: 1
```
Often your data includes outliers or other observations which you wish to remove before further analysis. Or perhaps you simply wish to work with some subset of your data.
In the **learning2014** data the variable 'points' denotes the students exam points in a statistics course exam. If the student did not attend an exam, the value of 'points' will be zero. We will remove these observations from the data.
`@instructions`
- Access the **dplyr** library
- As an example, create object `male_students` by selecting the male students from `learning2014`
- Override `learning2014` and select rows where the 'points' variable is greater than zero.
- If you do not remember how logical comparison works in R, see the 'Logical comparison' exercise from the course 'R short and sweet'.
`@hint`
- The "greater than" logical operator is `>`
`@pre_exercise_code`
```{r}
lrn14 <- read.table("http://www.helsinki.fi/~kvehkala/JYTmooc/JYTOPKYS3-data.txt", sep="\t", header=TRUE)
lrn14$attitude <- lrn14$Attitude / 10
deep_questions <- c("D03", "D11", "D19", "D27", "D07", "D14", "D22", "D30","D06", "D15", "D23", "D31")
lrn14$deep <- rowMeans(lrn14[, deep_questions])
surface_questions <- c("SU02","SU10","SU18","SU26", "SU05","SU13","SU21","SU29","SU08","SU16","SU24","SU32")
lrn14$surf <- rowMeans(lrn14[, surface_questions])
strategic_questions <- c("ST01","ST09","ST17","ST25","ST04","ST12","ST20","ST28")
lrn14$stra <- rowMeans(lrn14[, strategic_questions])
learning2014 <- lrn14[, c("gender","Age","attitude", "deep", "stra", "surf", "Points")]
colnames(learning2014)[2] <- "age"
colnames(learning2014)[7] <- "points"
```
`@sample_code`
```{r}
# learning2014 is available
# access the dplyr library
library(dplyr)
# select male students
male_students <- filter(learning2014, gender == "M")
# select rows where points is greater than zero
learning2014 <-
```
`@solution`
```{r}
# learning2014 is available
# access the dplyr library
library(dplyr)
# select male students
male_students <- filter(learning2014, gender == "M")
# select rows where Points is greater than zero
learning2014 <- filter(learning2014, points > 0)
```
`@sct`
```{r}
test_object("learning2014", incorrect_msg = "Please exclude the observations from learning2014 where points <= 0")
test_error()
success_msg("Awesome!")
```
---
## Visualizations with ggplot2
```yaml
type: NormalExercise
key: 7b821b76a2
lang: r
xp: 100
skills: 1
```
[**ggplot2**](http://ggplot2.org/) is a popular library for creating stunning graphics with R. It has some advantages over the basic plotting system in R, mainly consistent use of function arguments and flexible plot alteration. ggplot2 is an implementation of Leland Wilkinson's *Grammar of Graphics* — a general scheme for data visualization.
In ggplot2, plots may be created via the convenience function `qplot()` where arguments and defaults are meant to be similar to base R's `plot()` function. More complex plotting capacity is available via `ggplot()`, which exposes the user to more explicit elements of the grammar. (from [wikipedia](https://en.wikipedia.org/wiki/Ggplot2))
RStudio has a [cheatsheet](https://www.rstudio.com/wp-content/uploads/2016/11/ggplot2-cheatsheet-2.1.pdf) for data visualization with ggplot2.
`@instructions`
- Access the **ggplot2** library
- Initialize the plot with data and aesthetic mappings
- Adjust the plot initialization: Add an aesthetic element to the plot by defining `col = gender` inside `aes()`.
- Define the visualization type (points)
- Draw the plot to see how it looks at this point
- *Add* a regression line to the plot
- *Add* the title "Student's attitude versus exam points" with `ggtitle("<insert title here>")` to the plot with regression line
- Draw the plot again to see the changes
`@hint`
- Use `+` to add the title to the plot
- The plot with regression line is saved in the object `p3`
- You can draw the plot by typing the object name where the plot is saved
`@pre_exercise_code`
```{r}
learning2014 <- read.table("http://s3.amazonaws.com/assets.datacamp.com/production/course_2218/datasets/learning2014.txt", sep = ",", header = T)
```
`@sample_code`
```{r}
# learning2014 is available
# Access the gglot2 library
library(ggplot2)
# initialize plot with data and aesthetic mapping
p1 <- ggplot(learning2014, aes(x = attitude, y = points))
# define the visualization type (points)
p2 <- p1 + geom_point()
# draw the plot
p2
# add a regression line
p3 <- p2 + geom_smooth(method = "lm")
# add a main title and draw the plot
p4 <- "change me!"
```
`@solution`
```{r}
# learning2014 is available
# Use the gglot2 library
library(ggplot2)
# initialize plot with data and aesthetic mapping
p1 <- ggplot(learning2014, aes(x = attitude, y = points, col = gender))
# define the visualization type (points)
p2 <- p1 + geom_point()
# draw the plot
p2
# add a regression line
p3 <- p2 + geom_smooth(method = "lm")
# add a main title and draw the plot
p4 <- p3 + ggtitle("Student's attitude versus exam points")
p4
```
`@sct`
```{r}
test_function("ggplot",args = c("data","mapping"), incorrect_msg = "Please adjust the call to `ggplot()` and add the `col = gender` aesthetic element to the plot")
test_function("ggtitle", args = "label", incorrect_msg = "Please use the `ggtitle()` function as instructed")
test_student_typed("p4", not_typed_msg = "Please type `p4` to draw the final plot object")
test_error()
success_msg("You're a graphical genius!")
```
---
## Exploring a data frame
```yaml
type: NormalExercise
key: a60db75e33
lang: r
xp: 100
skills: 1
```
Often the most interesting feature of your data are the relationships between the variables. If there are only a handful of variables saved as columns in a data frame, it is possible to visualize all of these relationships neatly in a single plot.
Base R offers a fast plotting function `pairs()`, which draws all possible scatter plots from the columns of a data frame, resulting in a scatter plot matrix. Libraries **GGally** and **ggplot2** together offer a slow but more detailed look at the variables, their distributions and relationships.
`@instructions`
- Draw a scatter matrix of the variables in learning2014 (other than gender)
- Adjust the code: Add the argument `col` to the `pairs()` function, defining the colour with the 'gender' variable in learning2014.
- Draw the plot again to see the changes.
- Access the **ggpot2** and **GGally** libraries and create the plot `p` with `ggpairs()`.
- Draw the plot. Note that the function is a bit slow.
- Adjust the argument `mapping` of `ggpairs()` by defining `col = gender` inside `aes()`.
- Draw the plot again.
- Adjust the code a little more: add another aesthetic element `alpha = 0.3` inside `aes()`.
- See the difference between the plots?
`@hint`
- You can use `$` to access a column of a data frame.
- Remember to separate function arguments with a comma
- You can draw the plot `p` by simply typing it's name: just like printing R objects.
`@pre_exercise_code`
```{r}
learning2014 <- read.table("http://s3.amazonaws.com/assets.datacamp.com/production/course_2218/datasets/learning2014.txt", sep = ",", header = T)
```
`@sample_code`
```{r}
# learning2014 is available
# draw a scatter plot matrix of the variables in learning2014.
# [-1] excludes the first column (gender)
pairs(learning2014[-1])
# access the GGally and ggplot2 libraries
library(GGally)
library(ggplot2)
# create a more advanced plot matrix with ggpairs()
p <- ggpairs(learning2014, mapping = aes(), lower = list(combo = wrap("facethist", bins = 20)))
# draw the plot
```
`@solution`
```{r}
# learning2014 is available
# draw a scatter plot matrix of the variables in learning2014.
# [-1] excludes the first column (gender)
pairs(learning2014[-1], col = learning2014$gender)
# access the GGally and ggplot2 libraries
library(GGally)
library(ggplot2)
# create a more advanced plot matrix with ggpairs()
p <- ggpairs(learning2014, mapping = aes(col = gender, alpha = 0.3), lower = list(combo = wrap("facethist", bins = 20)))
# draw the plot
p
```
`@sct`
```{r}
test_function("pairs", args = c("x" , "col"),incorrect_msg = "Please define the `col` argument inside `pairs()` as instructed")
test_function("ggpairs", args = c("data", "mapping"), incorrect_msg = "Please use the `aes()` function to define the `mapping` argument of `ggpairs()`. Give `aes()` the arguments `col` and `alpha`.")
test_student_typed("p", not_typed_msg = "Please draw the plot created with `ggpairs()`")
test_error()
success_msg("Very nice work! That was a tough one!")
```
---
## Simple regression
```yaml
type: NormalExercise
key: 6eeeae4d73
lang: r
xp: 100
skills: 1
```
[Regression analysis](https://en.wikipedia.org/wiki/Regression_analysis) with R is easy once you have your data in a neat data frame. You can simply use the `lm()` function to fit a linear model. The first argument of `lm()` is a `formula`, which defines the target variable and the explanatory variable(s).
The formula should be `y ~ x`, where `y` is the target variable and `x` the explanatory variable. The second argument of `lm()` is `data`, which should be a data frame where `y` and `x` are columns.
The output of `lm()` is a linear model object, which can be saved for later use. The generic function `summary()` can be used to print out a summary of the model.
`@instructions`
- Create a scatter plot of 'points' versus 'attitude'.
- Fit a regression model where 'points' is the target and 'attitude' is the explanatory variable
- Print out the summary of the linear model object
`@hint`
- Replace `1` with the name of the explanatory variable in the formula inside `lm()`
- Use `summary()` on the model object to print out a summary
`@pre_exercise_code`
```{r}
learning2014 <- read.table("http://s3.amazonaws.com/assets.datacamp.com/production/course_2218/datasets/learning2014.txt", sep = ",", header = T)
```
`@sample_code`
```{r}
# learning2014 is available
# a scatter plot of points versus attitude
library(ggplot2)
qplot(attitude, points, data = learning2014) + geom_smooth(method = "lm")
# fit a linear model
my_model <- lm(points ~ 1, data = learning2014)
# print out a summary of the model
```
`@solution`
```{r}
# learning2014 is available
# a scatter plot of points versus attitude
library(ggplot2)
qplot(attitude, points, data = learning2014) + geom_smooth(method = "lm")
# fit a linear model
my_model <- lm(points ~ attitude, data = learning2014)
# print out a summary of the model
summary(my_model)
```
`@sct`
```{r}
test_object("my_model", incorrect_msg = "Please define 'attitude' as the explanatory variable and create the linear model object `my_model` using `lm()`")
test_output_contains("summary(my_model)", incorrect_msg = "Please print out a summary of the model")
test_error()
success_msg("Good work!")
```
---
## Video: Linear regression
```yaml
type: VideoExercise
key: 5d4203282b
lang: r
xp: 50
skills: 1
video_link: //player.vimeo.com/video/199820260
```
---
## Multiple regression
```yaml
type: NormalExercise
key: 4f88f9dfca
lang: r
xp: 100
skills: 1
```
When there are more than one explanatory variables in the linear model, it is called multiple regression. In R, it is easy to include more than one explanatory variables in your linear model. This is done by simply defining more explanatory variables with the `formula` argument of `lm()`, as below
```
y ~ x1 + x2 + ..
```
Here `y` is again the target variable and `x1, x2, ..` are the explanatory variables.
`@instructions`
- Draw a plot matrix of the learning2014 data with `ggpairs()`
- Fit a regression model where `points` is the target variable and both `attitude` and `stra` are the explanatory variables.
- Print out a summary of the model.
- Adjust the code: Add one more explanatory variable to the model. Based on the plot matrix, choose the variable with the third highest (absolute) correlation with the target variable and use that as the third variable.
- Print out a summary of the new model.
`@hint`
- The variable with the third highest absolute correlation with `points` is `surf`.
`@pre_exercise_code`
```{r}
learning2014 <- read.table("http://s3.amazonaws.com/assets.datacamp.com/production/course_2218/datasets/learning2014.txt", sep = ",", header = T)
library(GGally)
library(ggplot2)
```
`@sample_code`
```{r}
# learning2014, GGally, ggplot2 are avaiable
# create an plot matrix with ggpairs()
ggpairs(learning2014, lower = list(combo = wrap("facethist", bins = 20)))
# create a regression model with multiple explanatory variables
my_model2 <- lm(points ~ attitude + stra, data = learning2014)
# print out a summary of the model
```
`@solution`
```{r}
# learning2014, GGally, ggplot2 are available
# create an plot matrix with ggpairs()
ggpairs(learning2014, lower = list(combo = wrap("facethist", bins = 20)))
# create a regression model with multiple explanatory variables
my_model2 <- lm(points ~ attitude + stra + surf, data = learning2014)
# print out a summary of the model
summary(my_model2)
```
`@sct`
```{r}
test_object("my_model2", incorrect_msg = "Please create `my_model2` by creating a linear model object with three explanatory variables as instructed")
test_output_contains("summary(my_model2)", incorrect_msg = "Please print out a summary of the linear model object `my_model2`")
test_error()
success_msg("Good work!")
```
---
## Graphical model validation
```yaml
type: NormalExercise
key: a6c1dfef61
lang: r
xp: 100
skills: 1
```
R makes it easy to graphically explore the validity of your model assumptions. If you give a linear model object as the first argument to the `plot()` function, the function automatically assumes you want diagnostic plots and will produce them. You can check the help page of plotting an lm object by typing `?plot.lm` or `help(plot.lm)` to the R console.
In the plot function you can then use the argument `which` to choose which plots you want. `which` must be an integer vector corresponding to the following list of plots:
which | graphic
----- | --------
1 | Residuals vs Fitted values
2 | Normal QQ-plot
3 | Standardized residuals vs Fitted values
4 | Cook's distances
5 | Residuals vs Leverage
6 | Cook's distance vs Leverage
<br>
We will focus on plots 1, 2 and 5: Residuals vs Fitted values, Normal QQ-plot and Residuals vs Leverage.
`@instructions`
- Create the linear model object `my_model2`
- Produce the following diagnostic plots using the `plot()` function: Residuals vs Fitted values, Normal QQ-plot and Residuals vs Leverage using the argument `which`.
- Before the call to the `plot()` function, add the following: `par(mfrow = c(2,2))`. This will place the following 4 graphics to the same plot. Execute the code again to see the effect.
`@hint`
- You can combine integers to an integer vector with `c()`. For example: `c(1,2,3)`.
`@pre_exercise_code`
```{r}
learning2014 <- read.table("http://s3.amazonaws.com/assets.datacamp.com/production/course_2218/datasets/learning2014.txt", sep = ",", header = T)
```
`@sample_code`
```{r}
# learning2014 is available
# create a regression model with multiple explanatory variables
my_model2 <- lm(points ~ attitude + stra, data = learning2014)
# draw diagnostic plots using the plot() function. Choose the plots 1, 2 and 5
```
`@solution`
```{r}
# learning2014 is available
# create a regression model with multiple explanatory variables
my_model2 <- lm(points ~ attitude + stra, data = learning2014)
# draw diagnostic plots using the plot() function. Choose the plots 1, 2 and 5
par(mfrow = c(2,2))
plot(my_model2, which = c(1,2,5))
```
`@sct`
```{r}
test_function("plot", args = c("x", "which"),incorrect_msg = "Please use `plot()` on the linear model object and choose the diagnostic plot 1,2 and 5 with the argument `which`. Remember that `c()` can be used to create vectors.")
test_function("par", args = "mfrow", incorrect_msg = "Please use the par function as instructed")
test_error()
success_msg("Great job!")
```
---
## Video: Model validation
```yaml
type: VideoExercise
key: 65c0249d0f
lang: r
xp: 50
skills: 1
video_link: //player.vimeo.com/video/199820384
```
---
## Making predictions
```yaml
type: NormalExercise
key: 2f5c9d30c7
lang: r
xp: 100
skills: 1
```
Okey, so let's assume that we have a linear model which seems to fit our standards. What can we do with it?
The model quantifies the relationship between the explanatory variable(s) and the dependent variable. The model can also be used for predicting the dependent variable based on new observations of the explanatory variable(s).
In R, predicting can be done using the `predict()` function. (see `?predict`). The first argument of predict is a model object and the argument `newdata` (a data.frame) can be used to make predictions based on new observations. One or more columns of `newdata` should have the same name as the explanatory variables in the model object.
`@instructions`
- Create object `m` and print out a summary of the model
- Create object `new_attitudes`
- Adjust the code: Create a new data frame with a column named 'attitude' holding the new attitudes defined in `new_attitudes`
- Print out the new data frame
- `predict()` the new student's exam points based on their attitudes, using the `newdata` argument
`@hint`
- Type `attitude = new_attitudes` inside the `data.frame()` function.
- Give the `new_data` data.frame as the `newdata` argument for `predict()`
`@pre_exercise_code`
```{r}
learning2014 <- read.table("http://s3.amazonaws.com/assets.datacamp.com/production/course_2218/datasets/learning2014.txt", sep = ",", header = T)
```
`@sample_code`
```{r}
# learning2014 is available
# Create model object m
m <- lm(points ~ attitude, data = learning2014)
# print out a summary of the model
# New observations
new_attitudes <- c("Mia" = 3.8, "Mike"= 4.4, "Riikka" = 2.2, "Pekka" = 2.9)
new_data <- data.frame(attitude = "change me!")
# Print out the new data
# Predict the new students exam points based on attitude
predict(m, newdata = "change me!")