library(tidyverse)

# Example - GSS Survey Data - Couch Potatoes: Married vs Not Married
########################################################################################

gss_survey <- read_csv("http://www.cknuckles.com/statsprogramming/data/gss_survey_data.csv")

gss_survey |>
  filter(year == "2018" & status == "Married") |>
  drop_na(tvhours,status) |>
  summarize(
    married_potatoes = sum(tvhours >= 3),
    n1 = n(),
    p_hat1 = married_potatoes/n1   # proportion of couch potatoes
  )

gss_survey |>
  filter(year == "2018" & status != "Married") |>
  drop_na(tvhours,status) |>
  summarize(
    non_married_potatoes = sum(tvhours >= 3),
    n2 = n(),
    p_hat2 = non_married_potatoes/n2   # proportion of couch potatoes
  )

# Combined Sampling Error
SE <- sqrt((.291*(1-.291))/392 + (.383*(1-.383))/441)
# Multiplier for 95% CI
z_alpha2 = 1.96
# CI
c((.291-.383) - z_alpha2*SE , (.291-.383) + z_alpha2*SE)

# Reversing the order of the sample proportions doesn't change the conclusion.
c((.383-.291) - z_alpha2*SE , (.383-.291) + z_alpha2*SE)    # 95% CI
