data(bessel_errors)
bessel_errors
# Total frequencies (each column should sum to 300).
colSums(bessel_errors[, c("observed", "expected")])
# Weighted mean of the absolute error, approximating the
# individual observations by the bin midpoints. Because every
# bin lower edge is at or above zero, this is the mean
# absolute error rather than the mean error itself.
wmean_obs <- with(bessel_errors,
sum(observed * midpoint) / sum(observed))
wmean_exp <- with(bessel_errors,
sum(expected * midpoint) / sum(expected))
c(observed = wmean_obs, expected_under_normal = wmean_exp)
# Weighted variance and standard deviation of the absolute
# error, using bin midpoints as plug-in values for the
# individual observations.
wvar_obs <- with(bessel_errors,
sum(observed * (midpoint - wmean_obs)^2) /
(sum(observed) - 1))
c(weighted_variance = wvar_obs,
weighted_sd = sqrt(wvar_obs))
# Side-by-side bar plot of observed and expected counts.
# Bessel's normal-model expectation tracks the empirical
# distribution closely except in the long right tail, where
# Bradley's three largest errors (counts 3, 1, 1) exceed what
# the normal model predicts.
op <- par(mar = c(5, 4, 4, 2))
barplot(rbind(bessel_errors$observed, bessel_errors$expected),
beside = TRUE,
names.arg = sprintf("%.1f-%.1f",
bessel_errors$lower,
bessel_errors$upper),
legend.text = c("Observed (Bradley)",
"Expected under normal model"),
args.legend = list(x = "topright", bty = "n"),
xlab = "Absolute error (seconds of arc)",
ylab = "Frequency",
main = "Bessel (1818) on Bradley's 300 stellar positions")
par(op)
Run the code above in your browser using DataLab