Skip to content

Commit

Permalink
run all code through styler for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherkenny committed Jul 5, 2024
1 parent fff3419 commit 76f5bba
Show file tree
Hide file tree
Showing 20 changed files with 830 additions and 809 deletions.
41 changes: 21 additions & 20 deletions 03_limits.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ means <- cumsum(Xs) / 1:n
ggplot(tibble(n = 1:n, estimate = means), aes(x = n, y = estimate)) +
geom_line() +
scale_x_continuous(labels = comma, limit = c(0, n*1.1), breaks = seq(0, n, length.out = 5)) +
scale_x_continuous(labels = comma, limit = c(0, n * 1.1), breaks = seq(0, n, length.out = 5)) +
scale_y_continuous(limits = c(0, 1)) +
annotate(geom = "text", x = 1.1*n, y = means[n], label = glue("Estimate at\nn = {comma(n)}", ":\n", means[n]), size = 2.8) +
annotate(geom = "text", x = 1.1 * n, y = means[n], label = glue("Estimate at\nn = {comma(n)}", ":\n", means[n]), size = 2.8) +
annotate(geom = "point", x = n, y = means[n]) +
labs(x = "n, or the number of times of a coin-flip experiment",
y = "Estimate of the Probability of Heads after n trials")
labs(
x = "n, or the number of times of a coin-flip experiment",
y = "Estimate of the Probability of Heads after n trials"
)
```

## Sequences
Expand Down Expand Up @@ -90,16 +92,16 @@ We find the sequence by simply "plugging in" the integers into each $n$. The imp
#| echo: false
#| fig-cap: Behavior of Some Sequences
seq <- 1:20
df <- tibble(n = seq, A = 2 - 1/(seq^2), B = (seq^2 + 1)/(seq), C = (-1)^seq * (1 - 1/seq))
df <- tibble(n = seq, A = 2 - 1 / (seq^2), B = (seq^2 + 1) / (seq), C = (-1)^seq * (1 - 1 / seq))
g0 <- ggplot(df, aes(x = n, y = A)) + geom_point()
g0 <- ggplot(df, aes(x = n, y = A)) +
geom_point()
gA <- g0 + labs(y = expression(A[n]))
gB <- g0 + aes(y = B) + labs(y = expression(B[n]))
gC <- g0 + aes(y = C) + labs(y = expression(C[n]))
gA + gB + gC + plot_layout(nrow = 1)
```

## The Limit of a Sequence
Expand Down Expand Up @@ -230,13 +232,13 @@ range2 <- tibble::tibble(x = c(-2, 2))
fx1 <- ggplot(range1, aes(x = x)) +
stat_function(fun = function(x) sqrt(x), size = 0.5) +
labs(x = expression(x), y = expression(f(x)), title = expression(f(x)==sqrt(x))) +
labs(x = expression(x), y = expression(f(x)), title = expression(f(x) == sqrt(x))) +
expand_limits(y = max(range1$x))
fx2 <-
fx2 <-
ggplot(range2, aes(x = x)) +
stat_function(fun = function(x) ifelse(x == 0, NA, 1/x), size = 0.5) +
labs(x = expression(x), y = expression(f(x)), title = expression(f(x)==frac(1,x)))
stat_function(fun = function(x) ifelse(x == 0, NA, 1 / x), size = 0.5) +
labs(x = expression(x), y = expression(f(x)), title = expression(f(x) == frac(1, x)))
fx1 + fx2 + plot_layout(nrow = 1)
```
Expand Down Expand Up @@ -282,21 +284,21 @@ range4 <- tibble(x = c(0, 5))
fx1 <- ggplot(range1, aes(x = x)) +
stat_function(fun = function(x) sqrt(x), size = 0.5) +
labs(y = expression(f(x)), title = expression(f(x)==sqrt(x)))
labs(y = expression(f(x)), title = expression(f(x) == sqrt(x)))
fx2 <- ggplot(range2, aes(x = x)) +
stat_function(fun = function(x) exp(x), size = 0.5) +
labs(y = expression(f(x)), title = expression(f(x)==e^x))
labs(y = expression(f(x)), title = expression(f(x) == e^x))
fx3 <- ggplot(range3, aes(x = x)) +
stat_function(fun = function(x) ifelse(x == 0, NA, 1 + 1/(x^2)), size = 0.5) +
labs(y = expression(f(x)), title = expression(f(x)==1+frac(1,x^2)))
stat_function(fun = function(x) ifelse(x == 0, NA, 1 + 1 / (x^2)), size = 0.5) +
labs(y = expression(f(x)), title = expression(f(x) == 1 + frac(1, x^2)))
fx4 <- ggplot(range4, aes(x = x)) +
stat_function(fun = function(x) ifelse(1 - (x %% 1) < 9e-2, NA, floor(x)), size = 0.5) +
geom_point(data = tibble(x = 0:5, y = 0:5), aes(y = y), pch = 19) +
geom_point(data = tibble(x = 1:5, y = (1:5) - 1), aes(y = y), pch = 21, fill = "white") +
labs(y = expression(f(x)), title = expression(f(x)==plain("floor(x)")))
labs(y = expression(f(x)), title = expression(f(x) == plain("floor(x)")))
fx1 + fx2 + fx3 + fx4 + plot_layout(ncol = 2)
```
Expand Down Expand Up @@ -329,7 +331,6 @@ Example @exm-seqbehav
Exercise @exr-limseq2

