-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter 2 code.R
50 lines (36 loc) · 997 Bytes
/
Chapter 2 code.R
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
# install ggplot2 package
install.packages("ggplot2")
# loaded the ggplot2 package
library("ggplot2")
x <- c(1, -.8, -.6, -.4, -.2, 0, .2, .4, .6, .8, 1)
y <- x^3
qplot(x, y)
x <- c(1, 2, 2, 2, 3, 3)
qplot(x, binwidth = 1)
x2 <- c(1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4)
qplot(x2, binwidth = 1)
x3 <- c(0, 1, 1, 2, 2, 2, 3, 3, 4)
qplot(x3, binwidth = 1)
replicate(3, 1 + 1)
# function that simulates a dice throw
# returns the sum of the two dice
roll <- function() {
die <- 1:6
dice <- sample(die, size = 2, replace = TRUE)
sum(dice)
}
rep_roll <- replicate(10, roll())
qplot(rep_roll, binwidth = 1)
rep_roll <- replicate(10000, roll())
qplot(rep_roll, binwidth = 1)
help("sample"
)
# function that simulates an unfair dice throw
# returns the sum of the two dice
roll_unfair <- function() {
die <- 1:6
dice <- sample(die, size = 2, replace = TRUE, prob = c(1/8, 1/8, 1/8, 1/8, 1/8, 3/8))
sum(dice)
}
rolls <- replicate(10000, roll_unfair())
qplot(rolls, binwidth = 1)