Question 1
A) see below!
[[1]]
[1] “0.5”
[[2]]
[1] “3 -5”
[[3]]
[1] “No real number solution”
quadratic <- function(a, b, c) {
D <- b^2 - 4*a*c
case_when(
D < 0 ~ "No real number solution",
D == 0 ~ (-b / (2*a)) |> as.character(),
D > 0 ~ {
x1 <- (-b + sqrt(D)) / (2*a)
x2 <- (-b - sqrt(D)) / (2*a)
paste(x1, x2)
}
)
}
#example
quadratic(1, 2, 5)
## Warning in sqrt(D): NaNs produced
## Warning in sqrt(D): NaNs produced
## [1] "No real number solution"
eqs <- tibble(
a = c(4, 1, 3),
b = c(-4, 2, 8),
c = c(1, -15, 9)
)
solutions <- pmap(eqs, quadratic)
## Warning in sqrt(D): NaNs produced
## Warning in sqrt(D): NaNs produced
solutions
## [[1]]
## [1] "0.5"
##
## [[2]]
## [1] "3 -5"
##
## [[3]]
## [1] "No real number solution"
Question 2
64.16814
cond <- function(d) { x1 <- d$temp[d$fail == 1]; x2 <- d$temp[d$fail == 0]
v1 <- var(x1); v2 <- var(x2)
m1 <- mean(x1); m2 <- mean(x2)
A <- 0.5 * (1/v2 - 1/v1)
B <- m1/v1 - m2/v2
C <- 0.5 * (m2^2/v2 - m1^2/v1) + log((length(x1) * sqrt(v2)) / (length(x2) * sqrt(v1)))
r <- c((-B + sqrt(B^2 - 4*A*C))/(2*A), (-B - sqrt(B^2 - 4*A*C))/(2*A))
case_when(
r[1] >= min(d$temp) & r[1] <= max(d$temp) ~ r[1],
r[2] >= min(d$temp) & r[2] <= max(d$temp) ~ r[2],
TRUE ~ NA_real_
)
}
data.frame(fail=as.factor(c(rep(1,7),rep(0,16))), temp=c(58,57,53,63,75,70,70,66,67,67,67,68,69,70,70,72,73,75,76,76,78,79,81)) %>% cond()
## [1] 64.16814
Question 3
See below!
names <- map_chr(got_chars, "name")
actors <- map(got_chars, "playedBy") %>% map_chr(~ paste(.x, collapse = ", "))
sentences <- map2(names, actors, ~ paste(.x, "is played by", .y)) %>% unlist() %>% print()
## [1] "Theon Greyjoy is played by Alfie Allen"
## [2] "Tyrion Lannister is played by Peter Dinklage"
## [3] "Victarion Greyjoy is played by "
## [4] "Will is played by Bronson Webb"
## [5] "Areo Hotah is played by DeObia Oparei"
## [6] "Chett is played by "
## [7] "Cressen is played by Oliver Ford"
## [8] "Arianne Martell is played by "
## [9] "Daenerys Targaryen is played by Emilia Clarke"
## [10] "Davos Seaworth is played by Liam Cunningham"
## [11] "Arya Stark is played by Maisie Williams"
## [12] "Arys Oakheart is played by "
## [13] "Asha Greyjoy is played by Gemma Whelan"
## [14] "Barristan Selmy is played by Ian McElhinney"
## [15] "Varamyr is played by "
## [16] "Brandon Stark is played by Isaac Hempstead-Wright"
## [17] "Brienne of Tarth is played by Gwendoline Christie"
## [18] "Catelyn Stark is played by Michelle Fairley"
## [19] "Cersei Lannister is played by Lena Headey"
## [20] "Eddard Stark is played by Sean Bean, Sebastian Croft, Robert Aramayo"
## [21] "Jaime Lannister is played by Nikolaj Coster-Waldau"
## [22] "Jon Connington is played by "
## [23] "Jon Snow is played by Kit Harington"
## [24] "Aeron Greyjoy is played by Michael Feast"
## [25] "Kevan Lannister is played by Ian Gelder"
## [26] "Melisandre is played by Carice van Houten"
## [27] "Merrett Frey is played by "
## [28] "Quentyn Martell is played by "
## [29] "Samwell Tarly is played by John Bradley-West"
## [30] "Sansa Stark is played by Sophie Turner"
Question 4
a) See below!
b) See below!
c) See below!
d) There appears to be a negative auto correlation, See below!
flights_list <- split(flights, flights$month)
r <- flights_list %>% map(~ lm(dep_delay ~ arr_delay, data = .x)) %>% map_dbl(~ summary(.x)$r.squared)
print(r)
## 1 2 3 4 5 6 7 8
## 0.8396646 0.8268583 0.8528125 0.8448023 0.8306929 0.8671995 0.8504234 0.8045664
## 9 10 11 12
## 0.8314524 0.7814353 0.7722201 0.8467227
val <- flights %>% group_by(month) %>% summarize(correlation = cor(dep_delay, arr_delay, use = "complete.obs")) %>% print()
## # A tibble: 12 × 2
## month correlation
## <int> <dbl>
## 1 1 0.916
## 2 2 0.909
## 3 3 0.923
## 4 4 0.919
## 5 5 0.911
## 6 6 0.931
## 7 7 0.922
## 8 8 0.897
## 9 9 0.912
## 10 10 0.884
## 11 11 0.879
## 12 12 0.920
val %>% mutate(c = correlation^2, difference = r - c) %>% print()
## # A tibble: 12 × 4
## month correlation c difference
## <int> <dbl> <dbl> <dbl>
## 1 1 0.916 0.840 1.11e-16
## 2 2 0.909 0.827 2.22e-16
## 3 3 0.923 0.853 2.22e-16
## 4 4 0.919 0.845 -2.22e-16
## 5 5 0.911 0.831 1.11e-16
## 6 6 0.931 0.867 -1.11e-16
## 7 7 0.922 0.850 0
## 8 8 0.897 0.805 0
## 9 9 0.912 0.831 -1.11e-16
## 10 10 0.884 0.781 4.44e-16
## 11 11 0.879 0.772 -2.22e-16
## 12 12 0.920 0.847 -2.22e-16
ggplot(data.frame(month = 1:12, r_squared = r), aes(x = month, y = r_squared)) + geom_point() + geom_line() + scale_x_continuous(breaks = 1:12)

