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

## ----setup--------------------------------------------------------------------
library(mariposa)
library(dplyr)

## -----------------------------------------------------------------------------
data(survey_data)
glimpse(survey_data)

## -----------------------------------------------------------------------------
# Find variables related to "trust"
find_var(survey_data, "trust")

## -----------------------------------------------------------------------------
# Descriptive statistics with survey weights
survey_data %>%
  describe(age, income, life_satisfaction, weights = sampling_weight)

## -----------------------------------------------------------------------------
# Frequency table
survey_data %>%
  frequency(education, weights = sampling_weight)

## -----------------------------------------------------------------------------
# Create age groups
survey_data <- rec(survey_data, age,
  rules = "18:29=1 [Young]; 30:49=2 [Middle]; 50:99=3 [Older]",
  suffix = "_group", as_factor = TRUE)

# Build a trust scale
survey_data <- survey_data %>%
  mutate(m_trust = row_means(., trust_government, trust_media, trust_science,
                             min_valid = 2))

## -----------------------------------------------------------------------------
# t-test with survey weights
survey_data %>%
  t_test(life_satisfaction, group = gender, weights = sampling_weight)

## -----------------------------------------------------------------------------
# ANOVA across education levels
result <- survey_data %>%
  oneway_anova(life_satisfaction, group = education, weights = sampling_weight)
result

## -----------------------------------------------------------------------------
summary(result, descriptives = FALSE)

## -----------------------------------------------------------------------------
# Which education groups differ?
tukey_test(result)

## -----------------------------------------------------------------------------
survey_data %>%
  pearson_cor(age, income, life_satisfaction, weights = sampling_weight)

## -----------------------------------------------------------------------------
survey_data %>%
  linear_regression(life_satisfaction ~ age + income + m_trust,
                    weights = sampling_weight)

## -----------------------------------------------------------------------------
result <- survey_data %>%
  t_test(life_satisfaction, group = gender, weights = sampling_weight)

# Compact
result

# Detailed
summary(result)

# Detailed, skip effect sizes
summary(result, effect_sizes = FALSE)

## -----------------------------------------------------------------------------
survey_data %>%
  group_by(region) %>%
  describe(income, life_satisfaction, weights = sampling_weight)

## -----------------------------------------------------------------------------
survey_data %>%
  group_by(region) %>%
  t_test(income, group = gender, weights = sampling_weight)

