Init

library(kirkegaard)
## Loading required package: tidyverse
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Loading required package: magrittr
## 
## 
## Attaching package: 'magrittr'
## 
## 
## The following object is masked from 'package:purrr':
## 
##     set_names
## 
## 
## The following object is masked from 'package:tidyr':
## 
##     extract
## 
## 
## Loading required package: weights
## 
## Loading required package: Hmisc
## 
## 
## Attaching package: 'Hmisc'
## 
## 
## The following objects are masked from 'package:dplyr':
## 
##     src, summarize
## 
## 
## The following objects are masked from 'package:base':
## 
##     format.pval, units
## 
## 
## Loading required package: assertthat
## 
## 
## Attaching package: 'assertthat'
## 
## 
## The following object is masked from 'package:tibble':
## 
##     has_name
## 
## 
## Loading required package: psych
## 
## 
## Attaching package: 'psych'
## 
## 
## The following object is masked from 'package:Hmisc':
## 
##     describe
## 
## 
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
## 
## 
## 
## Attaching package: 'kirkegaard'
## 
## 
## The following object is masked from 'package:psych':
## 
##     rescale
## 
## 
## The following object is masked from 'package:assertthat':
## 
##     are_equal
## 
## 
## The following object is masked from 'package:purrr':
## 
##     is_logical
## 
## 
## The following object is masked from 'package:base':
## 
##     +
load_packages(
  readxl,
  lavaan
)
## This is lavaan 0.6-15
## lavaan is FREE software! Please report any bugs.
## 
## Attaching package: 'lavaan'
## 
## The following object is masked from 'package:psych':
## 
##     cor2cov
theme_set(theme_bw())

options(
    digits = 3
)

#how much data to impute
imp_max_miss = .10

Functions

log10_fix = function(x) {
  log10(x) %>% mapvalues(from = -Inf, to = NA, warn_missing = F)
}

Data

#GDP per capita, world bank
#https://ourworldindata.org/grapher/gdp-per-capita-worldbank
gdp_pc_ppp_wb = read_csv("data/gdp-per-capita-worldbank.csv") %>% 
  df_legalize_names() %>% 
  rename(ISO = Code)
## Rows: 6166 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Entity, Code
## dbl (2): Year, GDP per capita, PPP (constant 2017 international $)
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#GNI, world bank
#https://ourworldindata.org/grapher/gross-national-income-per-capita
gni_pc = read_csv("data/gross-national-income-per-capita.csv") %>% 
  df_legalize_names() %>% 
  rename(ISO = Code)
## Rows: 4333 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Entity, Code
## dbl (2): Year, GNI per capita, PPP (constant 2017 international $)
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#median income
#https://worldpopulationreview.com/country-rankings/median-income-by-country
median_income = read_csv("data/median-income-by-country-2023.csv")  %>% 
  df_legalize_names() %>% 
  mutate(ISO = pu_translate(country))
## Rows: 162 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): country
## dbl (4): medianIncome, meanIncome, gdpPerCapitaPPP, pop2023
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#HDI
#https://ourworldindata.org/grapher/human-development-index
hdi = read_csv("data/human-development-index.csv") %>% 
  df_legalize_names() %>% 
  rename(ISO = Code)
## Rows: 5923 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Entity, Code
## dbl (2): Year, Human Development Index
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#SPI
#https://www.socialprogress.org/
spi = read_excel("data/2022_social_progress_index_dataset-1663250377.xlsx", sheet = 2, skip = 2) %>% 
  df_legalize_names() %>% 
  rename(ISO = SPI_country_code)

#becker IQ data
becker = read_excel("data/NIQ-DATA (V1.3.3).xlsx", sheet = "FAV", range = "A2:N205") %>% 
  df_legalize_names()

#regional dummies
smart_fraction_data = read_rds("data/smart_fraction_data_out.rds")

#join the datasets for year 2019, pre-covid
d = full_join(
  gdp_pc_ppp_wb %>% filter(Year == 2019, !is.na(ISO)) %>% select(ISO, GDP_per_capita_PPP_constant_2017_international) %>% rename(GDPpc_PPP_2019 = GDP_per_capita_PPP_constant_2017_international),
  gni_pc %>% filter(Year == 2019, !is.na(ISO)) %>% select(ISO, GNI_per_capita_PPP_constant_2017_international) %>% rename(GNIpc_PPP_2019 = GNI_per_capita_PPP_constant_2017_international)
) %>% 
  full_join(
    median_income %>% filter(!is.na(ISO)) %>% select(ISO, medianIncome, meanIncome)
  ) %>% 
  full_join(
    hdi %>% filter(Year == 2019, !is.na(ISO)) %>% select(ISO, Human_Development_Index) %>% rename(HDI_2019 = Human_Development_Index)
  ) %>% 
  full_join(
    spi %>% filter(SPI_year == 2019, !is.na(ISO)) %>% select(ISO, Social_Progress_Index:Quality_weighted_universities_points) %>% rename(SPI_2019 = Social_Progress_Index)
  ) %>% 
  full_join(
    becker %>% select(-Country)
  ) %>% 
  left_join(
    smart_fraction_data %>% select(ISO, starts_with("UN_"))
  ) %>% mutate(
    nice_name = pu_translate(ISO, reverse = T)
  )
## Joining with `by = join_by(ISO)`
## Joining with `by = join_by(ISO)`
## Joining with `by = join_by(ISO)`
## Joining with `by = join_by(ISO)`
## Joining with `by = join_by(ISO)`
## Joining with `by = join_by(ISO)`
## No match: OWID_KOS
## No match: OWID_WRL
## No match: WWW
## No match: WBG
## No match: KNA.
assert_that(!any(is.na(d$ISO)))
## [1] TRUE

Recoding

#log10 variants
d %<>% mutate(
  medianIncome_log10 = log10_fix(medianIncome),
  meanIncome_log10 = log10_fix(meanIncome),
  GNIpc_PPP_2019_log10 = log10_fix(GNIpc_PPP_2019),
  GDPpc_PPP_2019_log10 = log10_fix(GDPpc_PPP_2019)
)

#list of variables
d_vars = d %>% df_var_table()

#get all the data plus all econs
d_spi_all = d %>% select(ISO, Child_stunting_0_low_risk_100_high_risk:Quality_weighted_universities_points, medianIncome_log10, meanIncome_log10, GDPpc_PPP_2019_log10, GNIpc_PPP_2019_log10) %>% 
  miss_filter(missing = imp_max_miss)
d_spi_all_imp = miss_impute(d_spi_all, leave_out = "ISO")
d_spi_all_imp_z = df_standardize(d_spi_all_imp, exclude_range_01 = F)
## Skipped ISO because it is a character (string)

Analysis

Simple

#GDP vs. GNI
d %>% 
  GG_scatter("GDPpc_PPP_2019", "GNIpc_PPP_2019", case_names = "nice_name") +
  geom_abline(slope = 1, linetype = "dotted") +
  scale_y_continuous("GNI per capita, PPP, 2019") +
  scale_x_continuous("GDP per capita, PPP, 2019") +
  ggtitle("GDP vs. GNI per capita, PPP, 2019")
## `geom_smooth()` using formula = 'y ~ x'

GG_save("figs/GNI_GDP.png")
## `geom_smooth()` using formula = 'y ~ x'
#GNI vs. average income
d %>% 
  GG_scatter("GNIpc_PPP_2019", "meanIncome", case_names = "nice_name", repel_names = T) +
  scale_x_continuous("GNI per capita, PPP, 2019") +
  scale_y_continuous("Average income, PPP, 2021") +
  ggtitle("GNI per capita (PPP, 2019) vs. average income 2021 (PPP)")
## `geom_smooth()` using formula = 'y ~ x'
## Warning: ggrepel: 87 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps

GG_save("figs/GNI_avg_income.png")
## `geom_smooth()` using formula = 'y ~ x'
## Warning: ggrepel: 75 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps
#GNI vs. average income
d %>% 
  GG_scatter("meanIncome", "medianIncome", case_names = "nice_name", repel_names = T) +
  scale_x_continuous("Mean income, PPP, 2021") +
  scale_y_continuous("Median income, PPP, 2021") +
  ggtitle("Mean income vs. median income (2021, PPP)") +
  geom_abline(slope = 1, linetype = "dotted")
## `geom_smooth()` using formula = 'y ~ x'
## Warning: ggrepel: 142 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps

GG_save("figs/mean_median_income.png")
## `geom_smooth()` using formula = 'y ~ x'
## Warning: ggrepel: 130 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps
#GNI vs. median income
d %>% 
  GG_scatter("medianIncome", "HDI_2019", case_names = "nice_name", repel_names = T) +
  scale_x_continuous("Median income, PPP, 2021") +
  scale_y_continuous("HDI, 2019") +
  ggtitle("Median income (2021, PPP) vs. Human Development Index (2019)") +
  geom_smooth(se = T)
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: ggrepel: 147 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps

GG_save("figs/median_income_HDI.png")
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: ggrepel: 122 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps
#median income vs. SPI
d %>% 
  GG_scatter("medianIncome", "SPI_2019", case_names = "nice_name", repel_names = T) +
  scale_x_continuous("Median income, PPP, 2021") +
  scale_y_continuous("SPI, 2019") +
  ggtitle("Median income (2021, PPP) vs. Social Progress Index (2019)") +
  geom_smooth(se = T)
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: ggrepel: 136 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps

GG_save("figs/median_income_SPI.png")
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: ggrepel: 108 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps
#log median income vs. SPI
d %>% 
  GG_scatter("medianIncome_log10", "SPI_2019", case_names = "nice_name", repel_names = T) +
  scale_x_continuous("Median income, PPP, 2021, log10") +
  scale_y_continuous("SPI, 2019") +
  ggtitle("Median income log10 (2021, PPP) vs. Social Progress Index (2019)") +
  geom_smooth(se = T)
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: ggrepel: 100 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps

GG_save("figs/median_income_log10_SPI.png")
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: ggrepel: 45 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps
#print top
d %>% select(nice_name, medianIncome_log10, SPI_2019) %>% arrange(-SPI_2019) %>% head(25) %>% print(n=Inf)
## # A tibble: 25 × 3
##    nice_name      medianIncome_log10 SPI_2019
##    <chr>                       <dbl>    <dbl>
##  1 Norway                       4.36     90.8
##  2 Denmark                      4.24     90.6
##  3 Finland                      4.21     90.5
##  4 Switzerland                  4.33     89.6
##  5 Iceland                      4.23     89.5
##  6 Netherlands                  4.23     89.4
##  7 Sweden                       4.25     89.2
##  8 Germany                      4.23     88.8
##  9 Australia                    4.23     88.1
## 10 Canada                       4.27     88.0
## 11 Luxembourg                   4.42     87.9
## 12 Ireland                      4.16     87.8
## 13 Japan                        4.15     87.7
## 14 Austria                      4.26     87.4
## 15 New Zealand                 NA        87.4
## 16 Belgium                      4.21     87.1
## 17 United Kingdom               4.17     86.5
## 18 France                       4.21     86.3
## 19 South Korea                  4.10     85.6
## 20 Spain                        4.07     85.5
## 21 Slovenia                     4.07     85.3
## 22 Estonia                      4.03     85.3
## 23 USA                          4.29     85.1
## 24 Portugal                     3.93     85  
## 25 Czech Republic               4.00     84.5

SPI EFA models

#EFA of SPI
#any missing?

d_spi = d %>% select(ISO, Child_stunting_0_low_risk_100_high_risk:Quality_weighted_universities_points) %>% 
  miss_filter(missing = imp_max_miss)
d_spi_imp = miss_impute(d_spi, leave_out = "ISO")

#EFA
spi_efa = fa(
  d_spi_imp %>% select(-ISO)
)

