What

This is an R notebook for the study:

Start

options(digits = 2)
library(pacman)
p_load(kirkegaard, readxl, sp, lavaan, lavaan.survey, rms)

#having issues with rgdal?
#apt install libproj-dev libgdal-dev libgdal1i
#https://stackoverflow.com/questions/15248815/rgdal-package-installation
#https://stackoverflow.com/questions/30790036/error-istruegpclibpermitstatus-is-not-true

#replacement of describe()
#not sure about why this bug exists, it works fine when done manually
describe = function(...) {
  y = psych::describe(...)
  class(y) = "data.frame"
  y
}

Data

#cognitive
cog = readxl::read_xlsx("data/2004-worldbank table.xlsx")
#remove Vietnam as a whole
#Ha Tay no longer exists, so we delete it
#https://en.wikipedia.org/wiki/H%C3%A0_T%C3%A2y_Province
cog = cog[-c(3, 62), ]

#social
social = readxl::read_xlsx("data/all_province.xlsx") %>% df_legalize_names()

#fix names so they match exactly
social$province_name = social$province_name %>% str_replace(" city", "")

#more social
#files from Vietnamese official statsbank
#they are messy and were partially reformated by hand first
#get sheets names
social_data_sheets = readxl::excel_sheets("data/combined_statbank.xlsx")

#get each sheet
social2 = map_df(social_data_sheets, function(sheet) {
  # browser()
  #read file with the right sheet
  d = readxl::read_xlsx("data/combined_statbank.xlsx", sheet = sheet)

  #get the variable
  var = colnames(d)[1]

  #clean name
  var = str_replace(var, "by province.*", "") %>%
    str_trim()

  #remove header rows
  headers = d[1:2, ]
  d = d[-c(1:2), ]

  #exclude wrong units
  #tricky, must be done by name match
  #we can exclude the obviously wrong ones
  #then approximate match the remainder
  #the obvious ones are the regions (superordinate units) and the one province that was merged into Hanoi

  #remove the city part like before
  #and double spaces
  colnames(d)[1] = "province"
  d$province %<>% str_replace(" [Cc]ity", "") %>% str_replace_all("\\s+", " ")

  exclude = c(
    "Red River Delta",
    "Northern midlands and mountain areas",
    "Northern Central area and Central coastal area",
    "North Central area and Central coastal area",
    "Central Highlands",
    "South East",
    "Mekong River Delta",
    "Ha Tay"
  )

  d = d %>% filter(!province %in% exclude)

  #error if more units than possible
  if (nrow(d) > 63) browser()

  #average across years
  d2 = rowMeans(d[, -1], na.rm = T)

  y = cbind(
    province_name = d[[1]],
    value = d2,
    var = var %>% str_legalize()
  )

  # colnames(y)[2] = var %>% str_legalize()

  y %>% as_data_frame()
})

#switch to ISO
#names are messy!!!
social2$ISO = pu_translate(social2$province_name, superunit = "VNM")
## No exact match: Thua Thien-Hue
## No exact match: Ba Ria - Vung Tau
## No exact match: Thua Thien-Hue
## No exact match: Ba Ria - Vung Tau
## No exact match: Thua Thien-Hue
## No exact match: Ba Ria - Vung Tau
## No exact match: Thua Thien-Hue
## No exact match: Ba Ria - Vung Tau
## No exact match: Thua Thien-Hue
## No exact match: Ba Ria - Vung Tau
## No exact match: Thua Thien-Hue
## No exact match: Ba Ria - Vung Tau
## No exact match: Thua Thien - Hue
## No exact match: Ba Ria - Vung Tau
## Best fuzzy match found: Thua Thien-Hue -> Thua Thien–Hue with distance 1.00
## Best fuzzy match found: Ba Ria - Vung Tau -> Ba Ria–Vung Tau with distance 3.00
## Best fuzzy match found: Thua Thien-Hue -> Thua Thien–Hue with distance 1.00
## Best fuzzy match found: Ba Ria - Vung Tau -> Ba Ria–Vung Tau with distance 3.00
## Best fuzzy match found: Thua Thien-Hue -> Thua Thien–Hue with distance 1.00
## Best fuzzy match found: Ba Ria - Vung Tau -> Ba Ria–Vung Tau with distance 3.00
## Best fuzzy match found: Thua Thien-Hue -> Thua Thien–Hue with distance 1.00
## Best fuzzy match found: Ba Ria - Vung Tau -> Ba Ria–Vung Tau with distance 3.00
## Best fuzzy match found: Thua Thien-Hue -> Thua Thien–Hue with distance 1.00
## Best fuzzy match found: Ba Ria - Vung Tau -> Ba Ria–Vung Tau with distance 3.00
## Best fuzzy match found: Thua Thien-Hue -> Thua Thien–Hue with distance 1.00
## Best fuzzy match found: Ba Ria - Vung Tau -> Ba Ria–Vung Tau with distance 3.00
## Best fuzzy match found: Thua Thien - Hue -> Thua Thien–Hue with distance 3.00
## Best fuzzy match found: Ba Ria - Vung Tau -> Ba Ria–Vung Tau with distance 3.00
social2$province_name = NULL

#spread
social2 %<>% spread(var, value)

#enforce unique ISO
assert_that(!any(duplicated(social2$ISO)))
## [1] TRUE
#income data
income = readxl::read_xls("data/income.xls", sheet = 2)
income$ISO = pu_translate(income$province, superunit = "VNM")
## No exact match: Thua Thien - Hue
## No exact match: Ho Chi Minh City
## No exact match: Ba Ria-Vung Tau
## Best fuzzy match found: Thua Thien - Hue -> Thua Thien–Hue with distance 3.00
## Best fuzzy match found: Ho Chi Minh City -> Ho Chi Minh with distance 5.00
## Best fuzzy match found: Ba Ria-Vung Tau -> Ba Ria–Vung Tau with distance 1.00
income$province = NULL
assert_that(!any(duplicated(income$ISO)))
## [1] TRUE
#demographics
demos = readxl::read_xlsx("data/demographics.xlsx")

#enforce consistency
assert_that(all(apply(demos[-c(1)], 1, function(x) {
  x[1] >= sum(x[-1], na.rm=T)
})))
## [1] TRUE
pop_included_ethnics = apply(demos[-c(1)], 1, function(x) {
  sum(x[-1], na.rm=T)
}) %>% sum()
print(pop_included_ethnics / sum(demos$total), 4)
## [1] 0.9645
#mismatches?
setdiff(social$province_name, cog$Province)
## [1] "Dien Bien" "Dak Nong"  "Hau Giang"
setdiff(cog$Province, social$province_name)
## character(0)
#get ISO and join
social$ISO = pu_translate(social$province_name)
## No exact match: Thua Thien Hue
## No exact match: Ba Ria - Vung Tau
## Best fuzzy match found: Thua Thien Hue -> Thua Thien–Hue with distance 1.00
## Best fuzzy match found: Ba Ria - Vung Tau -> Ba Ria–Vung Tau with distance 3.00
cog$ISO = pu_translate(cog$Province)
## No exact match: Thua Thien Hue
## No exact match: Ba Ria - Vung Tau
## Best fuzzy match found: Thua Thien Hue -> Thua Thien–Hue with distance 1.00
## Best fuzzy match found: Ba Ria - Vung Tau -> Ba Ria–Vung Tau with distance 3.00
demos$ISO = pu_translate(demos$province)
## No exact match: Thua Thien Hue
## No exact match: Ba Ria - Vung Tau
## Best fuzzy match found: Thua Thien Hue -> Thua Thien–Hue with distance 1.00
## Best fuzzy match found: Ba Ria - Vung Tau -> Ba Ria–Vung Tau with distance 3.00
#join
d = full_join(social, cog, by = "ISO")
d = full_join(d, demos, by = "ISO")
d = full_join(d, social2, by = "ISO")
d = full_join(d, income, by = "ISO")

#geometric data for plotting
geo = read_rds("data/geo63.rds")
#translate ISO-2 to custom ISO-3
geo$ISO = geo$ISO.1 %>% str_replace("VN", "VNM")

#join density into main
d = full_join(d, geo %>% as_data_frame() %>% select(ISO, Density), by = "ISO")

#rename / create
d$CA = d$`Mean(PRD500)`
d$CA_ise = 1 / d$SE
d$pop = d$Population_N %>% str_replace_all(",", "") %>% as.integer()
d$pop_sqrt = d$pop %>% sqrt()

#ethnic fractions
ethnics = names(demos)[-c(1, 2, ncol(demos))]

for (ethnic in ethnics) {
  #any missing should be 0
  d[[ethnic]] = if_else(is.na(d[[ethnic]]), true = 0, false = d[[ethnic]])
  
  #get fraction of total and same
  d[str_c(ethnic, "_frac")] = d[[ethnic]] / d$total
}

d$other_frac = 1 - apply(d %>% select(ends_with("_frac")), 1, sum)
d$other_frac %>% describe()
#Kinh
print(sum(d$kinh) / sum(d$total), digits = 4)
## [1] 0.8573
#prepare geodata
#https://github.com/tidyverse/ggplot2/wiki/plotting-polygon-shapefiles
geo@data$id = rownames(geo@data)
geo_points = fortify(geo, region = "id")
geo_df = inner_join(geo_points, geo@data, by = "id")

#centroid-ish for each province
province_meta = geo_df %>% 
  group_by(ISO) %>% 
  dplyr::summarize(
    lat = mean(range(lat)),
    long = mean(range(long)),
    name = unique(NAME_2)
  )

#move geopos data to main
#join bc order may differ
d = full_join(d, province_meta %>% select(-name))
## Joining, by = "ISO"

Descriptives

#CA
describe(d$CA)
#ethnics
describe(d[ethnics + "_frac"])

S factor

#impute some missing data for statbank data
#we impute zero for investment based on the plausible assumption that no data here means no investment recorded
#we also transform it to per capita basis
d$Foreign_direct_investment_projects_licensed_in_2016 %<>% plyr::mapvalues(NA, 0) %>% as.numeric()

#per capita
d$Foreign_direct_investment_projects_licensed_in_2016_per_capita = d$Foreign_direct_investment_projects_licensed_in_2016 / d$pop

#S vars
S_vars = c("Poverty_GSO_WB_poverty_headcount_pct",
           "Main_employment_agriculture_pct",
           "Main_employment_wage_work_pct",
           "Main_light_source_electricity_pct",
           "Sanitation_indoor_flush_toilet_pct",
           "Water_indoor_tap_public_tap_or_well_pct",
           "Secondary_school_attendance_Lower_11_15_years_pct",
           "Secondary_school_attendance_Upper_16_18_years_pct",
           "Stunting_total",
           "Foreign_direct_investment_projects_licensed_in_2016_per_capita",
           "Index_of_Industrial_production",
           "Infant_mortality_rate",
           "Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population",
           "Under_five_mortality_rate",
           "monthly_income")

#collect, impute
S_data = d[S_vars] %>% map_df(as.numeric)
S_data_orig = S_data

#much more missing data?
miss_by_var(S_data_orig)
##                                         Poverty_GSO_WB_poverty_headcount_pct 
##                                                                            0 
##                                              Main_employment_agriculture_pct 
##                                                                            0 
##                                                Main_employment_wage_work_pct 
##                                                                            0 
##                                            Main_light_source_electricity_pct 
##                                                                            0 
##                                           Sanitation_indoor_flush_toilet_pct 
##                                                                            0 
##                                      Water_indoor_tap_public_tap_or_well_pct 
##                                                                            0 
##                            Secondary_school_attendance_Lower_11_15_years_pct 
##                                                                            0 
##                            Secondary_school_attendance_Upper_16_18_years_pct 
##                                                                            0 
##                                                               Stunting_total 
##                                                                            0 
##               Foreign_direct_investment_projects_licensed_in_2016_per_capita 
##                                                                            0 
##                                               Index_of_Industrial_production 
##                                                                            0 
##                                                        Infant_mortality_rate 
##                                                                            0 
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population 
##                                                                            1 
##                                                    Under_five_mortality_rate 
##                                                                            0 
##                                                               monthly_income 
##                                                                            3
miss_plot(S_data_orig)

miss_amount(S_data_orig)
## cases with missing data  vars with missing data cells with missing data 
##                  0.0600                  0.1300                  0.0042
#impute
S_data = miss_impute(S_data)

#ranks
S_data_ranks = df_rank(S_data)

#control for pop density
S_data_density = df_residualize(cbind(S_data, density = d$Density), resid.vars = "density", return.resid.vars = F, weights = d$pop_sqrt)

