## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----eval = FALSE-------------------------------------------------------------
# library(tidycensus)
# library(dplyr)
# library(acsmoe)
# 
# vars <- paste0("B01001_0", c(20:25, 44:49))
# 
# ramsey <- get_acs(
#   geography = "tract",
#   variables = vars,
#   state = "MN",
#   county = "Ramsey",
#   year = 2016
# )
# 
# ramsey65 <- ramsey |>
#   group_by(GEOID) |>
#   summarize(
#     estimate_65plus = sum(estimate),
#     moe_65plus = acs_sum(estimate, moe)$moe,
#     .groups = "drop"
#   )

## -----------------------------------------------------------------------------
library(acsmoe)

tracts <- data.frame(
  region = c("north", "north", "south", "south"),
  population = c(1000, 1200, 900, 1100),
  population_moe = c(120, 140, 100, 130),
  households = c(420, 500, 360, 440),
  households_moe = c(60, 70, 50, 65)
)

acs_aggregate(
  tracts,
  group_var = "region",
  value_cols = c("population", "households"),
  moe_cols = c("population_moe", "households_moe")
)

## -----------------------------------------------------------------------------
estimates <- c(1000, 1200)
moes <- c(120, 140)
ses <- moe_to_se(moes)

cov_mat <- matrix(
  c(ses[1]^2, 1500,
    1500, ses[2]^2),
  nrow = 2
)

acs_sum(estimates, moes, cov = cov_mat)

## -----------------------------------------------------------------------------
acs_aggregate(
  tracts,
  group_var = "region",
  value_cols = "population",
  moe_cols = "population_moe",
  cov_strategy = "constant",
  cov_value = 0.25
)

