-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.R
68 lines (59 loc) · 1.96 KB
/
common.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
tipping_df <- function(){
return(
read.csv("data/restaurant_tipping.csv", header = T, sep = ";", dec = ",")
);
}
theme_df <- function() {
return(
ggplot2::theme(panel.background = element_rect(fill = NA),
panel.grid.major = element_line(colour = "grey50"),
panel.ontop = TRUE,
plot.caption = element_text(hjust = 0))
);
}
sim_chi2 <- function(degree_of_freedom = 1, M = 10) {
if (degree_of_freedom < 1) stop("Degree of Freedom < 1", call. = T);
chi_2 <- rep(0, M);
for (i in 1:degree_of_freedom) {
z_i <- rnorm(n = M, mean = 0, sd = 1);
z_i <- z_i^2;
chi_2 <- chi_2 + z_i;
}
return(chi_2);
}
sim_t <- function(degree_of_freedom = 1, M = 10) {
v <- degree_of_freedom;
c <- sim_chi2(degree_of_freedom = v, M = M);
z <- rnorm(n = M, mean = 0, sd = 1);
l <- z / sqrt(c / v);
return(l);
}
sim_f <- function(degree_of_freedom_1 = 1,
degree_of_freedom_2 = 1,
M = 10) {
dof_1 <- degree_of_freedom_1;
dof_2 <- degree_of_freedom_2;
c1 <- sim_chi2(degree_of_freedom = dof_1, M = M);
c2 <- sim_chi2(degree_of_freedom = dof_2, M = M);
f <- (c1/dof_1) / (c2/dof_2);
}
plot_random_walks <- function(df) {
K <- max(unique(df$run));
N <- max(unique(df$x));
p <-
df %>%
ggplot(aes(x, y)) +
geom_point(alpha = .3,
size = .5,
colour = "black") +
geom_hline(colour = "blue", alpha = .3, aes(yintercept = mean(y))) +
geom_line(data = df_p, alpha = .7, colour = "red", aes(x = x, y = y)) +
theme_light() +
labs(x = "Step", y = "Cumulative sum of y",
title = sprintf("%s Random Walk%s of %s step%s", K, ifelse(K > 1, "s", ""),
N, ifelse(N > 1, "s", "")),
subtitle = sprintf("Time-Average shown in red and overall mean (%s) in blue.", round(mean(df$y), 3)),
caption = "Christian Bitter"
);
return(p);
}