spi_efa
## Factor Analysis using method =  minres
## Call: fa(r = d_spi_imp %>% select(-ISO))
## Standardized loadings (pattern matrix) based upon correlation matrix
##                                                                                     MR1
## Child_stunting_0_low_risk_100_high_risk                                           -0.87
## Infectious_diseases_DALYs_100_000                                                 -0.74
## Maternal_mortality_rate_deaths_100_000_live_births                                -0.82
## Child_mortality_rate_deaths_1_000_live_births                                     -0.87
## Undernourishment_pct_of_pop                                                       -0.73
## Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk                        -0.60
## Unsafe_water_sanitation_and_hygiene_DALYs_100_000                                 -0.78
## Access_to_improved_sanitation_proportion_of_pop                                    0.85
## Access_to_improved_water_source_proportion_of_pop                                  0.79
## Satisfaction_with_water_quality_proportion_of_pop                                  0.72
## Household_air_pollution_DALYs_100_000                                             -0.83
## Dissatisfaction_with_housing_affordability_proportion_of_pop                      -0.24
## Access_to_electricity_pct_of_pop                                                   0.77
## Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop                         0.82
## Political_killings_and_torture_0_low_freedom_1_high_freedom                        0.69
## Interpersonal_violence_DALYs_100_000                                              -0.28
## Transportation_related_injuries_DALYs_100_000                                     -0.63
## Intimate_Partner_Violence_pct_of_women_aged_15plus                                -0.81
## Money_Stolen_proportion_of_pop                                                    -0.58
## Equal_access_to_quality_education_0_unequal_4_equal                                0.79
## Population_with_no_schooling_proportion_of_pop                                    -0.75
## Secondary_school_attainment_pct_of_pop_aged_25plus                                 0.79
## Primary_school_enrollment_pct_of_children                                          0.68
## Gender_parity_in_secondary_attainment_distance_from_parity                        -0.77
## Alternative_sources_of_information_index_0_low_1_high                              0.32
## Mobile_telephone_subscriptions_subscriptions_1_000_people                          0.63
## Internet_users_pct_of_pop                                                          0.90
## Access_to_online_governance_0_low_1_high                                           0.77
## Equal_access_to_quality_healthcare_0_unequal_4_equal                               0.85
## Life_expectancy_at_60_years                                                        0.82
## Premature_deaths_from_non_communicable_diseases_deaths_100_000                    -0.61
## Access_to_essential_health_services_0_none_100_full_coverage                       0.95
## Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop             0.53
## Outdoor_air_pollution_DALYs_100_000                                               -0.35
## Lead_exposure_DALYs_100_000                                                       -0.65
## Particulate_matter_pollution_mean_annual_exposure_g_m3                            -0.55
## Species_protection_0_low_100_high                                                  0.29
## Freedom_of_religion_0_no_freedom_4_full_freedom                                    0.35
## Property_rights_for_women_0_no_rights_5_full_rights                                0.74
## Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom                           0.56
## Access_to_justice_0_nonexistent_1_observed                                         0.69
## Freedom_of_discussion_0_low_1_high                                                 0.54
## Political_rights_0_and_lower_no_rights_40_full_rights                              0.67
## Freedom_of_domestic_movement_0_low_1_high                                          0.59
## Early_marriage_pct_of_married_women_aged_15_19                                    -0.72
## Satisfied_demand_for_contraception_pct_satisfied_demand                            0.61
## Young_people_not_in_education_employment_or_training_pct_of_youth                 -0.50
## Vulnerable_employment_pct_of_total_employment                                     -0.84
## Perception_of_corruption_0_high_corruption_100_low_corruption                      0.81
## Equal_protection_index_0_low_1_high                                                0.68
## Equal_access_index_0_low_1_high                                                    0.58
## Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal                0.65
## Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal  0.76
## Acceptance_of_gays_and_lesbians_proportion_of_pop                                  0.60
## Discrimination_and_violence_against_minorities_0_low_10_high                      -0.50
## Academic_freedom_0_low_1_high                                                      0.50
## Women_with_advanced_education_proportion_of_females                                0.86
## Expected_years_of_tertiary_schooling_years                                         0.80
## Citable_documents_documents_1_000_people                                           0.73
## Quality_weighted_universities_points                                               0.28
##                                                                                      h2
## Child_stunting_0_low_risk_100_high_risk                                           0.752
## Infectious_diseases_DALYs_100_000                                                 0.546
## Maternal_mortality_rate_deaths_100_000_live_births                                0.670
## Child_mortality_rate_deaths_1_000_live_births                                     0.750
## Undernourishment_pct_of_pop                                                       0.536
## Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk                        0.358
## Unsafe_water_sanitation_and_hygiene_DALYs_100_000                                 0.604
## Access_to_improved_sanitation_proportion_of_pop                                   0.723
## Access_to_improved_water_source_proportion_of_pop                                 0.618
## Satisfaction_with_water_quality_proportion_of_pop                                 0.514
## Household_air_pollution_DALYs_100_000                                             0.696
## Dissatisfaction_with_housing_affordability_proportion_of_pop                      0.057
## Access_to_electricity_pct_of_pop                                                  0.593
## Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop                        0.677
## Political_killings_and_torture_0_low_freedom_1_high_freedom                       0.481
## Interpersonal_violence_DALYs_100_000                                              0.081
## Transportation_related_injuries_DALYs_100_000                                     0.393
## Intimate_Partner_Violence_pct_of_women_aged_15plus                                0.655
## Money_Stolen_proportion_of_pop                                                    0.341
## Equal_access_to_quality_education_0_unequal_4_equal                               0.626
## Population_with_no_schooling_proportion_of_pop                                    0.566
## Secondary_school_attainment_pct_of_pop_aged_25plus                                0.631
## Primary_school_enrollment_pct_of_children                                         0.465
## Gender_parity_in_secondary_attainment_distance_from_parity                        0.597
## Alternative_sources_of_information_index_0_low_1_high                             0.106
## Mobile_telephone_subscriptions_subscriptions_1_000_people                         0.391
## Internet_users_pct_of_pop                                                         0.808
## Access_to_online_governance_0_low_1_high                                          0.596
## Equal_access_to_quality_healthcare_0_unequal_4_equal                              0.716
## Life_expectancy_at_60_years                                                       0.677
## Premature_deaths_from_non_communicable_diseases_deaths_100_000                    0.370
## Access_to_essential_health_services_0_none_100_full_coverage                      0.907
## Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop            0.284
## Outdoor_air_pollution_DALYs_100_000                                               0.123
## Lead_exposure_DALYs_100_000                                                       0.422
## Particulate_matter_pollution_mean_annual_exposure_g_m3                            0.307
## Species_protection_0_low_100_high                                                 0.082
## Freedom_of_religion_0_no_freedom_4_full_freedom                                   0.120
## Property_rights_for_women_0_no_rights_5_full_rights                               0.549
## Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom                          0.309
## Access_to_justice_0_nonexistent_1_observed                                        0.472
## Freedom_of_discussion_0_low_1_high                                                0.290
## Political_rights_0_and_lower_no_rights_40_full_rights                             0.454
## Freedom_of_domestic_movement_0_low_1_high                                         0.343
## Early_marriage_pct_of_married_women_aged_15_19                                    0.519
## Satisfied_demand_for_contraception_pct_satisfied_demand                           0.371
## Young_people_not_in_education_employment_or_training_pct_of_youth                 0.254
## Vulnerable_employment_pct_of_total_employment                                     0.713
## Perception_of_corruption_0_high_corruption_100_low_corruption                     0.655
## Equal_protection_index_0_low_1_high                                               0.458
## Equal_access_index_0_low_1_high                                                   0.342
## Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal               0.423
## Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal 0.577
## Acceptance_of_gays_and_lesbians_proportion_of_pop                                 0.362
## Discrimination_and_violence_against_minorities_0_low_10_high                      0.255
## Academic_freedom_0_low_1_high                                                     0.247
## Women_with_advanced_education_proportion_of_females                               0.746
## Expected_years_of_tertiary_schooling_years                                        0.640
## Citable_documents_documents_1_000_people                                          0.537
## Quality_weighted_universities_points                                              0.077
##                                                                                      u2
## Child_stunting_0_low_risk_100_high_risk                                           0.248
## Infectious_diseases_DALYs_100_000                                                 0.454
## Maternal_mortality_rate_deaths_100_000_live_births                                0.330
## Child_mortality_rate_deaths_1_000_live_births                                     0.250
## Undernourishment_pct_of_pop                                                       0.464
## Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk                        0.642
## Unsafe_water_sanitation_and_hygiene_DALYs_100_000                                 0.396
## Access_to_improved_sanitation_proportion_of_pop                                   0.277
## Access_to_improved_water_source_proportion_of_pop                                 0.382
## Satisfaction_with_water_quality_proportion_of_pop                                 0.486
## Household_air_pollution_DALYs_100_000                                             0.304
## Dissatisfaction_with_housing_affordability_proportion_of_pop                      0.943
## Access_to_electricity_pct_of_pop                                                  0.407
## Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop                        0.323
## Political_killings_and_torture_0_low_freedom_1_high_freedom                       0.519
## Interpersonal_violence_DALYs_100_000                                              0.919
## Transportation_related_injuries_DALYs_100_000                                     0.607
## Intimate_Partner_Violence_pct_of_women_aged_15plus                                0.345
## Money_Stolen_proportion_of_pop                                                    0.659
## Equal_access_to_quality_education_0_unequal_4_equal                               0.374
## Population_with_no_schooling_proportion_of_pop                                    0.434
## Secondary_school_attainment_pct_of_pop_aged_25plus                                0.369
## Primary_school_enrollment_pct_of_children                                         0.535
## Gender_parity_in_secondary_attainment_distance_from_parity                        0.403
## Alternative_sources_of_information_index_0_low_1_high                             0.894
## Mobile_telephone_subscriptions_subscriptions_1_000_people                         0.609
## Internet_users_pct_of_pop                                                         0.192
## Access_to_online_governance_0_low_1_high                                          0.404
## Equal_access_to_quality_healthcare_0_unequal_4_equal                              0.284
## Life_expectancy_at_60_years                                                       0.323
## Premature_deaths_from_non_communicable_diseases_deaths_100_000                    0.630
## Access_to_essential_health_services_0_none_100_full_coverage                      0.093
## Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop            0.716
## Outdoor_air_pollution_DALYs_100_000                                               0.877
## Lead_exposure_DALYs_100_000                                                       0.578
## Particulate_matter_pollution_mean_annual_exposure_g_m3                            0.693
## Species_protection_0_low_100_high                                                 0.918
## Freedom_of_religion_0_no_freedom_4_full_freedom                                   0.880
## Property_rights_for_women_0_no_rights_5_full_rights                               0.451
## Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom                          0.691
## Access_to_justice_0_nonexistent_1_observed                                        0.528
## Freedom_of_discussion_0_low_1_high                                                0.710
## Political_rights_0_and_lower_no_rights_40_full_rights                             0.546
## Freedom_of_domestic_movement_0_low_1_high                                         0.657
## Early_marriage_pct_of_married_women_aged_15_19                                    0.481
## Satisfied_demand_for_contraception_pct_satisfied_demand                           0.629
## Young_people_not_in_education_employment_or_training_pct_of_youth                 0.746
## Vulnerable_employment_pct_of_total_employment                                     0.287
## Perception_of_corruption_0_high_corruption_100_low_corruption                     0.345
## Equal_protection_index_0_low_1_high                                               0.542
## Equal_access_index_0_low_1_high                                                   0.658
## Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal               0.577
## Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal 0.423
## Acceptance_of_gays_and_lesbians_proportion_of_pop                                 0.638
## Discrimination_and_violence_against_minorities_0_low_10_high                      0.745
## Academic_freedom_0_low_1_high                                                     0.753
## Women_with_advanced_education_proportion_of_females                               0.254
## Expected_years_of_tertiary_schooling_years                                        0.360
## Citable_documents_documents_1_000_people                                          0.463
## Quality_weighted_universities_points                                              0.923
##                                                                                   com
## Child_stunting_0_low_risk_100_high_risk                                             1
## Infectious_diseases_DALYs_100_000                                                   1
## Maternal_mortality_rate_deaths_100_000_live_births                                  1
## Child_mortality_rate_deaths_1_000_live_births                                       1
## Undernourishment_pct_of_pop                                                         1
## Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk                          1
## Unsafe_water_sanitation_and_hygiene_DALYs_100_000                                   1
## Access_to_improved_sanitation_proportion_of_pop                                     1
## Access_to_improved_water_source_proportion_of_pop                                   1
## Satisfaction_with_water_quality_proportion_of_pop                                   1
## Household_air_pollution_DALYs_100_000                                               1
## Dissatisfaction_with_housing_affordability_proportion_of_pop                        1
## Access_to_electricity_pct_of_pop                                                    1
## Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop                          1
## Political_killings_and_torture_0_low_freedom_1_high_freedom                         1
## Interpersonal_violence_DALYs_100_000                                                1
## Transportation_related_injuries_DALYs_100_000                                       1
## Intimate_Partner_Violence_pct_of_women_aged_15plus                                  1
## Money_Stolen_proportion_of_pop                                                      1
## Equal_access_to_quality_education_0_unequal_4_equal                                 1
## Population_with_no_schooling_proportion_of_pop                                      1
## Secondary_school_attainment_pct_of_pop_aged_25plus                                  1
## Primary_school_enrollment_pct_of_children                                           1
## Gender_parity_in_secondary_attainment_distance_from_parity                          1
## Alternative_sources_of_information_index_0_low_1_high                               1
## Mobile_telephone_subscriptions_subscriptions_1_000_people                           1
## Internet_users_pct_of_pop                                                           1
## Access_to_online_governance_0_low_1_high                                            1
## Equal_access_to_quality_healthcare_0_unequal_4_equal                                1
## Life_expectancy_at_60_years                                                         1
## Premature_deaths_from_non_communicable_diseases_deaths_100_000                      1
## Access_to_essential_health_services_0_none_100_full_coverage                        1
## Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop              1
## Outdoor_air_pollution_DALYs_100_000                                                 1
## Lead_exposure_DALYs_100_000                                                         1
## Particulate_matter_pollution_mean_annual_exposure_g_m3                              1
## Species_protection_0_low_100_high                                                   1
## Freedom_of_religion_0_no_freedom_4_full_freedom                                     1
## Property_rights_for_women_0_no_rights_5_full_rights                                 1
## Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom                            1
## Access_to_justice_0_nonexistent_1_observed                                          1
## Freedom_of_discussion_0_low_1_high                                                  1
## Political_rights_0_and_lower_no_rights_40_full_rights                               1
## Freedom_of_domestic_movement_0_low_1_high                                           1
## Early_marriage_pct_of_married_women_aged_15_19                                      1
## Satisfied_demand_for_contraception_pct_satisfied_demand                             1
## Young_people_not_in_education_employment_or_training_pct_of_youth                   1
## Vulnerable_employment_pct_of_total_employment                                       1
## Perception_of_corruption_0_high_corruption_100_low_corruption                       1
## Equal_protection_index_0_low_1_high                                                 1
## Equal_access_index_0_low_1_high                                                     1
## Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal                 1
## Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal   1
## Acceptance_of_gays_and_lesbians_proportion_of_pop                                   1
## Discrimination_and_violence_against_minorities_0_low_10_high                        1
## Academic_freedom_0_low_1_high                                                       1
## Women_with_advanced_education_proportion_of_females                                 1
## Expected_years_of_tertiary_schooling_years                                          1
## Citable_documents_documents_1_000_people                                            1
## Quality_weighted_universities_points                                                1
## 
##                  MR1
## SS loadings    28.43
## Proportion Var  0.47
## 
## Mean item complexity =  1
## Test of the hypothesis that 1 factor is sufficient.
## 
## df null model =  1770  with the objective function =  96.6 with Chi Square =  13245
## df of  the model are 1710  and the objective function was  56.4 
## 
## The root mean square of the residuals (RMSR) is  0.14 
## The df corrected root mean square of the residuals is  0.15 
## 
## The harmonic n.obs is  159 with the empirical chi square  11783  with prob <  0 
## The total n.obs was  159  with Likelihood Chi Square =  7702  with prob <  0 
## 
## Tucker Lewis Index of factoring reliability =  0.456
## RMSEA index =  0.148  and the 90 % confidence intervals are  0.146 0.152
## BIC =  -966
## Fit based upon off diagonal values = 0.91
## Measures of factor score adequacy             
##                                                    MR1
## Correlation of (regression) scores with factors   1.00
## Multiple R square of scores with factors          0.99
## Minimum correlation of possible factor scores     0.99
spi_efa %>% fa_plot_loadings() +
  geom_point(mapping = aes(color = fa)) +
  scale_color_gradient(low = "red", high = "blue", guide = NULL) +
  ylim(-1, 1) +
  ggtitle("Exploratory factor analysis of Social Progress Index indicators",
          subtitle = str_glue("n = {nrow(d_spi)} countries"))

GG_save("figs/SPI EFA.png")

#save scores to main dataset
#remove if exists
d$SPI_EFA = NULL
d = full_join(
  tibble(
    SPI_EFA = spi_efa$scores[, 1] %>% standardize(),
    ISO = d_spi$ISO
    ),
  d
)
## Joining with `by = join_by(ISO)`
#compare
GG_scatter(d, "SPI_2019", "SPI_EFA", case_names = "nice_name") +
  ylab("SPI, factor score") +
  xlab("SPI, premade score")
## `geom_smooth()` using formula = 'y ~ x'

GG_save("figs/SPI premade vs. EFA.png")
## `geom_smooth()` using formula = 'y ~ x'
d %>% filter(!is.na(SPI_2019)) %>% select(nice_name, SPI_2019, SPI_EFA) %>% mutate(SPI_2019_rank = rank(-SPI_2019), SPI_EFA_rank = rank(-SPI_EFA), rank_diff = SPI_2019_rank - SPI_EFA_rank) %>% arrange(-SPI_EFA) %>% head(30) %>% print(n=Inf)
## # A tibble: 30 × 6
##    nice_name      SPI_2019 SPI_EFA SPI_2019_rank SPI_EFA_rank rank_diff
##    <chr>             <dbl>   <dbl>         <dbl>        <dbl>     <dbl>
##  1 Netherlands        89.4    1.61             6            1         5
##  2 Norway             90.8    1.58             1            2        -1
##  3 Luxembourg         87.9    1.58            11            3         8
##  4 Germany            88.8    1.54             8            4         4
##  5 Sweden             89.2    1.54             7            5         2
##  6 Canada             88.0    1.53            10            6         4
##  7 Switzerland        89.6    1.50             4            7        -3
##  8 Finland            90.5    1.49             3            8        -5
##  9 France             86.3    1.48            18            9         9
## 10 Iceland            89.5    1.47             5           10        -5
## 11 Denmark            90.6    1.46             2           11        -9
## 12 Japan              87.7    1.46            13           12         1
## 13 Australia          88.1    1.41             9           13        -4
## 14 Ireland            87.8    1.40            12           14        -2
## 15 United Kingdom     86.5    1.40            17           15         2
## 16 Belgium            87.1    1.39            16           16         0
## 17 New Zealand        87.4    1.38            15           17        -2
## 18 Slovenia           85.3    1.35            21           18         3
## 19 Austria            87.4    1.35            14           19        -5
## 20 Czech Republic     84.5    1.27            25           20         5
## 21 Estonia            85.3    1.25            22           21         1
## 22 Italy              84.4    1.22            26           22         4
## 23 Spain              85.5    1.22            20           23        -3
## 24 Portugal           85      1.20            24           24         0
## 25 Cyprus             82.2    1.12            30           25         5
## 26 USA                85.1    1.12            23           26        -3
## 27 Singapore          83.4    1.12            27           27         0
## 28 Malta              83.2    1.11            28           28         0
## 29 South Korea        85.6    1.09            19           29       -10
## 30 Slovakia           80.6    1.05            36           30         6
#rerun with economic indicators
d %>% select(GNIpc_PPP_2019, GDPpc_PPP_2019, meanIncome, medianIncome) %>% wtd.cors()
##                GNIpc_PPP_2019 GDPpc_PPP_2019 meanIncome medianIncome
## GNIpc_PPP_2019          1.000          0.985      0.973        0.970
## GDPpc_PPP_2019          0.985          1.000      0.954        0.950
## meanIncome              0.973          0.954      1.000        0.995
## medianIncome            0.970          0.950      0.995        1.000
d %>% select(GNIpc_PPP_2019, GDPpc_PPP_2019, meanIncome, medianIncome) %>% map_df(log10_fix) %>% wtd.cors()
##                GNIpc_PPP_2019 GDPpc_PPP_2019 meanIncome medianIncome
## GNIpc_PPP_2019          1.000          0.998      0.954        0.955
## GDPpc_PPP_2019          0.998          1.000      0.932        0.931
## meanIncome              0.954          0.932      1.000        0.994
## medianIncome            0.955          0.931      0.994        1.000
#EFA of SPI + GNI + median income
d_spi2 = d %>% select(ISO, Child_stunting_0_low_risk_100_high_risk:Quality_weighted_universities_points, GDPpc_PPP_2019_log10) %>% 
  miss_filter(missing = imp_max_miss)
