APSTA-GE 2006: Applied Statistics for Social Science Research
\[ \DeclareMathOperator{\E}{\mathbb{E}} \DeclareMathOperator{\P}{\mathbb{P}} \DeclareMathOperator{\V}{\mathbb{V}} \DeclareMathOperator{\L}{\mathscr{L}} \DeclareMathOperator{\I}{\text{I}} \]

“The purrr::map* functions transform their input by applying a function to each element of a list or atomic vector and returning an object of the same length as the input.”
min = 1, the second has min = 2, and so on through min = 10; all have max = 15samples and means. How do the two objects differ, and what did you expect to see?pivot_longer() “lengthens” data, increasing the number of rows and decreasing the number of columns. The inverse transformation is pivot_wider().
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
# A tibble: 6 × 3
Species length_width measure
<fct> <chr> <dbl>
1 setosa Sepal.Length 5.1
2 setosa Sepal.Width 3.5
3 setosa Petal.Length 1.4
4 setosa Petal.Width 0.2
5 setosa Sepal.Length 4.9
6 setosa Sepal.Width 3
[1] "list"
[1] 105.1271 106.8939 108.6904 110.5171
| 1 | 2 | 3 |
|---|---|---|
| 105.1271 | 110.5171 | 116.1834 |
| 106.8939 | 114.2631 | 122.1403 |
| 108.6904 | 118.1360 | 128.4025 |
| 110.5171 | 122.1403 | 134.9859 |
| 112.3745 | 126.2802 | 141.9068 |
| 114.2631 | 130.5605 | 149.1825 |
| 116.1834 | 134.9859 | 156.8312 |
| 118.1360 | 139.5612 | 164.8721 |
| 120.1215 | 144.2917 | 173.3253 |
| 122.1403 | 149.1825 | 182.2119 |
map() functions.purrr::map() functions instead of R’s *apply().rates <- seq(0.05, 0.20, length = 10)
P <- 100
time <- seq(1, 50, length = 50)
Pe <- function(A, r, t) A * exp(r * t)
time |> map(\(x) Pe(A = P, r = rates, t = x))
# above is a shortcut for
map(time, function(x) Pe(A = P, r = rates, t = x))
# and the above is a shortcut for the following loop
l <- list()
for (i in seq_along(time)) {
l[[i]] <- Pe(A = P, r = rates, t = time[i])
}| rate | year | value |
|---|---|---|
| 0.05 | 1 | 105.1271 |
| 0.05 | 2 | 110.5171 |
| 0.05 | 3 | 116.1834 |
| 0.05 | 4 | 122.1403 |
| 0.05 | 5 | 128.4025 |
| 0.05 | 6 | 134.9859 |
| 0.05 | 7 | 141.9068 |
| 0.05 | 8 | 149.1825 |
| 0.05 | 9 | 156.8312 |
| 0.05 | 10 | 164.8721 |

RStudio cheat sheets

install.packages("HistData")library(HistData)?Arbuthnot| Year | Males | Females | Plague | Mortality | Ratio | Total |
|---|---|---|---|---|---|---|
| 1629 | 5218 | 4683 | 0 | 8771 | 1.114243 | 9.901 |
| 1630 | 4858 | 4457 | 1317 | 10554 | 1.089971 | 9.315 |
| 1631 | 4422 | 4102 | 274 | 8562 | 1.078011 | 8.524 |
| 1632 | 4994 | 4590 | 8 | 9535 | 1.088017 | 9.584 |
| 1633 | 5158 | 4839 | 0 | 8393 | 1.065923 | 9.997 |
| 1634 | 5035 | 4820 | 1 | 10400 | 1.044606 | 9.855 |



Image source: Calculus Volume 1
OpenStax: Here is a more complete list
\[ \begin{aligned} \int_a^b [f(x)+g(x)]\,dx &= \int_a^b f(x)\,dx + \int_a^b g(x)\,dx, \\ \int_a^b c f(x)\,dx &= c\int_a^b f(x)\,dx. \end{aligned} \]
Unlike differentiation, integration has no universal procedure that produces a closed-form antiderivative for every function
Many common integrals have closed-form analytical solutions, but many important integrals in statistics do not
For well-behaved integrands in one or two dimensions, numerical quadrature is often practical
Higher-dimensional problems may use Monte Carlo, importance sampling, quasi-Monte Carlo, or Markov chain Monte Carlo (MCMC)
For simple integrals, we can sometimes find a closed-form solution using substitution or integration by parts


