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

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

## -----------------------------------------------------------------------------
survey_data %>%
  group_by(gender) %>%
  normality_test(age, income)

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

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

## -----------------------------------------------------------------------------
survey_data %>%
  t_test(trust_government, trust_media, trust_science,
         group = gender, weights = sampling_weight)

## -----------------------------------------------------------------------------
# Is average life satisfaction different from the scale midpoint (3)?
survey_data %>%
  t_test(life_satisfaction, mu = 3, weights = sampling_weight)

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

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

## -----------------------------------------------------------------------------
# Tukey HSD: balanced comparison of all pairs
tukey_test(result)

## -----------------------------------------------------------------------------
# Scheffe: more conservative (fewer false positives)
scheffe_test(result)

## -----------------------------------------------------------------------------
levene_test(result)

## -----------------------------------------------------------------------------
survey_data %>%
  factorial_anova(dv = income, between = c(gender, education),
                  weights = sampling_weight)

## -----------------------------------------------------------------------------
survey_data %>%
  factorial_anova(dv = life_satisfaction, between = c(gender, region),
                  weights = sampling_weight) %>%
  summary()

## -----------------------------------------------------------------------------
survey_data %>%
  ancova(dv = income, between = education, covariate = age,
         weights = sampling_weight)

## -----------------------------------------------------------------------------
survey_data %>%
  mann_whitney(political_orientation, group = region,
               weights = sampling_weight)

## -----------------------------------------------------------------------------
kw_result <- survey_data %>%
  kruskal_wallis(life_satisfaction, group = education)

kw_result

## -----------------------------------------------------------------------------
dunn_test(kw_result)

## -----------------------------------------------------------------------------
data(longitudinal_data_wide)

longitudinal_data_wide %>%
  wilcoxon_test(score_T1, score_T2)

## -----------------------------------------------------------------------------
friedman_result <- longitudinal_data_wide %>%
  friedman_test(score_T1, score_T2, score_T3)

friedman_result

## -----------------------------------------------------------------------------
pairwise_wilcoxon(friedman_result)

## -----------------------------------------------------------------------------
survey_data %>%
  binomial_test(gender)

## -----------------------------------------------------------------------------
survey_data %>%
  chi_square(education, employment, weights = sampling_weight)

## -----------------------------------------------------------------------------
# Phi coefficient (2x2 tables)
survey_data %>%
  phi(gender, employment, weights = sampling_weight)

## -----------------------------------------------------------------------------
# Cramer's V (larger tables)
survey_data %>%
  cramers_v(education, employment, weights = sampling_weight)

## -----------------------------------------------------------------------------
small_sample <- survey_data %>% slice_sample(n = 30)

small_sample %>%
  fisher_test(gender, region)

## -----------------------------------------------------------------------------
# Equal proportions (default)
survey_data %>%
  chisq_gof(education)

## -----------------------------------------------------------------------------
# Custom expected proportions
survey_data %>%
  chisq_gof(education, expected = c(0.30, 0.25, 0.25, 0.20))

## ----eval = FALSE-------------------------------------------------------------
# test_data <- survey_data %>%
#   mutate(
#     trust_gov_high = ifelse(trust_government > 3, 1, 0),
#     trust_media_high = ifelse(trust_media > 3, 1, 0)
#   )
# 
# test_data %>%
#   mcnemar_test(var1 = trust_gov_high, var2 = trust_media_high)

## -----------------------------------------------------------------------------
# 1. Describe the groups
survey_data %>%
  group_by(education) %>%
  describe(life_satisfaction, weights = sampling_weight)

# 2. Test for overall differences
anova_result <- survey_data %>%
  oneway_anova(life_satisfaction, group = education,
               weights = sampling_weight)
anova_result

# 3. Check assumptions
levene_test(anova_result)

# 4. Post-hoc: which groups differ?
tukey_test(anova_result)

