-
Notifications
You must be signed in to change notification settings - Fork 5
/
tutorials.Rmd
785 lines (508 loc) · 18.5 KB
/
tutorials.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
---
title: "Math Lessons"
author: "R-Girls"
output:
learnr::tutorial:
css: css/custom.css
runtime: shiny_prerendered
---
```{r setup, include=FALSE}
library(learnr)
library(tidyverse)
library(tibble)
library(fontawesome)
library(astsa)
library(grid)
knitr::opts_chunk$set(echo = FALSE)
```
## Generating Sequences Part 1
### Lesson objectives
- Generating sequences using R code
### Packages used in this lesson:
- `tidyverse`
### Success criteria
* Generate different sequences using R code
### Keywords
* sequence
* term = the numbers in the sequence
* nth term
### Worked Example 1
First we will tell R to generate a sequence.
This is how you generate a sequence of numbers from 1 to 100 where the terms increase by 1.
```{r chunk11, exercise=TRUE}
seq(from=1,to=100,by=1)
```
Use the above example R code in chunk1 to generate the following sequences. Remember to edit the numbers in the code.
#### Activity 1 Questions
Q1 Generate a sequence from 1 to 100 where the terms increase by 10
```{r chunk21, exercise=TRUE}
```
Q2 Generate a sequence from 5 to 95 where the terms increase by 5.
```{r chunk31, exercise=TRUE}
```
Q3 Generate a sequence from 3 to 399 where the terms increase by 4.
```{r chunk41, exercise=TRUE}
```
Q4 Generate a sequence from 1 to 9 where the terms increase by 3.
```{r chunk51, exercise=TRUE}
```
### Worked Example 2
Now we will generate a sequence that starts from a number and goes up to the nth term, where the nth term is for example 10.
For example a sequence from 1 where the terms go up by 1 till the 10th term.
```{r chunk61, exercise=TRUE}
seq(from=1, length.out=10, by=1)
```
Use the above example R code in chunk6 to generate the following sequences.
Knit your document after each question to check your sequence.
#### Activity 2 Questions
Q1 Generate a sequence from 1 where the terms go up by 4 up to the 100th term
```{r chunk71, exercise=TRUE}
```
Q2 Generate a sequence from 5 where the terms go up by 10 up to the 89th term
```{r chunk81, exercise=TRUE}
```
Q3 Generate a sequence from 3 where the terms go up by 3 up to the 33rd term
```{r chunk91, exercise=TRUE}
```
### Worked Example 3
So the terms have increased by a particular number, but we can also decrease by a number and produce a backward sequence.
For example, we can sequence from 100 to 1 by -1
```{r chunk101, exercise=TRUE}
seq(from=100, to=1, by=-1)
```
Use the above example R code in chunk10 to answer the following questions.
Knit your document after each question to check your sequence is correct.
#### Activity 3 Questions
Q1 Generate a sequence that goes down from 50 to 0 where the terms go down by 5.
```{r chunk111, exercise=TRUE}
```
Q2 Generate a sequence from 10 to -10 where the terms go down by 1.
```{r chunk121, exercise=TRUE}
```
Q3 Generate a sequence that goes down from 20 to 10 where the terms go down by 2.
```{r chunk141, exercise=TRUE}
```
## Generating Sequences Part 2
### Lesson objectives
- Investigating sequences
### Packages used in this lesson:
- `tidyverse`
### Success criteria
* Find the relationship between the terms
* Does a number lie in a sequence
* Find a missing term in the sequence
### Keywords
* sequence
* term
* nth term
#### Worked Example 1
This is how you find the difference between the terms in a sequence.
The sequence is 3, 7, 11, 15.
Knit your document to see the sequence generated by the R code in chunk1.
```{r chunk1, exercise=TRUE}
x <- c(3,7,11,15)
diff(x)
```
The R code tells you the difference between each number in the sequence.
##### Activity 1 Questions
Now use the above example R code in chunk1 to find the difference between the terms in the following sequences
Knit the document after each question to see the answer. Check your answer.
Q1 2,7,12,17,22
```{r chunk2, exercise=TRUE}
```
Q2 3,9,15,21,27
```{r chunk3, exercise=TRUE}
```
Q3 1,4,10,19,31
```{r chunk4, exercise=TRUE}
```
#### Worked Example 2
Now we will see if a number lies in a sequence.
First we will generate the first 20 terms of the sequence 3,7,11,15 and then we will see if the numbers 55 and 56 lie in the sequence. The computer will generate the sequence and if it finds the number it will report it below the sequence. If it does not find it then it will say numeric (0).
```{r chunk5, exercise=TRUE}
x <- seq(from=3, length.out=20, by=4)
x
x[x==55]
x[x==56]
```
Use the above example R code in chunk5 to answer the following questions.
Remember to tell R how many terms you want it to generate by editing the length.out number. Remember to tell R what numbers to look for in the sequence by editing the x== numbers.
##### Activity 2 Questions
Q1 Generate the first 100 terms of the sequence 3,7,11,15 and see if the numbers 117 and 395 lie in the sequence. Do they?
```{r chunk6, exercise=TRUE}
```
#### Worked Example 3
Now we will find the missing number in a sequence. Our sequence is 4, ?, 16, 22. Term 2 is missing. So we generate the sequence based on what we know which is that the difference between 16 and 22 is 6. The computer will put in the missing term.
```{r chunk7, exercise=TRUE}
seq(from=4, to=22, by=6)
```
Answer: The missing term is 10.
Use the above example R code to answer the following questions.
Remember to knit to see your results.
##### Activity 3 Questions
Q1 What is the missing term in the sequence 3,12,?,30.
```{r chunk8, exercise=TRUE}
```
## Using Pythagoras' Theorem
### Lesson Objective
Use Pythagoras' Theorem to find the lengths of missing sides of a right angle triangle
### Packages used in this lesson:
- `tidyverse`
### Success criteria
Write the R code to work out the missing side
Calculate the missing side
### Keywords
* Pythagoras theorem
* Length of a side
* Hypotenuse
* square
* square root
#### Worked Example 1: Finding the hypotenuse
First knit the document so that you can see images of the triangles and the formulae you will be working with.
This is a worked example for you to follow.
The formula to find c is: $$c = \sqrt{a^2 + b^2}$$
Check how the formula is written in the output .
Run the R code in chunk1 by clicking on the little arrow to the right of the code chunk. This will draw you the triangle that you are working with below the code chunk.
```{r chunk21, echo=FALSE, exercise=TRUE}
x <- c(0, 0.5, 0)
y <- c(0, 0, 1.5)
plot(x,y,axes=F, ylab='', xlab='', xlim=c(-0.3,2), ylim=c(-0.3,2))
polygon(x, y, col = 'deeppink')
text(0.2,-0.1, "b=14cm")
text(-0.2,0.5, "a=20cm")
text(0.5,0.5, "c")
```
The length of side c is missing.
The R code in chunk2 below works out the missing side c. Click on the little arrow on the right of the chunk to run the code.
The answer will be shown below the code chunk.
```{r chunk22, exercise=TRUE}
a <- 20
b <- 14
c <- sqrt(a^2+b^2)
c
```
The length of $c = r round(c,2)$ cm
The inline code above tells R to round c to 2 decimal places. Knit the document to check this.
#### Activity1: Your turn
Run R code chunk3. This will draw you the triangle with the length of side c missing.
```{r chunk23, echo=F, exercise=TRUE}
x <- c(0, 0.5, 0.5)
y <- c(0, 0, 1.5)
plot(x,y,axes=F, ylab='', xlab='', xlim=c(-0.3,2), ylim=c(-0.3,2))
polygon(x, y, col = 'deeppink')
text(0.2,-0.1, "b=4cm")
text(0.7,0.5, "a=15cm")
text(0.1,0.5, "c")
```
Write your own code in chunk4 to find length c in the triangle where a = 15 cm and b = 4 cm.
Use the code in chunk2 to help you.
```{r chunk24, exercise=TRUE}
```
#### Worked Example 2
Use Pythagoras' theorem to find one of the other sides.
So find the length of side a when b = 14 cm and c = 25 cm.
Knit the document to see the formula and triangle.
The formula to find the length of side a is: $$a= \sqrt{c^2 - b^2}$$
The code in chunk5 will draw the triangle for you.
```{r chunk25, echo=F, exercise=TRUE}
x <- c(0, 0.8, 0)
y <- c(0, 0, 1.5)
plot(x,y,axes=F, ylab='', xlab='', xlim=c(-0.3,2), ylim=c(-0.3,2))
polygon(x, y, col = 'deeppink')
text(0.4,-0.1, "b=14cm")
text(-0.2,0.5, "a")
text(0.6,0.9, "c=25cm")
```
The code in Chunk6 calculates the length of side a when b and c are known.
Run the code by clicking on the little arrow on the right of the code.
```{r chunk26, exercise=TRUE}
b <- 14
c <- 25
a <- sqrt(c^2-b^2)
a
```
The length of a = r round(a,2) cm.
#### Activity2: Your turn
Use the R code from the above examples to help you find the missing sides in the table below
| a | b | c |
|-----|-----|-----|
| 36 | | 81 |
| | 9 | 49 |
| 10 | 10 | |
Find length b when a=36 and c=81
```{r chunk27, exercise=TRUE}
```
Answer b =
Find length a when b=9 and c=49
```{r chunk28, exercise=TRUE}
a
```
Find length c when a=10 and b=10
```{r chunk29, exercise=TRUE}
c
```
### Extension
Can you round your answers from code chunks 7, 8 and 9 to 2 decimal places? Use the space below and knit the document to see if you have been successful.
```{r chunk30, exercise=TRUE}
```
## Finding the Equation - Part 1
### Lesson objectives
- Plotting straight line graphs $y=mx+c$
### Packages used in this lesson:
- `tidyverse`
### Success criteria
- Plot a straight line graph $y = mx+c$
- Compare graphs
### Keywords
* slope
* gradient = m
* intercept = c
* x-axis
* y-axis
#### Worked Example 1
This is a worked example for you to follow.
We will show you how to plot the line graph for $y=2x + 5$.
```{r chunk16, exercise=TRUE }
x <- seq(from=-4, to=4, by=1) # sequence the x-axis from -4 to 4
y <- 2*x+5
mydata <- tibble (x,y)
ggplot(mydata) +
aes(x,y) +
geom_line (col='red')+
geom_vline (xintercept = 0, col='black')+
geom_hline (yintercept = 0, col='black')
```
Now close the image by clicking on the X to the right of the graph
#### Activity1:
Write your own code to draw the following graphs.
Use code chunk1 from the example above to help you.
1. y = 2x (in code chunk2)
2. y = 3x + 10 (in code chunk3)
3. y = 5x + 2.5 (in code chunk4)
```{r chunk26, exercise=TRUE}
```
```{r chunk36, exercise=TRUE}
```
```{r chunk46, exercise=TRUE}
```
#### Worked Example 2
Drawing more than one line on a graph helps us to compare the lines and see what is the same and what is different.
Here is a worked example for four different lines.
1. y=3x+0
2. y=3x+1
3. y=3x+2
4. y=3x+3
```{r chunk56, exercise=TRUE}
x <- seq(-4, 4) # sequence from -4 to 4
y1 <- 3*x+0
y2 <- 3*x+1
y3 <- 3*x+2
y4 <- 3*x+3
mydata <- tibble (x,y1,y2,y3,y4)
ggplot(mydata) +
geom_line (aes(x=x, y=y1), col='red')+
geom_line (aes(x=x, y=y2), col='blue')+
geom_line (aes(x=x, y=y3), col='green')+
geom_line (aes(x=x, y=y4), col='grey') +
geom_vline (xintercept = 0, col='black')+
geom_hline (yintercept = 0, col='black')
```
Question: From the graph what looks the same and what looks different about these lines?
Answer:
#### Activity2:
Draw the following lines on a graph
1. y=1x+5
2. y=2x+5
3. y=3x+5
4. y=4x+5
Use code chunk5 from the example above to help you. Remember to update the R code with the new lines.
Run the code. Knit the document.
```{r chunk66, exercise=TRUE}
```
Question: What is the same and what is different about the lines?
```{r chunk66, exercise=TRUE}
```
## Finding the Equation - Part 2
### Lesson objectives
Plotting and finding the equations of straight line graphs $y=mx+c$
### Packages used in this lesson:
- `tidyverse`
### Success criteria
* Plot a straight line graph $y = mx+c$
* Plot straight line graphs $y = mx+c$ with different intercepts
* Plot a straight line graph $y = mx+c$ with a negative gradient
* Find the equations of straight line graphs
### Keywords
* slope
* gradient
* intercept
* x-axis
* y-axis
#### Worked Example 1
This is a worked example for you to follow.
We will show you how to plot the line graph for y=2x + 5.
```{r chunk13, exercise=TRUE}
x <- seq(from=-4, to=4, by=1) # sequence the x-axis from -4 to 4
y <- 2*x+5
mydata <- tibble (x,y)
ggplot(mydata) +
aes(x,y) +
geom_point () +
geom_line (col='red')+
geom_vline (xintercept = 0, col='black')+
geom_hline (yintercept = 0, col='black')
```
#### Worked Example 2
We will now see what happens when you change the intercept c to a negative value.
1. y=3x+5
2. y=3x-5
Run the code in chunk2 by clicking on the little arrow on the right of the code chunk.
```{r chunk55, exercise=TRUE}
x <- seq(-4, 4) # sequence from -4 to 4
y1 <- 3*x+5
y2 <- 3*x-5
mydata <- tibble (x,y1,y2,y)
ggplot(mydata) +
geom_line (aes(x=x, y=y1), col='red')+
geom_line (aes(x=x, y=y2), col='blue')+
geom_vline (xintercept = 0, col='black')+
geom_hline (yintercept = 0, col='black')
```
Question: What is the same and what is different about these lines?
Knit your document and check the output.
#### Activity 1
Draw these lines on a graph. Use the R code from chunk2 to help you. Remember to update the code with these new lines.
1. y=x+10
2. y=x-10
```{r chunk33, exercise=TRUE}
```
Question: What is the same and what is different about these lines?
#### Activity 2
Now we will investigate m (the gradient). Draw these lines on a graph. Use R code from chunk2 to help you.
Run the code and knit the document.
1. y=-2x+5
2. y= 2x+5
```{r chunk43, exercise=TRUE}
```
Question: What is the same and what is different about these lines?
#### Activity 3 Answer the following questions
```{r chunk53, echo=FALSE, exercise=TRUE}
cat ("Q1 In the equation y=mx+c, what happens when you change c?")
cat ("Q2 What happens when you change m?")
```
#### Activity 4: Work out the equation from a line graph
Write down the equations of the following four lines on the graph below.
Knit the document to get a good view of the graph.
```{r chunk63, echo=FALSE, exercise=TRUE}
x <- seq(-10, 10)
y <- x
ggplot() +
aes(x,y)+
geom_blank()+
geom_abline(slope=1, intercept=0, col='red')+
geom_abline(slope=-1, intercept=2.5, col='cyan')+
geom_abline(slope=2, intercept=10, col='blue')+
geom_abline(slope=0, intercept=5, col='orange')+
geom_vline (xintercept=0, col='black')+
geom_hline (yintercept=0, col='black')
```
## Representing Data with boxplots
### Lesson objectives
- To understand how to visualise the shape of data using box plots\
- To produce box plots and the related five number summary\
- To interpret box plots\
### Packages used in this lesson:
- `tidyverse`
- `astsa`
### Success criteria
* Draw a box plot using R code
* Produce a five number summary using R code
* Read data from a box plot
* Compare two box plots
### Keywords
box plot,
minimum, maximum, lower quartile, median, upper quartile, five number summary, inter-quartile range, range
### What is a box plot
A box plot is a way to show the shape or distribution of a set of data.
It shows five useful features of the data, known as the five number summary.
Minimum - the smallest value\
Maximum - the largest value\
Median - the middle or 50% value\
Lower quartile - the value half way between the minimum and the median or 25% value\
Upper quartile - the value half way between the median and the maximum or 75% value\
The difference between the upper and lower quartile is known as the inter-quartile range (IQR)
### Worked Example 1
Zaynab keeps a record of her journey times to school each morning. The times are to the nearest minute:
29,21,16,25,21,19,18,30,21,21,12,26,19,21,20,19,30,29,16,21,18,18,27,18,20
Let us draw a box plot and obtain the five number summary and the inter-quartile range (IQR). We will do this in three steps.
The R code in chunk1 tells R to store all the times into a variable called y and then to use these times to draw a box plot of the data.
```{r chunk43, echo=FALSE, exercise=TRUE}
y <- c(29,21,16,25,21,19,18,30,21,21,12,26,19,21,20,19,30,29,16,21,18,18,27,18,20)
boxplot(y, xlab="Times (mins)", horizontal=T)
```
To help us read values from the box plot it is useful to have gridlines on the plot.\
Here is a simple trick to get gridlines on the plot.
The code in chunk2 tells R to use the numbers it store in variable y to draw a box plot and then to add grid lines.
Click on the little arrow on the right of the code chunk. The box plot will be drawn below the chunk.
```{r chunk45, echo=FALSE, exercise=TRUE}
# we can calculate the five number summary like this
boxplot(y, xlab="Times (mins)", horizontal=T)
Grid()
boxplot(y, xlab="Times (mins)", horizontal=T, add=T)
```
We can also get R to calculate the five number summary for us.
```{r chunk46, echo=FALSE, exercise=TRUE}
# we can calculate the five number summary like this
summary(y) # five number summary but also provides the mean
IQR(y) # inter-quartile range
```
### Activity 1:
Produce a box plot, the five number summary and IQR of the following data
Aisha records the length of time spent by each member of her class on their last English homework assignment.
The times are recorded to the nearest minute.
85,124,55,140,120,61,95,105,118,180,55,78,130,112,70,126,60,90,115,60,142,100,105,65,100,75
Use the R code in chunk1 to help you draw the box plot. Remember to replace the example data with the new dataset.
```{r chunk47, echo=FALSE, exercise=TRUE}
```
Now use the code in chunk2 to add grid lines to your box plot
```{r chunk48, echo=FALSE, exercise=TRUE}
```
Now calculate the five number summary using code in chunk3
```{r chunk49, echo=FALSE, exercise=TRUE}
```
### Worked Example 2
Below are the percentage exam marks out of 100 for Maths and English.
Maths (%): 98,79,51,54,62,61,56,87,70,60,93,51,52,54,68
English (%): 37,50,58,45,93,47,47,45,38,61,65,46,97,99,54
Produce two box plots side by side
```{r chunk59, echo=FALSE, exercise=TRUE}
maths <- c(98,79,51,54,62,61,56,87,70,60,93,51,52,54,68)
english <- c(37,50,58,45,93,47,47,45,38,61,65,46,97,99,54)
boxplot(maths, english, xlab="Marks%", names=c("Maths", "English"), horizontal=T)
Grid()
boxplot(maths, english, xlab="Marks%", names=c("Maths", "English"), horizontal=T, add=T)
summary(maths)
summary(english)
IQR(maths)
IQR(english)
```
Write down the five number summaries below
What are the inter-quartile ranges
### Activity 2:
Below are the heights of boys and girls of a similar age.
Boys (cm): 181,157,159,179,186,159,178,162,137,184,140,173,176
Girls (cm): 172,151,176,159,139,179,178,162,134,166,164,172,170
Produce two box plots side by side with a grid and answer the following questions.
Remember to replace the example data with the new information (boys, girls, heights, data)
```{r chunk67, echo=FALSE, exercise=TRUE}
```
#### Questions
Are the statements below True or False - and explain why
1. The girls are taller on average
2. Half the girls are over 165 cm tall
3. The girls show less spread in height
4. The boys show less spread in height
5. The shortest person is a girl
6. The tallest person is a boy
7. Half the boys are over 172 cm tall
8. Half the girls are under 165cm tall