```{r}
```

Example @exm-limfun1
Expand Down Expand Up @@ -370,7 +371,7 @@ Divide each part by $x$, and we get $x + \frac{2}{x}$ on the numerator, $1$ on t
range0 <- tibble(x = c(-4, 2))
ggplot(range0, aes(x = x)) +
stat_function(fun = function(x) ifelse(x == 0, NA, (x^2 + 2*x)/(x)), size = 0.5) +
labs(y = expression(f(x)), title = expression(f(x)==frac(x^2+2*x, x^2))) +
geom_point(data = tibble(x = 0, y = 2), aes(y = y), pch = 21, fill = "white")
stat_function(fun = function(x) ifelse(x == 0, NA, (x^2 + 2 * x) / (x)), size = 0.5) +
labs(y = expression(f(x)), title = expression(f(x) == frac(x^2 + 2 * x, x^2))) +
geom_point(data = tibble(x = 0, y = 2), aes(y = y), pch = 21, fill = "white")
```
58 changes: 31 additions & 27 deletions 04_calculus.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@ range <- tibble::tibble(x = c(-3, 3))
fx0 <- ggplot(range, aes(x = x)) +
labs(x = expression(x), y = expression(f(x)))
fx <- fx0 +
stat_function(fun = function(x) 2*x, size = 0.5) +
fx <- fx0 +
stat_function(fun = function(x) 2 * x, size = 0.5) +
labs(title = "f(x) = 2x") +
expand_limits(y = 0)
fprimex <- fx0 + stat_function(fun = function(x) 2, size = 0.5, linetype = "dashed") +
labs(x = expression(x), y = expression(f~plain("'")~(x)))
labs(x = expression(x), y = expression(f ~ plain("'") ~ (x)))
gx <- fx0 +
gx <- fx0 +
stat_function(fun = function(x) x^3, size = 0.5) +
labs(y = expression(g(x)), title = expression(g(x)==x^3)) +
labs(y = expression(g(x)), title = expression(g(x) == x^3)) +
expand_limits(y = 0)
gprimex <- fx0 + stat_function(fun = function(x) 3*(x^2), size = 0.5, linetype = "dashed") +
labs(x = expression(x), y = expression(g~plain("'")~(x)))
gprimex <- fx0 + stat_function(fun = function(x) 3 * (x^2), size = 0.5, linetype = "dashed") +
labs(x = expression(x), y = expression(g ~ plain("'") ~ (x)))
fx + fprimex + gx + gprimex + plot_layout(ncol = 2)
```
Expand Down Expand Up @@ -246,15 +246,17 @@ To repeat the main rule in Theorem @thm-derivexplog, the intuition is that
range <- tibble::tibble(x = c(-3, 3))
fx0 <- ggplot(range, aes(x = x)) +
labs(x = expression(x), y = expression(f(x)),
caption = expression(f(x)==e^x))
labs(
x = expression(x), y = expression(f(x)),
caption = expression(f(x) == e^x)
)
fx <- fx0 +
fx <- fx0 +
stat_function(fun = function(x) exp(x), size = 0.5) +
expand_limits(y = 0)
fprimex <- fx0 + stat_function(fun = function(x) exp(x), size = 0.5) +
labs(x = expression(x), y = expression(f~plain("'")~(x)))
labs(x = expression(x), y = expression(f ~ plain("'") ~ (x)))
fx + fprimex + plot_layout(nrow = 1)
```
Expand Down Expand Up @@ -286,15 +288,17 @@ The natural log is the mirror image of the natural exponent and has mirroring pr
range <- tibble::tibble(x = c(-0.1, 3))
fx0 <- ggplot(range, aes(x = x)) +
labs(x = expression(x), y = expression(f(x)),
caption = expression(f(x)==log(x)))
labs(
x = expression(x), y = expression(f(x)),
caption = expression(f(x) == log(x))
)
fx <- fx0 +
fx <- fx0 +
stat_function(fun = function(x) ifelse(x <= 0, NA, log(x)), size = 0.5) +
expand_limits(y = 0)
fprimex <- fx0 + stat_function(fun = function(x) ifelse(x <= 0, NA, 1/x), size = 0.5) +
labs(x = expression(x), y = expression(f~plain("'")~(x)))
fprimex <- fx0 + stat_function(fun = function(x) ifelse(x <= 0, NA, 1 / x), size = 0.5) +
labs(x = expression(x), y = expression(f ~ plain("'") ~ (x)))
fx + fprimex + plot_layout(nrow = 1)
```
Expand Down Expand Up @@ -473,10 +477,10 @@ fx <- ggplot(range1, aes(x = x)) +
labs(y = expression(f(x)))
Fx <- ggplot(range1, aes(x = x)) +
stat_function(fun = function(x) (x^3)/3 - 4*x, size = 0.5, linetype = "dashed") +
stat_function(fun = function(x) (x^3)/3 - 4*x + 1, size = 0.5, linetype = "dotted") +
stat_function(fun = function(x) (x^3)/3 - 4*x - 1, size = 0.5, linetype = "dotdash") +
labs(y = expression(integral(f(x)*dx)))
stat_function(fun = function(x) (x^3) / 3 - 4 * x, size = 0.5, linetype = "dashed") +
stat_function(fun = function(x) (x^3) / 3 - 4 * x + 1, size = 0.5, linetype = "dotted") +
stat_function(fun = function(x) (x^3) / 3 - 4 * x - 1, size = 0.5, linetype = "dotdash") +
labs(y = expression(integral(f(x) * dx)))
fx + Fx + plot_layout(ncol = 1)
```
Expand Down Expand Up @@ -516,23 +520,23 @@ Suppose we want to determine the area $A(R)$ of a region $R$ defined by a curve
#| label: fig-defintfig
#| echo: false
#| fig-cap: The Riemann Integral as a Sum of Evaluations
f3 <- function(x) -15*(x - 5) + (x - 5)^3 + 50
f3 <- function(x) -15 * (x - 5) + (x - 5)^3 + 50
d1 <- tibble(x = seq(0, 10, 1)) %>% mutate(f = f3(x))
d2 <- tibble(x = seq(0, 10, 0.1)) %>% mutate(f = f3(x))
d1 <- tibble(x = seq(0, 10, 1)) %>% mutate(f = f3(x))
d2 <- tibble(x = seq(0, 10, 0.1)) %>% mutate(f = f3(x))
range <- tibble::tibble(x = c(0, 10))
fx0 <- ggplot(range, aes(x = x)) +
labs(x = expression(x), y = expression(f(x)))
fx <- fx0 +
expand_limits(y = 0) +
fx <- fx0 +
expand_limits(y = 0) +
scale_y_continuous(expand = c(0, 0)) +
scale_x_continuous(expand = c(0, 0))
g1 <- fx + geom_col(data = d1, aes(x, f), width = 1, fill = "gray", alpha = 0.5, color = "black") + stat_function(fun = f3, size = 1.5) + labs(title = "Evaluating f with width = 1 intervals")
g2 <- fx + geom_col(data = d2, aes(x, f), width = 0.1, fill = "gray", alpha = 0.5, color = "black") + stat_function(fun = f3, size = 1.5) +labs(title = "Evaluating f with width = 0.1 intervals")
g1 <- fx + geom_col(data = d1, aes(x, f), width = 1, fill = "gray", alpha = 0.5, color = "black") + stat_function(fun = f3, size = 1.5) + labs(title = "Evaluating f with width = 1 intervals")
g2 <- fx + geom_col(data = d2, aes(x, f), width = 0.1, fill = "gray", alpha = 0.5, color = "black") + stat_function(fun = f3, size = 1.5) + labs(title = "Evaluating f with width = 0.1 intervals")
g1 + g2 + plot_layout(nrow = 1)
```
Expand Down
22 changes: 12 additions & 10 deletions 05_optimization.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,16 @@ So for example, $f(x) = x^2 + 2$ and $f^\prime(x) = 2x$
#| fig-cap: Maxima and Minima
range <- tibble::tibble(x = c(-3, 3))
fx0 <- ggplot(range, aes(x = x)) +
labs(x = expression(x), y = expression(f(x)),
labs = expression(f(x)==x^2+2))
fx <- fx0 +
labs(
x = expression(x), y = expression(f(x)),
labs = expression(f(x) == x^2 + 2)
)
fx <- fx0 +
stat_function(fun = function(x) x^2 + 2, linewidth = 0.5) +
expand_limits(y = 0)
fprimex <- fx0 + stat_function(fun = function(x) 2*x, linewidth = 0.5, linetype = "dashed") +
labs(x = expression(x), y = expression(f~plain("'")~(x)))
fprimex <- fx0 + stat_function(fun = function(x) 2 * x, linewidth = 0.5, linetype = "dashed") +
labs(x = expression(x), y = expression(f ~ plain("'") ~ (x)))
fx + fprimex + plot_layout(nrow = 1)
```
Expand All @@ -117,14 +119,14 @@ fx0 <- ggplot(range, aes(x = x)) +
labs(x = expression(x), y = expression(f(x))) +
theme_bw()
fx <- fx0 +
fx <- fx0 +
stat_function(fun = function(x) x^3 + x^2 + 2, size = 0.5) +
expand_limits(y = 0)
fprimex <- fx0 + stat_function(fun = function(x) 3*x^2, size = 0.5, linetype = "dashed") +
labs(x = expression(x), y = expression(f~plain("'")~(x)))
fprimex <- fx0 + stat_function(fun = function(x) 3 * x^2, size = 0.5, linetype = "dashed") +
labs(x = expression(x), y = expression(f ~ plain("'") ~ (x)))
fx + fprimex + plot_annotation(expression(f(x)==x^3+2))
fx + fprimex + plot_annotation(expression(f(x) == x^3 + 2))
```