#cors
wtd.cors(S_data)
##                                                                              Poverty_GSO_WB_poverty_headcount_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                        1.000
## Main_employment_agriculture_pct                                                                             0.750
## Main_employment_wage_work_pct                                                                              -0.687
## Main_light_source_electricity_pct                                                                          -0.820
## Sanitation_indoor_flush_toilet_pct                                                                         -0.572
## Water_indoor_tap_public_tap_or_well_pct                                                                    -0.669
## Secondary_school_attendance_Lower_11_15_years_pct                                                          -0.473
## Secondary_school_attendance_Upper_16_18_years_pct                                                          -0.440
## Stunting_total                                                                                              0.761
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                             -0.469
## Index_of_Industrial_production                                                                              0.053
## Infant_mortality_rate                                                                                       0.850
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                0.552
## Under_five_mortality_rate                                                                                   0.848
## monthly_income                                                                                             -0.492
##                                                                              Main_employment_agriculture_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                   0.750
## Main_employment_agriculture_pct                                                                        1.000
## Main_employment_wage_work_pct                                                                         -0.979
## Main_light_source_electricity_pct                                                                     -0.522
## Sanitation_indoor_flush_toilet_pct                                                                    -0.871
## Water_indoor_tap_public_tap_or_well_pct                                                               -0.781
## Secondary_school_attendance_Lower_11_15_years_pct                                                     -0.063
## Secondary_school_attendance_Upper_16_18_years_pct                                                     -0.119
## Stunting_total                                                                                         0.745
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                        -0.537
## Index_of_Industrial_production                                                                         0.072
## Infant_mortality_rate                                                                                  0.639
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                           0.695
## Under_five_mortality_rate                                                                              0.631
## monthly_income                                                                                        -0.702
##                                                                              Main_employment_wage_work_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                -0.687
## Main_employment_agriculture_pct                                                                     -0.979
## Main_employment_wage_work_pct                                                                        1.000
## Main_light_source_electricity_pct                                                                    0.464
## Sanitation_indoor_flush_toilet_pct                                                                   0.857
## Water_indoor_tap_public_tap_or_well_pct                                                              0.738
## Secondary_school_attendance_Lower_11_15_years_pct                                                    0.038
## Secondary_school_attendance_Upper_16_18_years_pct                                                    0.075
## Stunting_total                                                                                      -0.720
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                       0.591
## Index_of_Industrial_production                                                                      -0.045
## Infant_mortality_rate                                                                               -0.591
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                        -0.613
## Under_five_mortality_rate                                                                           -0.582
## monthly_income                                                                                       0.698
##                                                                              Main_light_source_electricity_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                    -0.820
## Main_employment_agriculture_pct                                                                         -0.522
## Main_employment_wage_work_pct                                                                            0.464
## Main_light_source_electricity_pct                                                                        1.000
## Sanitation_indoor_flush_toilet_pct                                                                       0.394
## Water_indoor_tap_public_tap_or_well_pct                                                                  0.444
## Secondary_school_attendance_Lower_11_15_years_pct                                                        0.601
## Secondary_school_attendance_Upper_16_18_years_pct                                                        0.509
## Stunting_total                                                                                          -0.578
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                           0.357
## Index_of_Industrial_production                                                                          -0.082
## Infant_mortality_rate                                                                                   -0.690
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                            -0.357
## Under_five_mortality_rate                                                                               -0.692
## monthly_income                                                                                           0.224
##                                                                              Sanitation_indoor_flush_toilet_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                     -0.572
## Main_employment_agriculture_pct                                                                          -0.871
## Main_employment_wage_work_pct                                                                             0.857
## Main_light_source_electricity_pct                                                                         0.394
## Sanitation_indoor_flush_toilet_pct                                                                        1.000
## Water_indoor_tap_public_tap_or_well_pct                                                                   0.639
## Secondary_school_attendance_Lower_11_15_years_pct                                                         0.109
## Secondary_school_attendance_Upper_16_18_years_pct                                                         0.176
## Stunting_total                                                                                           -0.653
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                            0.370
## Index_of_Industrial_production                                                                           -0.087
## Infant_mortality_rate                                                                                    -0.445
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                             -0.636
## Under_five_mortality_rate                                                                                -0.437
## monthly_income                                                                                            0.728
##                                                                              Water_indoor_tap_public_tap_or_well_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                          -0.669
## Main_employment_agriculture_pct                                                                               -0.781
## Main_employment_wage_work_pct                                                                                  0.738
## Main_light_source_electricity_pct                                                                              0.444
## Sanitation_indoor_flush_toilet_pct                                                                             0.639
## Water_indoor_tap_public_tap_or_well_pct                                                                        1.000
## Secondary_school_attendance_Lower_11_15_years_pct                                                             -0.034
## Secondary_school_attendance_Upper_16_18_years_pct                                                              0.020
## Stunting_total                                                                                                -0.681
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                 0.408
## Index_of_Industrial_production                                                                                -0.022
## Infant_mortality_rate                                                                                         -0.644
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                  -0.647
## Under_five_mortality_rate                                                                                     -0.638
## monthly_income                                                                                                 0.572
##                                                                              Secondary_school_attendance_Lower_11_15_years_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                                  -0.47282
## Main_employment_agriculture_pct                                                                                       -0.06318
## Main_employment_wage_work_pct                                                                                          0.03810
## Main_light_source_electricity_pct                                                                                      0.60068
## Sanitation_indoor_flush_toilet_pct                                                                                     0.10910
## Water_indoor_tap_public_tap_or_well_pct                                                                               -0.03408
## Secondary_school_attendance_Lower_11_15_years_pct                                                                      1.00000
## Secondary_school_attendance_Upper_16_18_years_pct                                                                      0.93300
## Stunting_total                                                                                                        -0.33705
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                         0.27264
## Index_of_Industrial_production                                                                                         0.02792
## Infant_mortality_rate                                                                                                 -0.31666
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                           0.00029
## Under_five_mortality_rate                                                                                             -0.32039
## monthly_income                                                                                                        -0.05256
##                                                                              Secondary_school_attendance_Upper_16_18_years_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                                    -0.440
## Main_employment_agriculture_pct                                                                                         -0.119
## Main_employment_wage_work_pct                                                                                            0.075
## Main_light_source_electricity_pct                                                                                        0.509
## Sanitation_indoor_flush_toilet_pct                                                                                       0.176
## Water_indoor_tap_public_tap_or_well_pct                                                                                  0.020
## Secondary_school_attendance_Lower_11_15_years_pct                                                                        0.933
## Secondary_school_attendance_Upper_16_18_years_pct                                                                        1.000
## Stunting_total                                                                                                          -0.349
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                           0.203
## Index_of_Industrial_production                                                                                           0.041
## Infant_mortality_rate                                                                                                   -0.252
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                            -0.174
## Under_five_mortality_rate                                                                                               -0.255
## monthly_income                                                                                                          -0.034
##                                                                              Stunting_total
## Poverty_GSO_WB_poverty_headcount_pct                                                 0.7613
## Main_employment_agriculture_pct                                                      0.7449
## Main_employment_wage_work_pct                                                       -0.7201
## Main_light_source_electricity_pct                                                   -0.5776
## Sanitation_indoor_flush_toilet_pct                                                  -0.6526
## Water_indoor_tap_public_tap_or_well_pct                                             -0.6813
## Secondary_school_attendance_Lower_11_15_years_pct                                   -0.3370
## Secondary_school_attendance_Upper_16_18_years_pct                                   -0.3494
## Stunting_total                                                                       1.0000
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                      -0.4063
## Index_of_Industrial_production                                                       0.0062
## Infant_mortality_rate                                                                0.7826
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population         0.5051
## Under_five_mortality_rate                                                            0.7767
## monthly_income                                                                      -0.6642
##                                                                              Foreign_direct_investment_projects_licensed_in_2016_per_capita
## Poverty_GSO_WB_poverty_headcount_pct                                                                                                 -0.469
## Main_employment_agriculture_pct                                                                                                      -0.537
## Main_employment_wage_work_pct                                                                                                         0.591
## Main_light_source_electricity_pct                                                                                                     0.357
## Sanitation_indoor_flush_toilet_pct                                                                                                    0.370
## Water_indoor_tap_public_tap_or_well_pct                                                                                               0.408
## Secondary_school_attendance_Lower_11_15_years_pct                                                                                     0.273
## Secondary_school_attendance_Upper_16_18_years_pct                                                                                     0.203
## Stunting_total                                                                                                                       -0.406
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                                        1.000
## Index_of_Industrial_production                                                                                                        0.027
## Infant_mortality_rate                                                                                                                -0.411
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                                         -0.186
## Under_five_mortality_rate                                                                                                            -0.405
## monthly_income                                                                                                                        0.340
##                                                                              Index_of_Industrial_production
## Poverty_GSO_WB_poverty_headcount_pct                                                                 0.0532
## Main_employment_agriculture_pct                                                                      0.0722
## Main_employment_wage_work_pct                                                                       -0.0448
## Main_light_source_electricity_pct                                                                   -0.0822
## Sanitation_indoor_flush_toilet_pct                                                                  -0.0869
## Water_indoor_tap_public_tap_or_well_pct                                                             -0.0217
## Secondary_school_attendance_Lower_11_15_years_pct                                                    0.0279
## Secondary_school_attendance_Upper_16_18_years_pct                                                    0.0412
## Stunting_total                                                                                       0.0062
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                       0.0273
## Index_of_Industrial_production                                                                       1.0000
## Infant_mortality_rate                                                                                0.1076
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                        -0.0582
## Under_five_mortality_rate                                                                            0.1137
## monthly_income                                                                                      -0.1157
##                                                                              Infant_mortality_rate
## Poverty_GSO_WB_poverty_headcount_pct                                                          0.85
## Main_employment_agriculture_pct                                                               0.64
## Main_employment_wage_work_pct                                                                -0.59
## Main_light_source_electricity_pct                                                            -0.69
## Sanitation_indoor_flush_toilet_pct                                                           -0.45
## Water_indoor_tap_public_tap_or_well_pct                                                      -0.64
## Secondary_school_attendance_Lower_11_15_years_pct                                            -0.32
## Secondary_school_attendance_Upper_16_18_years_pct                                            -0.25
## Stunting_total                                                                                0.78
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                               -0.41
## Index_of_Industrial_production                                                                0.11
## Infant_mortality_rate                                                                         1.00
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                  0.35
## Under_five_mortality_rate                                                                     1.00
## monthly_income                                                                               -0.47
##                                                                              Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population
## Poverty_GSO_WB_poverty_headcount_pct                                                                                                              0.55229
## Main_employment_agriculture_pct                                                                                                                   0.69509
## Main_employment_wage_work_pct                                                                                                                    -0.61262
## Main_light_source_electricity_pct                                                                                                                -0.35723
## Sanitation_indoor_flush_toilet_pct                                                                                                               -0.63586
## Water_indoor_tap_public_tap_or_well_pct                                                                                                          -0.64739
## Secondary_school_attendance_Lower_11_15_years_pct                                                                                                 0.00029
## Secondary_school_attendance_Upper_16_18_years_pct                                                                                                -0.17385
## Stunting_total                                                                                                                                    0.50514
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                                                   -0.18629
## Index_of_Industrial_production                                                                                                                   -0.05820
## Infant_mortality_rate                                                                                                                             0.35278
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                                                      1.00000
## Under_five_mortality_rate                                                                                                                         0.34483
## monthly_income                                                                                                                                   -0.54551
##                                                                              Under_five_mortality_rate
## Poverty_GSO_WB_poverty_headcount_pct                                                              0.85
## Main_employment_agriculture_pct                                                                   0.63
## Main_employment_wage_work_pct                                                                    -0.58
## Main_light_source_electricity_pct                                                                -0.69
## Sanitation_indoor_flush_toilet_pct                                                               -0.44
## Water_indoor_tap_public_tap_or_well_pct                                                          -0.64
## Secondary_school_attendance_Lower_11_15_years_pct                                                -0.32
## Secondary_school_attendance_Upper_16_18_years_pct                                                -0.25
## Stunting_total                                                                                    0.78
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                   -0.40
## Index_of_Industrial_production                                                                    0.11
## Infant_mortality_rate                                                                             1.00
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                      0.34
## Under_five_mortality_rate                                                                         1.00
## monthly_income                                                                                   -0.46
##                                                                              monthly_income
## Poverty_GSO_WB_poverty_headcount_pct                                                 -0.492
## Main_employment_agriculture_pct                                                      -0.702
## Main_employment_wage_work_pct                                                         0.698
## Main_light_source_electricity_pct                                                     0.224
## Sanitation_indoor_flush_toilet_pct                                                    0.728
## Water_indoor_tap_public_tap_or_well_pct                                               0.572
## Secondary_school_attendance_Lower_11_15_years_pct                                    -0.053
## Secondary_school_attendance_Upper_16_18_years_pct                                    -0.034
## Stunting_total                                                                       -0.664
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                        0.340
## Index_of_Industrial_production                                                       -0.116
## Infant_mortality_rate                                                                -0.469
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population         -0.546
## Under_five_mortality_rate                                                            -0.459
## monthly_income                                                                        1.000
wtd.cors(S_data_ranks)
##                                                                              Poverty_GSO_WB_poverty_headcount_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                         1.00
## Main_employment_agriculture_pct                                                                              0.83
## Main_employment_wage_work_pct                                                                               -0.82
## Main_light_source_electricity_pct                                                                           -0.70
## Sanitation_indoor_flush_toilet_pct                                                                          -0.64
## Water_indoor_tap_public_tap_or_well_pct                                                                     -0.67
## Secondary_school_attendance_Lower_11_15_years_pct                                                           -0.42
## Secondary_school_attendance_Upper_16_18_years_pct                                                           -0.42
## Stunting_total                                                                                               0.81
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                              -0.73
## Index_of_Industrial_production                                                                              -0.17
## Infant_mortality_rate                                                                                        0.85
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                 0.49
## Under_five_mortality_rate                                                                                    0.85
## monthly_income                                                                                              -0.58
##                                                                              Main_employment_agriculture_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                    0.83
## Main_employment_agriculture_pct                                                                         1.00
## Main_employment_wage_work_pct                                                                          -0.98
## Main_light_source_electricity_pct                                                                      -0.50
## Sanitation_indoor_flush_toilet_pct                                                                     -0.83
## Water_indoor_tap_public_tap_or_well_pct                                                                -0.79
## Secondary_school_attendance_Lower_11_15_years_pct                                                      -0.07
## Secondary_school_attendance_Upper_16_18_years_pct                                                      -0.14
## Stunting_total                                                                                          0.73
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                         -0.56
## Index_of_Industrial_production                                                                         -0.18
## Infant_mortality_rate                                                                                   0.76
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                            0.75
## Under_five_mortality_rate                                                                               0.76
## monthly_income                                                                                         -0.68
##                                                                              Main_employment_wage_work_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                -0.821
## Main_employment_agriculture_pct                                                                     -0.984
## Main_employment_wage_work_pct                                                                        1.000
## Main_light_source_electricity_pct                                                                    0.503
## Sanitation_indoor_flush_toilet_pct                                                                   0.800
## Water_indoor_tap_public_tap_or_well_pct                                                              0.746
## Secondary_school_attendance_Lower_11_15_years_pct                                                    0.084
## Secondary_school_attendance_Upper_16_18_years_pct                                                    0.144
## Stunting_total                                                                                      -0.718
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                       0.598
## Index_of_Industrial_production                                                                       0.216
## Infant_mortality_rate                                                                               -0.742
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                        -0.725
## Under_five_mortality_rate                                                                           -0.741
## monthly_income                                                                                       0.663
##                                                                              Main_light_source_electricity_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                     -0.70
## Main_employment_agriculture_pct                                                                          -0.50
## Main_employment_wage_work_pct                                                                             0.50
## Main_light_source_electricity_pct                                                                         1.00
## Sanitation_indoor_flush_toilet_pct                                                                        0.34
## Water_indoor_tap_public_tap_or_well_pct                                                                   0.43
## Secondary_school_attendance_Lower_11_15_years_pct                                                         0.75
## Secondary_school_attendance_Upper_16_18_years_pct                                                         0.73
## Stunting_total                                                                                           -0.58
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                            0.69
## Index_of_Industrial_production                                                                            0.25
## Infant_mortality_rate                                                                                    -0.51
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                             -0.30
## Under_five_mortality_rate                                                                                -0.51
## monthly_income                                                                                            0.11
##                                                                              Sanitation_indoor_flush_toilet_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                      -0.64
## Main_employment_agriculture_pct                                                                           -0.83
## Main_employment_wage_work_pct                                                                              0.80
## Main_light_source_electricity_pct                                                                          0.34
## Sanitation_indoor_flush_toilet_pct                                                                         1.00
## Water_indoor_tap_public_tap_or_well_pct                                                                    0.65
## Secondary_school_attendance_Lower_11_15_years_pct                                                          0.05
## Secondary_school_attendance_Upper_16_18_years_pct                                                          0.13
## Stunting_total                                                                                            -0.58
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                             0.30
## Index_of_Industrial_production                                                                             0.06
## Infant_mortality_rate                                                                                     -0.53
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                              -0.70
## Under_five_mortality_rate                                                                                 -0.53
## monthly_income                                                                                             0.69
##                                                                              Water_indoor_tap_public_tap_or_well_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                          -0.668
## Main_employment_agriculture_pct                                                                               -0.789
## Main_employment_wage_work_pct                                                                                  0.746
## Main_light_source_electricity_pct                                                                              0.434
## Sanitation_indoor_flush_toilet_pct                                                                             0.645
## Water_indoor_tap_public_tap_or_well_pct                                                                        1.000
## Secondary_school_attendance_Lower_11_15_years_pct                                                              0.039
## Secondary_school_attendance_Upper_16_18_years_pct                                                              0.086
## Stunting_total                                                                                                -0.672
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                 0.446
## Index_of_Industrial_production                                                                                 0.217
## Infant_mortality_rate                                                                                         -0.720
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                  -0.680
## Under_five_mortality_rate                                                                                     -0.719
## monthly_income                                                                                                 0.606
##                                                                              Secondary_school_attendance_Lower_11_15_years_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                                    -0.417
## Main_employment_agriculture_pct                                                                                         -0.070
## Main_employment_wage_work_pct                                                                                            0.084
## Main_light_source_electricity_pct                                                                                        0.746
## Sanitation_indoor_flush_toilet_pct                                                                                       0.050
## Water_indoor_tap_public_tap_or_well_pct                                                                                  0.039
## Secondary_school_attendance_Lower_11_15_years_pct                                                                        1.000
## Secondary_school_attendance_Upper_16_18_years_pct                                                                        0.948
## Stunting_total                                                                                                          -0.299
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                           0.463
## Index_of_Industrial_production                                                                                           0.182
## Infant_mortality_rate                                                                                                   -0.167
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                            -0.014
## Under_five_mortality_rate                                                                                               -0.167
## monthly_income                                                                                                          -0.124
##                                                                              Secondary_school_attendance_Upper_16_18_years_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                                    -0.419
## Main_employment_agriculture_pct                                                                                         -0.137
## Main_employment_wage_work_pct                                                                                            0.144
## Main_light_source_electricity_pct                                                                                        0.732
## Sanitation_indoor_flush_toilet_pct                                                                                       0.130
## Water_indoor_tap_public_tap_or_well_pct                                                                                  0.086
## Secondary_school_attendance_Lower_11_15_years_pct                                                                        0.948
## Secondary_school_attendance_Upper_16_18_years_pct                                                                        1.000
## Stunting_total                                                                                                          -0.280
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                           0.427
## Index_of_Industrial_production                                                                                           0.159
## Infant_mortality_rate                                                                                                   -0.132
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                            -0.164
## Under_five_mortality_rate                                                                                               -0.131
## monthly_income                                                                                                          -0.084
##                                                                              Stunting_total
## Poverty_GSO_WB_poverty_headcount_pct                                                   0.81
## Main_employment_agriculture_pct                                                        0.73
## Main_employment_wage_work_pct                                                         -0.72
## Main_light_source_electricity_pct                                                     -0.58
## Sanitation_indoor_flush_toilet_pct                                                    -0.58
## Water_indoor_tap_public_tap_or_well_pct                                               -0.67
## Secondary_school_attendance_Lower_11_15_years_pct                                     -0.30
## Secondary_school_attendance_Upper_16_18_years_pct                                     -0.28
## Stunting_total                                                                         1.00
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                        -0.57
## Index_of_Industrial_production                                                        -0.17
## Infant_mortality_rate                                                                  0.81
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population           0.46
## Under_five_mortality_rate                                                              0.81
## monthly_income                                                                        -0.57
##                                                                              Foreign_direct_investment_projects_licensed_in_2016_per_capita
## Poverty_GSO_WB_poverty_headcount_pct                                                                                                  -0.73
## Main_employment_agriculture_pct                                                                                                       -0.56
## Main_employment_wage_work_pct                                                                                                          0.60
## Main_light_source_electricity_pct                                                                                                      0.69
## Sanitation_indoor_flush_toilet_pct                                                                                                     0.30
## Water_indoor_tap_public_tap_or_well_pct                                                                                                0.45
## Secondary_school_attendance_Lower_11_15_years_pct                                                                                      0.46
## Secondary_school_attendance_Upper_16_18_years_pct                                                                                      0.43
## Stunting_total                                                                                                                        -0.57
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                                         1.00
## Index_of_Industrial_production                                                                                                         0.33
## Infant_mortality_rate                                                                                                                 -0.59
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                                          -0.24
## Under_five_mortality_rate                                                                                                             -0.59
## monthly_income                                                                                                                         0.28
##                                                                              Index_of_Industrial_production
## Poverty_GSO_WB_poverty_headcount_pct                                                                  -0.17
## Main_employment_agriculture_pct                                                                       -0.18
## Main_employment_wage_work_pct                                                                          0.22
## Main_light_source_electricity_pct                                                                      0.25
## Sanitation_indoor_flush_toilet_pct                                                                     0.06
## Water_indoor_tap_public_tap_or_well_pct                                                                0.22
## Secondary_school_attendance_Lower_11_15_years_pct                                                      0.18
## Secondary_school_attendance_Upper_16_18_years_pct                                                      0.16
## Stunting_total                                                                                        -0.17
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                         0.33
## Index_of_Industrial_production                                                                         1.00
## Infant_mortality_rate                                                                                 -0.15
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                          -0.17
## Under_five_mortality_rate                                                                             -0.15
## monthly_income                                                                                        -0.04
##                                                                              Infant_mortality_rate
## Poverty_GSO_WB_poverty_headcount_pct                                                          0.85
## Main_employment_agriculture_pct                                                               0.76
## Main_employment_wage_work_pct                                                                -0.74
## Main_light_source_electricity_pct                                                            -0.51
## Sanitation_indoor_flush_toilet_pct                                                           -0.53
## Water_indoor_tap_public_tap_or_well_pct                                                      -0.72
## Secondary_school_attendance_Lower_11_15_years_pct                                            -0.17
## Secondary_school_attendance_Upper_16_18_years_pct                                            -0.13
## Stunting_total                                                                                0.81
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                               -0.59
## Index_of_Industrial_production                                                               -0.15
## Infant_mortality_rate                                                                         1.00
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                  0.46
## Under_five_mortality_rate                                                                     1.00
## monthly_income                                                                               -0.63
##                                                                              Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population
## Poverty_GSO_WB_poverty_headcount_pct                                                                                                                0.486
## Main_employment_agriculture_pct                                                                                                                     0.746
## Main_employment_wage_work_pct                                                                                                                      -0.725
## Main_light_source_electricity_pct                                                                                                                  -0.298
## Sanitation_indoor_flush_toilet_pct                                                                                                                 -0.701
## Water_indoor_tap_public_tap_or_well_pct                                                                                                            -0.680
## Secondary_school_attendance_Lower_11_15_years_pct                                                                                                  -0.014
## Secondary_school_attendance_Upper_16_18_years_pct                                                                                                  -0.164
## Stunting_total                                                                                                                                      0.461
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                                                     -0.245
## Index_of_Industrial_production                                                                                                                     -0.167
## Infant_mortality_rate                                                                                                                               0.456
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                                                        1.000
## Under_five_mortality_rate                                                                                                                           0.455
## monthly_income                                                                                                                                     -0.536
##                                                                              Under_five_mortality_rate
## Poverty_GSO_WB_poverty_headcount_pct                                                              0.85
## Main_employment_agriculture_pct                                                                   0.76
## Main_employment_wage_work_pct                                                                    -0.74
## Main_light_source_electricity_pct                                                                -0.51
## Sanitation_indoor_flush_toilet_pct                                                               -0.53
## Water_indoor_tap_public_tap_or_well_pct                                                          -0.72
## Secondary_school_attendance_Lower_11_15_years_pct                                                -0.17
## Secondary_school_attendance_Upper_16_18_years_pct                                                -0.13
## Stunting_total                                                                                    0.81
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                   -0.59
## Index_of_Industrial_production                                                                   -0.15
## Infant_mortality_rate                                                                             1.00
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                      0.45
## Under_five_mortality_rate                                                                         1.00
## monthly_income                                                                                   -0.63
##                                                                              monthly_income
## Poverty_GSO_WB_poverty_headcount_pct                                                 -0.576
## Main_employment_agriculture_pct                                                      -0.680
## Main_employment_wage_work_pct                                                         0.663
## Main_light_source_electricity_pct                                                     0.113
## Sanitation_indoor_flush_toilet_pct                                                    0.692
## Water_indoor_tap_public_tap_or_well_pct                                               0.606
## Secondary_school_attendance_Lower_11_15_years_pct                                    -0.124
## Secondary_school_attendance_Upper_16_18_years_pct                                    -0.084
## Stunting_total                                                                       -0.574
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                        0.278
## Index_of_Industrial_production                                                       -0.040
## Infant_mortality_rate                                                                -0.635
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population         -0.536
## Under_five_mortality_rate                                                            -0.634
## monthly_income                                                                        1.000
wtd.cors(S_data_density)
##                                                                              Poverty_GSO_WB_poverty_headcount_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                         1.00
## Main_employment_agriculture_pct                                                                              0.65
## Main_employment_wage_work_pct                                                                               -0.56
## Main_light_source_electricity_pct                                                                           -0.79
## Sanitation_indoor_flush_toilet_pct                                                                          -0.42
## Water_indoor_tap_public_tap_or_well_pct                                                                     -0.56
## Secondary_school_attendance_Lower_11_15_years_pct                                                           -0.37
## Secondary_school_attendance_Upper_16_18_years_pct                                                           -0.31
## Stunting_total                                                                                               0.65
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                              -0.35
## Index_of_Industrial_production                                                                               0.04
## Infant_mortality_rate                                                                                        0.81
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                 0.42
## Under_five_mortality_rate                                                                                    0.81
## monthly_income                                                                                              -0.21
##                                                                              Main_employment_agriculture_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                   0.646
## Main_employment_agriculture_pct                                                                        1.000
## Main_employment_wage_work_pct                                                                         -0.970
## Main_light_source_electricity_pct                                                                     -0.401
## Sanitation_indoor_flush_toilet_pct                                                                    -0.827
## Water_indoor_tap_public_tap_or_well_pct                                                               -0.711
## Secondary_school_attendance_Lower_11_15_years_pct                                                      0.160
## Secondary_school_attendance_Upper_16_18_years_pct                                                      0.117
## Stunting_total                                                                                         0.610
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                        -0.424
## Index_of_Industrial_production                                                                         0.064
## Infant_mortality_rate                                                                                  0.514
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                           0.613
## Under_five_mortality_rate                                                                              0.507
## monthly_income                                                                                        -0.539
##                                                                              Main_employment_wage_work_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                -0.559
## Main_employment_agriculture_pct                                                                     -0.970
## Main_employment_wage_work_pct                                                                        1.000
## Main_light_source_electricity_pct                                                                    0.327
## Sanitation_indoor_flush_toilet_pct                                                                   0.811
## Water_indoor_tap_public_tap_or_well_pct                                                              0.652
## Secondary_school_attendance_Lower_11_15_years_pct                                                   -0.188
## Secondary_school_attendance_Upper_16_18_years_pct                                                   -0.169
## Stunting_total                                                                                      -0.575
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                       0.493
## Index_of_Industrial_production                                                                      -0.031
## Infant_mortality_rate                                                                               -0.449
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                        -0.507
## Under_five_mortality_rate                                                                           -0.441
## monthly_income                                                                                       0.543
##                                                                              Main_light_source_electricity_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                    -0.790
## Main_employment_agriculture_pct                                                                         -0.401
## Main_employment_wage_work_pct                                                                            0.327
## Main_light_source_electricity_pct                                                                        1.000
## Sanitation_indoor_flush_toilet_pct                                                                       0.247
## Water_indoor_tap_public_tap_or_well_pct                                                                  0.325
## Secondary_school_attendance_Lower_11_15_years_pct                                                        0.543
## Secondary_school_attendance_Upper_16_18_years_pct                                                        0.427
## Stunting_total                                                                                          -0.466
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                           0.254
## Index_of_Industrial_production                                                                          -0.074
## Infant_mortality_rate                                                                                   -0.631
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                            -0.230
## Under_five_mortality_rate                                                                               -0.635
## monthly_income                                                                                          -0.058
##                                                                              Sanitation_indoor_flush_toilet_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                     -0.416
## Main_employment_agriculture_pct                                                                          -0.827
## Main_employment_wage_work_pct                                                                             0.811
## Main_light_source_electricity_pct                                                                         0.247
## Sanitation_indoor_flush_toilet_pct                                                                        1.000
## Water_indoor_tap_public_tap_or_well_pct                                                                   0.533
## Secondary_school_attendance_Lower_11_15_years_pct                                                        -0.074
## Secondary_school_attendance_Upper_16_18_years_pct                                                        -0.015
## Stunting_total                                                                                           -0.514
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                            0.225
## Index_of_Industrial_production                                                                           -0.080
## Infant_mortality_rate                                                                                    -0.273
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                             -0.547
## Under_five_mortality_rate                                                                                -0.267
## monthly_income                                                                                            0.633
##                                                                              Water_indoor_tap_public_tap_or_well_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                         -0.5633
## Main_employment_agriculture_pct                                                                              -0.7105
## Main_employment_wage_work_pct                                                                                 0.6524
## Main_light_source_electricity_pct                                                                             0.3254
## Sanitation_indoor_flush_toilet_pct                                                                            0.5327
## Water_indoor_tap_public_tap_or_well_pct                                                                       1.0000
## Secondary_school_attendance_Lower_11_15_years_pct                                                            -0.2351
## Secondary_school_attendance_Upper_16_18_years_pct                                                            -0.1952
## Stunting_total                                                                                               -0.5641
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                0.2830
## Index_of_Industrial_production                                                                               -0.0061
## Infant_mortality_rate                                                                                        -0.5439
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                 -0.5612
## Under_five_mortality_rate                                                                                    -0.5389
## monthly_income                                                                                                0.3853
##                                                                              Secondary_school_attendance_Lower_11_15_years_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                                    -0.370
## Main_employment_agriculture_pct                                                                                          0.160
## Main_employment_wage_work_pct                                                                                           -0.188
## Main_light_source_electricity_pct                                                                                        0.543
## Sanitation_indoor_flush_toilet_pct                                                                                      -0.074
## Water_indoor_tap_public_tap_or_well_pct                                                                                 -0.235
## Secondary_school_attendance_Lower_11_15_years_pct                                                                        1.000
## Secondary_school_attendance_Upper_16_18_years_pct                                                                        0.924
## Stunting_total                                                                                                          -0.119
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                           0.166
## Index_of_Industrial_production                                                                                           0.043
## Infant_mortality_rate                                                                                                   -0.193
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                             0.175
## Under_five_mortality_rate                                                                                               -0.200
## monthly_income                                                                                                          -0.398
##                                                                              Secondary_school_attendance_Upper_16_18_years_pct
## Poverty_GSO_WB_poverty_headcount_pct                                                                                    -0.308
## Main_employment_agriculture_pct                                                                                          0.117
## Main_employment_wage_work_pct                                                                                           -0.169
## Main_light_source_electricity_pct                                                                                        0.427
## Sanitation_indoor_flush_toilet_pct                                                                                      -0.015
## Water_indoor_tap_public_tap_or_well_pct                                                                                 -0.195
## Secondary_school_attendance_Lower_11_15_years_pct                                                                        0.924
## Secondary_school_attendance_Upper_16_18_years_pct                                                                        1.000
## Stunting_total                                                                                                          -0.096
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                           0.070
## Index_of_Industrial_production                                                                                           0.059
## Infant_mortality_rate                                                                                                   -0.095
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                            -0.011
## Under_five_mortality_rate                                                                                               -0.102
## monthly_income                                                                                                          -0.410
##                                                                              Stunting_total
## Poverty_GSO_WB_poverty_headcount_pct                                                  0.646
## Main_employment_agriculture_pct                                                       0.610
## Main_employment_wage_work_pct                                                        -0.575
## Main_light_source_electricity_pct                                                    -0.466
## Sanitation_indoor_flush_toilet_pct                                                   -0.514
## Water_indoor_tap_public_tap_or_well_pct                                              -0.564
## Secondary_school_attendance_Lower_11_15_years_pct                                    -0.119
## Secondary_school_attendance_Upper_16_18_years_pct                                    -0.096
## Stunting_total                                                                        1.000
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                       -0.197
## Index_of_Industrial_production                                                       -0.029
## Infant_mortality_rate                                                                 0.725
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population          0.317
## Under_five_mortality_rate                                                             0.723
## monthly_income                                                                       -0.358
##                                                                              Foreign_direct_investment_projects_licensed_in_2016_per_capita
## Poverty_GSO_WB_poverty_headcount_pct                                                                                                 -0.350
## Main_employment_agriculture_pct                                                                                                      -0.424
## Main_employment_wage_work_pct                                                                                                         0.493
## Main_light_source_electricity_pct                                                                                                     0.254
## Sanitation_indoor_flush_toilet_pct                                                                                                    0.225
## Water_indoor_tap_public_tap_or_well_pct                                                                                               0.283
## Secondary_school_attendance_Lower_11_15_years_pct                                                                                     0.166
## Secondary_school_attendance_Upper_16_18_years_pct                                                                                     0.070
## Stunting_total                                                                                                                       -0.197
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                                        1.000
## Index_of_Industrial_production                                                                                                        0.044
## Infant_mortality_rate                                                                                                                -0.292
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                                         -0.030
## Under_five_mortality_rate                                                                                                            -0.288
## monthly_income                                                                                                                        0.114
##                                                                              Index_of_Industrial_production
## Poverty_GSO_WB_poverty_headcount_pct                                                                 0.0402
## Main_employment_agriculture_pct                                                                      0.0636
## Main_employment_wage_work_pct                                                                       -0.0314
## Main_light_source_electricity_pct                                                                   -0.0735
## Sanitation_indoor_flush_toilet_pct                                                                  -0.0802
## Water_indoor_tap_public_tap_or_well_pct                                                             -0.0061
## Secondary_school_attendance_Lower_11_15_years_pct                                                    0.0431
## Secondary_school_attendance_Upper_16_18_years_pct                                                    0.0595
## Stunting_total                                                                                      -0.0290
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                       0.0440
## Index_of_Industrial_production                                                                       1.0000
## Infant_mortality_rate                                                                                0.1019
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                        -0.0801
## Under_five_mortality_rate                                                                            0.1085
## monthly_income                                                                                      -0.1209
##                                                                              Infant_mortality_rate
## Poverty_GSO_WB_poverty_headcount_pct                                                         0.807
## Main_employment_agriculture_pct                                                              0.514
## Main_employment_wage_work_pct                                                               -0.449
## Main_light_source_electricity_pct                                                           -0.631
## Sanitation_indoor_flush_toilet_pct                                                          -0.273
## Water_indoor_tap_public_tap_or_well_pct                                                     -0.544
## Secondary_school_attendance_Lower_11_15_years_pct                                           -0.193
## Secondary_school_attendance_Upper_16_18_years_pct                                           -0.095
## Stunting_total                                                                               0.725
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                              -0.292
## Index_of_Industrial_production                                                               0.102
## Infant_mortality_rate                                                                        1.000
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                 0.190
## Under_five_mortality_rate                                                                    1.000
## monthly_income                                                                              -0.213
##                                                                              Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population
## Poverty_GSO_WB_poverty_headcount_pct                                                                                                                0.424
## Main_employment_agriculture_pct                                                                                                                     0.613
## Main_employment_wage_work_pct                                                                                                                      -0.507
## Main_light_source_electricity_pct                                                                                                                  -0.230
## Sanitation_indoor_flush_toilet_pct                                                                                                                 -0.547
## Water_indoor_tap_public_tap_or_well_pct                                                                                                            -0.561
## Secondary_school_attendance_Lower_11_15_years_pct                                                                                                   0.175
## Secondary_school_attendance_Upper_16_18_years_pct                                                                                                  -0.011
## Stunting_total                                                                                                                                      0.317
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                                                     -0.030
## Index_of_Industrial_production                                                                                                                     -0.080
## Infant_mortality_rate                                                                                                                               0.190
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                                                        1.000
## Under_five_mortality_rate                                                                                                                           0.184
## monthly_income                                                                                                                                     -0.391
##                                                                              Under_five_mortality_rate
## Poverty_GSO_WB_poverty_headcount_pct                                                              0.81
## Main_employment_agriculture_pct                                                                   0.51
## Main_employment_wage_work_pct                                                                    -0.44
## Main_light_source_electricity_pct                                                                -0.63
## Sanitation_indoor_flush_toilet_pct                                                               -0.27
## Water_indoor_tap_public_tap_or_well_pct                                                          -0.54
## Secondary_school_attendance_Lower_11_15_years_pct                                                -0.20
## Secondary_school_attendance_Upper_16_18_years_pct                                                -0.10
## Stunting_total                                                                                    0.72
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                   -0.29
## Index_of_Industrial_production                                                                    0.11
## Infant_mortality_rate                                                                             1.00
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                      0.18
## Under_five_mortality_rate                                                                         1.00
## monthly_income                                                                                   -0.21
##                                                                              monthly_income
## Poverty_GSO_WB_poverty_headcount_pct                                                 -0.206
## Main_employment_agriculture_pct                                                      -0.539
## Main_employment_wage_work_pct                                                         0.543
## Main_light_source_electricity_pct                                                    -0.058
## Sanitation_indoor_flush_toilet_pct                                                    0.633
## Water_indoor_tap_public_tap_or_well_pct                                               0.385
## Secondary_school_attendance_Lower_11_15_years_pct                                    -0.398
## Secondary_school_attendance_Upper_16_18_years_pct                                    -0.410
## Stunting_total                                                                       -0.358
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                        0.114
## Index_of_Industrial_production                                                       -0.121
## Infant_mortality_rate                                                                -0.213
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population         -0.391
## Under_five_mortality_rate                                                            -0.206
## monthly_income                                                                        1.000
#factor analysis
S_list = list(
  standard = fa(S_data),
  ranks = fa(S_data_ranks),
  weights = fa(S_data, weight = d$pop_sqrt),
  ctrl_density = fa(S_data_density)
)