d_spi2_imp = miss_impute(d_spi2, leave_out = "ISO")

#EFA
spi2_efa = fa(
  d_spi2_imp %>% select(-ISO)
)

spi2_efa
## Factor Analysis using method =  minres
## Call: fa(r = d_spi2_imp %>% select(-ISO))
## Standardized loadings (pattern matrix) based upon correlation matrix
##                                                                                     MR1
## Child_stunting_0_low_risk_100_high_risk                                           -0.87
## Infectious_diseases_DALYs_100_000                                                 -0.74
## Maternal_mortality_rate_deaths_100_000_live_births                                -0.82
## Child_mortality_rate_deaths_1_000_live_births                                     -0.87
## Undernourishment_pct_of_pop                                                       -0.74
## Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk                        -0.60
## Unsafe_water_sanitation_and_hygiene_DALYs_100_000                                 -0.78
## Access_to_improved_sanitation_proportion_of_pop                                    0.85
## Access_to_improved_water_source_proportion_of_pop                                  0.79
## Satisfaction_with_water_quality_proportion_of_pop                                  0.72
## Household_air_pollution_DALYs_100_000                                             -0.84
## Dissatisfaction_with_housing_affordability_proportion_of_pop                      -0.24
## Access_to_electricity_pct_of_pop                                                   0.77
## Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop                         0.83
## Political_killings_and_torture_0_low_freedom_1_high_freedom                        0.69
## Interpersonal_violence_DALYs_100_000                                              -0.28
## Transportation_related_injuries_DALYs_100_000                                     -0.62
## Intimate_Partner_Violence_pct_of_women_aged_15plus                                -0.81
## Money_Stolen_proportion_of_pop                                                    -0.59
## Equal_access_to_quality_education_0_unequal_4_equal                                0.79
## Population_with_no_schooling_proportion_of_pop                                    -0.75
## Secondary_school_attainment_pct_of_pop_aged_25plus                                 0.80
## Primary_school_enrollment_pct_of_children                                          0.67
## Gender_parity_in_secondary_attainment_distance_from_parity                        -0.78
## Alternative_sources_of_information_index_0_low_1_high                              0.33
## Mobile_telephone_subscriptions_subscriptions_1_000_people                          0.64
## Internet_users_pct_of_pop                                                          0.90
## Access_to_online_governance_0_low_1_high                                           0.78
## Equal_access_to_quality_healthcare_0_unequal_4_equal                               0.85
## Life_expectancy_at_60_years                                                        0.82
## Premature_deaths_from_non_communicable_diseases_deaths_100_000                    -0.61
## Access_to_essential_health_services_0_none_100_full_coverage                       0.95
## Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop             0.54
## Outdoor_air_pollution_DALYs_100_000                                               -0.35
## Lead_exposure_DALYs_100_000                                                       -0.65
## Particulate_matter_pollution_mean_annual_exposure_g_m3                            -0.55
## Species_protection_0_low_100_high                                                  0.30
## Freedom_of_religion_0_no_freedom_4_full_freedom                                    0.34
## Property_rights_for_women_0_no_rights_5_full_rights                                0.75
## Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom                           0.56
## Access_to_justice_0_nonexistent_1_observed                                         0.70
## Freedom_of_discussion_0_low_1_high                                                 0.54
## Political_rights_0_and_lower_no_rights_40_full_rights                              0.68
## Freedom_of_domestic_movement_0_low_1_high                                          0.59
## Early_marriage_pct_of_married_women_aged_15_19                                    -0.73
## Satisfied_demand_for_contraception_pct_satisfied_demand                            0.61
## Young_people_not_in_education_employment_or_training_pct_of_youth                 -0.50
## Vulnerable_employment_pct_of_total_employment                                     -0.85
## Perception_of_corruption_0_high_corruption_100_low_corruption                      0.81
## Equal_protection_index_0_low_1_high                                                0.67
## Equal_access_index_0_low_1_high                                                    0.58
## Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal                0.65
## Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal  0.76
## Acceptance_of_gays_and_lesbians_proportion_of_pop                                  0.60
## Discrimination_and_violence_against_minorities_0_low_10_high                      -0.50
## Academic_freedom_0_low_1_high                                                      0.50
## Women_with_advanced_education_proportion_of_females                                0.86
## Expected_years_of_tertiary_schooling_years                                         0.81
## Citable_documents_documents_1_000_people                                           0.74
## Quality_weighted_universities_points                                               0.28
## GDPpc_PPP_2019_log10                                                               0.92
##                                                                                      h2
## Child_stunting_0_low_risk_100_high_risk                                           0.756
## Infectious_diseases_DALYs_100_000                                                 0.549
## Maternal_mortality_rate_deaths_100_000_live_births                                0.672
## Child_mortality_rate_deaths_1_000_live_births                                     0.753
## Undernourishment_pct_of_pop                                                       0.541
## Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk                        0.361
## Unsafe_water_sanitation_and_hygiene_DALYs_100_000                                 0.606
## Access_to_improved_sanitation_proportion_of_pop                                   0.726
## Access_to_improved_water_source_proportion_of_pop                                 0.621
## Satisfaction_with_water_quality_proportion_of_pop                                 0.516
## Household_air_pollution_DALYs_100_000                                             0.701
## Dissatisfaction_with_housing_affordability_proportion_of_pop                      0.059
## Access_to_electricity_pct_of_pop                                                  0.599
## Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop                        0.687
## Political_killings_and_torture_0_low_freedom_1_high_freedom                       0.478
## Interpersonal_violence_DALYs_100_000                                              0.080
## Transportation_related_injuries_DALYs_100_000                                     0.390
## Intimate_Partner_Violence_pct_of_women_aged_15plus                                0.655
## Money_Stolen_proportion_of_pop                                                    0.348
## Equal_access_to_quality_education_0_unequal_4_equal                               0.627
## Population_with_no_schooling_proportion_of_pop                                    0.567
## Secondary_school_attainment_pct_of_pop_aged_25plus                                0.633
## Primary_school_enrollment_pct_of_children                                         0.448
## Gender_parity_in_secondary_attainment_distance_from_parity                        0.601
## Alternative_sources_of_information_index_0_low_1_high                             0.110
## Mobile_telephone_subscriptions_subscriptions_1_000_people                         0.410
## Internet_users_pct_of_pop                                                         0.814
## Access_to_online_governance_0_low_1_high                                          0.606
## Equal_access_to_quality_healthcare_0_unequal_4_equal                              0.718
## Life_expectancy_at_60_years                                                       0.678
## Premature_deaths_from_non_communicable_diseases_deaths_100_000                    0.370
## Access_to_essential_health_services_0_none_100_full_coverage                      0.910
## Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop            0.286
## Outdoor_air_pollution_DALYs_100_000                                               0.119
## Lead_exposure_DALYs_100_000                                                       0.421
## Particulate_matter_pollution_mean_annual_exposure_g_m3                            0.303
## Species_protection_0_low_100_high                                                 0.088
## Freedom_of_religion_0_no_freedom_4_full_freedom                                   0.118
## Property_rights_for_women_0_no_rights_5_full_rights                               0.564
## Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom                          0.313
## Access_to_justice_0_nonexistent_1_observed                                        0.485
## Freedom_of_discussion_0_low_1_high                                                0.296
## Political_rights_0_and_lower_no_rights_40_full_rights                             0.461
## Freedom_of_domestic_movement_0_low_1_high                                         0.348
## Early_marriage_pct_of_married_women_aged_15_19                                    0.528
## Satisfied_demand_for_contraception_pct_satisfied_demand                           0.373
## Young_people_not_in_education_employment_or_training_pct_of_youth                 0.252
## Vulnerable_employment_pct_of_total_employment                                     0.720
## Perception_of_corruption_0_high_corruption_100_low_corruption                     0.655
## Equal_protection_index_0_low_1_high                                               0.452
## Equal_access_index_0_low_1_high                                                   0.335
## Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal               0.420
## Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal 0.576
## Acceptance_of_gays_and_lesbians_proportion_of_pop                                 0.363
## Discrimination_and_violence_against_minorities_0_low_10_high                      0.254
## Academic_freedom_0_low_1_high                                                     0.251
## Women_with_advanced_education_proportion_of_females                               0.747
## Expected_years_of_tertiary_schooling_years                                        0.651
## Citable_documents_documents_1_000_people                                          0.540
## Quality_weighted_universities_points                                              0.078
## GDPpc_PPP_2019_log10                                                              0.848
##                                                                                     u2
## Child_stunting_0_low_risk_100_high_risk                                           0.24
## Infectious_diseases_DALYs_100_000                                                 0.45
## Maternal_mortality_rate_deaths_100_000_live_births                                0.33
## Child_mortality_rate_deaths_1_000_live_births                                     0.25
## Undernourishment_pct_of_pop                                                       0.46
## Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk                        0.64
## Unsafe_water_sanitation_and_hygiene_DALYs_100_000                                 0.39
## Access_to_improved_sanitation_proportion_of_pop                                   0.27
## Access_to_improved_water_source_proportion_of_pop                                 0.38
## Satisfaction_with_water_quality_proportion_of_pop                                 0.48
## Household_air_pollution_DALYs_100_000                                             0.30
## Dissatisfaction_with_housing_affordability_proportion_of_pop                      0.94
## Access_to_electricity_pct_of_pop                                                  0.40
## Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop                        0.31
## Political_killings_and_torture_0_low_freedom_1_high_freedom                       0.52
## Interpersonal_violence_DALYs_100_000                                              0.92
## Transportation_related_injuries_DALYs_100_000                                     0.61
## Intimate_Partner_Violence_pct_of_women_aged_15plus                                0.34
## Money_Stolen_proportion_of_pop                                                    0.65
## Equal_access_to_quality_education_0_unequal_4_equal                               0.37
## Population_with_no_schooling_proportion_of_pop                                    0.43
## Secondary_school_attainment_pct_of_pop_aged_25plus                                0.37
## Primary_school_enrollment_pct_of_children                                         0.55
## Gender_parity_in_secondary_attainment_distance_from_parity                        0.40
## Alternative_sources_of_information_index_0_low_1_high                             0.89
## Mobile_telephone_subscriptions_subscriptions_1_000_people                         0.59
## Internet_users_pct_of_pop                                                         0.19
## Access_to_online_governance_0_low_1_high                                          0.39
## Equal_access_to_quality_healthcare_0_unequal_4_equal                              0.28
## Life_expectancy_at_60_years                                                       0.32
## Premature_deaths_from_non_communicable_diseases_deaths_100_000                    0.63
## Access_to_essential_health_services_0_none_100_full_coverage                      0.09
## Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop            0.71
## Outdoor_air_pollution_DALYs_100_000                                               0.88
## Lead_exposure_DALYs_100_000                                                       0.58
## Particulate_matter_pollution_mean_annual_exposure_g_m3                            0.70
## Species_protection_0_low_100_high                                                 0.91
## Freedom_of_religion_0_no_freedom_4_full_freedom                                   0.88
## Property_rights_for_women_0_no_rights_5_full_rights                               0.44
## Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom                          0.69
## Access_to_justice_0_nonexistent_1_observed                                        0.52
## Freedom_of_discussion_0_low_1_high                                                0.70
## Political_rights_0_and_lower_no_rights_40_full_rights                             0.54
## Freedom_of_domestic_movement_0_low_1_high                                         0.65
## Early_marriage_pct_of_married_women_aged_15_19                                    0.47
## Satisfied_demand_for_contraception_pct_satisfied_demand                           0.63
## Young_people_not_in_education_employment_or_training_pct_of_youth                 0.75
## Vulnerable_employment_pct_of_total_employment                                     0.28
## Perception_of_corruption_0_high_corruption_100_low_corruption                     0.34
## Equal_protection_index_0_low_1_high                                               0.55
## Equal_access_index_0_low_1_high                                                   0.67
## Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal               0.58
## Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal 0.42
## Acceptance_of_gays_and_lesbians_proportion_of_pop                                 0.64
## Discrimination_and_violence_against_minorities_0_low_10_high                      0.75
## Academic_freedom_0_low_1_high                                                     0.75
## Women_with_advanced_education_proportion_of_females                               0.25
## Expected_years_of_tertiary_schooling_years                                        0.35
## Citable_documents_documents_1_000_people                                          0.46
## Quality_weighted_universities_points                                              0.92
## GDPpc_PPP_2019_log10                                                              0.15
##                                                                                   com
## Child_stunting_0_low_risk_100_high_risk                                             1
## Infectious_diseases_DALYs_100_000                                                   1
## Maternal_mortality_rate_deaths_100_000_live_births                                  1
## Child_mortality_rate_deaths_1_000_live_births                                       1
## Undernourishment_pct_of_pop                                                         1
## Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk                          1
## Unsafe_water_sanitation_and_hygiene_DALYs_100_000                                   1
## Access_to_improved_sanitation_proportion_of_pop                                     1
## Access_to_improved_water_source_proportion_of_pop                                   1
## Satisfaction_with_water_quality_proportion_of_pop                                   1
## Household_air_pollution_DALYs_100_000                                               1
## Dissatisfaction_with_housing_affordability_proportion_of_pop                        1
## Access_to_electricity_pct_of_pop                                                    1
## Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop                          1
## Political_killings_and_torture_0_low_freedom_1_high_freedom                         1
## Interpersonal_violence_DALYs_100_000                                                1
## Transportation_related_injuries_DALYs_100_000                                       1
## Intimate_Partner_Violence_pct_of_women_aged_15plus                                  1
## Money_Stolen_proportion_of_pop                                                      1
## Equal_access_to_quality_education_0_unequal_4_equal                                 1
## Population_with_no_schooling_proportion_of_pop                                      1
## Secondary_school_attainment_pct_of_pop_aged_25plus                                  1
## Primary_school_enrollment_pct_of_children                                           1
## Gender_parity_in_secondary_attainment_distance_from_parity                          1
## Alternative_sources_of_information_index_0_low_1_high                               1
## Mobile_telephone_subscriptions_subscriptions_1_000_people                           1
## Internet_users_pct_of_pop                                                           1
## Access_to_online_governance_0_low_1_high                                            1
## Equal_access_to_quality_healthcare_0_unequal_4_equal                                1
## Life_expectancy_at_60_years                                                         1
## Premature_deaths_from_non_communicable_diseases_deaths_100_000                      1
## Access_to_essential_health_services_0_none_100_full_coverage                        1
## Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop              1
## Outdoor_air_pollution_DALYs_100_000                                                 1
## Lead_exposure_DALYs_100_000                                                         1
## Particulate_matter_pollution_mean_annual_exposure_g_m3                              1
## Species_protection_0_low_100_high                                                   1
## Freedom_of_religion_0_no_freedom_4_full_freedom                                     1
## Property_rights_for_women_0_no_rights_5_full_rights                                 1
## Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom                            1
## Access_to_justice_0_nonexistent_1_observed                                          1
## Freedom_of_discussion_0_low_1_high                                                  1
## Political_rights_0_and_lower_no_rights_40_full_rights                               1
## Freedom_of_domestic_movement_0_low_1_high                                           1
## Early_marriage_pct_of_married_women_aged_15_19                                      1
## Satisfied_demand_for_contraception_pct_satisfied_demand                             1
## Young_people_not_in_education_employment_or_training_pct_of_youth                   1
## Vulnerable_employment_pct_of_total_employment                                       1
## Perception_of_corruption_0_high_corruption_100_low_corruption                       1
## Equal_protection_index_0_low_1_high                                                 1
## Equal_access_index_0_low_1_high                                                     1
## Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal                 1
## Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal   1
## Acceptance_of_gays_and_lesbians_proportion_of_pop                                   1
## Discrimination_and_violence_against_minorities_0_low_10_high                        1
## Academic_freedom_0_low_1_high                                                       1
## Women_with_advanced_education_proportion_of_females                                 1
## Expected_years_of_tertiary_schooling_years                                          1
## Citable_documents_documents_1_000_people                                            1
## Quality_weighted_universities_points                                                1
## GDPpc_PPP_2019_log10                                                                1
## 
##                  MR1
## SS loadings    29.43
## Proportion Var  0.48
## 
## Mean item complexity =  1
## Test of the hypothesis that 1 factor is sufficient.
## 
## df null model =  1830  with the objective function =  100 with Chi Square =  13637
## df of  the model are 1769  and the objective function was  57.9 
## 
## The root mean square of the residuals (RMSR) is  0.14 
## The df corrected root mean square of the residuals is  0.14 
## 
## The harmonic n.obs is  158 with the empirical chi square  11746  with prob <  0 
## The total n.obs was  158  with Likelihood Chi Square =  7825  with prob <  0 
## 
## Tucker Lewis Index of factoring reliability =  0.466
## RMSEA index =  0.147  and the 90 % confidence intervals are  0.144 0.151
## BIC =  -1131
## Fit based upon off diagonal values = 0.92
## Measures of factor score adequacy             
##                                                    MR1
## Correlation of (regression) scores with factors   1.00
## Multiple R square of scores with factors          0.99
## Minimum correlation of possible factor scores     0.99
spi2_efa %>% fa_plot_loadings() +
  geom_point(mapping = aes(color = fa)) +
  scale_color_gradient(low = "red", high = "blue", guide = NULL) +
  ylim(-1, 1) +
  ggtitle("Exploratory factor analysis of Social Progress Index indicators + GDP + median income",
          subtitle = str_glue("n = {nrow(d_spi2)} countries"))