The second derivative $f''(x)$ identifies whether the function $f(x)$ at the point $x$ is
Expand Down Expand Up @@ -179,7 +181,7 @@ A function $f$ is strictly concave over the set S \underline{if} $\forall x_1,x_
range1 <- tibble(x = c(-4, 4))
fx1 <- ggplot(range1, aes(x = x)) +
stat_function(fun = function(x) -x^2, size = 0.5) +
labs(y = expression(f(x)), title = "Concave")
labs(y = expression(f(x)), title = "Concave")
fx2 <- ggplot(range1, aes(x = x)) +
stat_function(fun = function(x) x^2, size = 0.5) +
labs(y = expression(f(x)), title = "Convex")
Expand Down
7 changes: 3 additions & 4 deletions 06_probability.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Consider subsets A {2, 8} and B {2,3,7} of the sample space you found. What is
#| echo: false
#| fig-cap: Probablity as a Measure^[Images of Probability and Random Variables drawn
#| by Shiro Kuriwaki and inspired by Blitzstein and Morris]
knitr::include_graphics('images/probability.png')
knitr::include_graphics("images/probability.png")
```

### Probability Definitions: Formal and Informal {.unnumbered}
Expand Down Expand Up @@ -324,7 +324,7 @@ Most questions in the social sciences involve events, rather than numbers per se
#| label: fig-rv-image
#| echo: false
#| fig-cap: The Random Variable as a Real-Valued Function
knitr::include_graphics('images/rv.png')
knitr::include_graphics("images/rv.png")
```