#print produces an error, so we have to print with try
try(print(S_list))
## $standard
## Factor Analysis using method =  minres
## Call: fa(r = S_data)
## Standardized loadings (pattern matrix) based upon correlation matrix
##                                                                                MR1
## Poverty_GSO_WB_poverty_headcount_pct                                          0.91
## Main_employment_agriculture_pct                                               0.92
## Main_employment_wage_work_pct                                                -0.87
## Main_light_source_electricity_pct                                            -0.68
## Sanitation_indoor_flush_toilet_pct                                           -0.77
## Water_indoor_tap_public_tap_or_well_pct                                      -0.79
## Secondary_school_attendance_Lower_11_15_years_pct                            -0.30
## Secondary_school_attendance_Upper_16_18_years_pct                            -0.31
## Stunting_total                                                                0.87
## Foreign_direct_investment_projects_licensed_in_2016_per_capita               -0.52
## Index_of_Industrial_production                                                0.06
## Infant_mortality_rate                                                         0.82
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population  0.62
## Under_five_mortality_rate                                                     0.82
## monthly_income                                                               -0.67
##                                                                                  h2
## Poverty_GSO_WB_poverty_headcount_pct                                         0.8262
## Main_employment_agriculture_pct                                              0.8411
## Main_employment_wage_work_pct                                                0.7555
## Main_light_source_electricity_pct                                            0.4691
## Sanitation_indoor_flush_toilet_pct                                           0.5872
## Water_indoor_tap_public_tap_or_well_pct                                      0.6163
## Secondary_school_attendance_Lower_11_15_years_pct                            0.0879
## Secondary_school_attendance_Upper_16_18_years_pct                            0.0932
## Stunting_total                                                               0.7644
## Foreign_direct_investment_projects_licensed_in_2016_per_capita               0.2752
## Index_of_Industrial_production                                               0.0042
## Infant_mortality_rate                                                        0.6786
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population 0.3893
## Under_five_mortality_rate                                                    0.6675
## monthly_income                                                               0.4436
##                                                                                u2
## Poverty_GSO_WB_poverty_headcount_pct                                         0.17
## Main_employment_agriculture_pct                                              0.16
## Main_employment_wage_work_pct                                                0.24
## Main_light_source_electricity_pct                                            0.53
## Sanitation_indoor_flush_toilet_pct                                           0.41
## Water_indoor_tap_public_tap_or_well_pct                                      0.38
## Secondary_school_attendance_Lower_11_15_years_pct                            0.91
## Secondary_school_attendance_Upper_16_18_years_pct                            0.91
## Stunting_total                                                               0.24
## Foreign_direct_investment_projects_licensed_in_2016_per_capita               0.72
## Index_of_Industrial_production                                               1.00
## Infant_mortality_rate                                                        0.32
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population 0.61
## Under_five_mortality_rate                                                    0.33
## monthly_income                                                               0.56
##                                                                              com
## Poverty_GSO_WB_poverty_headcount_pct                                           1
## Main_employment_agriculture_pct                                                1
## Main_employment_wage_work_pct                                                  1
## Main_light_source_electricity_pct                                              1
## Sanitation_indoor_flush_toilet_pct                                             1
## Water_indoor_tap_public_tap_or_well_pct                                        1
## Secondary_school_attendance_Lower_11_15_years_pct                              1
## Secondary_school_attendance_Upper_16_18_years_pct                              1
## Stunting_total                                                                 1
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                 1
## Index_of_Industrial_production                                                 1
## Infant_mortality_rate                                                          1
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population   1
## Under_five_mortality_rate                                                      1
## monthly_income                                                                 1
## 
##                MR1
## SS loadings    7.5
## Proportion Var 0.5
## 
## Mean item complexity =  1
## Test of the hypothesis that 1 factor is sufficient.
## 
## The degrees of freedom for the null model are  105  and the objective function was  26 with Chi Square of  1469
## The degrees of freedom for the model are 90  and the objective function was  17 
## 
## The root mean square of the residuals (RMSR) is  0.15 
## The df corrected root mean square of the residuals is  0.16 
## 
## The harmonic number of observations is  63 with the empirical chi square  307  with prob <  1.5e-25 
## The total number of observations was  63  with Likelihood Chi Square =  932  with prob <  3.6e-140 
## 
## Tucker Lewis Index of factoring reliability =  0.27
## RMSEA index =  0.41  and the 90 % confidence intervals are  0.37 NA
## BIC =  560
## Fit based upon off diagonal values = 0.91
## Measures of factor score adequacy             
##                                                    MR1
## Correlation of (regression) scores with factors   0.99
## Multiple R square of scores with factors          0.99
## Minimum correlation of possible factor scores     0.97
## 
## $ranks
## Factor Analysis using method =  minres
## Call: fa(r = S_data_ranks)
## Standardized loadings (pattern matrix) based upon correlation matrix
##                                                                                MR1
## Poverty_GSO_WB_poverty_headcount_pct                                         -0.94
## Main_employment_agriculture_pct                                              -0.93
## Main_employment_wage_work_pct                                                 0.92
## Main_light_source_electricity_pct                                             0.63
## Sanitation_indoor_flush_toilet_pct                                            0.74
## Water_indoor_tap_public_tap_or_well_pct                                       0.80
## Secondary_school_attendance_Lower_11_15_years_pct                             0.28
## Secondary_school_attendance_Upper_16_18_years_pct                             0.30
## Stunting_total                                                               -0.85
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                0.66
## Index_of_Industrial_production                                                0.22
## Infant_mortality_rate                                                        -0.87
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population -0.65
## Under_five_mortality_rate                                                    -0.87
## monthly_income                                                                0.65
##                                                                                 h2
## Poverty_GSO_WB_poverty_headcount_pct                                         0.879
## Main_employment_agriculture_pct                                              0.870
## Main_employment_wage_work_pct                                                0.844
## Main_light_source_electricity_pct                                            0.402
## Sanitation_indoor_flush_toilet_pct                                           0.541
## Water_indoor_tap_public_tap_or_well_pct                                      0.642
## Secondary_school_attendance_Lower_11_15_years_pct                            0.077
## Secondary_school_attendance_Upper_16_18_years_pct                            0.092
## Stunting_total                                                               0.719
## Foreign_direct_investment_projects_licensed_in_2016_per_capita               0.439
## Index_of_Industrial_production                                               0.047
## Infant_mortality_rate                                                        0.765
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population 0.418
## Under_five_mortality_rate                                                    0.764
## monthly_income                                                               0.428
##                                                                                u2
## Poverty_GSO_WB_poverty_headcount_pct                                         0.12
## Main_employment_agriculture_pct                                              0.13
## Main_employment_wage_work_pct                                                0.16
## Main_light_source_electricity_pct                                            0.60
## Sanitation_indoor_flush_toilet_pct                                           0.46
## Water_indoor_tap_public_tap_or_well_pct                                      0.36
## Secondary_school_attendance_Lower_11_15_years_pct                            0.92
## Secondary_school_attendance_Upper_16_18_years_pct                            0.91
## Stunting_total                                                               0.28
## Foreign_direct_investment_projects_licensed_in_2016_per_capita               0.56
## Index_of_Industrial_production                                               0.95
## Infant_mortality_rate                                                        0.23
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population 0.58
## Under_five_mortality_rate                                                    0.24
## monthly_income                                                               0.57
##                                                                              com
## Poverty_GSO_WB_poverty_headcount_pct                                           1
## Main_employment_agriculture_pct                                                1
## Main_employment_wage_work_pct                                                  1
## Main_light_source_electricity_pct                                              1
## Sanitation_indoor_flush_toilet_pct                                             1
## Water_indoor_tap_public_tap_or_well_pct                                        1
## Secondary_school_attendance_Lower_11_15_years_pct                              1
## Secondary_school_attendance_Upper_16_18_years_pct                              1
## Stunting_total                                                                 1
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                 1
## Index_of_Industrial_production                                                 1
## Infant_mortality_rate                                                          1
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population   1
## Under_five_mortality_rate                                                      1
## monthly_income                                                                 1
## 
##                 MR1
## SS loadings    7.93
## Proportion Var 0.53
## 
## Mean item complexity =  1
## Test of the hypothesis that 1 factor is sufficient.
## 
## The degrees of freedom for the null model are  105  and the objective function was  28 with Chi Square of  1562
## The degrees of freedom for the model are 90  and the objective function was  17 
## 
## The root mean square of the residuals (RMSR) is  0.17 
## The df corrected root mean square of the residuals is  0.18 
## 
## The harmonic number of observations is  63 with the empirical chi square  363  with prob <  1.7e-34 
## The total number of observations was  63  with Likelihood Chi Square =  942  with prob <  5e-142 
## 
## Tucker Lewis Index of factoring reliability =  0.31
## RMSEA index =  0.41  and the 90 % confidence intervals are  0.37 NA
## BIC =  569
## Fit based upon off diagonal values = 0.91
## Measures of factor score adequacy             
##                                                    MR1
## Correlation of (regression) scores with factors   0.99
## Multiple R square of scores with factors          0.98
## Minimum correlation of possible factor scores     0.96
## 
## $weights
## Factor Analysis using method =  minres
## Call: fa(r = S_data, weight = d$pop_sqrt)
## Standardized loadings (pattern matrix) based upon correlation matrix
##                                                                                MR1
## Poverty_GSO_WB_poverty_headcount_pct                                          0.89
## Main_employment_agriculture_pct                                               0.93
## Main_employment_wage_work_pct                                                -0.89
## Main_light_source_electricity_pct                                            -0.64
## Sanitation_indoor_flush_toilet_pct                                           -0.80
## Water_indoor_tap_public_tap_or_well_pct                                      -0.80
## Secondary_school_attendance_Lower_11_15_years_pct                            -0.26
## Secondary_school_attendance_Upper_16_18_years_pct                            -0.28
## Stunting_total                                                                0.89
## Foreign_direct_investment_projects_licensed_in_2016_per_capita               -0.53
## Index_of_Industrial_production                                                0.05
## Infant_mortality_rate                                                         0.81
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population  0.67
## Under_five_mortality_rate                                                     0.80
## monthly_income                                                               -0.72
##                                                                                  h2
## Poverty_GSO_WB_poverty_headcount_pct                                         0.7949
## Main_employment_agriculture_pct                                              0.8592
## Main_employment_wage_work_pct                                                0.7870
## Main_light_source_electricity_pct                                            0.4105
## Sanitation_indoor_flush_toilet_pct                                           0.6440
## Water_indoor_tap_public_tap_or_well_pct                                      0.6418
## Secondary_school_attendance_Lower_11_15_years_pct                            0.0691
## Secondary_school_attendance_Upper_16_18_years_pct                            0.0799
## Stunting_total                                                               0.7879
## Foreign_direct_investment_projects_licensed_in_2016_per_capita               0.2784
## Index_of_Industrial_production                                               0.0026
## Infant_mortality_rate                                                        0.6547
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population 0.4533
## Under_five_mortality_rate                                                    0.6394
## monthly_income                                                               0.5167
##                                                                                u2
## Poverty_GSO_WB_poverty_headcount_pct                                         0.21
## Main_employment_agriculture_pct                                              0.14
## Main_employment_wage_work_pct                                                0.21
## Main_light_source_electricity_pct                                            0.59
## Sanitation_indoor_flush_toilet_pct                                           0.36
## Water_indoor_tap_public_tap_or_well_pct                                      0.36
## Secondary_school_attendance_Lower_11_15_years_pct                            0.93
## Secondary_school_attendance_Upper_16_18_years_pct                            0.92
## Stunting_total                                                               0.21
## Foreign_direct_investment_projects_licensed_in_2016_per_capita               0.72
## Index_of_Industrial_production                                               1.00
## Infant_mortality_rate                                                        0.35
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population 0.55
## Under_five_mortality_rate                                                    0.36
## monthly_income                                                               0.48
##                                                                              com
## Poverty_GSO_WB_poverty_headcount_pct                                           1
## Main_employment_agriculture_pct                                                1
## Main_employment_wage_work_pct                                                  1
## Main_light_source_electricity_pct                                              1
## Sanitation_indoor_flush_toilet_pct                                             1
## Water_indoor_tap_public_tap_or_well_pct                                        1
## Secondary_school_attendance_Lower_11_15_years_pct                              1
## Secondary_school_attendance_Upper_16_18_years_pct                              1
## Stunting_total                                                                 1
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                 1
## Index_of_Industrial_production                                                 1
## Infant_mortality_rate                                                          1
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population   1
## Under_five_mortality_rate                                                      1
## monthly_income                                                                 1
## 
##                 MR1
## SS loadings    7.62
## Proportion Var 0.51
## 
## Mean item complexity =  1
## Test of the hypothesis that 1 factor is sufficient.
## 
## The degrees of freedom for the null model are  105  and the objective function was  27 with Chi Square of  1515
## The degrees of freedom for the model are 90  and the objective function was  17 
## 
## The root mean square of the residuals (RMSR) is  0.16 
## The df corrected root mean square of the residuals is  0.17 
## 
## The harmonic number of observations is  63 with the empirical chi square  319  with prob <  2.7e-27 
## The total number of observations was  63  with Likelihood Chi Square =  955  with prob <  1.2e-144 
## 
## Tucker Lewis Index of factoring reliability =  0.28
## RMSEA index =  0.42  and the 90 % confidence intervals are  0.37 NA
## BIC =  582
## Fit based upon off diagonal values = 0.91
## Measures of factor score adequacy             
##                                                    MR1
## Correlation of (regression) scores with factors   0.99
## Multiple R square of scores with factors          0.99
## Minimum correlation of possible factor scores     0.98
## 
## $ctrl_density
## Factor Analysis using method =  minres
## Call: fa(r = S_data_density)
## Standardized loadings (pattern matrix) based upon correlation matrix
##                                                                                MR1
## Poverty_GSO_WB_poverty_headcount_pct                                          0.84
## Main_employment_agriculture_pct                                               0.90
## Main_employment_wage_work_pct                                                -0.83
## Main_light_source_electricity_pct                                            -0.58
## Sanitation_indoor_flush_toilet_pct                                           -0.69
## Water_indoor_tap_public_tap_or_well_pct                                      -0.74
## Secondary_school_attendance_Lower_11_15_years_pct                            -0.05
## Secondary_school_attendance_Upper_16_18_years_pct                            -0.04
## Stunting_total                                                                0.77
## Foreign_direct_investment_projects_licensed_in_2016_per_capita               -0.39
## Index_of_Industrial_production                                                0.06
## Infant_mortality_rate                                                         0.77
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population  0.52
## Under_five_mortality_rate                                                     0.76
## monthly_income                                                               -0.45
##                                                                                  h2
## Poverty_GSO_WB_poverty_headcount_pct                                         0.7072
## Main_employment_agriculture_pct                                              0.8128
## Main_employment_wage_work_pct                                                0.6908
## Main_light_source_electricity_pct                                            0.3386
## Sanitation_indoor_flush_toilet_pct                                           0.4723
## Water_indoor_tap_public_tap_or_well_pct                                      0.5540
## Secondary_school_attendance_Lower_11_15_years_pct                            0.0028
## Secondary_school_attendance_Upper_16_18_years_pct                            0.0014
## Stunting_total                                                               0.5908
## Foreign_direct_investment_projects_licensed_in_2016_per_capita               0.1522
## Index_of_Industrial_production                                               0.0034
## Infant_mortality_rate                                                        0.5868
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population 0.2703
## Under_five_mortality_rate                                                    0.5787
## monthly_income                                                               0.2054
##                                                                                u2
## Poverty_GSO_WB_poverty_headcount_pct                                         0.29
## Main_employment_agriculture_pct                                              0.19
## Main_employment_wage_work_pct                                                0.31
## Main_light_source_electricity_pct                                            0.66
## Sanitation_indoor_flush_toilet_pct                                           0.53
## Water_indoor_tap_public_tap_or_well_pct                                      0.45
## Secondary_school_attendance_Lower_11_15_years_pct                            1.00
## Secondary_school_attendance_Upper_16_18_years_pct                            1.00
## Stunting_total                                                               0.41
## Foreign_direct_investment_projects_licensed_in_2016_per_capita               0.85
## Index_of_Industrial_production                                               1.00
## Infant_mortality_rate                                                        0.41
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population 0.73
## Under_five_mortality_rate                                                    0.42
## monthly_income                                                               0.79
##                                                                              com
## Poverty_GSO_WB_poverty_headcount_pct                                           1
## Main_employment_agriculture_pct                                                1
## Main_employment_wage_work_pct                                                  1
## Main_light_source_electricity_pct                                              1
## Sanitation_indoor_flush_toilet_pct                                             1
## Water_indoor_tap_public_tap_or_well_pct                                        1
## Secondary_school_attendance_Lower_11_15_years_pct                              1
## Secondary_school_attendance_Upper_16_18_years_pct                              1
## Stunting_total                                                                 1
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                 1
## Index_of_Industrial_production                                                 1
## Infant_mortality_rate                                                          1
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population   1
## Under_five_mortality_rate                                                      1
## monthly_income                                                                 1
## 
##                MR1
## SS loadings    6.0
## Proportion Var 0.4
## 
## Mean item complexity =  1
## Test of the hypothesis that 1 factor is sufficient.
## 
## The degrees of freedom for the null model are  105  and the objective function was  24 with Chi Square of  1325
## The degrees of freedom for the model are 90  and the objective function was  17 
## 
## The root mean square of the residuals (RMSR) is  0.19 
## The df corrected root mean square of the residuals is  0.21 
## 
## The harmonic number of observations is  63 with the empirical chi square  494  with prob <  5.6e-57 
## The total number of observations was  63  with Likelihood Chi Square =  957  with prob <  5.3e-145 
## 
## Tucker Lewis Index of factoring reliability =  0.16
## RMSEA index =  0.42  and the 90 % confidence intervals are  0.37 NA
## BIC =  584
## Fit based upon off diagonal values = 0.8
## Measures of factor score adequacy             
##                                                    MR1
## Correlation of (regression) scores with factors   0.99
## Multiple R square of scores with factors          0.98
## Minimum correlation of possible factor scores     0.95
#plot
fa_plot_loadings(S_list, reverse_vector = c(-1, 1, -1, -1)) +
  scale_y_continuous(limits = c(-1, 1))