GG_save("figs/SPI2 EFA.png")

#save scores to main dataset
#remove if exists
d$SPI2_EFA = NULL
d = full_join(
  tibble(
    SPI2_EFA = spi2_efa$scores[, 1] %>% standardize(),
    ISO = d_spi2$ISO
    ),
  d
)
## Joining with `by = join_by(ISO)`
d %>% filter(!is.na(SPI_EFA)) %>% select(nice_name, SPI_EFA, SPI2_EFA) %>% mutate(SPI_EFA_rank = rank(-SPI_EFA), SPI2_EFA_rank = rank(-SPI2_EFA), rank_diff = SPI_EFA_rank - SPI2_EFA_rank) %>% arrange(-SPI2_EFA) %>% head(30) %>% print(n=Inf)
## # A tibble: 30 × 6
##    nice_name      SPI_EFA SPI2_EFA SPI_EFA_rank SPI2_EFA_rank rank_diff
##    <chr>            <dbl>    <dbl>        <dbl>         <dbl>     <dbl>
##  1 Luxembourg        1.58     1.67            3             1         2
##  2 Netherlands       1.61     1.60            1             2        -1
##  3 Norway            1.58     1.59            2             3        -1
##  4 Germany           1.54     1.53            4             4         0
##  5 Switzerland       1.50     1.50            7             5         2
##  6 Ireland           1.40     1.50           14             6         8
##  7 Sweden            1.54     1.50            5             7        -2
##  8 Canada            1.53     1.49            6             8        -2
##  9 Finland           1.49     1.47            8             9        -1
## 10 France            1.48     1.46            9            10        -1
## 11 Iceland           1.47     1.45           10            11        -1
## 12 Denmark           1.46     1.45           11            12        -1
## 13 Japan             1.46     1.43           12            13        -1
## 14 Belgium           1.39     1.40           16            14         2
## 15 Australia         1.41     1.39           13            15        -2
## 16 United Kingdom    1.40     1.36           15            16        -1
## 17 Austria           1.35     1.36           19            17         2
## 18 New Zealand       1.38     1.33           17            18        -1
## 19 Slovenia          1.35     1.33           18            19        -1
## 20 Italy             1.22     1.27           22            20         2
## 21 Czech Republic    1.27     1.27           20            21        -1
## 22 Spain             1.22     1.22           23            22         1
## 23 Estonia           1.25     1.20           21            23        -2
## 24 Portugal          1.20     1.19           24            24         0
## 25 Singapore         1.12     1.17           27            25         2
## 26 USA               1.12     1.13           26            26         0
## 27 Cyprus            1.12     1.13           25            27        -2
## 28 Malta             1.11     1.13           28            28         0
## 29 South Korea       1.09     1.10           29            29         0
## 30 Greece            1.04     1.06           33            30         3
#SPI with median income
d_spi3 = d %>% select(ISO, Child_stunting_0_low_risk_100_high_risk:Quality_weighted_universities_points, medianIncome_log10) %>% 
  miss_filter(missing = imp_max_miss)
d_spi3_imp = miss_impute(d_spi3, leave_out = "ISO")

#EFA
spi3_efa = fa(
  d_spi3_imp %>% select(-ISO)
)

spi3_efa
## Factor Analysis using method =  minres
## Call: fa(r = d_spi3_imp %>% select(-ISO))
## Standardized loadings (pattern matrix) based upon correlation matrix
##                                                                                     MR1
## Child_stunting_0_low_risk_100_high_risk                                           -0.87
## Infectious_diseases_DALYs_100_000                                                 -0.73
## Maternal_mortality_rate_deaths_100_000_live_births                                -0.81
## Child_mortality_rate_deaths_1_000_live_births                                     -0.86
## Undernourishment_pct_of_pop                                                       -0.72
## Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk                        -0.59
## Unsafe_water_sanitation_and_hygiene_DALYs_100_000                                 -0.77
## Access_to_improved_sanitation_proportion_of_pop                                    0.85
## Access_to_improved_water_source_proportion_of_pop                                  0.78
## Satisfaction_with_water_quality_proportion_of_pop                                  0.74
## Household_air_pollution_DALYs_100_000                                             -0.83
## Dissatisfaction_with_housing_affordability_proportion_of_pop                      -0.25
## Access_to_electricity_pct_of_pop                                                   0.77
## Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop                         0.82
## Political_killings_and_torture_0_low_freedom_1_high_freedom                        0.69
## Interpersonal_violence_DALYs_100_000                                              -0.28
## Transportation_related_injuries_DALYs_100_000                                     -0.63
## Intimate_Partner_Violence_pct_of_women_aged_15plus                                -0.80
## Money_Stolen_proportion_of_pop                                                    -0.60
## Equal_access_to_quality_education_0_unequal_4_equal                                0.79
## Population_with_no_schooling_proportion_of_pop                                    -0.75
## Secondary_school_attainment_pct_of_pop_aged_25plus                                 0.80
## Primary_school_enrollment_pct_of_children                                          0.66
## Gender_parity_in_secondary_attainment_distance_from_parity                        -0.77
## Alternative_sources_of_information_index_0_low_1_high                              0.35
## Mobile_telephone_subscriptions_subscriptions_1_000_people                          0.63
## Internet_users_pct_of_pop                                                          0.90
## Access_to_online_governance_0_low_1_high                                           0.78
## Equal_access_to_quality_healthcare_0_unequal_4_equal                               0.85
## Life_expectancy_at_60_years                                                        0.82
## Premature_deaths_from_non_communicable_diseases_deaths_100_000                    -0.60
## Access_to_essential_health_services_0_none_100_full_coverage                       0.95
## Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop             0.55
## Outdoor_air_pollution_DALYs_100_000                                               -0.38
## Lead_exposure_DALYs_100_000                                                       -0.64
## Particulate_matter_pollution_mean_annual_exposure_g_m3                            -0.58
## Species_protection_0_low_100_high                                                  0.31
## Freedom_of_religion_0_no_freedom_4_full_freedom                                    0.34
## Property_rights_for_women_0_no_rights_5_full_rights                                0.75
## Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom                           0.57
## Access_to_justice_0_nonexistent_1_observed                                         0.69
## Freedom_of_discussion_0_low_1_high                                                 0.55
## Political_rights_0_and_lower_no_rights_40_full_rights                              0.68
## Freedom_of_domestic_movement_0_low_1_high                                          0.59
## Early_marriage_pct_of_married_women_aged_15_19                                    -0.72
## Satisfied_demand_for_contraception_pct_satisfied_demand                            0.60
## Young_people_not_in_education_employment_or_training_pct_of_youth                 -0.50
## Vulnerable_employment_pct_of_total_employment                                     -0.85
## Perception_of_corruption_0_high_corruption_100_low_corruption                      0.81
## Equal_protection_index_0_low_1_high                                                0.67
## Equal_access_index_0_low_1_high                                                    0.60
## Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal                0.65
## Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal  0.76
## Acceptance_of_gays_and_lesbians_proportion_of_pop                                  0.62
## Discrimination_and_violence_against_minorities_0_low_10_high                      -0.50
## Academic_freedom_0_low_1_high                                                      0.51
## Women_with_advanced_education_proportion_of_females                                0.87
## Expected_years_of_tertiary_schooling_years                                         0.81
## Citable_documents_documents_1_000_people                                           0.74
## Quality_weighted_universities_points                                               0.27
## medianIncome_log10                                                                 0.88
##                                                                                      h2
## Child_stunting_0_low_risk_100_high_risk                                           0.763
## Infectious_diseases_DALYs_100_000                                                 0.535
## Maternal_mortality_rate_deaths_100_000_live_births                                0.661
## Child_mortality_rate_deaths_1_000_live_births                                     0.746
## Undernourishment_pct_of_pop                                                       0.518
## Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk                        0.351
## Unsafe_water_sanitation_and_hygiene_DALYs_100_000                                 0.591
## Access_to_improved_sanitation_proportion_of_pop                                   0.718
## Access_to_improved_water_source_proportion_of_pop                                 0.614
## Satisfaction_with_water_quality_proportion_of_pop                                 0.541
## Household_air_pollution_DALYs_100_000                                             0.690
## Dissatisfaction_with_housing_affordability_proportion_of_pop                      0.061
## Access_to_electricity_pct_of_pop                                                  0.593
## Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop                        0.677
## Political_killings_and_torture_0_low_freedom_1_high_freedom                       0.476
## Interpersonal_violence_DALYs_100_000                                              0.077
## Transportation_related_injuries_DALYs_100_000                                     0.396
## Intimate_Partner_Violence_pct_of_women_aged_15plus                                0.647
## Money_Stolen_proportion_of_pop                                                    0.361
## Equal_access_to_quality_education_0_unequal_4_equal                               0.623
## Population_with_no_schooling_proportion_of_pop                                    0.561
## Secondary_school_attainment_pct_of_pop_aged_25plus                                0.644
## Primary_school_enrollment_pct_of_children                                         0.432
## Gender_parity_in_secondary_attainment_distance_from_parity                        0.588
## Alternative_sources_of_information_index_0_low_1_high                             0.121
## Mobile_telephone_subscriptions_subscriptions_1_000_people                         0.396
## Internet_users_pct_of_pop                                                         0.818
## Access_to_online_governance_0_low_1_high                                          0.601
## Equal_access_to_quality_healthcare_0_unequal_4_equal                              0.720
## Life_expectancy_at_60_years                                                       0.679
## Premature_deaths_from_non_communicable_diseases_deaths_100_000                    0.364
## Access_to_essential_health_services_0_none_100_full_coverage                      0.910
## Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop            0.300
## Outdoor_air_pollution_DALYs_100_000                                               0.148
## Lead_exposure_DALYs_100_000                                                       0.410
## Particulate_matter_pollution_mean_annual_exposure_g_m3                            0.341
## Species_protection_0_low_100_high                                                 0.097
## Freedom_of_religion_0_no_freedom_4_full_freedom                                   0.113
## Property_rights_for_women_0_no_rights_5_full_rights                               0.558
## Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom                          0.330
## Access_to_justice_0_nonexistent_1_observed                                        0.476
## Freedom_of_discussion_0_low_1_high                                                0.307
## Political_rights_0_and_lower_no_rights_40_full_rights                             0.466
## Freedom_of_domestic_movement_0_low_1_high                                         0.349
## Early_marriage_pct_of_married_women_aged_15_19                                    0.524
## Satisfied_demand_for_contraception_pct_satisfied_demand                           0.355
## Young_people_not_in_education_employment_or_training_pct_of_youth                 0.254
## Vulnerable_employment_pct_of_total_employment                                     0.715
## Perception_of_corruption_0_high_corruption_100_low_corruption                     0.655
## Equal_protection_index_0_low_1_high                                               0.454
## Equal_access_index_0_low_1_high                                                   0.357
## Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal               0.425
## Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal 0.579
## Acceptance_of_gays_and_lesbians_proportion_of_pop                                 0.386
## Discrimination_and_violence_against_minorities_0_low_10_high                      0.249
## Academic_freedom_0_low_1_high                                                     0.261
## Women_with_advanced_education_proportion_of_females                               0.753
## Expected_years_of_tertiary_schooling_years                                        0.660
## Citable_documents_documents_1_000_people                                          0.551
## Quality_weighted_universities_points                                              0.076
## medianIncome_log10                                                                0.774
##                                                                                     u2
## Child_stunting_0_low_risk_100_high_risk                                           0.24
## Infectious_diseases_DALYs_100_000                                                 0.46
## Maternal_mortality_rate_deaths_100_000_live_births                                0.34
## Child_mortality_rate_deaths_1_000_live_births                                     0.25
## Undernourishment_pct_of_pop                                                       0.48
## Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk                        0.65
## Unsafe_water_sanitation_and_hygiene_DALYs_100_000                                 0.41
## Access_to_improved_sanitation_proportion_of_pop                                   0.28
## Access_to_improved_water_source_proportion_of_pop                                 0.39
## Satisfaction_with_water_quality_proportion_of_pop                                 0.46
## Household_air_pollution_DALYs_100_000                                             0.31
## Dissatisfaction_with_housing_affordability_proportion_of_pop                      0.94
## Access_to_electricity_pct_of_pop                                                  0.41
## Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop                        0.32
## Political_killings_and_torture_0_low_freedom_1_high_freedom                       0.52
## Interpersonal_violence_DALYs_100_000                                              0.92
## Transportation_related_injuries_DALYs_100_000                                     0.60
## Intimate_Partner_Violence_pct_of_women_aged_15plus                                0.35
## Money_Stolen_proportion_of_pop                                                    0.64
## Equal_access_to_quality_education_0_unequal_4_equal                               0.38
## Population_with_no_schooling_proportion_of_pop                                    0.44
## Secondary_school_attainment_pct_of_pop_aged_25plus                                0.36
## Primary_school_enrollment_pct_of_children                                         0.57
## Gender_parity_in_secondary_attainment_distance_from_parity                        0.41
## Alternative_sources_of_information_index_0_low_1_high                             0.88
## Mobile_telephone_subscriptions_subscriptions_1_000_people                         0.60
## Internet_users_pct_of_pop                                                         0.18
## Access_to_online_governance_0_low_1_high                                          0.40
## Equal_access_to_quality_healthcare_0_unequal_4_equal                              0.28
## Life_expectancy_at_60_years                                                       0.32
## Premature_deaths_from_non_communicable_diseases_deaths_100_000                    0.64
## Access_to_essential_health_services_0_none_100_full_coverage                      0.09
## Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop            0.70
## Outdoor_air_pollution_DALYs_100_000                                               0.85
## Lead_exposure_DALYs_100_000                                                       0.59
## Particulate_matter_pollution_mean_annual_exposure_g_m3                            0.66
## Species_protection_0_low_100_high                                                 0.90
## Freedom_of_religion_0_no_freedom_4_full_freedom                                   0.89
## Property_rights_for_women_0_no_rights_5_full_rights                               0.44
## Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom                          0.67
## Access_to_justice_0_nonexistent_1_observed                                        0.52
## Freedom_of_discussion_0_low_1_high                                                0.69
## Political_rights_0_and_lower_no_rights_40_full_rights                             0.53
## Freedom_of_domestic_movement_0_low_1_high                                         0.65
## Early_marriage_pct_of_married_women_aged_15_19                                    0.48
## Satisfied_demand_for_contraception_pct_satisfied_demand                           0.64
## Young_people_not_in_education_employment_or_training_pct_of_youth                 0.75
## Vulnerable_employment_pct_of_total_employment                                     0.29
## Perception_of_corruption_0_high_corruption_100_low_corruption                     0.35
## Equal_protection_index_0_low_1_high                                               0.55
## Equal_access_index_0_low_1_high                                                   0.64
## Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal               0.57
## Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal 0.42
## Acceptance_of_gays_and_lesbians_proportion_of_pop                                 0.61
## Discrimination_and_violence_against_minorities_0_low_10_high                      0.75
## Academic_freedom_0_low_1_high                                                     0.74
## Women_with_advanced_education_proportion_of_females                               0.25
## Expected_years_of_tertiary_schooling_years                                        0.34
## Citable_documents_documents_1_000_people                                          0.45
## Quality_weighted_universities_points                                              0.92
## medianIncome_log10                                                                0.23
##                                                                                   com
## Child_stunting_0_low_risk_100_high_risk                                             1
## Infectious_diseases_DALYs_100_000                                                   1
## Maternal_mortality_rate_deaths_100_000_live_births                                  1
## Child_mortality_rate_deaths_1_000_live_births                                       1
## Undernourishment_pct_of_pop                                                         1
## Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk                          1
## Unsafe_water_sanitation_and_hygiene_DALYs_100_000                                   1
## Access_to_improved_sanitation_proportion_of_pop                                     1
## Access_to_improved_water_source_proportion_of_pop                                   1
## Satisfaction_with_water_quality_proportion_of_pop                                   1
## Household_air_pollution_DALYs_100_000                                               1
## Dissatisfaction_with_housing_affordability_proportion_of_pop                        1
## Access_to_electricity_pct_of_pop                                                    1
## Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop                          1
## Political_killings_and_torture_0_low_freedom_1_high_freedom                         1
## Interpersonal_violence_DALYs_100_000                                                1
## Transportation_related_injuries_DALYs_100_000                                       1
## Intimate_Partner_Violence_pct_of_women_aged_15plus                                  1
## Money_Stolen_proportion_of_pop                                                      1
## Equal_access_to_quality_education_0_unequal_4_equal                                 1
## Population_with_no_schooling_proportion_of_pop                                      1
## Secondary_school_attainment_pct_of_pop_aged_25plus                                  1
## Primary_school_enrollment_pct_of_children                                           1
## Gender_parity_in_secondary_attainment_distance_from_parity                          1
## Alternative_sources_of_information_index_0_low_1_high                               1
## Mobile_telephone_subscriptions_subscriptions_1_000_people                           1
## Internet_users_pct_of_pop                                                           1
## Access_to_online_governance_0_low_1_high                                            1
## Equal_access_to_quality_healthcare_0_unequal_4_equal                                1
## Life_expectancy_at_60_years                                                         1
## Premature_deaths_from_non_communicable_diseases_deaths_100_000                      1
## Access_to_essential_health_services_0_none_100_full_coverage                        1
## Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop              1
## Outdoor_air_pollution_DALYs_100_000                                                 1
## Lead_exposure_DALYs_100_000                                                         1
## Particulate_matter_pollution_mean_annual_exposure_g_m3                              1
## Species_protection_0_low_100_high                                                   1
## Freedom_of_religion_0_no_freedom_4_full_freedom                                     1
## Property_rights_for_women_0_no_rights_5_full_rights                                 1
## Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom                            1
## Access_to_justice_0_nonexistent_1_observed                                          1
## Freedom_of_discussion_0_low_1_high                                                  1
## Political_rights_0_and_lower_no_rights_40_full_rights                               1
## Freedom_of_domestic_movement_0_low_1_high                                           1
## Early_marriage_pct_of_married_women_aged_15_19                                      1
## Satisfied_demand_for_contraception_pct_satisfied_demand                             1
## Young_people_not_in_education_employment_or_training_pct_of_youth                   1
## Vulnerable_employment_pct_of_total_employment                                       1
## Perception_of_corruption_0_high_corruption_100_low_corruption                       1
## Equal_protection_index_0_low_1_high                                                 1
## Equal_access_index_0_low_1_high                                                     1
## Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal                 1
## Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal   1
## Acceptance_of_gays_and_lesbians_proportion_of_pop                                   1
## Discrimination_and_violence_against_minorities_0_low_10_high                        1
## Academic_freedom_0_low_1_high                                                       1
## Women_with_advanced_education_proportion_of_females                                 1
## Expected_years_of_tertiary_schooling_years                                          1
## Citable_documents_documents_1_000_people                                            1
## Quality_weighted_universities_points                                                1
## medianIncome_log10                                                                  1
## 
##                  MR1
## SS loadings    29.40
## Proportion Var  0.48
## 
## Mean item complexity =  1
## Test of the hypothesis that 1 factor is sufficient.
## 
## df null model =  1830  with the objective function =  101 with Chi Square =  13386
## df of  the model are 1769  and the objective function was  58.8 
## 
## The root mean square of the residuals (RMSR) is  0.14 
## The df corrected root mean square of the residuals is  0.14 
## 
## The harmonic n.obs is  155 with the empirical chi square  11363  with prob <  0 
## The total n.obs was  155  with Likelihood Chi Square =  7771  with prob <  0 
## 
## Tucker Lewis Index of factoring reliability =  0.46
## RMSEA index =  0.148  and the 90 % confidence intervals are  0.145 0.152
## BIC =  -1151
## Fit based upon off diagonal values = 0.92
## Measures of factor score adequacy             
##                                                    MR1
## Correlation of (regression) scores with factors   1.00
## Multiple R square of scores with factors          0.99
## Minimum correlation of possible factor scores     0.99
spi3_efa %>% fa_plot_loadings() +
  geom_point(mapping = aes(color = fa)) +
  scale_color_gradient(low = "red", high = "blue", guide = NULL) +
  ylim(-1, 1) +
  ggtitle("Exploratory factor analysis of Social Progress Index indicators + GDP + median income",
          subtitle = str_glue("n = {nrow(d_spi3)} countries"))