::: {#def-rv}
Expand Down Expand Up @@ -757,14 +757,13 @@ range <- tibble::tibble(x = c(-5, 5))
fx0 <- ggplot(range, aes(x = x)) +
labs(x = expression(x), y = expression(f(x)))
fx <- fx0 +
fx <- fx0 +
stat_function(fun = function(x) dnorm(x, mean = 0, sd = 1), linewidth = 0.5) +
stat_function(fun = function(x) dnorm(x, mean = 0, sd = sqrt(2)), linewidth = 1.5) +
expand_limits(y = 0) +
labs(caption = "Thick line: variance = 2, Normal line: variance = 1")
fx
```

## Summarizing Observed Events (Data)
Expand Down
32 changes: 18 additions & 14 deletions 11_data-handling_counting.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ R is an object oriented programming language primarily used for statistical comp
- Different objects have different allowable procedures:

```{r}
# Adding a string and a string does not work because the '+' operator
# Adding a string and a string does not work because the '+' operator
# does not work for strings:
# 'Harvard' + 'Gov'
Expand All @@ -114,7 +114,7 @@ R is an object oriented programming language primarily used for statistical comp
x <- 9
class(9)
class(x)
class('Harvard')
class("Harvard")
```

Object oriented programming makes languages flexible and powerful:
Expand Down Expand Up @@ -143,7 +143,6 @@ n_pb <- 3
n_jelly <- 9
# write instructions in R here
```

## Base-R vs. tidyverse
Expand Down Expand Up @@ -271,7 +270,8 @@ dim(ober)
From your tutorials, you also know how to do graphics! Graphics are useful for grasping your data, but we will cover them more deeply in Chapter @sec-dataviz.

```{r}
ggplot(ober, aes(x = Fame)) + geom_histogram()
ggplot(ober, aes(x = Fame)) +
geom_histogram()
```

What about the distribution of fame by regime?
Expand Down Expand Up @@ -312,10 +312,12 @@ First get a map of the Greek world.
#| message: false
#| warning: false
#| eval: false
greece <- get_map(location = c(lon = 22.6382849, lat = 39.543287),
zoom = 5,
source = "stamen",
maptype = "toner")
greece <- get_map(
location = c(lon = 22.6382849, lat = 39.543287),
zoom = 5,
source = "stamen",
maptype = "toner"
)
ggmap(greece)
```

Expand All @@ -329,12 +331,14 @@ Ober's data has the latitude and longitude of each polis. Because the map of Gre
#| warning: false
#| eval: false
gg_ober <- ggmap(greece) +
geom_point(data = ober,
aes(y = Latitude, x = Longitude),
size = 0.5,
color = "orange")
gg_ober +
scale_x_continuous(limits = c(10, 35)) +
geom_point(
data = ober,
aes(y = Latitude, x = Longitude),
size = 0.5,
color = "orange"
)
gg_ober +
scale_x_continuous(limits = c(10, 35)) +
scale_y_continuous(limits = c(32, 44)) +
theme_void()
```
Expand Down
Loading

0 comments on commit 76f5bba

Please sign in to comment.