forked from KimmoVehkalahti/IODS-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise2 IODS.Rmd
658 lines (433 loc) · 26.5 KB
/
Exercise2 IODS.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
---
title: "**Introduction to Open Data Science, Exercise Set 2**"
subtitle: "**Regression and model validation**"
output:
html_document:
theme: flatly
highlight: haddock
toc: true
toc_depth: 2
number_section: false
---
This set consists of a few numbered exercises.
Go to each exercise in turn and do as follows:
1. Read the brief description of the exercise.
2. Run the (possible) pre-exercise-code chunk.
3. Follow the instructions to fix the R code!
## 2.0 INSTALL THE REQUIRED PACKAGES FIRST!
One or more extra packages (in addition to `tidyverse`) will be needed below.
```{r}
# Select (with mouse or arrow keys) the install.packages("...") and
# run it (by Ctrl+Enter / Cmd+Enter):
# install.packages("GGally")
```
## 2.1 Reading data from the web
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 whether the data is saved locally on your computer or somewhere else behind a web URL.
After the correct function has been identified and data read into R, the data will usually be in R `data.frame` format. Te dimensions of a data frame are ($n$,$d$), where $n$ is the number of rows (the observations) and $d$ the number of columns (the variables).
**The purpose of this course is to expose you to some basic and more advanced tasks of programming and data analysis with R.**
```{r, echo=FALSE}
# Pre-exercise-code (Run this code chunk first! Do NOT edit it.)
# Click the green arrow ("Run Current Chunk") in the upper-right corner of this chunk. This will initialize the R objects needed in the exercise. Then move to Instructions of the exercise to start working.
# (No pre-exercise code in this exercise! Just go on!)
```
### 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.
### R code
```{r}
# This is a code chunk in RStudio editor.
# Work with the exercise in this chunk, step-by-step. Fix the R code!
# 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
```
## 2.2 Scaling variables
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.
```{r, echo=FALSE}
# Pre-exercise-code (Run this code chunk first! Do NOT edit it.)
# Click the green arrow ("Run Current Chunk") in the upper-right corner of this chunk. This will initialize the R objects needed in the exercise. Then move to Instructions of the exercise to start working.
# read the data into memory
lrn14 <- read.table("http://www.helsinki.fi/~kvehkala/JYTmooc/JYTOPKYS3-data.txt", sep="\t", header=TRUE)
```
### 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.
### R code
```{r}
# This is a code chunk in RStudio editor.
# Work with the exercise in this chunk, step-by-step. Fix the R code!
#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!"
```
## 2.3 Combining variables
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.
```{r, echo=FALSE}
# Pre-exercise-code (Run this code chunk first! Do NOT edit it.)
# Click the green arrow ("Run Current Chunk") in the upper-right corner of this chunk. This will initialize the R objects needed in the exercise. Then move to Instructions of the exercise to start working.
# 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
```
### 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`
Hints:
- 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
### R code
```{r}
# Work with the exercise in this chunk, step-by-step. Fix the R code!
# 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
deep_columns <- select(lrn14, one_of(deep_questions))
# and create column 'deep' by averaging
lrn14$deep <- rowMeans(deep_columns)
# select the columns related to surface learning
surface_columns <- select(lrn14, one_of(surface_questions))
# and create column 'surf' by averaging
lrn14$surf <- rowMeans(surface_columns)
# select the columns related to strategic learning
strategic_columns <- "change me!"
# and create column 'stra' by averaging
```
## 2.4 Selecting columns
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 are also convenient [data wrangling cheatsheets by RStudio](https://www.rstudio.com/resources/cheatsheets/) to help you get started (dplyr, tidyr etc.)
```{r, echo=FALSE}
# Pre-exercise-code (Run this code chunk first! Do NOT edit it.)
# Click the green arrow ("Run Current Chunk") in the upper-right corner of this chunk. This will initialize the R objects needed in the exercise. Then move to Instructions of the exercise to start working.
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])
```
### 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
### R code
```{r}
# Work with the exercise in this chunk, step-by-step. Fix the R code!
# 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 structure of the new dataset
```
## 2.5 Modifying column names
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 cheatsheets](https://www.rstudio.com/resources/cheatsheets/).
```{r, echo=FALSE}
# Pre-exercise-code (Run this code chunk first! Do NOT edit it.)
# Click the green arrow ("Run Current Chunk") in the upper-right corner of this chunk. This will initialize the R objects needed in the exercise. Then move to Instructions of the exercise to start working.
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")]
```
### 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'?
### R code
```{r}
# Work with the exercise in this chunk, step-by-step. Fix the R code!
# 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
```
## 2.6 Excluding observations
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.
```{r, echo=FALSE}
# Pre-exercise-code (Run this code chunk first! Do NOT edit it.)
# Click the green arrow ("Run Current Chunk") in the upper-right corner of this chunk. This will initialize the R objects needed in the exercise. Then move to Instructions of the exercise to start working.
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"
```
### 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 `>`
### R code
```{r}
# Work with the exercise in this chunk, step-by-step. Fix the R code!
# 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 <-
```
## 2.7 Visualizations with ggplot2
[**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/resources/cheatsheets/) for data visualization with ggplot2.
```{r, echo=FALSE}
# Pre-exercise-code (Run this code chunk first! Do NOT edit it.)
# Click the green arrow ("Run Current Chunk") in the upper-right corner of this chunk. This will initialize the R objects needed in the exercise. Then move to Instructions of the exercise to start working.
learning2014 <- read.table("https://raw.githubusercontent.com/KimmoVehkalahti/Helsinki-Open-Data-Science/master/datasets/learning2014.txt",
sep = ",", header = T)
```
### 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
Hints:
- 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
### R code
```{r}
# Work with the exercise in this chunk, step-by-step. Fix the R code!
# 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
p4 <- "change me!"
# draw the plot!
```
## 2.8 Exploring a data frame
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.
```{r, echo=FALSE}
# Pre-exercise-code (Run this code chunk first! Do NOT edit it.)
# Click the green arrow ("Run Current Chunk") in the upper-right corner of this chunk. This will initialize the R objects needed in the exercise. Then move to Instructions of the exercise to start working.
learning2014 <- read.table("https://raw.githubusercontent.com/KimmoVehkalahti/Helsinki-Open-Data-Science/master/datasets/learning2014.txt",
sep = ",", header = T)
```
### 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?
Hints:
- 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.
### R code
```{r}
# Work with the exercise in this chunk, step-by-step. Fix the R code!
# 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
```
## 2.9 Simple regression
[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 (or outcome) variable and `x` the explanatory variable (predictor). 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.
```{r, echo=FALSE}
# Pre-exercise-code (Run this code chunk first! Do NOT edit it.)
# Click the green arrow ("Run Current Chunk") in the upper-right corner of this chunk. This will initialize the R objects needed in the exercise. Then move to Instructions of the exercise to start working.
learning2014 <- read.table("https://raw.githubusercontent.com/KimmoVehkalahti/Helsinki-Open-Data-Science/master/datasets/learning2014.txt",
sep = ",", header = T)
```
### 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
Hints:
- 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
### R code
```{r}
# Work with the exercise in this chunk, step-by-step. Fix the R code!
# 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 + attitude + gender + age, data = learning2014)
summary(my_model)
# print out a summary of the model
```
## 2.10 Multiple regression
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.
```{r, echo=FALSE}
# Pre-exercise-code (Run this code chunk first! Do NOT edit it.)
# Click the green arrow ("Run Current Chunk") in the upper-right corner of this chunk. This will initialize the R objects needed in the exercise. Then move to Instructions of the exercise to start working.
learning2014 <- read.table("https://raw.githubusercontent.com/KimmoVehkalahti/Helsinki-Open-Data-Science/master/datasets/learning2014.txt",
sep = ",", header = T)
```
### 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`.
### R code
```{r}
# Work with the exercise in this chunk, step-by-step. Fix the R code!
# learning2014 is available
library(GGally)
library(ggplot2)
# 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
```
## 2.11 Graphical model validation
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.
```{r, echo=FALSE}
# Pre-exercise-code (Run this code chunk first! Do NOT edit it.)
# Click the green arrow ("Run Current Chunk") in the upper-right corner of this chunk. This will initialize the R objects needed in the exercise. Then move to Instructions of the exercise to start working.
learning2014 <- read.table("https://raw.githubusercontent.com/KimmoVehkalahti/Helsinki-Open-Data-Science/master/datasets/learning2014.txt",
sep = ",", header = T)
```
### 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)`.
### R code
```{r}
# Work with the exercise in this chunk, step-by-step. Fix the R code!
# 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
```
## 2.12 Making predictions
Okay, 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.
```{r, echo=FALSE}
# Pre-exercise-code (Run this code chunk first! Do NOT edit it.)
# Click the green arrow ("Run Current Chunk") in the upper-right corner of this chunk. This will initialize the R objects needed in the exercise. Then move to Instructions of the exercise to start working.
learning2014 <- read.table("https://raw.githubusercontent.com/KimmoVehkalahti/Helsinki-Open-Data-Science/master/datasets/learning2014.txt",
sep = ",", header = T)
```
### 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
Hints:
- Type `attitude = new_attitudes` inside the `data.frame()` function.
- Give the `new_data` data.frame as the `newdata` argument for `predict()`
### R code
```{r}
# Work with the exercise in this chunk, step-by-step. Fix the R code!
# 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!")
```
**Awesome work!**