GG_save("figs/SPI3 EFA.png")

#save scores to main dataset
#remove if exists
d$SPI3_EFA = NULL
d = full_join(
  tibble(
    SPI3_EFA = spi3_efa$scores[, 1] %>% standardize(),
    ISO = d_spi3$ISO
    ),
  d
)
## Joining with `by = join_by(ISO)`
d %>% filter(!is.na(SPI_EFA)) %>% select(nice_name, SPI_EFA, SPI3_EFA) %>% mutate(SPI_EFA_rank = rank(-SPI_EFA), SPI3_EFA_rank = rank(-SPI3_EFA), rank_diff = SPI_EFA_rank - SPI3_EFA_rank) %>% arrange(-SPI3_EFA) %>% head(30) %>% print(n=Inf)
## # A tibble: 30 × 6
##    nice_name      SPI_EFA SPI3_EFA SPI_EFA_rank SPI3_EFA_rank rank_diff
##    <chr>            <dbl>    <dbl>        <dbl>         <dbl>     <dbl>
##  1 Netherlands       1.61     1.62            1             1         0
##  2 Norway            1.58     1.59            2             2         0
##  3 Luxembourg        1.58     1.59            3             3         0
##  4 Sweden            1.54     1.54            5             4         1
##  5 Canada            1.53     1.54            6             5         1
##  6 Germany           1.54     1.52            4             6        -2
##  7 Iceland           1.47     1.52           10             7         3
##  8 Switzerland       1.50     1.51            7             8        -1
##  9 Finland           1.49     1.48            8             9        -1
## 10 Denmark           1.46     1.48           11            10         1
## 11 Japan             1.46     1.48           12            11         1
## 12 France            1.48     1.46            9            12        -3
## 13 Belgium           1.39     1.42           16            13         3
## 14 United Kingdom    1.40     1.41           15            14         1
## 15 Australia         1.41     1.41           13            15        -2
## 16 Ireland           1.40     1.39           14            16        -2
## 17 New Zealand       1.38     1.39           17            17         0
## 18 Austria           1.35     1.34           19            18         1
## 19 Slovenia          1.35     1.33           18            19        -1
## 20 Spain             1.22     1.27           23            20         3
## 21 Czech Republic    1.27     1.26           20            21        -1
## 22 Estonia           1.25     1.24           21            22        -1
## 23 Portugal          1.20     1.21           24            23         1
## 24 Italy             1.22     1.18           22            24        -2
## 25 USA               1.12     1.14           26            25         1
## 26 Cyprus            1.12     1.13           25            26        -1
## 27 Singapore         1.12     1.12           27            27         0
## 28 Malta             1.11     1.11           28            28         0
## 29 South Korea       1.09     1.10           29            29         0
## 30 Slovakia          1.05     1.06           30            30         0

SPI bifactor models