GG_save("figs/S_loadings_compare.png")

#check for method variance
fa_mv = fa_all_methods(S_data, weights = d$sqrt_pop)
## 1 out of 40 - regression_minres
## Saving results from regression_minres
## 2 out of 40 - Thurstone_minres
## Saving results from Thurstone_minres
## 3 out of 40 - tenBerge_minres
## Saving results from tenBerge_minres
## 4 out of 40 - Anderson_minres
## Skipping Anderson_minres due to extraction error
## 5 out of 40 - Bartlett_minres
## Saving results from Bartlett_minres
## 6 out of 40 - regression_ols
## Saving results from regression_ols
## 7 out of 40 - Thurstone_ols
## Saving results from Thurstone_ols
## 8 out of 40 - tenBerge_ols
## Saving results from tenBerge_ols
## 9 out of 40 - Anderson_ols
## Skipping Anderson_ols due to extraction error
## 10 out of 40 - Bartlett_ols
## Saving results from Bartlett_ols
## 11 out of 40 - regression_wls
## Saving results from regression_wls
## 12 out of 40 - Thurstone_wls
## Saving results from Thurstone_wls
## 13 out of 40 - tenBerge_wls
## Saving results from tenBerge_wls
## 14 out of 40 - Anderson_wls
## Skipping Anderson_wls due to extraction error
## 15 out of 40 - Bartlett_wls
## Saving results from Bartlett_wls
## 16 out of 40 - regression_gls
## Saving results from regression_gls
## 17 out of 40 - Thurstone_gls
## Saving results from Thurstone_gls
## 18 out of 40 - tenBerge_gls
## Saving results from tenBerge_gls
## 19 out of 40 - Anderson_gls
## Skipping Anderson_gls due to extraction error
## 20 out of 40 - Bartlett_gls
## Saving results from Bartlett_gls
## 21 out of 40 - regression_pa
## Saving results from regression_pa
## 22 out of 40 - Thurstone_pa
## Saving results from Thurstone_pa
## 23 out of 40 - tenBerge_pa
## Saving results from tenBerge_pa
## 24 out of 40 - Anderson_pa
## Skipping Anderson_pa due to extraction error
## 25 out of 40 - Bartlett_pa
## Saving results from Bartlett_pa
## 26 out of 40 - regression_ml
## Saving results from regression_ml
## 27 out of 40 - Thurstone_ml
## Saving results from Thurstone_ml
## 28 out of 40 - tenBerge_ml
## Saving results from tenBerge_ml
## 29 out of 40 - Anderson_ml
## Skipping Anderson_ml due to extraction error
## 30 out of 40 - Bartlett_ml
## Saving results from Bartlett_ml
## 31 out of 40 - regression_minchi
## Saving results from regression_minchi
## 32 out of 40 - Thurstone_minchi
## Saving results from Thurstone_minchi
## 33 out of 40 - tenBerge_minchi
## Saving results from tenBerge_minchi
## 34 out of 40 - Anderson_minchi
## Skipping Anderson_minchi due to extraction error
## 35 out of 40 - Bartlett_minchi
## Saving results from Bartlett_minchi
## 36 out of 40 - regression_minrank
## Saving results from regression_minrank
## 37 out of 40 - Thurstone_minrank
## Saving results from Thurstone_minrank
## 38 out of 40 - tenBerge_minrank
## Saving results from tenBerge_minrank
## 39 out of 40 - Anderson_minrank
## Skipping Anderson_minrank due to extraction error
## 40 out of 40 - Bartlett_minrank
## Saving results from Bartlett_minrank
#examine cors of scores
fa_mv$scores %>% wtd.cors()
##                    regression_minres Thurstone_minres tenBerge_minres
## regression_minres               1.00             1.00            1.00
## Thurstone_minres                1.00             1.00            1.00
## tenBerge_minres                 1.00             1.00            1.00
## Bartlett_minres                 0.99             0.99            0.99
## regression_ols                  1.00             1.00            1.00
## Thurstone_ols                   1.00             1.00            1.00
## tenBerge_ols                    1.00             1.00            1.00
## Bartlett_ols                    0.99             0.99            0.99
## regression_wls                  0.99             0.99            0.99
## Thurstone_wls                   0.99             0.99            0.99
## tenBerge_wls                    0.99             0.99            0.99
## Bartlett_wls                    0.99             0.99            0.99
## regression_gls                  0.99             0.99            0.99
## Thurstone_gls                   0.99             0.99            0.99
## tenBerge_gls                    0.99             0.99            0.99
## Bartlett_gls                    0.99             0.99            0.99
## regression_pa                   1.00             1.00            1.00
## Thurstone_pa                    1.00             1.00            1.00
## tenBerge_pa                     1.00             1.00            1.00
## Bartlett_pa                     0.99             0.99            0.99
## regression_ml                  -0.93            -0.93           -0.93
## Thurstone_ml                   -0.93            -0.93           -0.93
## tenBerge_ml                    -0.93            -0.93           -0.93
## Bartlett_ml                    -0.93            -0.93           -0.93
## regression_minchi               0.90             0.90            0.90
## Thurstone_minchi                0.90             0.90            0.90
## tenBerge_minchi                 0.90             0.90            0.90
## Bartlett_minchi                 0.88             0.88            0.88
## regression_minrank              0.99             0.99            0.99
## Thurstone_minrank               0.99             0.99            0.99
## tenBerge_minrank                0.99             0.99            0.99
## Bartlett_minrank                0.99             0.99            0.99
##                    Bartlett_minres regression_ols Thurstone_ols
## regression_minres             0.99           1.00          1.00
## Thurstone_minres              0.99           1.00          1.00
## tenBerge_minres               0.99           1.00          1.00
## Bartlett_minres               1.00           0.99          0.99
## regression_ols                0.99           1.00          1.00
## Thurstone_ols                 0.99           1.00          1.00
## tenBerge_ols                  0.99           1.00          1.00
## Bartlett_ols                  1.00           0.99          0.99
## regression_wls                1.00           0.99          0.99
## Thurstone_wls                 1.00           0.99          0.99
## tenBerge_wls                  1.00           0.99          0.99
## Bartlett_wls                  1.00           0.99          0.99
## regression_gls                1.00           0.99          0.99
## Thurstone_gls                 1.00           0.99          0.99
## tenBerge_gls                  1.00           0.99          0.99
## Bartlett_gls                  1.00           0.99          0.99
## regression_pa                 0.99           1.00          1.00
## Thurstone_pa                  0.99           1.00          1.00
## tenBerge_pa                   0.99           1.00          1.00
## Bartlett_pa                   1.00           0.99          0.99
## regression_ml                -0.94          -0.93         -0.93
## Thurstone_ml                 -0.94          -0.93         -0.93
## tenBerge_ml                  -0.94          -0.93         -0.93
## Bartlett_ml                  -0.94          -0.93         -0.93
## regression_minchi             0.91           0.90          0.90
## Thurstone_minchi              0.91           0.90          0.90
## tenBerge_minchi               0.91           0.90          0.90
## Bartlett_minchi               0.89           0.88          0.88
## regression_minrank            1.00           0.99          0.99
## Thurstone_minrank             1.00           0.99          0.99
## tenBerge_minrank              1.00           0.99          0.99
## Bartlett_minrank              1.00           0.99          0.99
##                    tenBerge_ols Bartlett_ols regression_wls Thurstone_wls
## regression_minres          1.00         0.99           0.99          0.99
## Thurstone_minres           1.00         0.99           0.99          0.99
## tenBerge_minres            1.00         0.99           0.99          0.99
## Bartlett_minres            0.99         1.00           1.00          1.00
## regression_ols             1.00         0.99           0.99          0.99
## Thurstone_ols              1.00         0.99           0.99          0.99
## tenBerge_ols               1.00         0.99           0.99          0.99
## Bartlett_ols               0.99         1.00           1.00          1.00
## regression_wls             0.99         1.00           1.00          1.00
## Thurstone_wls              0.99         1.00           1.00          1.00
## tenBerge_wls               0.99         1.00           1.00          1.00
## Bartlett_wls               0.99         1.00           1.00          1.00
## regression_gls             0.99         1.00           1.00          1.00
## Thurstone_gls              0.99         1.00           1.00          1.00
## tenBerge_gls               0.99         1.00           1.00          1.00
## Bartlett_gls               0.99         1.00           1.00          1.00
## regression_pa              1.00         0.99           0.99          0.99
## Thurstone_pa               1.00         0.99           0.99          0.99
## tenBerge_pa                1.00         0.99           0.99          0.99
## Bartlett_pa                0.99         1.00           1.00          1.00
## regression_ml             -0.93        -0.94          -0.93         -0.93
## Thurstone_ml              -0.93        -0.94          -0.93         -0.93
## tenBerge_ml               -0.93        -0.94          -0.93         -0.93
## Bartlett_ml               -0.93        -0.94          -0.93         -0.93
## regression_minchi          0.90         0.91           0.92          0.92
## Thurstone_minchi           0.90         0.91           0.92          0.92
## tenBerge_minchi            0.90         0.91           0.92          0.92
## Bartlett_minchi            0.88         0.89           0.89          0.89
## regression_minrank         0.99         1.00           1.00          1.00
## Thurstone_minrank          0.99         1.00           1.00          1.00
## tenBerge_minrank           0.99         1.00           1.00          1.00
## Bartlett_minrank           0.99         1.00           1.00          1.00
##                    tenBerge_wls Bartlett_wls regression_gls Thurstone_gls
## regression_minres          0.99         0.99           0.99          0.99
## Thurstone_minres           0.99         0.99           0.99          0.99
## tenBerge_minres            0.99         0.99           0.99          0.99
## Bartlett_minres            1.00         1.00           1.00          1.00
## regression_ols             0.99         0.99           0.99          0.99
## Thurstone_ols              0.99         0.99           0.99          0.99
## tenBerge_ols               0.99         0.99           0.99          0.99
## Bartlett_ols               1.00         1.00           1.00          1.00
## regression_wls             1.00         1.00           1.00          1.00
## Thurstone_wls              1.00         1.00           1.00          1.00
## tenBerge_wls               1.00         1.00           1.00          1.00
## Bartlett_wls               1.00         1.00           1.00          1.00
## regression_gls             1.00         1.00           1.00          1.00
## Thurstone_gls              1.00         1.00           1.00          1.00
## tenBerge_gls               1.00         1.00           1.00          1.00
## Bartlett_gls               1.00         1.00           1.00          1.00
## regression_pa              0.99         0.99           0.99          0.99
## Thurstone_pa               0.99         0.99           0.99          0.99
## tenBerge_pa                0.99         0.99           0.99          0.99
## Bartlett_pa                1.00         1.00           1.00          1.00
## regression_ml             -0.93        -0.93          -0.93         -0.93
## Thurstone_ml              -0.93        -0.93          -0.93         -0.93
## tenBerge_ml               -0.93        -0.93          -0.93         -0.93
## Bartlett_ml               -0.93        -0.93          -0.93         -0.93
## regression_minchi          0.92         0.92           0.92          0.92
## Thurstone_minchi           0.92         0.92           0.92          0.92
## tenBerge_minchi            0.92         0.92           0.92          0.92
## Bartlett_minchi            0.89         0.89           0.89          0.89
## regression_minrank         1.00         1.00           1.00          1.00
## Thurstone_minrank          1.00         1.00           1.00          1.00
## tenBerge_minrank           1.00         1.00           1.00          1.00
## Bartlett_minrank           1.00         1.00           1.00          1.00
##                    tenBerge_gls Bartlett_gls regression_pa Thurstone_pa
## regression_minres          0.99         0.99          1.00         1.00
## Thurstone_minres           0.99         0.99          1.00         1.00
## tenBerge_minres            0.99         0.99          1.00         1.00
## Bartlett_minres            1.00         1.00          0.99         0.99
## regression_ols             0.99         0.99          1.00         1.00
## Thurstone_ols              0.99         0.99          1.00         1.00
## tenBerge_ols               0.99         0.99          1.00         1.00
## Bartlett_ols               1.00         1.00          0.99         0.99
## regression_wls             1.00         1.00          0.99         0.99
## Thurstone_wls              1.00         1.00          0.99         0.99
## tenBerge_wls               1.00         1.00          0.99         0.99
## Bartlett_wls               1.00         1.00          0.99         0.99
## regression_gls             1.00         1.00          0.99         0.99
## Thurstone_gls              1.00         1.00          0.99         0.99
## tenBerge_gls               1.00         1.00          0.99         0.99
## Bartlett_gls               1.00         1.00          0.99         0.99
## regression_pa              0.99         0.99          1.00         1.00
## Thurstone_pa               0.99         0.99          1.00         1.00
## tenBerge_pa                0.99         0.99          1.00         1.00
## Bartlett_pa                1.00         1.00          0.99         0.99
## regression_ml             -0.93        -0.93         -0.93        -0.93
## Thurstone_ml              -0.93        -0.93         -0.93        -0.93
## tenBerge_ml               -0.93        -0.93         -0.93        -0.93
## Bartlett_ml               -0.93        -0.93         -0.93        -0.93
## regression_minchi          0.92         0.92          0.90         0.90
## Thurstone_minchi           0.92         0.92          0.90         0.90
## tenBerge_minchi            0.92         0.92          0.90         0.90
## Bartlett_minchi            0.89         0.89          0.88         0.88
## regression_minrank         1.00         1.00          0.99         0.99
## Thurstone_minrank          1.00         1.00          0.99         0.99
## tenBerge_minrank           1.00         1.00          0.99         0.99
## Bartlett_minrank           1.00         1.00          0.99         0.99
##                    tenBerge_pa Bartlett_pa regression_ml Thurstone_ml
## regression_minres         1.00        0.99         -0.93        -0.93
## Thurstone_minres          1.00        0.99         -0.93        -0.93
## tenBerge_minres           1.00        0.99         -0.93        -0.93
## Bartlett_minres           0.99        1.00         -0.94        -0.94
## regression_ols            1.00        0.99         -0.93        -0.93
## Thurstone_ols             1.00        0.99         -0.93        -0.93
## tenBerge_ols              1.00        0.99         -0.93        -0.93
## Bartlett_ols              0.99        1.00         -0.94        -0.94
## regression_wls            0.99        1.00         -0.93        -0.93
## Thurstone_wls             0.99        1.00         -0.93        -0.93
## tenBerge_wls              0.99        1.00         -0.93        -0.93
## Bartlett_wls              0.99        1.00         -0.93        -0.93
## regression_gls            0.99        1.00         -0.93        -0.93
## Thurstone_gls             0.99        1.00         -0.93        -0.93
## tenBerge_gls              0.99        1.00         -0.93        -0.93
## Bartlett_gls              0.99        1.00         -0.93        -0.93
## regression_pa             1.00        0.99         -0.93        -0.93
## Thurstone_pa              1.00        0.99         -0.93        -0.93
## tenBerge_pa               1.00        0.99         -0.93        -0.93
## Bartlett_pa               0.99        1.00         -0.94        -0.94
## regression_ml            -0.93       -0.94          1.00         1.00
## Thurstone_ml             -0.93       -0.94          1.00         1.00
## tenBerge_ml              -0.93       -0.94          1.00         1.00
## Bartlett_ml              -0.93       -0.94          1.00         1.00
## regression_minchi         0.90        0.91         -0.76        -0.76
## Thurstone_minchi          0.90        0.91         -0.76        -0.76
## tenBerge_minchi           0.90        0.91         -0.76        -0.76
## Bartlett_minchi           0.88        0.89         -0.71        -0.71
## regression_minrank        0.99        1.00         -0.92        -0.92
## Thurstone_minrank         0.99        1.00         -0.92        -0.92
## tenBerge_minrank          0.99        1.00         -0.92        -0.92
## Bartlett_minrank          0.99        1.00         -0.93        -0.93
##                    tenBerge_ml Bartlett_ml regression_minchi
## regression_minres        -0.93       -0.93              0.90
## Thurstone_minres         -0.93       -0.93              0.90
## tenBerge_minres          -0.93       -0.93              0.90
## Bartlett_minres          -0.94       -0.94              0.91
## regression_ols           -0.93       -0.93              0.90
## Thurstone_ols            -0.93       -0.93              0.90
## tenBerge_ols             -0.93       -0.93              0.90
## Bartlett_ols             -0.94       -0.94              0.91
## regression_wls           -0.93       -0.93              0.92
## Thurstone_wls            -0.93       -0.93              0.92
## tenBerge_wls             -0.93       -0.93              0.92
## Bartlett_wls             -0.93       -0.93              0.92
## regression_gls           -0.93       -0.93              0.92
## Thurstone_gls            -0.93       -0.93              0.92
## tenBerge_gls             -0.93       -0.93              0.92
## Bartlett_gls             -0.93       -0.93              0.92
## regression_pa            -0.93       -0.93              0.90
## Thurstone_pa             -0.93       -0.93              0.90
## tenBerge_pa              -0.93       -0.93              0.90
## Bartlett_pa              -0.94       -0.94              0.91
## regression_ml             1.00        1.00             -0.76
## Thurstone_ml              1.00        1.00             -0.76
## tenBerge_ml               1.00        1.00             -0.76
## Bartlett_ml               1.00        1.00             -0.76
## regression_minchi        -0.76       -0.76              1.00
## Thurstone_minchi         -0.76       -0.76              1.00
## tenBerge_minchi          -0.76       -0.76              1.00
## Bartlett_minchi          -0.71       -0.71              1.00
## regression_minrank       -0.92       -0.92              0.92
## Thurstone_minrank        -0.92       -0.92              0.92
## tenBerge_minrank         -0.92       -0.92              0.92
## Bartlett_minrank         -0.93       -0.93              0.92
##                    Thurstone_minchi tenBerge_minchi Bartlett_minchi
## regression_minres              0.90            0.90            0.88
## Thurstone_minres               0.90            0.90            0.88
## tenBerge_minres                0.90            0.90            0.88
## Bartlett_minres                0.91            0.91            0.89
## regression_ols                 0.90            0.90            0.88
## Thurstone_ols                  0.90            0.90            0.88
## tenBerge_ols                   0.90            0.90            0.88
## Bartlett_ols                   0.91            0.91            0.89
## regression_wls                 0.92            0.92            0.89
## Thurstone_wls                  0.92            0.92            0.89
## tenBerge_wls                   0.92            0.92            0.89
## Bartlett_wls                   0.92            0.92            0.89
## regression_gls                 0.92            0.92            0.89
## Thurstone_gls                  0.92            0.92            0.89
## tenBerge_gls                   0.92            0.92            0.89
## Bartlett_gls                   0.92            0.92            0.89
## regression_pa                  0.90            0.90            0.88
## Thurstone_pa                   0.90            0.90            0.88
## tenBerge_pa                    0.90            0.90            0.88
## Bartlett_pa                    0.91            0.91            0.89
## regression_ml                 -0.76           -0.76           -0.71
## Thurstone_ml                  -0.76           -0.76           -0.71
## tenBerge_ml                   -0.76           -0.76           -0.71
## Bartlett_ml                   -0.76           -0.76           -0.71
## regression_minchi              1.00            1.00            1.00
## Thurstone_minchi               1.00            1.00            1.00
## tenBerge_minchi                1.00            1.00            1.00
## Bartlett_minchi                1.00            1.00            1.00
## regression_minrank             0.92            0.92            0.89
## Thurstone_minrank              0.92            0.92            0.89
## tenBerge_minrank               0.92            0.92            0.89
## Bartlett_minrank               0.92            0.92            0.89
##                    regression_minrank Thurstone_minrank tenBerge_minrank
## regression_minres                0.99              0.99             0.99
## Thurstone_minres                 0.99              0.99             0.99
## tenBerge_minres                  0.99              0.99             0.99
## Bartlett_minres                  1.00              1.00             1.00
## regression_ols                   0.99              0.99             0.99
## Thurstone_ols                    0.99              0.99             0.99
## tenBerge_ols                     0.99              0.99             0.99
## Bartlett_ols                     1.00              1.00             1.00
## regression_wls                   1.00              1.00             1.00
## Thurstone_wls                    1.00              1.00             1.00
## tenBerge_wls                     1.00              1.00             1.00
## Bartlett_wls                     1.00              1.00             1.00
## regression_gls                   1.00              1.00             1.00
## Thurstone_gls                    1.00              1.00             1.00
## tenBerge_gls                     1.00              1.00             1.00
## Bartlett_gls                     1.00              1.00             1.00
## regression_pa                    0.99              0.99             0.99
## Thurstone_pa                     0.99              0.99             0.99
## tenBerge_pa                      0.99              0.99             0.99
## Bartlett_pa                      1.00              1.00             1.00
## regression_ml                   -0.92             -0.92            -0.92
## Thurstone_ml                    -0.92             -0.92            -0.92
## tenBerge_ml                     -0.92             -0.92            -0.92
## Bartlett_ml                     -0.92             -0.92            -0.92
## regression_minchi                0.92              0.92             0.92
## Thurstone_minchi                 0.92              0.92             0.92
## tenBerge_minchi                  0.92              0.92             0.92
## Bartlett_minchi                  0.89              0.89             0.89
## regression_minrank               1.00              1.00             1.00
## Thurstone_minrank                1.00              1.00             1.00
## tenBerge_minrank                 1.00              1.00             1.00
## Bartlett_minrank                 1.00              1.00             1.00
##                    Bartlett_minrank
## regression_minres              0.99
## Thurstone_minres               0.99
## tenBerge_minres                0.99
## Bartlett_minres                1.00
## regression_ols                 0.99
## Thurstone_ols                  0.99
## tenBerge_ols                   0.99
## Bartlett_ols                   1.00
## regression_wls                 1.00
## Thurstone_wls                  1.00
## tenBerge_wls                   1.00
## Bartlett_wls                   1.00
## regression_gls                 1.00
## Thurstone_gls                  1.00
## tenBerge_gls                   1.00
## Bartlett_gls                   1.00
## regression_pa                  0.99
## Thurstone_pa                   0.99
## tenBerge_pa                    0.99
## Bartlett_pa                    1.00
## regression_ml                 -0.93
## Thurstone_ml                  -0.93
## tenBerge_ml                   -0.93
## Bartlett_ml                   -0.93
## regression_minchi              0.92
## Thurstone_minchi               0.92
## tenBerge_minchi                0.92
## Bartlett_minchi                0.89
## regression_minrank             1.00
## Thurstone_minrank              1.00
## tenBerge_minrank               1.00
## Bartlett_minrank               1.00
fa_mv$scores %>% wtd.cors() %>% MAT_half() %>% GG_denhist()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#save scores + reverse
d$S_fa = S_list$weights$scores %>% as.vector() %>% `*`(-1)
#subdatasets
list_group_factors = list(
  economy = S_data[c("monthly_income", "Poverty_GSO_WB_poverty_headcount_pct")],
  health = S_data[c("Stunting_total", "Infant_mortality_rate", "Under_five_mortality_rate")],
  education = S_data[c("Secondary_school_attendance_Upper_16_18_years_pct", "Secondary_school_attendance_Lower_11_15_years_pct")],
  employment = S_data[c("Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population")]
)

