library(psych)
library(lavaan)
## This is lavaan 0.6-20
## lavaan is FREE software! Please report any bugs.
## 
## Attaching package: 'lavaan'
## The following object is masked from 'package:psych':
## 
##     cor2cov
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.2
## ✔ ggplot2   4.0.0     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.1.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ ggplot2::%+%()   masks psych::%+%()
## ✖ ggplot2::alpha() masks psych::alpha()
## ✖ dplyr::filter()  masks stats::filter()
## ✖ dplyr::lag()     masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(stats)
cor_check <- read.csv("clean_for_final_no_hhid.csv", header = TRUE)
cor_matrix <-cor(cor_check)
cor_matrix
##                   urb_rural       radio  television       fridge    car_truck
## urb_rural        1.00000000 -0.21434592 -0.23222495 -0.036749985 -0.123027653
## radio           -0.21434592  1.00000000  0.24108663  0.128109749  0.120004115
## television      -0.23222495  0.24108663  1.00000000  0.424289401  0.367539642
## fridge          -0.03674998  0.12810975  0.42428940  1.000000000  0.307808948
## car_truck       -0.12302765  0.12000412  0.36753964  0.307808948  1.000000000
## share_toilet     0.16308352 -0.19293831 -0.30968035 -0.210922766 -0.195109710
## cell_phone      -0.40928981  0.40354016  0.39732916  0.220108225  0.209737402
## watch           -0.25915545  0.41641450  0.27966149  0.147900980  0.164834295
## wi_categorical  -0.52271666  0.49684732  0.41057222  0.215980990  0.213062920
## wi_continuous   -0.50329776  0.47570274  0.65264662  0.406380675  0.410612227
## sewing_machine  -0.09647882  0.12226642  0.18329996  0.114130591  0.174197719
## computer        -0.09507447  0.08835868  0.33727272  0.281515481  0.368187000
## educ_years      -0.21886101  0.19222930  0.23236640  0.104783385  0.123911101
## h2o_source      -0.15775544  0.09102962  0.04889348 -0.018854571 -0.007984212
## h2o_non_potable -0.16606088  0.09720467  0.06696648 -0.002606159  0.014976546
## toilet_type     -0.16066200  0.14802831  0.26493311  0.169070982  0.142761709
##                 share_toilet cell_phone       watch wi_categorical
## urb_rural         0.16308352 -0.4092898 -0.25915545     -0.5227167
## radio            -0.19293831  0.4035402  0.41641450      0.4968473
## television       -0.30968035  0.3973292  0.27966149      0.4105722
## fridge           -0.21092277  0.2201082  0.14790098      0.2159810
## car_truck        -0.19510971  0.2097374  0.16483429      0.2130629
## share_toilet      1.00000000 -0.3020402 -0.20172126     -0.3342539
## cell_phone       -0.30204024  1.0000000  0.43521427      0.6743321
## watch            -0.20172126  0.4352143  1.00000000      0.5283469
## wi_categorical   -0.33425387  0.6743321  0.52834691      1.0000000
## wi_continuous    -0.43538583  0.6991135  0.52958440      0.9003056
## sewing_machine   -0.09501931  0.1497773  0.12629283      0.1731788
## computer         -0.15233372  0.1476639  0.11779512      0.1464573
## educ_years       -0.16548074  0.2792422  0.20415779      0.3064320
## h2o_source       -0.02025666  0.1194927  0.09417639      0.1972271
## h2o_non_potable  -0.03098177  0.1286117  0.09898773      0.2074885
## toilet_type      -0.32079815  0.2927904  0.19534578      0.3532453
##                 wi_continuous sewing_machine     computer  educ_years
## urb_rural          -0.5032978    -0.09647882 -0.095074468 -0.21886101
## radio               0.4757027     0.12226642  0.088358676  0.19222930
## television          0.6526466     0.18329996  0.337272719  0.23236640
## fridge              0.4063807     0.11413059  0.281515481  0.10478338
## car_truck           0.4106122     0.17419772  0.368187000  0.12391110
## share_toilet       -0.4353858    -0.09501931 -0.152333722 -0.16548074
## cell_phone          0.6991135     0.14977732  0.147663895  0.27924220
## watch               0.5295844     0.12629283  0.117795120  0.20415779
## wi_categorical      0.9003056     0.17317879  0.146457278  0.30643201
## wi_continuous       1.0000000     0.23218620  0.335184787  0.34367114
## sewing_machine      0.2321862     1.00000000  0.100987937  0.08124354
## computer            0.3351848     0.10098794  1.000000000  0.11648895
## educ_years          0.3436711     0.08124354  0.116488952  1.00000000
## h2o_source          0.1581769     0.02050645  0.010410039  0.04411881
## h2o_non_potable     0.1768214     0.01982354  0.009605861  0.05011904
## toilet_type         0.4190494     0.06445736  0.143776271  0.15047469
##                   h2o_source h2o_non_potable toilet_type
## urb_rural       -0.157755440    -0.166060883 -0.16066200
## radio            0.091029618     0.097204672  0.14802831
## television       0.048893479     0.066966484  0.26493311
## fridge          -0.018854571    -0.002606159  0.16907098
## car_truck       -0.007984212     0.014976546  0.14276171
## share_toilet    -0.020256660    -0.030981771 -0.32079815
## cell_phone       0.119492738     0.128611651  0.29279041
## watch            0.094176391     0.098987732  0.19534578
## wi_categorical   0.197227124     0.207488540  0.35324532
## wi_continuous    0.158176866     0.176821427  0.41904938
## sewing_machine   0.020506446     0.019823545  0.06445736
## computer         0.010410039     0.009605861  0.14377627
## educ_years       0.044118806     0.050119041  0.15047469
## h2o_source       1.000000000     0.981853153  0.09398919
## h2o_non_potable  0.981853153     1.000000000  0.10336721
## toilet_type      0.093989188     0.103367214  1.00000000
final_df <- read.csv("clean_for_final.csv", header = TRUE)
pov.model <- '
health =~ h2o_source + share_toilet
wealth =~ radio + car_truck + cell_phone + watch + sewing_machine + computer
wealth ~~ health  + educ_years'
#Structural Model (Prediction)