#use their theoretical model
spi_model = str_glue("
#general factor
S =~ {str_c(names(d_spi_all_imp[-1]), collapse = ' + ')}

#nutrition basic medical care
nutrition_basic_med_care =~ Child_stunting_0_low_risk_100_high_risk + Infectious_diseases_DALYs_100_000 + Maternal_mortality_rate_deaths_100_000_live_births + Child_mortality_rate_deaths_1_000_live_births + Undernourishment_pct_of_pop + Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk

#water sanitation
water_sanitation =~ Unsafe_water_sanitation_and_hygiene_DALYs_100_000 + Access_to_improved_sanitation_proportion_of_pop + Access_to_improved_water_source_proportion_of_pop + Satisfaction_with_water_quality_proportion_of_pop

#shelter
shelter =~ Household_air_pollution_DALYs_100_000 + Dissatisfaction_with_housing_affordability_proportion_of_pop + Access_to_electricity_pct_of_pop + Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop

#personal safety
personal_safety =~ Political_killings_and_torture_0_low_freedom_1_high_freedom + Interpersonal_violence_DALYs_100_000 + Transportation_related_injuries_DALYs_100_000 + Intimate_Partner_Violence_pct_of_women_aged_15plus + Money_Stolen_proportion_of_pop

#access basic knowledge
basic_knowledge =~ Equal_access_to_quality_education_0_unequal_4_equal + Population_with_no_schooling_proportion_of_pop + Secondary_school_attainment_pct_of_pop_aged_25plus + Primary_school_enrollment_pct_of_children + Gender_parity_in_secondary_attainment_distance_from_parity

#access to information
info_access =~ Alternative_sources_of_information_index_0_low_1_high + Mobile_telephone_subscriptions_subscriptions_1_000_people + Internet_users_pct_of_pop + Access_to_online_governance_0_low_1_high

#health and wellness
health_wellness =~ Equal_access_to_quality_healthcare_0_unequal_4_equal + Life_expectancy_at_60_years + Premature_deaths_from_non_communicable_diseases_deaths_100_000 + Access_to_essential_health_services_0_none_100_full_coverage + Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop

#environmental quality
envir_quality =~ Outdoor_air_pollution_DALYs_100_000 + Lead_exposure_DALYs_100_000 + Particulate_matter_pollution_mean_annual_exposure_g_m3 + Species_protection_0_low_100_high

#personal rights
personal_rights =~ Freedom_of_religion_0_no_freedom_4_full_freedom + Property_rights_for_women_0_no_rights_5_full_rights + Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom + Access_to_justice_0_nonexistent_1_observed + Freedom_of_discussion_0_low_1_high + Political_rights_0_and_lower_no_rights_40_full_rights

#personal freedom and choice
personal_freedom_a_choice =~ Freedom_of_domestic_movement_0_low_1_high + Early_marriage_pct_of_married_women_aged_15_19 + Satisfied_demand_for_contraception_pct_satisfied_demand + Young_people_not_in_education_employment_or_training_pct_of_youth + Vulnerable_employment_pct_of_total_employment + Perception_of_corruption_0_high_corruption_100_low_corruption

#inclusiveness
inclusiveness =~ Equal_protection_index_0_low_1_high + Equal_access_index_0_low_1_high + Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal + Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal + Acceptance_of_gays_and_lesbians_proportion_of_pop + Discrimination_and_violence_against_minorities_0_low_10_high

#access advanced education
advanced_education =~ Academic_freedom_0_low_1_high + Women_with_advanced_education_proportion_of_females + Expected_years_of_tertiary_schooling_years + Citable_documents_documents_1_000_people + Quality_weighted_universities_points

#economics
economics =~ medianIncome_log10 + meanIncome_log10 + GDPpc_PPP_2019_log10 + GNIpc_PPP_2019_log10
")

#attempt to fit
spi_all_fit_spi_model = sem(
  spi_model,
  data = d_spi_all_imp_z,
  orthogonal = T,
  std.lv = T,
  std.ov = T,
  ridge = 1,
  estimator = "GLS"
)

spi_all_fit_spi_model
spi_all_fit_spi_model %>% summary()

parameterestimates(spi_all_fit_spi_model, standardized = T)

#get scores
# spi_all_fit_spi_model_scores = lavaan::lavScores(spi_all_fit_spi_model)
spi_all_fit_spi_model_scores = predict(spi_all_fit_spi_model)

SPI hierarhical models

Bifactor failed us, but maybe a hierarchical factor will work.

SPI_hier_model = str_glue("
#nutrition basic medical care
nutrition_basic_med_care =~ Child_stunting_0_low_risk_100_high_risk + Infectious_diseases_DALYs_100_000 + Maternal_mortality_rate_deaths_100_000_live_births + Child_mortality_rate_deaths_1_000_live_births + Undernourishment_pct_of_pop + Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk

#water sanitation
water_sanitation =~ Unsafe_water_sanitation_and_hygiene_DALYs_100_000 + Access_to_improved_sanitation_proportion_of_pop + Access_to_improved_water_source_proportion_of_pop + Satisfaction_with_water_quality_proportion_of_pop

#shelter
shelter =~ Household_air_pollution_DALYs_100_000 + Dissatisfaction_with_housing_affordability_proportion_of_pop + Access_to_electricity_pct_of_pop + Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop

#personal safety
personal_safety =~ Political_killings_and_torture_0_low_freedom_1_high_freedom + Interpersonal_violence_DALYs_100_000 + Transportation_related_injuries_DALYs_100_000 + Intimate_Partner_Violence_pct_of_women_aged_15plus + Money_Stolen_proportion_of_pop

#access basic knowledge
basic_knowledge =~ Equal_access_to_quality_education_0_unequal_4_equal + Population_with_no_schooling_proportion_of_pop + Secondary_school_attainment_pct_of_pop_aged_25plus + Primary_school_enrollment_pct_of_children + Gender_parity_in_secondary_attainment_distance_from_parity

#access to information
info_access =~ Alternative_sources_of_information_index_0_low_1_high + Mobile_telephone_subscriptions_subscriptions_1_000_people + Internet_users_pct_of_pop + Access_to_online_governance_0_low_1_high

#health and wellness
health_wellness =~ Equal_access_to_quality_healthcare_0_unequal_4_equal + Life_expectancy_at_60_years + Premature_deaths_from_non_communicable_diseases_deaths_100_000 + Access_to_essential_health_services_0_none_100_full_coverage + Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop

#environmental quality
envir_quality =~ Outdoor_air_pollution_DALYs_100_000 + Lead_exposure_DALYs_100_000 + Particulate_matter_pollution_mean_annual_exposure_g_m3 + Species_protection_0_low_100_high

#personal rights
personal_rights =~ Freedom_of_religion_0_no_freedom_4_full_freedom + Property_rights_for_women_0_no_rights_5_full_rights + Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom + Access_to_justice_0_nonexistent_1_observed + Freedom_of_discussion_0_low_1_high + Political_rights_0_and_lower_no_rights_40_full_rights

#personal freedom and choice
personal_freedom_a_choice =~ Freedom_of_domestic_movement_0_low_1_high + Early_marriage_pct_of_married_women_aged_15_19 + Satisfied_demand_for_contraception_pct_satisfied_demand + Young_people_not_in_education_employment_or_training_pct_of_youth + Vulnerable_employment_pct_of_total_employment + Perception_of_corruption_0_high_corruption_100_low_corruption

#inclusiveness
inclusiveness =~ Equal_protection_index_0_low_1_high + Equal_access_index_0_low_1_high + Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal + Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal + Acceptance_of_gays_and_lesbians_proportion_of_pop + Discrimination_and_violence_against_minorities_0_low_10_high

#access advanced education
advanced_education =~ Academic_freedom_0_low_1_high + Women_with_advanced_education_proportion_of_females + Expected_years_of_tertiary_schooling_years + Citable_documents_documents_1_000_people + Quality_weighted_universities_points

#economics
economics =~ medianIncome_log10 + meanIncome_log10 + GDPpc_PPP_2019_log10 + GNIpc_PPP_2019_log10

#second order
basic_human_needs =~ nutrition_basic_med_care + water_sanitation + shelter + personal_safety
foundations_wellbeing =~ basic_knowledge + info_access + health_wellness + envir_quality
opportunity =~ personal_rights + personal_freedom_a_choice + inclusiveness + advanced_education

#third order
S =~ basic_human_needs + foundations_wellbeing + opportunity + economics
")

#fit
SPI_hier_fit = sem(
  SPI_hier_model,
  data = d_spi_all_imp_z,
  estimator = "DWLS"
  #options: ML GLS WLS ULS DWLS DLS
)

EFA bifactor

efa_bifactor = omega(
  d_spi_all_imp_z[-1],
  nfactors = 3
)
## Loading required namespace: GPArotation
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.

efa_bifactor
## Omega 
## Call: omegah(m = m, nfactors = nfactors, fm = fm, key = key, flip = flip, 
##     digits = digits, title = title, sl = sl, labels = labels, 
##     plot = plot, n.obs = n.obs, rotate = rotate, Phi = Phi, option = option, 
##     covar = covar)
## Alpha:                 0.98 
## G.6:                   1 
## Omega Hierarchical:    0.61 
## Omega H asymptotic:    0.62 
## Omega Total            0.99 
## 
## Schmid Leiman Factor loadings greater than  0.2 
##                                                                                      g
## Child_stunting_0_low_risk_100_high_risk-                                          0.62
## Infectious_diseases_DALYs_100_000-                                                0.47
## Maternal_mortality_rate_deaths_100_000_live_births-                               0.55
## Child_mortality_rate_deaths_1_000_live_births-                                    0.59
## Undernourishment_pct_of_pop-                                                      0.51
## Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk-                       0.36
## Unsafe_water_sanitation_and_hygiene_DALYs_100_000-                                0.48
## Access_to_improved_sanitation_proportion_of_pop                                   0.56
## Access_to_improved_water_source_proportion_of_pop                                 0.52
## Satisfaction_with_water_quality_proportion_of_pop                                 0.65
## Household_air_pollution_DALYs_100_000-                                            0.51
## Dissatisfaction_with_housing_affordability_proportion_of_pop-                     0.22
## Access_to_electricity_pct_of_pop                                                  0.46
## Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop                        0.52
## Political_killings_and_torture_0_low_freedom_1_high_freedom                       0.57
## Interpersonal_violence_DALYs_100_000-                                             0.28
## Transportation_related_injuries_DALYs_100_000-                                    0.53
## Intimate_Partner_Violence_pct_of_women_aged_15plus-                               0.60
## Money_Stolen_proportion_of_pop-                                                   0.43
## Equal_access_to_quality_education_0_unequal_4_equal                               0.66
## Population_with_no_schooling_proportion_of_pop-                                   0.51
## Secondary_school_attainment_pct_of_pop_aged_25plus                                0.56
## Primary_school_enrollment_pct_of_children                                         0.47
## Gender_parity_in_secondary_attainment_distance_from_parity-                       0.50
## Alternative_sources_of_information_index_0_low_1_high                             0.34
## Mobile_telephone_subscriptions_subscriptions_1_000_people                         0.41
## Internet_users_pct_of_pop                                                         0.68
## Access_to_online_governance_0_low_1_high                                          0.62
## Equal_access_to_quality_healthcare_0_unequal_4_equal                              0.69
## Life_expectancy_at_60_years                                                       0.67
## Premature_deaths_from_non_communicable_diseases_deaths_100_000-                   0.56
## Access_to_essential_health_services_0_none_100_full_coverage                      0.73
## Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop            0.55
## Outdoor_air_pollution_DALYs_100_000-                                              0.49
## Lead_exposure_DALYs_100_000-                                                      0.57
## Particulate_matter_pollution_mean_annual_exposure_g_m3-                           0.50
## Species_protection_0_low_100_high                                                 0.38
## Freedom_of_religion_0_no_freedom_4_full_freedom                                   0.37
## Property_rights_for_women_0_no_rights_5_full_rights                               0.56
## Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom                          0.47
## Access_to_justice_0_nonexistent_1_observed                                        0.61
## Freedom_of_discussion_0_low_1_high                                                0.46
## Political_rights_0_and_lower_no_rights_40_full_rights                             0.60
## Freedom_of_domestic_movement_0_low_1_high                                         0.51
## Early_marriage_pct_of_married_women_aged_15_19-                                   0.52
## Satisfied_demand_for_contraception_pct_satisfied_demand                           0.49
## Young_people_not_in_education_employment_or_training_pct_of_youth-                0.54
## Vulnerable_employment_pct_of_total_employment-                                    0.59
## Perception_of_corruption_0_high_corruption_100_low_corruption                     0.75
## Equal_protection_index_0_low_1_high                                               0.61
## Equal_access_index_0_low_1_high                                                   0.57
## Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal               0.61
## Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal 0.65
## Acceptance_of_gays_and_lesbians_proportion_of_pop                                 0.61
## Discrimination_and_violence_against_minorities_0_low_10_high-                     0.48
## Academic_freedom_0_low_1_high                                                     0.46
## Women_with_advanced_education_proportion_of_females                               0.66
## Expected_years_of_tertiary_schooling_years                                        0.58
## Citable_documents_documents_1_000_people                                          0.71
## Quality_weighted_universities_points                                              0.23
## medianIncome_log10                                                                0.73
## meanIncome_log10                                                                  0.73
## GDPpc_PPP_2019_log10                                                              0.69
## GNIpc_PPP_2019_log10                                                              0.69
##                                                                                     F1*
## Child_stunting_0_low_risk_100_high_risk-                                           0.63
## Infectious_diseases_DALYs_100_000-                                                 0.72
## Maternal_mortality_rate_deaths_100_000_live_births-                                0.71
## Child_mortality_rate_deaths_1_000_live_births-                                     0.73
## Undernourishment_pct_of_pop-                                                       0.51
## Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk-                        0.65
## Unsafe_water_sanitation_and_hygiene_DALYs_100_000-                                 0.75
## Access_to_improved_sanitation_proportion_of_pop                                    0.74
## Access_to_improved_water_source_proportion_of_pop                                  0.66
## Satisfaction_with_water_quality_proportion_of_pop                                  0.33
## Household_air_pollution_DALYs_100_000-                                             0.82
## Dissatisfaction_with_housing_affordability_proportion_of_pop-                          
## Access_to_electricity_pct_of_pop                                                   0.80
## Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop                         0.77
## Political_killings_and_torture_0_low_freedom_1_high_freedom                            
## Interpersonal_violence_DALYs_100_000-                                                  
## Transportation_related_injuries_DALYs_100_000-                                     0.30
## Intimate_Partner_Violence_pct_of_women_aged_15plus-                                0.52
## Money_Stolen_proportion_of_pop-                                                    0.55
## Equal_access_to_quality_education_0_unequal_4_equal                                0.36
## Population_with_no_schooling_proportion_of_pop-                                    0.59
## Secondary_school_attainment_pct_of_pop_aged_25plus                                 0.61
## Primary_school_enrollment_pct_of_children                                          0.46
## Gender_parity_in_secondary_attainment_distance_from_parity-                        0.67
## Alternative_sources_of_information_index_0_low_1_high                                  
## Mobile_telephone_subscriptions_subscriptions_1_000_people                          0.54
## Internet_users_pct_of_pop                                                          0.63
## Access_to_online_governance_0_low_1_high                                           0.44
## Equal_access_to_quality_healthcare_0_unequal_4_equal                               0.42
## Life_expectancy_at_60_years                                                        0.43
## Premature_deaths_from_non_communicable_diseases_deaths_100_000-                    0.25
## Access_to_essential_health_services_0_none_100_full_coverage                       0.61
## Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop                 
## Outdoor_air_pollution_DALYs_100_000-                                              -0.20
## Lead_exposure_DALYs_100_000-                                                       0.22
## Particulate_matter_pollution_mean_annual_exposure_g_m3-                            0.22
## Species_protection_0_low_100_high                                                      
## Freedom_of_religion_0_no_freedom_4_full_freedom                                   -0.21
## Property_rights_for_women_0_no_rights_5_full_rights                                0.35
## Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom                               
## Access_to_justice_0_nonexistent_1_observed                                             
## Freedom_of_discussion_0_low_1_high                                                     
## Political_rights_0_and_lower_no_rights_40_full_rights                                  
## Freedom_of_domestic_movement_0_low_1_high                                              
## Early_marriage_pct_of_married_women_aged_15_19-                                    0.57
## Satisfied_demand_for_contraception_pct_satisfied_demand                            0.31
## Young_people_not_in_education_employment_or_training_pct_of_youth-                     
## Vulnerable_employment_pct_of_total_employment-                                     0.70
## Perception_of_corruption_0_high_corruption_100_low_corruption                      0.20
## Equal_protection_index_0_low_1_high                                                    
## Equal_access_index_0_low_1_high                                                        
## Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal                    
## Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal  0.30
## Acceptance_of_gays_and_lesbians_proportion_of_pop                                      
## Discrimination_and_violence_against_minorities_0_low_10_high-                          
## Academic_freedom_0_low_1_high                                                          
## Women_with_advanced_education_proportion_of_females                                0.53
## Expected_years_of_tertiary_schooling_years                                         0.56
## Citable_documents_documents_1_000_people                                               
## Quality_weighted_universities_points                                                   
## medianIncome_log10                                                                 0.55
## meanIncome_log10                                                                   0.55
## GDPpc_PPP_2019_log10                                                               0.65
## GNIpc_PPP_2019_log10                                                               0.65
##                                                                                     F2*
## Child_stunting_0_low_risk_100_high_risk-                                               
## Infectious_diseases_DALYs_100_000-                                                     
## Maternal_mortality_rate_deaths_100_000_live_births-                                    
## Child_mortality_rate_deaths_1_000_live_births-                                         
## Undernourishment_pct_of_pop-                                                           
## Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk-                            
## Unsafe_water_sanitation_and_hygiene_DALYs_100_000-                                     
## Access_to_improved_sanitation_proportion_of_pop                                        
## Access_to_improved_water_source_proportion_of_pop                                      
## Satisfaction_with_water_quality_proportion_of_pop                                      
## Household_air_pollution_DALYs_100_000-                                                 
## Dissatisfaction_with_housing_affordability_proportion_of_pop-                     -0.23
## Access_to_electricity_pct_of_pop                                                       
## Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop                             
## Political_killings_and_torture_0_low_freedom_1_high_freedom                        0.66
## Interpersonal_violence_DALYs_100_000-                                                  
## Transportation_related_injuries_DALYs_100_000-                                         
## Intimate_Partner_Violence_pct_of_women_aged_15plus-                                    
## Money_Stolen_proportion_of_pop-                                                   -0.24
## Equal_access_to_quality_education_0_unequal_4_equal                                    
## Population_with_no_schooling_proportion_of_pop-                                        
## Secondary_school_attainment_pct_of_pop_aged_25plus                                     
## Primary_school_enrollment_pct_of_children                                              
## Gender_parity_in_secondary_attainment_distance_from_parity-                            
## Alternative_sources_of_information_index_0_low_1_high                              0.78
## Mobile_telephone_subscriptions_subscriptions_1_000_people                              
## Internet_users_pct_of_pop                                                              
## Access_to_online_governance_0_low_1_high                                               
## Equal_access_to_quality_healthcare_0_unequal_4_equal                                   
## Life_expectancy_at_60_years                                                            
## Premature_deaths_from_non_communicable_diseases_deaths_100_000-                        
## Access_to_essential_health_services_0_none_100_full_coverage                           
## Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop            -0.22
## Outdoor_air_pollution_DALYs_100_000-                                               0.39
## Lead_exposure_DALYs_100_000-                                                       0.24
## Particulate_matter_pollution_mean_annual_exposure_g_m3-                            0.33
## Species_protection_0_low_100_high                                                  0.22
## Freedom_of_religion_0_no_freedom_4_full_freedom                                    0.67
## Property_rights_for_women_0_no_rights_5_full_rights                                0.48
## Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom                           0.76
## Access_to_justice_0_nonexistent_1_observed                                         0.58
## Freedom_of_discussion_0_low_1_high                                                 0.81
## Political_rights_0_and_lower_no_rights_40_full_rights                              0.69
## Freedom_of_domestic_movement_0_low_1_high                                          0.68
## Early_marriage_pct_of_married_women_aged_15_19-                                        
## Satisfied_demand_for_contraception_pct_satisfied_demand                                
## Young_people_not_in_education_employment_or_training_pct_of_youth-                     
## Vulnerable_employment_pct_of_total_employment-                                         
## Perception_of_corruption_0_high_corruption_100_low_corruption                      0.26
## Equal_protection_index_0_low_1_high                                                0.52
## Equal_access_index_0_low_1_high                                                    0.66
## Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal                0.43
## Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal  0.25
## Acceptance_of_gays_and_lesbians_proportion_of_pop                                  0.27
## Discrimination_and_violence_against_minorities_0_low_10_high-                      0.36
## Academic_freedom_0_low_1_high                                                      0.81
## Women_with_advanced_education_proportion_of_females                                    
## Expected_years_of_tertiary_schooling_years                                             
## Citable_documents_documents_1_000_people                                               
## Quality_weighted_universities_points                                                   
## medianIncome_log10                                                                     
## meanIncome_log10                                                                       
## GDPpc_PPP_2019_log10                                                                   
## GNIpc_PPP_2019_log10                                                                   
##                                                                                     F3*
## Child_stunting_0_low_risk_100_high_risk-                                               
## Infectious_diseases_DALYs_100_000-                                                     
## Maternal_mortality_rate_deaths_100_000_live_births-                                    
## Child_mortality_rate_deaths_1_000_live_births-                                         
## Undernourishment_pct_of_pop-                                                           
## Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk-                            
## Unsafe_water_sanitation_and_hygiene_DALYs_100_000-                                     
## Access_to_improved_sanitation_proportion_of_pop                                        
## Access_to_improved_water_source_proportion_of_pop                                      
## Satisfaction_with_water_quality_proportion_of_pop                                  0.38
## Household_air_pollution_DALYs_100_000-                                                 
## Dissatisfaction_with_housing_affordability_proportion_of_pop-                      0.24
## Access_to_electricity_pct_of_pop                                                       
## Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop                             
## Political_killings_and_torture_0_low_freedom_1_high_freedom                            
## Interpersonal_violence_DALYs_100_000-                                              0.28
## Transportation_related_injuries_DALYs_100_000-                                         
## Intimate_Partner_Violence_pct_of_women_aged_15plus-                                    
## Money_Stolen_proportion_of_pop-                                                        
## Equal_access_to_quality_education_0_unequal_4_equal                                0.24
## Population_with_no_schooling_proportion_of_pop-                                        
## Secondary_school_attainment_pct_of_pop_aged_25plus                                     
## Primary_school_enrollment_pct_of_children                                              
## Gender_parity_in_secondary_attainment_distance_from_parity-                            
## Alternative_sources_of_information_index_0_low_1_high                                  
## Mobile_telephone_subscriptions_subscriptions_1_000_people                              
## Internet_users_pct_of_pop                                                              
## Access_to_online_governance_0_low_1_high                                               
## Equal_access_to_quality_healthcare_0_unequal_4_equal                               0.22
## Life_expectancy_at_60_years                                                        0.20
## Premature_deaths_from_non_communicable_diseases_deaths_100_000-                    0.22
## Access_to_essential_health_services_0_none_100_full_coverage                           
## Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop             0.49
## Outdoor_air_pollution_DALYs_100_000-                                               0.34
## Lead_exposure_DALYs_100_000-                                                       0.21
## Particulate_matter_pollution_mean_annual_exposure_g_m3-                                
## Species_protection_0_low_100_high                                                  0.29
## Freedom_of_religion_0_no_freedom_4_full_freedom                                        
## Property_rights_for_women_0_no_rights_5_full_rights                                    
## Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom                               
## Access_to_justice_0_nonexistent_1_observed                                             
## Freedom_of_discussion_0_low_1_high                                                     
## Political_rights_0_and_lower_no_rights_40_full_rights                                  
## Freedom_of_domestic_movement_0_low_1_high                                              
## Early_marriage_pct_of_married_women_aged_15_19-                                        
## Satisfied_demand_for_contraception_pct_satisfied_demand                                
## Young_people_not_in_education_employment_or_training_pct_of_youth-                 0.47
## Vulnerable_employment_pct_of_total_employment-                                         
## Perception_of_corruption_0_high_corruption_100_low_corruption                      0.38
## Equal_protection_index_0_low_1_high                                                    
## Equal_access_index_0_low_1_high                                                        
## Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal                0.23
## Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal  0.22
## Acceptance_of_gays_and_lesbians_proportion_of_pop                                  0.32
## Discrimination_and_violence_against_minorities_0_low_10_high-                          
## Academic_freedom_0_low_1_high                                                          
## Women_with_advanced_education_proportion_of_females                                    
## Expected_years_of_tertiary_schooling_years                                             
## Citable_documents_documents_1_000_people                                           0.41
## Quality_weighted_universities_points                                                   
## medianIncome_log10                                                                     
## meanIncome_log10                                                                       
## GDPpc_PPP_2019_log10                                                                   
## GNIpc_PPP_2019_log10                                                                   
##                                                                                     h2
## Child_stunting_0_low_risk_100_high_risk-                                          0.81
## Infectious_diseases_DALYs_100_000-                                                0.74
## Maternal_mortality_rate_deaths_100_000_live_births-                               0.82
## Child_mortality_rate_deaths_1_000_live_births-                                    0.88
## Undernourishment_pct_of_pop-                                                      0.53
## Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk-                       0.58
## Unsafe_water_sanitation_and_hygiene_DALYs_100_000-                                0.81
## Access_to_improved_sanitation_proportion_of_pop                                   0.86
## Access_to_improved_water_source_proportion_of_pop                                 0.72
## Satisfaction_with_water_quality_proportion_of_pop                                 0.68
## Household_air_pollution_DALYs_100_000-                                            0.94
## Dissatisfaction_with_housing_affordability_proportion_of_pop-                     0.18
## Access_to_electricity_pct_of_pop                                                  0.87
## Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop                        0.88
## Political_killings_and_torture_0_low_freedom_1_high_freedom                       0.79
## Interpersonal_violence_DALYs_100_000-                                             0.18
## Transportation_related_injuries_DALYs_100_000-                                    0.42
## Intimate_Partner_Violence_pct_of_women_aged_15plus-                               0.66
## Money_Stolen_proportion_of_pop-                                                   0.56
## Equal_access_to_quality_education_0_unequal_4_equal                               0.65
## Population_with_no_schooling_proportion_of_pop-                                   0.61
## Secondary_school_attainment_pct_of_pop_aged_25plus                                0.69
## Primary_school_enrollment_pct_of_children                                         0.44
## Gender_parity_in_secondary_attainment_distance_from_parity-                       0.70
## Alternative_sources_of_information_index_0_low_1_high                             0.75
## Mobile_telephone_subscriptions_subscriptions_1_000_people                         0.46
## Internet_users_pct_of_pop                                                         0.87
## Access_to_online_governance_0_low_1_high                                          0.62
## Equal_access_to_quality_healthcare_0_unequal_4_equal                              0.73
## Life_expectancy_at_60_years                                                       0.70
## Premature_deaths_from_non_communicable_diseases_deaths_100_000-                   0.46
## Access_to_essential_health_services_0_none_100_full_coverage                      0.94
## Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop            0.62
## Outdoor_air_pollution_DALYs_100_000-                                              0.55
## Lead_exposure_DALYs_100_000-                                                      0.47
## Particulate_matter_pollution_mean_annual_exposure_g_m3-                           0.42
## Species_protection_0_low_100_high                                                 0.29
## Freedom_of_religion_0_no_freedom_4_full_freedom                                   0.65
## Property_rights_for_women_0_no_rights_5_full_rights                               0.67
## Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom                          0.82
## Access_to_justice_0_nonexistent_1_observed                                        0.74
## Freedom_of_discussion_0_low_1_high                                                0.89
## Political_rights_0_and_lower_no_rights_40_full_rights                             0.86
## Freedom_of_domestic_movement_0_low_1_high                                         0.73
## Early_marriage_pct_of_married_women_aged_15_19-                                   0.59
## Satisfied_demand_for_contraception_pct_satisfied_demand                           0.37
## Young_people_not_in_education_employment_or_training_pct_of_youth-                0.52
## Vulnerable_employment_pct_of_total_employment-                                    0.83
## Perception_of_corruption_0_high_corruption_100_low_corruption                     0.82
## Equal_protection_index_0_low_1_high                                               0.68
## Equal_access_index_0_low_1_high                                                   0.79
## Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal               0.62
## Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal 0.63
## Acceptance_of_gays_and_lesbians_proportion_of_pop                                 0.55
## Discrimination_and_violence_against_minorities_0_low_10_high-                     0.39
## Academic_freedom_0_low_1_high                                                     0.87
## Women_with_advanced_education_proportion_of_females                               0.76
## Expected_years_of_tertiary_schooling_years                                        0.67
## Citable_documents_documents_1_000_people                                          0.74
## Quality_weighted_universities_points                                              0.09
## medianIncome_log10                                                                0.88
## meanIncome_log10                                                                  0.87
## GDPpc_PPP_2019_log10                                                              0.91
## GNIpc_PPP_2019_log10                                                              0.92
##                                                                                     u2
## Child_stunting_0_low_risk_100_high_risk-                                          0.19
## Infectious_diseases_DALYs_100_000-                                                0.26
## Maternal_mortality_rate_deaths_100_000_live_births-                               0.18
## Child_mortality_rate_deaths_1_000_live_births-                                    0.12
## Undernourishment_pct_of_pop-                                                      0.47
## Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk-                       0.42
## Unsafe_water_sanitation_and_hygiene_DALYs_100_000-                                0.19
## Access_to_improved_sanitation_proportion_of_pop                                   0.14
## Access_to_improved_water_source_proportion_of_pop                                 0.28
## Satisfaction_with_water_quality_proportion_of_pop                                 0.32
## Household_air_pollution_DALYs_100_000-                                            0.06
## Dissatisfaction_with_housing_affordability_proportion_of_pop-                     0.82
## Access_to_electricity_pct_of_pop                                                  0.13
## Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop                        0.12
## Political_killings_and_torture_0_low_freedom_1_high_freedom                       0.21
## Interpersonal_violence_DALYs_100_000-                                             0.82
## Transportation_related_injuries_DALYs_100_000-                                    0.58
## Intimate_Partner_Violence_pct_of_women_aged_15plus-                               0.34
## Money_Stolen_proportion_of_pop-                                                   0.44
## Equal_access_to_quality_education_0_unequal_4_equal                               0.35
## Population_with_no_schooling_proportion_of_pop-                                   0.39
## Secondary_school_attainment_pct_of_pop_aged_25plus                                0.31
## Primary_school_enrollment_pct_of_children                                         0.56
## Gender_parity_in_secondary_attainment_distance_from_parity-                       0.30
## Alternative_sources_of_information_index_0_low_1_high                             0.25
## Mobile_telephone_subscriptions_subscriptions_1_000_people                         0.54
## Internet_users_pct_of_pop                                                         0.13
## Access_to_online_governance_0_low_1_high                                          0.38
## Equal_access_to_quality_healthcare_0_unequal_4_equal                              0.27
## Life_expectancy_at_60_years                                                       0.30
## Premature_deaths_from_non_communicable_diseases_deaths_100_000-                   0.54
## Access_to_essential_health_services_0_none_100_full_coverage                      0.06
## Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop            0.38
## Outdoor_air_pollution_DALYs_100_000-                                              0.45
## Lead_exposure_DALYs_100_000-                                                      0.53
## Particulate_matter_pollution_mean_annual_exposure_g_m3-                           0.58
## Species_protection_0_low_100_high                                                 0.71
## Freedom_of_religion_0_no_freedom_4_full_freedom                                   0.35
## Property_rights_for_women_0_no_rights_5_full_rights                               0.33
## Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom                          0.18
## Access_to_justice_0_nonexistent_1_observed                                        0.26
## Freedom_of_discussion_0_low_1_high                                                0.11
## Political_rights_0_and_lower_no_rights_40_full_rights                             0.14
## Freedom_of_domestic_movement_0_low_1_high                                         0.27
## Early_marriage_pct_of_married_women_aged_15_19-                                   0.41
## Satisfied_demand_for_contraception_pct_satisfied_demand                           0.63
## Young_people_not_in_education_employment_or_training_pct_of_youth-                0.48
## Vulnerable_employment_pct_of_total_employment-                                    0.17
## Perception_of_corruption_0_high_corruption_100_low_corruption                     0.18
## Equal_protection_index_0_low_1_high                                               0.32
## Equal_access_index_0_low_1_high                                                   0.21
## Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal               0.38
## Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal 0.37
## Acceptance_of_gays_and_lesbians_proportion_of_pop                                 0.45
## Discrimination_and_violence_against_minorities_0_low_10_high-                     0.61
## Academic_freedom_0_low_1_high                                                     0.13
## Women_with_advanced_education_proportion_of_females                               0.24
## Expected_years_of_tertiary_schooling_years                                        0.33
## Citable_documents_documents_1_000_people                                          0.26
## Quality_weighted_universities_points                                              0.91
## medianIncome_log10                                                                0.12
## meanIncome_log10                                                                  0.13
## GDPpc_PPP_2019_log10                                                              0.09
## GNIpc_PPP_2019_log10                                                              0.08
##                                                                                     p2
## Child_stunting_0_low_risk_100_high_risk-                                          0.48
## Infectious_diseases_DALYs_100_000-                                                0.29
## Maternal_mortality_rate_deaths_100_000_live_births-                               0.38
## Child_mortality_rate_deaths_1_000_live_births-                                    0.39
## Undernourishment_pct_of_pop-                                                      0.49
## Diet_low_in_fruits_and_vegetables_0_low_risk_100_high_risk-                       0.22
## Unsafe_water_sanitation_and_hygiene_DALYs_100_000-                                0.28
## Access_to_improved_sanitation_proportion_of_pop                                   0.36
## Access_to_improved_water_source_proportion_of_pop                                 0.38
## Satisfaction_with_water_quality_proportion_of_pop                                 0.63
## Household_air_pollution_DALYs_100_000-                                            0.28
## Dissatisfaction_with_housing_affordability_proportion_of_pop-                     0.28
## Access_to_electricity_pct_of_pop                                                  0.25
## Usage_of_clean_fuels_and_technology_for_cooking_pct_of_pop                        0.31
## Political_killings_and_torture_0_low_freedom_1_high_freedom                       0.42
## Interpersonal_violence_DALYs_100_000-                                             0.44
## Transportation_related_injuries_DALYs_100_000-                                    0.67
## Intimate_Partner_Violence_pct_of_women_aged_15plus-                               0.55
## Money_Stolen_proportion_of_pop-                                                   0.32
## Equal_access_to_quality_education_0_unequal_4_equal                               0.67
## Population_with_no_schooling_proportion_of_pop-                                   0.43
## Secondary_school_attainment_pct_of_pop_aged_25plus                                0.46
## Primary_school_enrollment_pct_of_children                                         0.51
## Gender_parity_in_secondary_attainment_distance_from_parity-                       0.36
## Alternative_sources_of_information_index_0_low_1_high                             0.15
## Mobile_telephone_subscriptions_subscriptions_1_000_people                         0.37
## Internet_users_pct_of_pop                                                         0.53
## Access_to_online_governance_0_low_1_high                                          0.62
## Equal_access_to_quality_healthcare_0_unequal_4_equal                              0.65
## Life_expectancy_at_60_years                                                       0.64
## Premature_deaths_from_non_communicable_diseases_deaths_100_000-                   0.69
## Access_to_essential_health_services_0_none_100_full_coverage                      0.56
## Satisfaction_with_availability_of_quality_healthcare_proportion_of_pop            0.49
## Outdoor_air_pollution_DALYs_100_000-                                              0.43
## Lead_exposure_DALYs_100_000-                                                      0.68
## Particulate_matter_pollution_mean_annual_exposure_g_m3-                           0.60
## Species_protection_0_low_100_high                                                 0.50
## Freedom_of_religion_0_no_freedom_4_full_freedom                                   0.21
## Property_rights_for_women_0_no_rights_5_full_rights                               0.47
## Freedom_of_peaceful_assembly_0_no_freedom_4_full_freedom                          0.27
## Access_to_justice_0_nonexistent_1_observed                                        0.51
## Freedom_of_discussion_0_low_1_high                                                0.24
## Political_rights_0_and_lower_no_rights_40_full_rights                             0.42
## Freedom_of_domestic_movement_0_low_1_high                                         0.36
## Early_marriage_pct_of_married_women_aged_15_19-                                   0.45
## Satisfied_demand_for_contraception_pct_satisfied_demand                           0.65
## Young_people_not_in_education_employment_or_training_pct_of_youth-                0.57
## Vulnerable_employment_pct_of_total_employment-                                    0.42
## Perception_of_corruption_0_high_corruption_100_low_corruption                     0.69
## Equal_protection_index_0_low_1_high                                               0.55
## Equal_access_index_0_low_1_high                                                   0.42
## Power_distributed_by_sexual_orientation_0_extremely_unequal_3_equal               0.60
## Access_to_public_services_distributed_by_social_group_0_extremely_unequal_4_equal 0.68
## Acceptance_of_gays_and_lesbians_proportion_of_pop                                 0.67
## Discrimination_and_violence_against_minorities_0_low_10_high-                     0.58
## Academic_freedom_0_low_1_high                                                     0.25
## Women_with_advanced_education_proportion_of_females                               0.58
## Expected_years_of_tertiary_schooling_years                                        0.51
## Citable_documents_documents_1_000_people                                          0.69
## Quality_weighted_universities_points                                              0.60
## medianIncome_log10                                                                0.60
## meanIncome_log10                                                                  0.60
## GDPpc_PPP_2019_log10                                                              0.52
## GNIpc_PPP_2019_log10                                                              0.52
## 
## With Sums of squares  of:
##    g  F1*  F2*  F3* 
## 19.8 13.6  7.0  2.2 
## 
## general/max  1.46   max/min =   6.21
## mean percent general =  0.47    with sd =  0.15 and cv of  0.31 
## Explained Common Variance of the general factor =  0.46 
## 
## The degrees of freedom are 1827  and the fit is  47.1 
## The number of observations was  150  with Chi Square =  5884  with prob <  0
## The root mean square of the residuals is  0.05 
## The df corrected root mean square of the residuals is  0.05
## RMSEA index =  0.121  and the 10 % confidence intervals are  0.119 0.126
## BIC =  -3270
## 
## Compare this with the adequacy of just a general factor and no group factors
## The degrees of freedom for just the general factor are 1952  and the fit is  79.8 
## The number of observations was  150  with Chi Square =  10070  with prob <  0
## The root mean square of the residuals is  0.25 
## The df corrected root mean square of the residuals is  0.25 
## 
## RMSEA index =  0.166  and the 10 % confidence intervals are  0.164 0.17
## BIC =  289 
## 
## Measures of factor score adequacy             
##                                                  g  F1*  F2*  F3*
## Correlation of scores with factors            0.83 0.91 0.92 0.78
## Multiple R square of scores with factors      0.70 0.82 0.85 0.61
## Minimum correlation of factor score estimates 0.39 0.64 0.70 0.22
## 
##  Total, General and Subset omega for each subset
##                                                  g  F1*  F2*  F3*
## Omega total for total scores and subscales    0.99 0.98 0.96 0.84
## Omega general for total scores and subscales  0.61 0.49 0.42 0.56
## Omega group for total scores and subscales    0.28 0.49 0.53 0.27
omega.diagram(efa_bifactor)

save_plot_to_file(omega.diagram(efa_bifactor), "figs/omega_fit.png")

#vary the group factor number
efa_bifactor_models = map(3:10, ~omega(d_spi_all_imp_z[-1], nfactors = .))
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.

## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.

## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.

## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.

## Warning in GPFoblq(A, Tmat = Tmat, normalize = normalize, eps = eps, maxit =
## maxit, : convergence not obtained in GPFoblq. 1000 iterations used.

## Warning in GPFoblq(A, Tmat = Tmat, normalize = normalize, eps = eps, maxit =
## maxit, : convergence not obtained in GPFoblq. 1000 iterations used.

## Warning in GPFoblq(A, Tmat = Tmat, normalize = normalize, eps = eps, maxit =
## maxit, : The estimated weights for the factor scores are probably incorrect.
## Try a different factor score estimation method.

## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect.  Try a
## different factor score estimation method.

## Warning in GPFoblq(A, Tmat = Tmat, normalize = normalize, eps = eps, maxit =
## maxit, : convergence not obtained in GPFoblq. 1000 iterations used.

## Warning in GPFoblq(A, Tmat = Tmat, normalize = normalize, eps = eps, maxit =
## maxit, : The estimated weights for the factor scores are probably incorrect.
## Try a different factor score estimation method.

#save all the scores
efa_bifactor_scores = d_spi_all_imp_z[, 1]
for (i in seq_along(efa_bifactor_models)) efa_bifactor_scores[[str_glue("SPI_omega_{i+2}")]] = efa_bifactor_models[[i]]$scores[, 1] %>% standardize()

#join them to main
for (v in str_subset(names(d), "SPI_omega_")) d[[v]] = NULL
d = full_join(
  d, 
  efa_bifactor_scores
)
## Joining with `by = join_by(ISO)`
#do they correlate?
d %>% 
  select(contains("SPI_omega_")) %>% 
  GG_heatmap()

GG_save("figs/efa_bifactor_scores.png")

#check for GDP bias
d %>% 
  GG_scatter("GDPpc_PPP_2019_log10", "SPI_omega_5", case_names = "nice_name") +
  scale_x_continuous("GDP per capita, PPP, 2019, log10") +
  scale_y_continuous("General factor of SPI (64 indicators, 2019, EFA bifactor, 5 group factors)") +
  ggtitle("GDP per capita log10 vs. very fancy bifactor model of SPI") +
  geom_smooth()
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

GG_save("figs/GDPpclog10_SPI_EFA_bifactor5.png")
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
#ranking
d %>% select(
  nice_name, GDPpc_PPP_2019_log10, medianIncome_log10, SPI_2019, SPI_EFA, SPI2_EFA, SPI3_EFA, SPI_omega_3, SPI_omega_5, SPI_omega_10
) %>%
  arrange(-SPI_omega_10) %>% 
  map_df(function(v) {
    if (!is.numeric(v)) return(v)
    rank(v*-1)
  }) %>% 
  head(25) %>% 
  print(n=25)
## # A tibble: 25 × 10
##    nice_name   GDPpc_PPP_2019_log10 medianIncome_log10 SPI_2019 SPI_EFA SPI2_EFA
##    <chr>                      <dbl>              <dbl>    <dbl>   <dbl>    <dbl>
##  1 Norway                        10                  3        1       2        3
##  2 Switzerland                    8                  4        4       7        5
##  3 Sweden                        20                  8        7       5        7
##  4 Canada                        24                  6       10       6        8
##  5 Iceland                       16                 12        5      10       11
##  6 France                        28                 14       18       9       10
##  7 Australia                     23                 11        9      13       15
##  8 Luxembourg                     2                  1       11       3        1
##  9 Netherlands                   17                 10        6       1        2
## 10 Germany                       19                 13        8       4        4
## 11 Italy                         33                 22       26      22       20
## 12 Denmark                       15                  9        2      11       12
## 13 Ireland                        5                 19       12      14        6
## 14 Finland                       25                 15        3       8        9
## 15 Belgium                       21                 16       16      16       14
## 16 New Zealand                   31                161       15      17       18
## 17 Austria                       18                  7       14      19       17
## 18 Singapore                      3                162       27      27       25
## 19 United Kin…                   26                 17       17      15       16
## 20 Malta                         30                 18       28      28       28
## 21 Spain                         37                 26       20      23       22
## 22 Japan                         34                 20       13      12       13
## 23 USA                           11                  5       23      26       26
## 24 Slovenia                      39                 25       21      18       19
## 25 Cyprus                        35                 23       30      25       27
## # ℹ 4 more variables: SPI3_EFA <dbl>, SPI_omega_3 <dbl>, SPI_omega_5 <dbl>,
## #   SPI_omega_10 <dbl>

Compare with NIQ

#what has the best correlation with national IQs?
nat_iq_model = str_glue("
#big G factor
G =~ QNWplusSASplusGEO + L_and_V12plusGEO + R

GDPpc_PPP_2019_log10 ~ G
GNIpc_PPP_2019_log10 ~ G
meanIncome_log10 ~ G
medianIncome_log10 ~ G
SPI_2019 ~ G
SPI_EFA ~ G
SPI2_EFA ~ G
SPI3_EFA ~ G
SPI_omega_3 ~ G
SPI_omega_4 ~ G
SPI_omega_5 ~ G
SPI_omega_6 ~ G
SPI_omega_7 ~ G
SPI_omega_8 ~ G
SPI_omega_9 ~ G
SPI_omega_10 ~ G
")

nat_iq_fit = cfa(nat_iq_model, data = d, std.ov = T, std.lv = T, estimator = "DWLS")
## Warning in lav_samplestats_from_data(lavdata = lavdata, lavoptions = lavoptions, : lavaan WARNING: number of observations (120) too small to compute Gamma
nat_iq_fit
## lavaan 0.6.15 ended normally after 63 iterations
## 
##   Estimator                                       DWLS
##   Optimization method                           NLMINB
##   Number of model parameters                       158
## 
##                                                   Used       Total
##   Number of observations                           120         215
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.169
##   Degrees of freedom                                32
##   P-value (Chi-square)                           1.000
nat_iq_validities = parameterestimates(nat_iq_fit, standardized = T) %>% 
  filter(
    op == "~",
    rhs == "G"
  ) %>% arrange(std.all)

nat_iq_validities
#regional predictive validity
regional_models = map_df(nat_iq_validities$lhs, function(v) {
  dd = d %>% mutate(
    yyy = d[[v]]
  )
  fit_continent = lm("yyy ~ UN_continent", data = dd)
  fit_macroregion = lm("yyy ~ UN_macroregion", data = dd)
  fit_region = lm("yyy ~ UN_region", data = dd)
  
  tibble(
    metric = v,
    r_adj_continent = summary(fit_continent)$adj.r.squared %>% sqrt(),
    r_adj_macroregion = summary(fit_macroregion)$adj.r.squared %>% sqrt(),
    r_adj_region = summary(fit_region)$adj.r.squared %>% sqrt()
  )
})

#compare
final_validation = full_join(
  nat_iq_validities %>% select(lhs, std.all) %>% rename(metric = lhs, r_nat_G = std.all),
  regional_models,
) %>% mutate(
  final_metric = (standardize(r_nat_G) + standardize(r_adj_region)) %>% standardize()
) %>% arrange(-final_metric)
## Joining with `by = join_by(metric)`
final_validation
final_validation[-1] %>% wtd.cors()
##                   r_nat_G r_adj_continent r_adj_macroregion r_adj_region
## r_nat_G             1.000           0.786             0.511        0.625
## r_adj_continent     0.786           1.000             0.838        0.865
## r_adj_macroregion   0.511           0.838             1.000        0.968
## r_adj_region        0.625           0.865             0.968        1.000
## final_metric        0.901           0.916             0.820        0.901
##                   final_metric
## r_nat_G                  0.901
## r_adj_continent          0.916
## r_adj_macroregion        0.820
## r_adj_region             0.901
## final_metric             1.000

Meta

#versions
write_sessioninfo()
## R version 4.3.0 (2023-04-21)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Linux Mint 21.1
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_DK.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_DK.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_DK.UTF-8 LC_IDENTIFICATION=C       
## 
## time zone: Europe/Berlin
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] lavaan_0.6-15         readxl_1.4.2          kirkegaard_2023-06-27
##  [4] psych_2.3.3           assertthat_0.2.1      weights_1.0.4        
##  [7] Hmisc_5.0-1           magrittr_2.0.3        lubridate_1.9.2      
## [10] forcats_1.0.0         stringr_1.5.0         dplyr_1.1.2          
## [13] purrr_1.0.1           readr_2.1.4           tidyr_1.3.0          
## [16] tibble_3.2.1          ggplot2_3.4.2         tidyverse_2.0.0      
## 
## loaded via a namespace (and not attached):
##   [1] mnormt_2.1.1         gridExtra_2.3        rlang_1.1.0         
##   [4] e1071_1.7-13         compiler_4.3.0       mgcv_1.8-42         
##   [7] gdata_2.18.0.1       systemfonts_1.0.4    vctrs_0.6.2         
##  [10] quadprog_1.5-8       pkgconfig_2.0.3      crayon_1.5.2        
##  [13] fastmap_1.1.1        backports_1.4.1      labeling_0.4.2      
##  [16] pbivnorm_0.6.0       utf8_1.2.3           rmarkdown_2.21      
##  [19] tzdb_0.3.0           nloptr_2.0.3         ragg_1.2.5          
##  [22] bit_4.0.5            xfun_0.39            cachem_1.0.7        
##  [25] jsonlite_1.8.4       highr_0.10           broom_1.0.4         
##  [28] parallel_4.3.0       cluster_2.1.4        R6_2.5.1            
##  [31] bslib_0.4.2          stringi_1.7.12       vcd_1.4-11          
##  [34] ranger_0.15.1        car_3.1-2            boot_1.3-28         
##  [37] rpart_4.1.19         lmtest_0.9-40        jquerylib_0.1.4     
##  [40] cellranger_1.1.0     Rcpp_1.0.10          knitr_1.42          
##  [43] zoo_1.8-12           base64enc_0.1-3      Matrix_1.5-1        
##  [46] splines_4.3.0        nnet_7.3-18          timechange_0.2.0    
##  [49] tidyselect_1.2.0     rstudioapi_0.14      abind_1.4-5         
##  [52] yaml_2.3.7           plyr_1.8.8           lattice_0.20-45     
##  [55] withr_2.5.0          evaluate_0.20        foreign_0.8-82      
##  [58] proxy_0.4-27         pillar_1.9.0         carData_3.0-5       
##  [61] mice_3.15.0          checkmate_2.2.0      VIM_6.2.2           
##  [64] stats4_4.3.0         generics_0.1.3       vroom_1.6.1         
##  [67] sp_1.6-0             hms_1.1.3            munsell_0.5.0       
##  [70] scales_1.2.1         laeken_0.5.2         minqa_1.2.5         
##  [73] gtools_3.9.4         class_7.3-21         glue_1.6.2          
##  [76] tools_4.3.0          robustbase_0.95-1    data.table_1.14.8   
##  [79] lme4_1.1-33          grid_4.3.0           colorspace_2.1-0    
##  [82] nlme_3.1-162         htmlTable_2.4.1      Formula_1.2-5       
##  [85] cli_3.6.1            textshaping_0.3.6    fansi_1.0.4         
##  [88] rematch_1.0.1        gtable_0.3.3         DEoptimR_1.0-12     
##  [91] sass_0.4.5           digest_0.6.31        GPArotation_2023.3-1
##  [94] ggrepel_0.9.3        farver_2.1.1         htmlwidgets_1.6.2   
##  [97] htmltools_0.5.5      lifecycle_1.0.3      bit64_4.0.5         
## [100] MASS_7.3-58.3
#write data to file for reuse
d %>% write_rds("data/data_for_reuse.rds")