#loop over, factor analyze, extract factor scores, return list of data frames
#then merge to one, then do a final factor analysis
second_order_scores = list_group_factors %>% imap_dfr(function(ds, name) {
  # browser()
  data_frame(
    score = fa(ds) %>% .$scores %>% as.vector(),
    factor = name,
    province = d$ISO
    )
}) %>% spread(factor, score)
## The estimated weights for the factor scores are probably incorrect.  Try a different factor extraction method.
#higher order S
S_ho = fa(second_order_scores[-1])
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate =
## rotate, : A loading greater than abs(1) was detected. Examine the loadings
## carefully.
## The estimated weights for the factor scores are probably incorrect.  Try a different factor extraction method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate =
## rotate, : An ultra-Heywood case was detected. Examine the results carefully
#mean Z
average_z = function(x, reverse = NULL, messages = F) {
  #standardize
  x = x %>% df_standardize(messages = messages)
  
  #reverse if needed
  for (v in reverse) {
    x[[v]] = x[[v]] * -1
  }
  
  rowMeans(x, na.rm = T)
}

#more robust approach
#average Z scores within class
second_order_scores2 = list(
  economy = S_data[c("monthly_income", "Poverty_GSO_WB_poverty_headcount_pct")] %>% average_z(2),
  health = S_data[c("Stunting_total", "Infant_mortality_rate", "Under_five_mortality_rate")] %>% average_z(1:3),
  education = S_data[c("Secondary_school_attendance_Upper_16_18_years_pct", "Secondary_school_attendance_Lower_11_15_years_pct")] %>% average_z(),
  employment = S_data[c("Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population")] %>% average_z(1)
) %>% as_data_frame()