Notice that the function takes another function as an argument. Functions that do this are called higher-order functions.
Source: OpenStax Calculus Volume 1
\[ \begin{aligned} \int x\sin(x^2)\,dx &= \tfrac{1}{2}\int \sin(u)\,du \\ &= -\tfrac{1}{2}\cos(u)+C \\ &= -\tfrac{1}{2}\cos(x^2)+C. \end{aligned} \]
When in doubt, you can always try WolframAlpha
The Python library SymPy can be used through the R package caracas
A computer algebra system returns one antiderivative and usually omits the arbitrary constant \(+C\)
\[\begin{align} \int \frac{x^{2}}{\sqrt{x^{2} + 4}}\, dx = \end{align}\]
\[\begin{align} \frac{x \sqrt{x^{2} + 4}}{2} - 2 \operatorname{asinh}{\left(\frac{x}{2} \right)} \end{align}\]
\[ f(x)= \begin{cases} \lambda e^{-\lambda x}, & x\ge 0, \\ 0, & x<0. \end{cases} \]
\[ \int_0^\infty \lambda e^{-\lambda x}\,dx = 1. \]
\[ \P(X>s+t\mid X>s)=\P(X>t)=e^{-\lambda t}. \]
The product rule gives a useful integration identity:
\[ \begin{aligned} (fg)' &= f'g + fg', \\ \int f(x)g'(x)\,dx &= f(x)g(x)-\int f'(x)g(x)\,dx, \\ \int u\,dv &= uv-\int v\,du. \end{aligned} \]
For example, integration by parts gives the mean waiting time:
\[ \begin{aligned} \E[X] &= \int_0^\infty x\lambda e^{-\lambda x}\,dx \\ &= \left[-xe^{-\lambda x}\right]_0^\infty + \int_0^\infty e^{-\lambda x}\,dx \\ &= \frac{1}{\lambda}. \end{aligned} \]
The exponential-growth function used earlier solves the following differential equation. Assume \(y(t)>0\):
\[ \frac{\text{d}[y(t)]}{\text{d}t} = k \cdot y(t) \]
We can now solve it:
\[ \begin{align*} \frac{1}{y} \, \text{d}y &= k \, \text{d}t \\ \int \frac{1}{y} \, \text{d}y &= \int k \, \text{d}t \\ \log(y) &= k \cdot t + C \\ y(t) &= y_0 \cdot e^{kt}, \, y_0 = e^C \end{align*} \]
Newton’s second law relates force to the rate of change of momentum:
\[ \begin{aligned} F &= ma \\ &= \frac{d}{dt}\left(m\frac{d[x(t)]}{dt}\right) = \frac{d}{dt}\left(mv(t)\right)\\ &= mg. \end{aligned} \]
Because \(m\) is constant, take it outside the derivative and cancel it from both sides:
\[ m\frac{d^2[x(t)]}{dt^2}=mg \quad\Longrightarrow\quad \frac{d^2[x(t)]}{dt^2}=g. \]
Integrate twice with respect to \(t\):
\[ \begin{aligned} \int \frac{d^2[x(t)]}{dt^2}\,dt &= \int g\,dt, \\ \frac{d[x(t)]}{dt} &= gt+C_1, \\[0.6em] \int \frac{d[x(t)]}{dt}\,dt &= \int (gt+C_1)\,dt, \\ x(t) &= \tfrac{1}{2}gt^2+C_1t+C_2. \end{aligned} \]
The ball is released from rest, and distance is measured from the release point, so
\[ \left.\frac{d[x(t)]}{dt}\right|_{t=0}=0 \Longrightarrow C_1=0, \qquad x(0)=0 \Longrightarrow C_2=0. \]
Therefore,
\[ \boxed{x(t)=\tfrac{1}{2}gt^2}. \]

Using the accepted value \(g_{\text{Moon}}=1.625\ \text{m/s}^2\), the ball reaches the bottom when \(x(t)=100\) m:
\[ \begin{aligned} 100 &= \tfrac{1}{2}g_{\text{Moon}}t^2, \\ t_{\text{impact}} &= \sqrt{\frac{2(100)}{g_{\text{Moon}}}} \\ &\approx 11.1\ \text{s}. \end{aligned} \]
The free-fall model describes the motion only until impact.
iris dataset (?iris)Sepal.Length, Sepal.Width, Petal.Length, and Petal.WidthSpecies (hint: use group_by() and summarise() from dplyr)
geom_density() and facet_wrap()):
\[ \int 2x \cos(x^2)\, dx \]
integrate() function to validate your answer.