[1] 5.16
[1] 0.15
APSTA-GE 2006: Applied Statistics for Social Science Research
The material for this session is based on Chapter 4 of the Regression and Other Stories by Gelman et al.
\[ \DeclareMathOperator{\E}{\mathbb{E}} \DeclareMathOperator{\P}{\mathbb{P}} \DeclareMathOperator{\V}{\mathbb{V}} \DeclareMathOperator{\L}{\mathscr{L}} \DeclareMathOperator{\I}{\text{I}} \]

\[ t_i=\alpha+\sqrt{\frac{2x_i}{g}}+\epsilon_i =\alpha+\beta\sqrt{x_i}+\epsilon_i, \qquad \beta=\sqrt{\frac{2}{g}}. \]

(*) A May 2023 Gallup poll found that 71% of Americans thought same-sex marriage should be legally recognized
Assume the conservative value \(p=0.5\). In a national survey, how large must \(n\) be so that \(\text{se}(\hat p)\) is at most:
\[ \text{se}_{\text{diff}} = \sqrt{\text{se}_1^2 + \text{se}_2^2} \]
Each drop gives 20 noisy time readings at fixed distance marks, and each drop costs oxygen. Here we omit the delay term and impose the physical constraint \(t(0)=0\). We repeatedly fit \(t=\beta\sqrt{x}+\varepsilon\) through the origin and transform \(\hat g=2/\hat\beta^2\).
set.seed(1)
g_true <- 1.625
timing_sd <- 0.10 # random timing noise, seconds
reps <- 500
marks <- seq(5, 100, by = 5) # fixed marks every 5 m
sqrt_marks <- sqrt(marks)
beta_true <- sqrt(2 / g_true)
se_ghat <- function(n_drops) {
z <- rep(sqrt_marks, n_drops) # fixed design values
g_hat <- replicate(reps, {
t_obs <- beta_true * z +
rnorm(length(z), 0, timing_sd)
beta_hat <- coef(lm(t_obs ~ 0 + z))[["z"]]
2 / beta_hat^2
})
sd(g_hat) # empirical SE of g_hat
} drops readings se_g half_width
1 1 20 0.0089 0.0175
2 2 40 0.0061 0.0119
3 3 60 0.0054 0.0107
4 5 100 0.0040 0.0079
5 10 200 0.0029 0.0057
6 12 240 0.0026 0.0052
7 15 300 0.0025 0.0048
8 20 400 0.0020 0.0039
9 40 800 0.0014 0.0027
readings is 20 times dropsse_g: empirical standard deviation of the 500 \(\hat g\) valueshalf_width: \(1.96\,\text{se}_g\), an approximate 95% margin; smaller is more precisehalf_width \(\leq0.05\) (half of 0.1); two decimals requires half_width \(\leq0.005\) (half of 0.01)# Generate fake data
p <- 0.3
n <- 20
data <- rbinom(1, n, p)
print(data)
# Estimate proportion and calculate confidence interval
p_hat <- data / n
se <- sqrt(p_hat * (1 - p_hat) / n)
ci <- p_hat + c(-2, 2) * se
print(ci)
# Put it in a loop
reps <- 100
for (i in 1:reps) {
data <- rbinom(1, n, p)
p_hat <- data / n
se <- sqrt(p_hat * (1 - p_hat) / n)
ci <- p_hat + c(-2, 2) * se
print(ci)
}# Read data from here: https://github.com/avehtari/ROS-Examples
library("foreign")
library("dplyr")
pew_pre <- read.dta(
paste0(
"https://raw.githubusercontent.com/avehtari/",
"ROS-Examples/master/Pew/data/",
"pew_research_center_june_elect_wknd_data.dta"
)
)
pew_pre <- pew_pre |> select(c("age", "regicert")) %>%
na.omit() |> filter(age != 99)
n <- nrow(pew_pre)
# Estimate a proportion (certain to have registered for voting?)
registered <- ifelse(pew_pre$regicert == "absolutely certain", 1, 0)
p_hat <- mean(registered)
se_hat <- sqrt((p_hat * (1 - p_hat)) / n)
round(p_hat + c(-2, 2) * se_hat, 4) # ci
# Estimate an average (mean age)
age <- pew_pre$age
y_hat <- mean(age)
se_hat <- sd(age) / sqrt(n)
round(y_hat + c(-2, 2) * se_hat, 4) # ci
# Estimate a difference of means
age2 <- age[registered == 1]
age1 <- age[registered == 0]
y_2_hat <- mean(age2)
se_2_hat <- sd(age2) / sqrt(length(age2))
y_1_hat <- mean(age1)
se_1_hat <- sd(age1) / sqrt(length(age1))
diff_hat <- y_2_hat - y_1_hat
se_diff_hat <- sqrt(se_1_hat ^ 2 + se_2_hat ^ 2)
round(diff_hat + c(-2, 2) * se_diff_hat, 4) # ci# Distance from the bull's eye in cm
data <- c(8, 6, 10, 5, 18)
# Calculate the sample mean
mean_data <- mean(data)
# Calculate the standard error of the mean
se_mean <- sd(data) / sqrt(length(data))
# Degrees of freedom
df <- length(data) - 1
# Calculate the 95% and 50% confidence interval
ci_95 <- mean_data + qt(c(0.025, 0.975), df) * se_mean
ci_50 <- mean_data + qt(c(0.25, 0.75), df) * se_mean
# Output the results
mean_data |> round(2)[1] 9.4
[1] 2.32
[1] 2.97 15.83
[1] 7.69 11.11
\[ \text{p-value}(y) = \P\!\left(T(Y_\text{rep}) \geq T(y) \mid H_0\right) \]