#factor analysis
S_ho2 = fa(second_order_scores2)
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate =
## rotate, : A loading greater than abs(1) was detected. Examine the loadings
## carefully.
## The estimated weights for the factor scores are probably incorrect.  Try a different factor extraction method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate =
## rotate, : An ultra-Heywood case was detected. Examine the results carefully
#robust S
second_order_scores2$S_uw = average_z(second_order_scores2)

#ordinary
second_order_scores2$S_fa = d$S_fa
second_order_scores2$CA = d$CA

#wtd
wtd.cors(second_order_scores2, weight = d$pop_sqrt)
##            economy health education employment S_uw S_fa   CA
## economy       1.00   0.83      0.21       0.69 0.90 0.93 0.37
## health        0.83   1.00      0.30       0.51 0.86 0.89 0.28
## education     0.21   0.30      1.00       0.11 0.53 0.25 0.70
## employment    0.69   0.51      0.11       1.00 0.76 0.70 0.26
## S_uw          0.90   0.86      0.53       0.76 1.00 0.91 0.53
## S_fa          0.93   0.89      0.25       0.70 0.91 1.00 0.38
## CA            0.37   0.28      0.70       0.26 0.53 0.38 1.00
#combine and save to main
d$S = average_z(data.frame(d$S_fa, second_order_scores2$S_uw))

#save domains to main as well
d$health = second_order_scores2$health
d$economy = second_order_scores2$economy
d$education = second_order_scores2$education
d$employment = second_order_scores2$employment

Scatterplots

#CA x S
#weights = pop sqrt
GG_scatter(d, "CA", "S", weights = "pop_sqrt", case_names = "province_name")
## Don't know how to automatically pick scale for object of type NULL. Defaulting to continuous.

GG_save("figs/CA_S.png")
## Don't know how to automatically pick scale for object of type NULL. Defaulting to continuous.
#inverse standard error of CA
GG_scatter(d, "CA", "S", weights = "CA_ise", case_names = "province_name")
## Don't know how to automatically pick scale for object of type NULL. Defaulting to continuous.

#no weights
GG_scatter(d, "CA", "S", case_names = "province_name")
## Don't know how to automatically pick scale for object of type NULL. Defaulting to continuous.

#Jensen's method
#tricky because we need the empirical loadings, not the ones from the factor analysis
#so we calculate these manually
S_loadings_cors = cbind(S_data, S = d$S) %>% wtd.cors(weight = d$pop_sqrt) %>% .[, ncol(S_data) + 1] %>% .[-length(.)]
S_criterion_cors = cbind(S_data, CA = d$CA) %>% wtd.cors(weight = d$pop_sqrt) %>% .[, ncol(S_data) + 1] %>% .[-length(.)]
S_criterion_cors2 = cbind(S_data, Kinh = d$kinh_frac) %>% wtd.cors(weight = d$pop_sqrt) %>% .[, ncol(S_data) + 1] %>% .[-length(.)]

#apply reversing
for (i in seq_along(S_loadings_cors)) {
  #is negative?
  if (S_loadings_cors[i] < 0) {
    S_loadings_cors[i] = S_loadings_cors[i] * -1
    S_criterion_cors[i] = S_criterion_cors[i] * -1
    S_criterion_cors2[i] = S_criterion_cors2[i] * -1
  }
}

#CA Jensen
Jensen_df = data_frame(
  loading = S_loadings_cors,
  CA_cor = S_criterion_cors,
  Kinh_cor = S_criterion_cors2,
  var = names(S_data)
) 

Jensen_df %>% 
  GG_scatter("loading", "CA_cor", case_names = "var") +
  scale_y_continuous("r (cognitive ability x S indicator)")
## Don't know how to automatically pick scale for object of type NULL. Defaulting to continuous.

GG_save("figs/CA_S_Jensen.png")
## Don't know how to automatically pick scale for object of type NULL. Defaulting to continuous.
Jensen_df %>% 
  GG_scatter("loading", "Kinh_cor", case_names = "var") +
  scale_y_continuous("r (Kinh% x S indicator)")
## Don't know how to automatically pick scale for object of type NULL. Defaulting to continuous.

GG_save("figs/Kinh_S_Jensen.png")
## Don't know how to automatically pick scale for object of type NULL. Defaulting to continuous.

Ethnics

