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

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

## -----------------------------------------------------------------------------
reliability(survey_data, trust_government, trust_media, trust_science)

## -----------------------------------------------------------------------------
rel <- reliability(survey_data, trust_government, trust_media, trust_science)
summary(rel)

## -----------------------------------------------------------------------------
reliability(survey_data, trust_government, trust_media, trust_science,
            weights = sampling_weight)

## -----------------------------------------------------------------------------
reliability(survey_data, starts_with("trust"))

## -----------------------------------------------------------------------------
survey_data %>%
  group_by(region) %>%
  reliability(trust_government, trust_media, trust_science)

## -----------------------------------------------------------------------------
efa(survey_data,
    political_orientation, environmental_concern, life_satisfaction,
    trust_government, trust_media, trust_science)

## -----------------------------------------------------------------------------
efa_result <- efa(survey_data,
    political_orientation, environmental_concern, life_satisfaction,
    trust_government, trust_media, trust_science)

summary(efa_result)

## -----------------------------------------------------------------------------
efa(survey_data,
    political_orientation, environmental_concern, life_satisfaction,
    trust_government, trust_media, trust_science,
    rotation = "varimax")

## -----------------------------------------------------------------------------
efa(survey_data,
    political_orientation, environmental_concern, life_satisfaction,
    trust_government, trust_media, trust_science,
    rotation = "promax")

## ----eval = FALSE-------------------------------------------------------------
# # Requires GPArotation package
# efa(survey_data,
#     political_orientation, environmental_concern, life_satisfaction,
#     trust_government, trust_media, trust_science,
#     rotation = "oblimin")

## -----------------------------------------------------------------------------
efa(survey_data,
    political_orientation, environmental_concern, life_satisfaction,
    trust_government, trust_media, trust_science,
    extraction = "ml")

## -----------------------------------------------------------------------------
efa(survey_data,
    political_orientation, environmental_concern, life_satisfaction,
    trust_government, trust_media, trust_science,
    extraction = "ml", rotation = "promax")

## -----------------------------------------------------------------------------
efa(survey_data,
    political_orientation, environmental_concern, life_satisfaction,
    trust_government, trust_media, trust_science,
    n_factors = 2)

## -----------------------------------------------------------------------------
efa(survey_data,
    political_orientation, environmental_concern, life_satisfaction,
    trust_government, trust_media, trust_science,
    weights = sampling_weight)

## -----------------------------------------------------------------------------
# Create mean index
survey_data <- survey_data %>%
  mutate(m_trust = row_means(., trust_government, trust_media, trust_science,
                             min_valid = 2))

# Transform to 0-100 scale
survey_data <- survey_data %>%
  mutate(trust_pomps = pomps(m_trust, scale_min = 1, scale_max = 5))

# Check the result
survey_data %>%
  describe(m_trust, trust_pomps)

## -----------------------------------------------------------------------------
# Group comparison
survey_data %>%
  t_test(m_trust, group = gender, weights = sampling_weight)

## -----------------------------------------------------------------------------
# As a predictor in regression
survey_data %>%
  linear_regression(life_satisfaction ~ m_trust + age + income,
                    weights = sampling_weight)

## -----------------------------------------------------------------------------
# 1. Check reliability
rel <- reliability(survey_data, trust_government, trust_media, trust_science)
rel
summary(rel)

# 2. Explore factor structure
efa_result <- efa(survey_data, trust_government, trust_media, trust_science)
efa_result

# 3. Create mean index (Alpha was acceptable)
survey_data <- survey_data %>%
  mutate(m_trust = row_means(., trust_government, trust_media, trust_science,
                             min_valid = 2))

# 4. Transform to POMPS
survey_data <- survey_data %>%
  mutate(trust_pomps = pomps(m_trust, scale_min = 1, scale_max = 5))

# 5. Use in further analysis
survey_data %>%
  group_by(education) %>%
  describe(m_trust, trust_pomps, weights = sampling_weight)