#wealth ~~ health  + educ_years
fit <- cfa(pov.model, std.lv = TRUE, ordered = c("share_toilet", "radio", "car_truck", "cell_phone", "watch", "computer", "sewing_machine"), data = final_df)
## Warning: lavaan->lav_object_post_check():  
##    covariance matrix of latent variables is not positive definite ; use 
##    lavInspect(fit, "cov.lv") to investigate.
summary(fit, fit.measures = TRUE, standardized = TRUE)
## lavaan 0.6-20 ended normally after 47 iterations
## 
##   Estimator                                       DWLS
##   Optimization method                           NLMINB
##   Number of model parameters                        21
## 
##   Number of observations                         14722
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                               734.340     876.879
##   Degrees of freedom                                26          26
##   P-value (Chi-square)                           0.000       0.000
##   Scaling correction factor                                  0.839
##   Shift parameter                                            1.672
##     simple second-order correction                                
## 
## Model Test Baseline Model:
## 
##   Test statistic                             27347.506   22843.679
##   Degrees of freedom                                36          36
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.197
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.974       0.963
##   Tucker-Lewis Index (TLI)                       0.964       0.948
##                                                                   
##   Robust Comparative Fit Index (CFI)                            NA
##   Robust Tucker-Lewis Index (TLI)                               NA
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.043       0.047
##   90 Percent confidence interval - lower         0.040       0.044
##   90 Percent confidence interval - upper         0.046       0.050
##   P-value H_0: RMSEA <= 0.050                    1.000       0.959
##   P-value H_0: RMSEA >= 0.080                    0.000       0.000
##                                                                   
##   Robust RMSEA                                                  NA
##   90 Percent confidence interval - lower                        NA
##   90 Percent confidence interval - upper                        NA
##   P-value H_0: Robust RMSEA <= 0.050                            NA
##   P-value H_0: Robust RMSEA >= 0.080                            NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.061       0.061
## 
## Parameter Estimates:
## 
##   Parameterization                               Delta
##   Standard errors                           Robust.sem
##   Information                                 Expected
##   Information saturated (h1) model        Unstructured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   health =~                                                             
##     h2o_source        0.688    0.145    4.743    0.000    0.688    0.084
##     share_toilet     -0.316    0.060   -5.251    0.000   -0.316   -0.316
##   wealth =~                                                             
##     radio             0.749    0.009   85.451    0.000    0.749    0.749
##     car_truck         0.801    0.014   55.754    0.000    0.801    0.801
##     cell_phone        0.864    0.007  119.274    0.000    0.864    0.864
##     watch             0.765    0.008   95.965    0.000    0.765    0.765
##     sewing_machine    0.531    0.019   28.684    0.000    0.531    0.531
##     computer          0.872    0.020   44.510    0.000    0.872    0.872
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   health ~~                                                             
##     wealth            1.634    0.307    5.324    0.000    1.634    1.634
##   wealth ~~                                                             
##     educ_years        1.568    0.048   32.676    0.000    1.568    0.349
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .h2o_source       51.095    0.070  726.075    0.000   51.095    6.213
##     educ_years        3.341    0.061   54.606    0.000    3.341    0.744
## 
## Thresholds:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     share_toilt|t1   -0.511    0.011  -47.136    0.000   -0.511   -0.511
##     radio|t1         -0.458    0.011  -42.658    0.000   -0.458   -0.458
##     car_truck|t1      1.729    0.018   93.690    0.000    1.729    1.729
##     cell_phone|t1     0.067    0.010    6.461    0.000    0.067    0.067
##     watch|t1         -0.174    0.010  -16.788    0.000   -0.174   -0.174
##     sewing_mchn|t1    1.740    0.019   93.546    0.000    1.740    1.740
##     computer|t1       2.070    0.024   85.635    0.000    2.070    2.070
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .h2o_source       67.167    0.473  142.068    0.000   67.167    0.993
##    .share_toilet      0.900                               0.900    0.900
##    .radio             0.438                               0.438    0.438
##    .car_truck         0.358                               0.358    0.358
##    .cell_phone        0.254                               0.254    0.254
##    .watch             0.415                               0.415    0.415
##    .sewing_machine    0.718                               0.718    0.718
##    .computer          0.240                               0.240    0.240
##     educ_years       20.169    0.388   52.047    0.000   20.169    1.000
##     health            1.000                               1.000    1.000
##     wealth            1.000                               1.000    1.000
### Run the SEM model