#cors
eth_cors_wtd = wtd.cors(d[c("CA", "S", names(list_group_factors), ethnics + "_frac", "lat", "long")], weight = d$pop_sqrt)
eth_cors = wtd.cors(d[c("CA", "S", names(list_group_factors), ethnics + "_frac", "lat", "long")])
combine_upperlower(eth_cors_wtd, eth_cors) %>% write_clipboard()
##         X
## 1        
## 2    0.41
## 3    0.31
## 4    0.21
## 5    0.66
## 6    0.22
## 7    0.38
## 8   -0.27
## 9   -0.03
## 10  -0.11
## 11  -0.42
## 12  -0.15
## 13  -0.23
## 14  -0.07
## 15  -0.13
## 16   0.34
## 17   0.08
## 18   0.47
## 19       
## 20   0.93
## 21   0.88
## 22   0.42
## 23   0.71
## 24   0.81
## 25  -0.47
## 26  -0.45
## 27  -0.18
## 28   0.05
## 29   0.34
## 30  -0.35
## 31  -0.64
## 32  -0.60
## 33  -0.43
## 34   0.35
## 35   0.37
## 36   0.94
## 37       
## 38   0.82
## 39   0.24
## 40   0.64
## 41   0.73
## 42  -0.41
## 43  -0.46
## 44  -0.19
## 45   0.10
## 46   0.47
## 47  -0.28
## 48  -0.61
## 49  -0.56
## 50  -0.55
## 51   0.31
## 52   0.28
## 53   0.90
## 54   0.83
## 55       
## 56   0.33
## 57   0.42
## 58   0.71
## 59  -0.29
## 60  -0.44
## 61  -0.03
## 62   0.13
## 63   0.29
## 64  -0.21
## 65  -0.65
## 66  -0.52
## 67  -0.38
## 68   0.19
## 69   0.70
## 70   0.40
## 71   0.21
## 72   0.30
## 73       
## 74   0.09
## 75   0.40
## 76  -0.10
## 77  -0.24
## 78   0.03
## 79  -0.41
## 80  -0.29
## 81  -0.02
## 82  -0.38
## 83  -0.26
## 84   0.43
## 85   0.32
## 86   0.26
## 87   0.75
## 88   0.69
## 89   0.51
## 90   0.11
## 91       
## 92   0.62
## 93  -0.62
## 94  -0.22
## 95  -0.30
## 96   0.23
## 97   0.37
## 98  -0.47
## 99  -0.36
## 100 -0.54
## 101 -0.50
## 102  0.30
## 103  0.38
## 104  0.74
## 105  0.63
## 106  0.70
## 107  0.38
## 108  0.55
## 109      
## 110 -0.67
## 111 -0.49
## 112 -0.25
## 113 -0.03
## 114  0.04
## 115 -0.54
## 116 -0.70
## 117 -0.73
## 118 -0.45
## 119  0.37
## 120 -0.25
## 121 -0.43
## 122 -0.36
## 123 -0.32
## 124 -0.12
## 125 -0.52
## 126 -0.67
## 127      
## 128 -0.07
## 129 -0.03
## 130 -0.10
## 131 -0.06
## 132  0.74
## 133  0.32
## 134  0.78
## 135  0.41
## 136 -0.15
## 137 -0.06
## 138 -0.41
## 139 -0.40
## 140 -0.40
## 141 -0.19
## 142 -0.25
## 143 -0.50
## 144 -0.05
## 145      
## 146  0.10
## 147 -0.08
## 148 -0.10
## 149 -0.07
## 150  0.61
## 151  0.15
## 152  0.29
## 153 -0.49
## 154 -0.11
## 155 -0.19
## 156 -0.20
## 157 -0.07
## 158  0.02
## 159 -0.31
## 160 -0.28
## 161 -0.02
## 162  0.13
## 163      
## 164 -0.06
## 165 -0.10
## 166 -0.06
## 167 -0.02
## 168  0.00
## 169  0.18
## 170 -0.15
## 171 -0.44
## 172  0.00
## 173  0.04
## 174  0.09
## 175 -0.44
## 176  0.21
## 177 -0.07
## 178 -0.09
## 179 -0.07
## 180 -0.06
## 181      
## 182  0.45
## 183 -0.08
## 184 -0.10
## 185 -0.12
## 186 -0.36
## 187 -0.11
## 188 -0.04
## 189  0.48
## 190  0.61
## 191  0.40
## 192 -0.22
## 193  0.46
## 194  0.05
## 195 -0.07
## 196 -0.11
## 197 -0.10
## 198  0.35
## 199      
## 200 -0.06
## 201 -0.06
## 202 -0.08
## 203 -0.43
## 204 -0.01
## 205 -0.21
## 206 -0.32
## 207 -0.25
## 208 -0.23
## 209 -0.03
## 210 -0.41
## 211 -0.54
## 212  0.76
## 213 -0.06
## 214 -0.05
## 215 -0.07
## 216 -0.06
## 217      
## 218  0.17
## 219  0.38
## 220  0.30
## 221 -0.01
## 222 -0.10
## 223 -0.57
## 224 -0.51
## 225 -0.61
## 226 -0.33
## 227 -0.34
## 228 -0.70
## 229  0.36
## 230  0.57
## 231  0.00
## 232 -0.09
## 233 -0.07
## 234  0.18
## 235      
## 236  0.64
## 237  0.42
## 238 -0.55
## 239 -0.13
## 240 -0.53
## 241 -0.47
## 242 -0.51
## 243 -0.23
## 244 -0.46
## 245 -0.71
## 246  0.79
## 247  0.14
## 248  0.01
## 249 -0.10
## 250 -0.08
## 251  0.38
## 252  0.66
## 253      
## 254  0.50
## 255 -0.38
## 256  0.37
## 257 -0.36
## 258 -0.49
## 259 -0.34
## 260  0.51
## 261 -0.44
## 262 -0.37
## 263  0.36
## 264  0.27
## 265  0.20
## 266 -0.35
## 267 -0.45
## 268  0.26
## 269  0.36
## 270  0.44
## 271      
## 272 -0.37
## 273  0.10
## 274  0.27
## 275  0.23
## 276  0.13
## 277  0.27
## 278  0.25
## 279  0.31
## 280 -0.13
## 281 -0.45
## 282 -0.17
## 283 -0.13
## 284  0.03
## 285  0.01
## 286 -0.47
## 287 -0.33
## 288 -0.32
## 289
#CA and each indicator
eth_cors2_wtd = wtd.cors(d[c("CA", names(S_data), "Total_fertility_rate", "Population_growth_rate", "lat", "long")], weight = d$pop_sqrt)
eth_cors2 = wtd.cors(d[c("CA", names(S_data), "Total_fertility_rate", "Population_growth_rate", "lat", "long")])
combine_upperlower(eth_cors2_wtd, eth_cors2)
##                                                                                  CA
## CA                                                                               NA
## Poverty_GSO_WB_poverty_headcount_pct                                         -0.369
## Main_employment_agriculture_pct                                              -0.234
## Main_employment_wage_work_pct                                                 0.240
## Main_light_source_electricity_pct                                             0.338
## Sanitation_indoor_flush_toilet_pct                                            0.301
## Water_indoor_tap_public_tap_or_well_pct                                       0.141
## Secondary_school_attendance_Lower_11_15_years_pct                             0.627
## Secondary_school_attendance_Upper_16_18_years_pct                             0.661
## Stunting_total                                                               -0.271
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                0.331
## Index_of_Industrial_production                                                0.173
## Infant_mortality_rate                                                        -0.158
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population -0.235
## Under_five_mortality_rate                                                    -0.155
## monthly_income                                                                0.166
## Total_fertility_rate                                                          0.144
## Population_growth_rate                                                        0.265
## lat                                                                           0.343
## long                                                                          0.067
##                                                                              Poverty_GSO_WB_poverty_headcount_pct
## CA                                                                                                         -0.418
## Poverty_GSO_WB_poverty_headcount_pct                                                                           NA
## Main_employment_agriculture_pct                                                                             0.750
## Main_employment_wage_work_pct                                                                              -0.687
## Main_light_source_electricity_pct                                                                          -0.820
## Sanitation_indoor_flush_toilet_pct                                                                         -0.572
## Water_indoor_tap_public_tap_or_well_pct                                                                    -0.669
## Secondary_school_attendance_Lower_11_15_years_pct                                                          -0.473
## Secondary_school_attendance_Upper_16_18_years_pct                                                          -0.440
## Stunting_total                                                                                              0.761
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                             -0.469
## Index_of_Industrial_production                                                                              0.053
## Infant_mortality_rate                                                                                       0.850
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                0.555
## Under_five_mortality_rate                                                                                   0.848
## monthly_income                                                                                             -0.500
## Total_fertility_rate                                                                                        0.579
## Population_growth_rate                                                                                      0.193
## lat                                                                                                         0.437
## long                                                                                                       -0.422
##                                                                              Main_employment_agriculture_pct
## CA                                                                                                    -0.276
## Poverty_GSO_WB_poverty_headcount_pct                                                                   0.744
## Main_employment_agriculture_pct                                                                           NA
## Main_employment_wage_work_pct                                                                         -0.979
## Main_light_source_electricity_pct                                                                     -0.522
## Sanitation_indoor_flush_toilet_pct                                                                    -0.871
## Water_indoor_tap_public_tap_or_well_pct                                                               -0.781
## Secondary_school_attendance_Lower_11_15_years_pct                                                     -0.063
## Secondary_school_attendance_Upper_16_18_years_pct                                                     -0.119
## Stunting_total                                                                                         0.745
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                        -0.535
## Index_of_Industrial_production                                                                         0.072
## Infant_mortality_rate                                                                                  0.639
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                           0.706
## Under_five_mortality_rate                                                                              0.631
## monthly_income                                                                                        -0.707
## Total_fertility_rate                                                                                   0.650
## Population_growth_rate                                                                                -0.112
## lat                                                                                                    0.538
## long                                                                                                  -0.365
##                                                                              Main_employment_wage_work_pct
## CA                                                                                                   0.285
## Poverty_GSO_WB_poverty_headcount_pct                                                                -0.689
## Main_employment_agriculture_pct                                                                     -0.981
## Main_employment_wage_work_pct                                                                           NA
## Main_light_source_electricity_pct                                                                    0.464
## Sanitation_indoor_flush_toilet_pct                                                                   0.857
## Water_indoor_tap_public_tap_or_well_pct                                                              0.738
## Secondary_school_attendance_Lower_11_15_years_pct                                                    0.038
## Secondary_school_attendance_Upper_16_18_years_pct                                                    0.075
## Stunting_total                                                                                      -0.720
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                       0.589
## Index_of_Industrial_production                                                                      -0.045
## Infant_mortality_rate                                                                               -0.591
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                        -0.630
## Under_five_mortality_rate                                                                           -0.582
## monthly_income                                                                                       0.702
## Total_fertility_rate                                                                                -0.628
## Population_growth_rate                                                                               0.202
## lat                                                                                                 -0.482
## long                                                                                                 0.327
##                                                                              Main_light_source_electricity_pct
## CA                                                                                                       0.417
## Poverty_GSO_WB_poverty_headcount_pct                                                                    -0.808
## Main_employment_agriculture_pct                                                                         -0.476
## Main_employment_wage_work_pct                                                                            0.430
## Main_light_source_electricity_pct                                                                           NA
## Sanitation_indoor_flush_toilet_pct                                                                       0.394
## Water_indoor_tap_public_tap_or_well_pct                                                                  0.444
## Secondary_school_attendance_Lower_11_15_years_pct                                                        0.601
## Secondary_school_attendance_Upper_16_18_years_pct                                                        0.509
## Stunting_total                                                                                          -0.578
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                           0.358
## Index_of_Industrial_production                                                                          -0.082
## Infant_mortality_rate                                                                                   -0.690
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                            -0.361
## Under_five_mortality_rate                                                                               -0.692
## monthly_income                                                                                           0.215
## Total_fertility_rate                                                                                    -0.351
## Population_growth_rate                                                                                  -0.281
## lat                                                                                                     -0.185
## long                                                                                                     0.459
##                                                                              Sanitation_indoor_flush_toilet_pct
## CA                                                                                                        0.342
## Poverty_GSO_WB_poverty_headcount_pct                                                                     -0.582
## Main_employment_agriculture_pct                                                                          -0.893
## Main_employment_wage_work_pct                                                                             0.880
## Main_light_source_electricity_pct                                                                         0.365
## Sanitation_indoor_flush_toilet_pct                                                                           NA
## Water_indoor_tap_public_tap_or_well_pct                                                                   0.639
## Secondary_school_attendance_Lower_11_15_years_pct                                                         0.109
## Secondary_school_attendance_Upper_16_18_years_pct                                                         0.176
## Stunting_total                                                                                           -0.653
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                            0.367
## Index_of_Industrial_production                                                                           -0.087
## Infant_mortality_rate                                                                                    -0.445
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                             -0.641
## Under_five_mortality_rate                                                                                -0.437
## monthly_income                                                                                            0.730
## Total_fertility_rate                                                                                     -0.519
## Population_growth_rate                                                                                    0.282
## lat                                                                                                      -0.463
## long                                                                                                      0.466
##                                                                              Water_indoor_tap_public_tap_or_well_pct
## CA                                                                                                            0.1817
## Poverty_GSO_WB_poverty_headcount_pct                                                                         -0.6734
## Main_employment_agriculture_pct                                                                              -0.7928
## Main_employment_wage_work_pct                                                                                 0.7563
## Main_light_source_electricity_pct                                                                             0.4223
## Sanitation_indoor_flush_toilet_pct                                                                            0.6718
## Water_indoor_tap_public_tap_or_well_pct                                                                           NA
## Secondary_school_attendance_Lower_11_15_years_pct                                                            -0.0341
## Secondary_school_attendance_Upper_16_18_years_pct                                                             0.0197
## Stunting_total                                                                                               -0.6813
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                0.4058
## Index_of_Industrial_production                                                                               -0.0217
## Infant_mortality_rate                                                                                        -0.6442
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                 -0.6468
## Under_five_mortality_rate                                                                                    -0.6382
## monthly_income                                                                                                0.5698
## Total_fertility_rate                                                                                         -0.6327
## Population_growth_rate                                                                                        0.0099
## lat                                                                                                          -0.5123
## long                                                                                                          0.1957
##                                                                              Secondary_school_attendance_Lower_11_15_years_pct
## CA                                                                                                                       0.675
## Poverty_GSO_WB_poverty_headcount_pct                                                                                    -0.441
## Main_employment_agriculture_pct                                                                                         -0.033
## Main_employment_wage_work_pct                                                                                            0.018
## Main_light_source_electricity_pct                                                                                        0.602
## Sanitation_indoor_flush_toilet_pct                                                                                       0.087
## Water_indoor_tap_public_tap_or_well_pct                                                                                 -0.043
## Secondary_school_attendance_Lower_11_15_years_pct                                                                           NA
## Secondary_school_attendance_Upper_16_18_years_pct                                                                        0.933
## Stunting_total                                                                                                          -0.337
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                           0.276
## Index_of_Industrial_production                                                                                           0.028
## Infant_mortality_rate                                                                                                   -0.317
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                            -0.017
## Under_five_mortality_rate                                                                                               -0.320
## monthly_income                                                                                                          -0.063
## Total_fertility_rate                                                                                                     0.187
## Population_growth_rate                                                                                                  -0.092
## lat                                                                                                                      0.428
## long                                                                                                                     0.280
##                                                                              Secondary_school_attendance_Upper_16_18_years_pct
## CA                                                                                                                       0.705
## Poverty_GSO_WB_poverty_headcount_pct                                                                                    -0.422
## Main_employment_agriculture_pct                                                                                         -0.092
## Main_employment_wage_work_pct                                                                                            0.059
## Main_light_source_electricity_pct                                                                                        0.523
## Sanitation_indoor_flush_toilet_pct                                                                                       0.150
## Water_indoor_tap_public_tap_or_well_pct                                                                                  0.013
## Secondary_school_attendance_Lower_11_15_years_pct                                                                        0.942
## Secondary_school_attendance_Upper_16_18_years_pct                                                                           NA
## Stunting_total                                                                                                          -0.349
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                           0.208
## Index_of_Industrial_production                                                                                           0.041
## Infant_mortality_rate                                                                                                   -0.252
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                            -0.195
## Under_five_mortality_rate                                                                                               -0.255
## monthly_income                                                                                                          -0.038
## Total_fertility_rate                                                                                                     0.249
## Population_growth_rate                                                                                                  -0.110
## lat                                                                                                                      0.415
## long                                                                                                                     0.316
##                                                                              Stunting_total
## CA                                                                                  -0.3603
## Poverty_GSO_WB_poverty_headcount_pct                                                 0.7422
## Main_employment_agriculture_pct                                                      0.7828
## Main_employment_wage_work_pct                                                       -0.7683
## Main_light_source_electricity_pct                                                   -0.5380
## Sanitation_indoor_flush_toilet_pct                                                  -0.7244
## Water_indoor_tap_public_tap_or_well_pct                                             -0.7012
## Secondary_school_attendance_Lower_11_15_years_pct                                   -0.3062
## Secondary_school_attendance_Upper_16_18_years_pct                                   -0.3353
## Stunting_total                                                                           NA
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                      -0.4082
## Index_of_Industrial_production                                                       0.0062
## Infant_mortality_rate                                                                0.7826
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population         0.5037
## Under_five_mortality_rate                                                            0.7767
## monthly_income                                                                      -0.6665
## Total_fertility_rate                                                                 0.6112
## Population_growth_rate                                                               0.1225
## lat                                                                                  0.2751
## long                                                                                -0.1607
##                                                                              Foreign_direct_investment_projects_licensed_in_2016_per_capita
## CA                                                                                                                                    0.344
## Poverty_GSO_WB_poverty_headcount_pct                                                                                                 -0.479
## Main_employment_agriculture_pct                                                                                                      -0.538
## Main_employment_wage_work_pct                                                                                                         0.585
## Main_light_source_electricity_pct                                                                                                     0.359
## Sanitation_indoor_flush_toilet_pct                                                                                                    0.397
## Water_indoor_tap_public_tap_or_well_pct                                                                                               0.424
## Secondary_school_attendance_Lower_11_15_years_pct                                                                                     0.272
## Secondary_school_attendance_Upper_16_18_years_pct                                                                                     0.205
## Stunting_total                                                                                                                       -0.403
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                                           NA
## Index_of_Industrial_production                                                                                                        0.027
## Infant_mortality_rate                                                                                                                -0.409
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                                         -0.197
## Under_five_mortality_rate                                                                                                            -0.404
## monthly_income                                                                                                                        0.334
## Total_fertility_rate                                                                                                                 -0.299
## Population_growth_rate                                                                                                                0.331
## lat                                                                                                                                  -0.023
## long                                                                                                                                  0.140
##                                                                              Index_of_Industrial_production
## CA                                                                                                   0.1310
## Poverty_GSO_WB_poverty_headcount_pct                                                                 0.0241
## Main_employment_agriculture_pct                                                                      0.0834
## Main_employment_wage_work_pct                                                                       -0.0591
## Main_light_source_electricity_pct                                                                   -0.0185
## Sanitation_indoor_flush_toilet_pct                                                                  -0.0943
## Water_indoor_tap_public_tap_or_well_pct                                                             -0.0249
## Secondary_school_attendance_Lower_11_15_years_pct                                                    0.0762
## Secondary_school_attendance_Upper_16_18_years_pct                                                    0.0711
## Stunting_total                                                                                       0.0063
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                       0.0310
## Index_of_Industrial_production                                                                           NA
## Infant_mortality_rate                                                                                0.1076
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                        -0.0637
## Under_five_mortality_rate                                                                            0.1137
## monthly_income                                                                                      -0.1183
## Total_fertility_rate                                                                                 0.1518
## Population_growth_rate                                                                               0.1754
## lat                                                                                                  0.1599
## long                                                                                                -0.1479
##                                                                              Infant_mortality_rate
## CA                                                                                          -0.204
## Poverty_GSO_WB_poverty_headcount_pct                                                         0.852
## Main_employment_agriculture_pct                                                              0.644
## Main_employment_wage_work_pct                                                               -0.600
## Main_light_source_electricity_pct                                                           -0.672
## Sanitation_indoor_flush_toilet_pct                                                          -0.467
## Water_indoor_tap_public_tap_or_well_pct                                                     -0.653
## Secondary_school_attendance_Lower_11_15_years_pct                                           -0.278
## Secondary_school_attendance_Upper_16_18_years_pct                                           -0.225
## Stunting_total                                                                               0.734
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                              -0.418
## Index_of_Industrial_production                                                               0.061
## Infant_mortality_rate                                                                           NA
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                 0.348
## Under_five_mortality_rate                                                                    1.000
## monthly_income                                                                              -0.473
## Total_fertility_rate                                                                         0.723
## Population_growth_rate                                                                       0.308
## lat                                                                                          0.403
## long                                                                                        -0.208
##                                                                              Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population
## CA                                                                                                                                                -0.2717
## Poverty_GSO_WB_poverty_headcount_pct                                                                                                               0.5731
## Main_employment_agriculture_pct                                                                                                                    0.7296
## Main_employment_wage_work_pct                                                                                                                     -0.6597
## Main_light_source_electricity_pct                                                                                                                 -0.3433
## Sanitation_indoor_flush_toilet_pct                                                                                                                -0.6834
## Water_indoor_tap_public_tap_or_well_pct                                                                                                           -0.6715
## Secondary_school_attendance_Lower_11_15_years_pct                                                                                                 -0.0355
## Secondary_school_attendance_Upper_16_18_years_pct                                                                                                 -0.2156
## Stunting_total                                                                                                                                     0.5976
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                                                                    -0.2139
## Index_of_Industrial_production                                                                                                                    -0.0078
## Infant_mortality_rate                                                                                                                              0.3961
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                                                                           NA
## Under_five_mortality_rate                                                                                                                          0.3401
## monthly_income                                                                                                                                    -0.5389
## Total_fertility_rate                                                                                                                               0.3566
## Population_growth_rate                                                                                                                             0.0396
## lat                                                                                                                                                0.4967
## long                                                                                                                                              -0.3348
##                                                                              Under_five_mortality_rate
## CA                                                                                              -0.199
## Poverty_GSO_WB_poverty_headcount_pct                                                             0.850
## Main_employment_agriculture_pct                                                                  0.633
## Main_employment_wage_work_pct                                                                   -0.588
## Main_light_source_electricity_pct                                                               -0.675
## Sanitation_indoor_flush_toilet_pct                                                              -0.455
## Water_indoor_tap_public_tap_or_well_pct                                                         -0.645
## Secondary_school_attendance_Lower_11_15_years_pct                                               -0.282
## Secondary_school_attendance_Upper_16_18_years_pct                                               -0.227
## Stunting_total                                                                                   0.724
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                                  -0.412
## Index_of_Industrial_production                                                                   0.065
## Infant_mortality_rate                                                                            1.000
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                     0.386
## Under_five_mortality_rate                                                                           NA
## monthly_income                                                                                  -0.462
## Total_fertility_rate                                                                             0.718
## Population_growth_rate                                                                           0.316
## lat                                                                                              0.399
## long                                                                                            -0.211
##                                                                              monthly_income
## CA                                                                                   0.2540
## Poverty_GSO_WB_poverty_headcount_pct                                                -0.5066
## Main_employment_agriculture_pct                                                     -0.7621
## Main_employment_wage_work_pct                                                        0.7581
## Main_light_source_electricity_pct                                                    0.2143
## Sanitation_indoor_flush_toilet_pct                                                   0.8007
## Water_indoor_tap_public_tap_or_well_pct                                              0.6073
## Secondary_school_attendance_Lower_11_15_years_pct                                   -0.0334
## Secondary_school_attendance_Upper_16_18_years_pct                                    0.0059
## Stunting_total                                                                      -0.7604
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                       0.3338
## Index_of_Industrial_production                                                      -0.1160
## Infant_mortality_rate                                                               -0.4741
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population        -0.6202
## Under_five_mortality_rate                                                           -0.4594
## monthly_income                                                                           NA
## Total_fertility_rate                                                                -0.6407
## Population_growth_rate                                                               0.2622
## lat                                                                                 -0.5224
## long                                                                                 0.1517
##                                                                              Total_fertility_rate
## CA                                                                                          0.093
## Poverty_GSO_WB_poverty_headcount_pct                                                        0.567
## Main_employment_agriculture_pct                                                             0.694
## Main_employment_wage_work_pct                                                              -0.676
## Main_light_source_electricity_pct                                                          -0.298
## Sanitation_indoor_flush_toilet_pct                                                         -0.585
## Water_indoor_tap_public_tap_or_well_pct                                                    -0.653
## Secondary_school_attendance_Lower_11_15_years_pct                                           0.240
## Secondary_school_attendance_Upper_16_18_years_pct                                           0.274
## Stunting_total                                                                              0.623
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                             -0.306
## Index_of_Industrial_production                                                              0.130
## Infant_mortality_rate                                                                       0.698
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                0.437
## Under_five_mortality_rate                                                                   0.691
## monthly_income                                                                             -0.662
## Total_fertility_rate                                                                           NA
## Population_growth_rate                                                                      0.162
## lat                                                                                         0.697
## long                                                                                       -0.244
##                                                                              Population_growth_rate
## CA                                                                                           0.3232
## Poverty_GSO_WB_poverty_headcount_pct                                                         0.0536
## Main_employment_agriculture_pct                                                             -0.2811
## Main_employment_wage_work_pct                                                                0.3593
## Main_light_source_electricity_pct                                                           -0.1686
## Sanitation_indoor_flush_toilet_pct                                                           0.4226
## Water_indoor_tap_public_tap_or_well_pct                                                      0.1732
## Secondary_school_attendance_Lower_11_15_years_pct                                           -0.0198
## Secondary_school_attendance_Upper_16_18_years_pct                                           -0.0393
## Stunting_total                                                                              -0.0960
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                               0.4396
## Index_of_Industrial_production                                                               0.1340
## Infant_mortality_rate                                                                        0.1512
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population                -0.0772
## Under_five_mortality_rate                                                                    0.1619
## monthly_income                                                                               0.3980
## Total_fertility_rate                                                                         0.0009
## Population_growth_rate                                                                           NA
## lat                                                                                          0.0964
## long                                                                                         0.0142
##                                                                                  lat
## CA                                                                            0.3761
## Poverty_GSO_WB_poverty_headcount_pct                                          0.3697
## Main_employment_agriculture_pct                                               0.5054
## Main_employment_wage_work_pct                                                -0.4538
## Main_light_source_electricity_pct                                            -0.0905
## Sanitation_indoor_flush_toilet_pct                                           -0.4461
## Water_indoor_tap_public_tap_or_well_pct                                      -0.4696
## Secondary_school_attendance_Lower_11_15_years_pct                             0.5128
## Secondary_school_attendance_Upper_16_18_years_pct                             0.4985
## Stunting_total                                                                0.2370
## Foreign_direct_investment_projects_licensed_in_2016_per_capita               -0.0039
## Index_of_Industrial_production                                                0.1640
## Infant_mortality_rate                                                         0.3747
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population  0.4365
## Under_five_mortality_rate                                                     0.3694
## monthly_income                                                               -0.4663
## Total_fertility_rate                                                          0.7002
## Population_growth_rate                                                        0.0447
## lat                                                                               NA
## long                                                                         -0.3845
##                                                                                long
## CA                                                                            0.100
## Poverty_GSO_WB_poverty_headcount_pct                                         -0.366
## Main_employment_agriculture_pct                                              -0.324
## Main_employment_wage_work_pct                                                 0.295
## Main_light_source_electricity_pct                                             0.399
## Sanitation_indoor_flush_toilet_pct                                            0.433
## Water_indoor_tap_public_tap_or_well_pct                                       0.163
## Secondary_school_attendance_Lower_11_15_years_pct                             0.241
## Secondary_school_attendance_Upper_16_18_years_pct                             0.276
## Stunting_total                                                               -0.138
## Foreign_direct_investment_projects_licensed_in_2016_per_capita                0.148
## Index_of_Industrial_production                                               -0.108
## Infant_mortality_rate                                                        -0.146
## Percentage_of_employed_workers_at_15_years_of_age_and_above_among_population -0.313
## Under_five_mortality_rate                                                    -0.148
## monthly_income                                                                0.144
## Total_fertility_rate                                                         -0.223
## Population_growth_rate                                                        0.069
## lat                                                                          -0.343
## long                                                                             NA
#plot all ethnicities
for (ethnic in ethnics) {
  #CA
  ca_plot = GG_scatter(d, ethnic + "_frac", "CA", weights = "pop_sqrt") +
    scale_x_continuous(str_to_upper_initial(ethnic) + " %", labels = scales::percent)
  
  #S
  s_plot = GG_scatter(d, ethnic + "_frac", "S", weights = "pop_sqrt") +
    scale_x_continuous(str_to_upper_initial(ethnic) + " %", labels = scales::percent)
  
  #save
  GG_save(filename = sprintf("figs/CA~%s.png", str_to_upper_initial(ethnic)), plot = ca_plot)
  GG_save(filename = sprintf("figs/S~%s.png", str_to_upper_initial(ethnic)), plot = s_plot)
  
  print(ca_plot)
  print(s_plot)
}

