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

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

## -----------------------------------------------------------------------------
survey_data %>%
  pearson_cor(age, income)

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

## -----------------------------------------------------------------------------
survey_data %>%
  pearson_cor(trust_government, trust_media, trust_science,
              weights = sampling_weight)

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

summary(result)

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

## -----------------------------------------------------------------------------
survey_data %>%
  spearman_rho(political_orientation, environmental_concern,
               weights = sampling_weight)

## -----------------------------------------------------------------------------
survey_data %>%
  spearman_rho(political_orientation, environmental_concern,
               life_satisfaction, trust_government,
               weights = sampling_weight)

## -----------------------------------------------------------------------------
survey_data %>%
  kendall_tau(political_orientation, life_satisfaction,
              weights = sampling_weight)

## -----------------------------------------------------------------------------
# Does the satisfaction-income correlation survive controlling for age?
survey_data %>%
  partial_cor(life_satisfaction, income, controls = age,
              weights = sampling_weight)

## -----------------------------------------------------------------------------
result <- survey_data %>%
  partial_cor(trust_government, trust_media, trust_science,
              controls = c(age, political_orientation))
summary(result)

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

spearman_result <- survey_data %>%
  spearman_rho(life_satisfaction, income, weights = sampling_weight)

kendall_result <- survey_data %>%
  kendall_tau(life_satisfaction, income, weights = sampling_weight)

comparison <- data.frame(
  Method = c("Pearson", "Spearman", "Kendall"),
  Correlation = c(pearson_result$correlations$correlation[1],
                  spearman_result$correlations$correlation[1],
                  kendall_result$correlations$correlation[1]),
  P_Value = c(pearson_result$correlations$p_value[1],
              spearman_result$correlations$p_value[1],
              kendall_result$correlations$p_value[1])
)
print(comparison)

## -----------------------------------------------------------------------------
# 1. Correlation matrix for key variables
cor_result <- survey_data %>%
  pearson_cor(age, income, life_satisfaction,
              political_orientation, environmental_concern,
              weights = sampling_weight)
cor_result

# 2. Identify the strongest correlations
significant <- cor_result$correlations %>%
  filter(p_value < 0.05) %>%
  arrange(desc(abs(correlation)))
print(significant)

# 3. Check if relationships vary by region
survey_data %>%
  group_by(region) %>%
  pearson_cor(age, income, weights = sampling_weight)

