library(tidyverse)

# Example - GSS Survey Data - Proportion of Couch Potatoes (tvhours >= 3)
########################################################################################

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

tvhours_2018 <- gss_survey |>
  filter(year == "2018") |>
  select(tvhours) |>
  drop_na(tvhours)
tvhours_2018

ggplot(data = tvhours_2018, mapping = aes(x = tvhours)) +
  geom_histogram( bins=15, color = "white")

summary(tvhours_2018$tvhours)

tvhours_2018 |>
  summarize(
    num_couch_potatoes = sum(tvhours >= 3),
    n = n(),
    p_hat = num_couch_potatoes/n   # proportion of couch potatoes
  )

# 99% CI
SE <- sqrt((.340*(1-.340))/833)
SE

c(.340-2.58*SE, .340+2.58*SE)


# Example - Political Poll: 55% of 1000 Likely Voters Support the Candidate
########################################################################################

SE <- sqrt((.55*(1-.55))/1000)

poll_margin_of_error <- (1.96*SE)  # Half of width of the 95% CI
poll_margin_of_error