Question 5
a) See below! Yes the beaufort scale is the same across 4 seasons
except the large spike during Summer. Spring and Winter have the most
common Beaufort scale. It is comparatively weaker
b) Yes, beaufort scale 6 or higher are considered to be above a
“storm breeze” approaching “hurricane force”. Summer and Fall are the
least likely to have strong winds.
c) According to the plot (See below!) Temperature is on average
higher during hours with strong wind compared to other hours.
clean <- weather %>%
filter(wind_speed < 1000, !is.na(wind_speed), !is.na(wind_dir)) %>%
mutate(
season = case_when(
month %in% c(12, 1, 2) ~ "Winter",
month %in% c(3, 4, 5) ~ "Spring",
month %in% c(6, 7, 8) ~ "Summer",
TRUE ~ "Fall"
),
wind_cardinal = case_when(
wind_dir > 337.5 | wind_dir <= 22.5 ~ "N",
between(wind_dir, 22.5, 67.5) ~ "NE",
between(wind_dir, 67.5, 112.5) ~ "E",
between(wind_dir, 112.5, 157.5) ~ "SE",
between(wind_dir, 157.5, 202.5) ~ "S",
between(wind_dir, 202.5, 247.5) ~ "SW",
between(wind_dir, 247.5, 292.5) ~ "W",
TRUE ~ "NW"
),
beaufort = case_when(
wind_speed < 1 ~ 0,
between(wind_speed, 1, 3) ~ 1,
between(wind_speed, 4, 7) ~ 2,
between(wind_speed, 8, 12) ~ 3,
between(wind_speed, 13, 18) ~ 4,
between(wind_speed, 19, 24) ~ 5,
between(wind_speed, 25, 31) ~ 6,
between(wind_speed, 32, 38) ~ 7,
between(wind_speed, 39, 46) ~ 8,
between(wind_speed, 47, 54) ~ 9,
TRUE ~ 10
), wind_type = case_when(beaufort >= 6 ~ "Strong", TRUE ~ "Normal"))
ggplot(clean, aes(x = season, fill = as.factor(beaufort))) + geom_bar(position = "dodge")

clean %>% group_by(season) %>% summarize(y = mean(wind_type == "Strong") * 100) %>% arrange(desc(y))
## # A tibble: 4 × 2
## season y
## <chr> <dbl>
## 1 Spring 17.3
## 2 Winter 16.2
## 3 Fall 15.4
## 4 Summer 14.0
clean %>% group_by(wind_cardinal) %>% summarize(y = mean(wind_type == "Strong") * 100) %>% arrange(y)
## # A tibble: 8 × 2
## wind_cardinal y
## <chr> <dbl>
## 1 N 10.7
## 2 SW 13.0
## 3 SE 14.0
## 4 S 16.4
## 5 NE 16.9
## 6 W 17.0
## 7 E 18.4
## 8 NW 19.9
ggplot(clean, aes(x = wind_type, y = temp, fill = wind_type)) + geom_boxplot()
## Warning: Removed 1 row containing non-finite outside the scale range
## (`stat_boxplot()`).

clean %>% group_by(wind_type) %>% summarize(avg_temp = mean(temp, na.rm = TRUE))
## # A tibble: 2 × 2
## wind_type avg_temp
## <chr> <dbl>
## 1 Normal 55.4
## 2 Strong 53.5