Maps

#join data
geo_df2 = left_join(geo_df, d %>% select(ISO, CA, S, ethnics + "_frac"), by = "ISO")

#maps
ggplot(geo_df2, aes(long, lat)) +  
  geom_polygon(aes(fill = Density, group = group)) +
  # ggrepel::geom_text_repel(data = province_meta, aes(label = name), color = "green", size = 2.5, ) +
  geom_path(aes(group = group), size = .1, color = "white") +
  scale_fill_continuous("Population density\n(persons/km sq.)") +
  xlab("Longitude") + ylab("Latitude") +
  theme_bw()

GG_save("maps/map_density.png")

ggplot(geo_df2) + 
  aes(long, lat, group = group, fill = CA) + 
  geom_polygon() +
  geom_path(aes(group = group), size = .1, color = "white") +
  scale_fill_continuous("Cognitive ability") +
  xlab("Longitude") + ylab("Latitude") +
  theme_bw()

GG_save("maps/map_ca.png")

ggplot(geo_df2) + 
  aes(long, lat, group = group, fill = S) + 
  geom_path(aes(group = group), size = .1, color = "white") +
  geom_polygon() +
  xlab("Longitude") + ylab("Latitude") +
  theme_bw()

GG_save("maps/map_s.png")

#Kinh
ggplot(geo_df2) + 
  aes(long, lat, group = group, fill = kinh_frac) + 
  geom_path(aes(group = group), size = .1, color = "white") +
  geom_polygon() +
  theme_bw() +
  xlab("Longitude") + ylab("Latitude") +
  scale_fill_continuous("Kinh %\n(main ethnic group)", labels = scales::percent, high = "blue", low = "green")

GG_save("maps/map_kinh.png")

#Hoa
ggplot(geo_df2) + 
  aes(long, lat, group = group, fill = hoa_frac) + 
  geom_path(aes(group = group), size = .1, color = "white") +
  geom_polygon() +
  theme_bw() +
  xlab("Longitude") + ylab("Latitude") +
  scale_fill_continuous("Hoa % (Chinese)", labels = scales::percent, high = "blue", low = "green")

GG_save("maps/map_hoa.png")

#remaining ethnicities
for (ethnic in setdiff(ethnics, c("kinh", "hoa"))) {
  ggplot(geo_df2) + 
  aes_string("long", "lat", group = "group", fill = ethnic + "_frac") + 
  geom_path(aes(group = group), size = .1, color = "white") +
  geom_polygon() +
  theme_bw() +
  xlab("Longitude") + ylab("Latitude") +
  scale_fill_continuous(ethnic + " %", labels = scales::percent, high = "blue", low = "green")
GG_save(sprintf("maps/map_%s.png", ethnic))
}

Multivariate

#path model
path1 = "
S ~ CA + kinh_frac
CA ~ kinh_frac"

#fit
path1_fit = lavaan::sem(path1, data = d, std.ov = T)
# fitmeasures(path1_fit) #nothing to see
parameterestimates(path1_fit, standardized = T)
#weights
path1_fit_wt = lavaan.survey(path1_fit,
              survey.design = survey::svydesign(id=~Unique_Identifier,
                                                weights = d$pop_sqrt,
                                                data = d))
## Warning in lavData(data = data, group = group, cluster = cluster, ov.names
## = ov.names, : lavaan WARNING: std.ov argument is ignored if only sample
## statistics are provided.
parameterestimates(path1_fit_wt, standardized = T)
#standard MR
ols(S ~ kinh_frac + CA, data = d, weights = d$pop_sqrt)
## Frequencies of Missing Values Due to Each Variable
##         S kinh_frac        CA (weights) 
##         0         0         3         0 
## 
## Linear Regression Model
##  
##  ols(formula = S ~ kinh_frac + CA, data = d, weights = d$pop_sqrt)
##  
##  
##                  Model Likelihood     Discrimination    
##                     Ratio Test           Indexes        
##  Obs       60    LR chi2     50.73    R2       0.571    
##  sigma22.5228    d.f.            2    R2 adj   0.556    
##  d.f.      57    Pr(> chi2) 0.0000    g        0.896    
##  
##  Residuals
##  
##      Min      1Q  Median      3Q     Max 
##  -1.0568 -0.4351 -0.1753  0.1971  2.1466 
##  
##  
##            Coef    S.E.   t     Pr(>|t|)
##  Intercept -5.1525 1.2658 -4.07 0.0001  
##  kinh_frac  2.6247 0.3836  6.84 <0.0001 
##  CA         0.0064 0.0027  2.38 0.0207  
## 
lm(S ~ kinh_frac + CA, data = d, weights = d$pop_sqrt) %>% MOD_summary()
## Warning in sqrt(.): NaNs produced
## 
##     ---- Model summary ----    
## Model coefficients
##           Beta    SE CI_lower CI_upper
## kinh_frac 0.76 0.112    0.541     0.99
## CA        0.23 0.097    0.037     0.42
## 
## 
## Model meta-data
##   outcome  N df   R2 R2-adj. R2-cv
## 1       S 60 57 0.57    0.56    NA
## 
## 
## Etas from analysis of variance
##           Eta Eta_partial
## kinh_frac NaN           1
## CA        NaN           1

Data output

write_rds(d, "data/data_out.rds")
write_csv(d, "data/data_out.csv", na = "")