pov2_model <- '
#measurement model
health =~ h2o_source  + share_toilet
wealth =~ radio+ car_truck + cell_phone + watch + sewing_machine +computer
wealth ~~ health + educ_years'

### regressions

educ ~ health
## educ ~ health
health ~ wealth
## health ~ wealth
educ ~ wealth 
## educ ~ wealth
fit <- sem(pov2_model, std.lv = TRUE,  ordered = c("share_toilet", "radio", "car_truck", "cell_phone", "watch", "sewing_machine", "computer"),data = final_df)
## Warning: lavaan->lav_object_post_check():  
##    covariance matrix of latent variables is not positive definite ; use 
##    lavInspect(fit, "cov.lv") to investigate.
summary(fit, standardized = TRUE,  fit.measures = TRUE)
## lavaan 0.6-20 ended normally after 47 iterations
## 
##   Estimator                                       DWLS
##   Optimization method                           NLMINB
##   Number of model parameters                        21
## 
##   Number of observations                         14722
## 
## Model Test User Model:
##                                               Standard      Scaled
##   Test Statistic                               734.340     876.879
##   Degrees of freedom                                26          26
##   P-value (Chi-square)                           0.000       0.000
##   Scaling correction factor                                  0.839
##   Shift parameter                                            1.672
##     simple second-order correction                                
## 
## Model Test Baseline Model:
## 
##   Test statistic                             27347.506   22843.679
##   Degrees of freedom                                36          36
##   P-value                                        0.000       0.000
##   Scaling correction factor                                  1.197
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    0.974       0.963
##   Tucker-Lewis Index (TLI)                       0.964       0.948
##                                                                   
##   Robust Comparative Fit Index (CFI)                            NA
##   Robust Tucker-Lewis Index (TLI)                               NA
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.043       0.047
##   90 Percent confidence interval - lower         0.040       0.044
##   90 Percent confidence interval - upper         0.046       0.050
##   P-value H_0: RMSEA <= 0.050                    1.000       0.959
##   P-value H_0: RMSEA >= 0.080                    0.000       0.000
##                                                                   
##   Robust RMSEA                                                  NA
##   90 Percent confidence interval - lower                        NA
##   90 Percent confidence interval - upper                        NA
##   P-value H_0: Robust RMSEA <= 0.050                            NA
##   P-value H_0: Robust RMSEA >= 0.080                            NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.061       0.061
## 
## Parameter Estimates:
## 
##   Parameterization                               Delta
##   Standard errors                           Robust.sem
##   Information                                 Expected
##   Information saturated (h1) model        Unstructured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   health =~                                                             
##     h2o_source        0.688    0.145    4.743    0.000    0.688    0.084
##     share_toilet     -0.316    0.060   -5.251    0.000   -0.316   -0.316
##   wealth =~                                                             
##     radio             0.749    0.009   85.451    0.000    0.749    0.749
##     car_truck         0.801    0.014   55.754    0.000    0.801    0.801
##     cell_phone        0.864    0.007  119.274    0.000    0.864    0.864
##     watch             0.765    0.008   95.965    0.000    0.765    0.765
##     sewing_machine    0.531    0.019   28.684    0.000    0.531    0.531
##     computer          0.872    0.020   44.510    0.000    0.872    0.872
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   health ~~                                                             
##     wealth            1.634    0.307    5.324    0.000    1.634    1.634
##   wealth ~~                                                             
##     educ_years        1.568    0.048   32.676    0.000    1.568    0.349
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .h2o_source       51.095    0.070  726.075    0.000   51.095    6.213
##     educ_years        3.341    0.061   54.606    0.000    3.341    0.744
## 
## Thresholds:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     share_toilt|t1   -0.511    0.011  -47.136    0.000   -0.511   -0.511
##     radio|t1         -0.458    0.011  -42.658    0.000   -0.458   -0.458
##     car_truck|t1      1.729    0.018   93.690    0.000    1.729    1.729
##     cell_phone|t1     0.067    0.010    6.461    0.000    0.067    0.067
##     watch|t1         -0.174    0.010  -16.788    0.000   -0.174   -0.174
##     sewing_mchn|t1    1.740    0.019   93.546    0.000    1.740    1.740
##     computer|t1       2.070    0.024   85.635    0.000    2.070    2.070
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .h2o_source       67.167    0.473  142.068    0.000   67.167    0.993
##    .share_toilet      0.900                               0.900    0.900
##    .radio             0.438                               0.438    0.438
##    .car_truck         0.358                               0.358    0.358
##    .cell_phone        0.254                               0.254    0.254
##    .watch             0.415                               0.415    0.415
##    .sewing_machine    0.718                               0.718    0.718
##    .computer          0.240                               0.240    0.240
##     educ_years       20.169    0.388   52.047    0.000   20.169    1.000
##     health            1.000                               1.000    1.000
##     wealth            1.000                               1.000    1.000