library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.2.1 --
## √ ggplot2 3.2.1     √ purrr   0.3.2
## √ tibble  2.1.2     √ dplyr   0.8.3
## √ tidyr   1.0.0     √ stringr 1.4.0
## √ readr   1.3.1     √ forcats 0.4.0
## Warning: package 'ggplot2' was built under R version 3.6.1
## Warning: package 'tidyr' was built under R version 3.6.1
## Warning: package 'dplyr' was built under R version 3.6.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(lavaan)
## Warning: package 'lavaan' was built under R version 3.6.1
## This is lavaan 0.6-5
## lavaan is BETA software! Please report any bugs.
library(semPlot)
## Warning: package 'semPlot' was built under R version 3.6.1
## Registered S3 methods overwritten by 'huge':
##   method    from   
##   plot.sim  BDgraph
##   print.sim BDgraph

读数据,并把男女分开,分别作了模型

ddd <- read_csv("./child20164.csv")
## Parsed with column specification:
## cols(
##   age = col_double(),
##   gender = col_double(),
##   edu_expect = col_double(),
##   care_edu = col_double(),
##   communicate = col_double(),
##   tutor_fees = col_double(),
##   pd501b = col_double(),
##   pd577r = col_double(),
##   parents_edu = col_double(),
##   finc = col_double(),
##   urban = col_double(),
##   total_care = col_double(),
##   tutor_fees_log = col_double(),
##   school_fees_log = col_double(),
##   other_log = col_double()
## )
ddd %>% colnames()
##  [1] "age"             "gender"          "edu_expect"     
##  [4] "care_edu"        "communicate"     "tutor_fees"     
##  [7] "pd501b"          "pd577r"          "parents_edu"    
## [10] "finc"            "urban"           "total_care"     
## [13] "tutor_fees_log"  "school_fees_log" "other_log"
ddd %>% count(urban)
## # A tibble: 2 x 2
##   urban     n
##   <dbl> <int>
## 1     0  1006
## 2     1   136
boy <- ddd %>%
   filter(gender == 1 )
girl <- ddd %>%
   filter(gender == 0 )
city <- ddd %>%
   filter(urban == 1 )
countryside <- ddd %>%
   filter(urban == 0 )
#df <- ddd %>%
  #select(gender, edu_expect, age)
#da <- as.data.frame(df)
#da


 #cor.test(ddd[,2],as.double(ddd[,3]))

#总的数据的模型

model <- '
   # latent variables
    f1 =~ a*tutor_fees_log + s*school_fees_log + d*other_log
    f2 =~ f*total_care + g*care_edu + h*communicate
    
   # regressions
     f1 ~ j*parents_edu
     f2 ~ k*parents_edu
    
     f1 ~ q*finc
     f2 ~ w*finc
    edu_expect~  e*parents_edu
     edu_expect~  r*finc
     #f1 ~  t*edu_expect
      edu_expect ~ y*f2
      edu_expect ~ u*f1
   # residual covariances
     # max_edu ~~ finc_per
      #total
     total_max_edu := e +  (j*u) + (k*y)
     total_finc := r +  (w*y) + (q*u)
     edu_pay := u
     edu_care := y
'
fit <- sem(model,data=ddd )
summary(fit,standardized = TRUE)
## lavaan 0.6-5 ended normally after 93 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                         21
##                                                       
##   Number of observations                          1142
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                82.848
##   Degrees of freedom                                21
##   P-value (Chi-square)                           0.000
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard errors                             Standard
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   f1 =~                                                                 
##     tutr_fs_lg (a)    1.000                               1.440    0.562
##     schl_fs_lg (s)    0.166    0.068    2.435    0.015    0.240    0.166
##     other_log  (d)    0.353    0.139    2.550    0.011    0.509    0.187
##   f2 =~                                                                 
##     total_care (f)    1.000                               0.153    0.204
##     care_edu   (g)    4.750    0.810    5.868    0.000    0.727    0.865
##     communicat (h)    4.211    0.686    6.139    0.000    0.644    0.791
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   f1 ~                                                                  
##     parents_ed (j)    0.076    0.019    3.963    0.000    0.053    0.201
##   f2 ~                                                                  
##     parents_ed (k)    0.004    0.001    3.000    0.003    0.029    0.111
##   f1 ~                                                                  
##     finc       (q)    0.206    0.056    3.717    0.000    0.143    0.188
##   f2 ~                                                                  
##     finc       (w)    0.007    0.004    1.832    0.067    0.048    0.062
##   edu_expect ~                                                          
##     parents_ed (e)    0.013    0.012    1.114    0.265    0.013    0.038
##     finc       (r)   -0.024    0.034   -0.728    0.467   -0.024   -0.024
##     f2         (y)    1.191    0.334    3.569    0.000    0.182    0.139
##     f1         (u)    0.164    0.078    2.106    0.035    0.236    0.180
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .tutor_fees_log    4.485    0.808    5.548    0.000    4.485    0.684
##    .school_fees_lg    2.017    0.089   22.657    0.000    2.017    0.972
##    .other_log         7.169    0.322   22.257    0.000    7.169    0.965
##    .total_care        0.540    0.023   23.666    0.000    0.540    0.958
##    .care_edu          0.178    0.054    3.305    0.001    0.178    0.252
##    .communicate       0.247    0.043    5.731    0.000    0.247    0.374
##    .edu_expect        1.617    0.074   21.763    0.000    1.617    0.942
##    .f1                1.900    0.797    2.385    0.017    0.917    0.917
##    .f2                0.023    0.007    3.151    0.002    0.982    0.982
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     total_max_edu     0.031    0.010    3.030    0.002    0.031    0.090
##     total_finc        0.018    0.030    0.612    0.541    0.018    0.018
##     edu_pay           0.164    0.078    2.106    0.035    0.236    0.180
##     edu_care          1.191    0.334    3.569    0.000    0.182    0.139
aa <- parameterEstimates(fit)#这里的est是没标注准化的系数,还有置信区间


Est <- parameterEstimates(fit, ci = FALSE, standardized = TRUE)#est为未标准化的,std.all为标准化后的
subset(Est, lhs == "f2")
##    lhs op         rhs label   est    se     z pvalue std.lv std.all
## 4   f2 =~  total_care     f 1.000 0.000    NA     NA  0.153   0.204
## 5   f2 =~    care_edu     g 4.750 0.810 5.868  0.000  0.727   0.865
## 6   f2 =~ communicate     h 4.211 0.686 6.139  0.000  0.644   0.791
## 8   f2  ~ parents_edu     k 0.004 0.001 3.000  0.003  0.029   0.111
## 10  f2  ~        finc     w 0.007 0.004 1.832  0.067  0.048   0.062
## 23  f2 ~~          f2       0.023 0.007 3.151  0.002  0.982   0.982
##    std.nox
## 4    0.204
## 5    0.865
## 6    0.791
## 8    0.029
## 10   0.048
## 23   0.982
fitMeasures(fit, c("cfi", "rmsea","nfi","srmr"))
##   cfi rmsea   nfi  srmr 
## 0.933 0.051 0.914 0.047
semPaths(fit,whatLabels="est",style="lisrel",layout = "tree2",
         residuals=TRUE)

只有男生的模型

model1 <- '
   # latent variables
    f1 =~ a*tutor_fees_log + s*school_fees_log + d*other_log
    f2 =~ f*total_care + g*care_edu + h*communicate
    
   # regressions
     f1 ~ j*parents_edu
     f2 ~ k*parents_edu
    
     f1 ~ q*finc
     f2 ~ w*finc
    edu_expect~  e*parents_edu
     edu_expect~  r*finc
     #f1 ~  t*edu_expect
      edu_expect ~ y*f2
      edu_expect ~ u*f1
   # residual covariances
     # max_edu ~~ finc_per
      #total
     total_max_edu := e +  (j*u) + (k*y)
     total_finc := r +  (w*y) + (q*u)
     edu_pay := u
     edu_care := y
'
fit1 <- sem(model1,data=boy )
summary(fit1)
## lavaan 0.6-5 ended normally after 90 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                         21
##                                                       
##   Number of observations                           627
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                46.264
##   Degrees of freedom                                21
##   P-value (Chi-square)                           0.001
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard errors                             Standard
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f1 =~                                               
##     tutr_fs_lg (a)    1.000                           
##     schl_fs_lg (s)    0.190    0.086    2.206    0.027
##     other_log  (d)    0.413    0.176    2.350    0.019
##   f2 =~                                               
##     total_care (f)    1.000                           
##     care_edu   (g)    5.889    1.658    3.552    0.000
##     communicat (h)    5.457    1.519    3.591    0.000
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f1 ~                                                
##     parents_ed (j)    0.081    0.026    3.167    0.002
##   f2 ~                                                
##     parents_ed (k)    0.004    0.002    2.335    0.020
##   f1 ~                                                
##     finc       (q)    0.177    0.071    2.472    0.013
##   f2 ~                                                
##     finc       (w)    0.008    0.004    1.731    0.084
##   edu_expect ~                                        
##     parents_ed (e)   -0.006    0.016   -0.381    0.703
##     finc       (r)   -0.054    0.044   -1.230    0.219
##     f2         (y)    1.983    0.740    2.680    0.007
##     f1         (u)    0.185    0.093    1.987    0.047
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .tutor_fees_log    4.174    1.003    4.161    0.000
##    .school_fees_lg    2.176    0.131   16.627    0.000
##    .other_log         7.130    0.445   16.015    0.000
##    .total_care        0.523    0.030   17.589    0.000
##    .care_edu          0.219    0.069    3.190    0.001
##    .communicate       0.234    0.060    3.927    0.000
##    .edu_expect        1.770    0.111   16.014    0.000
##    .f1                2.199    0.997    2.206    0.027
##    .f2                0.014    0.008    1.844    0.065
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     total_max_edu     0.017    0.014    1.180    0.238
##     total_finc       -0.006    0.041   -0.145    0.885
##     edu_pay           0.185    0.093    1.987    0.047
##     edu_care          1.983    0.740    2.680    0.007
parameterEstimates(fit1)#这里的est是没标注准化的系数,还有置信区间
##                lhs op             rhs         label    est    se      z
## 1               f1 =~  tutor_fees_log             a  1.000 0.000     NA
## 2               f1 =~ school_fees_log             s  0.190 0.086  2.206
## 3               f1 =~       other_log             d  0.413 0.176  2.350
## 4               f2 =~      total_care             f  1.000 0.000     NA
## 5               f2 =~        care_edu             g  5.889 1.658  3.552
## 6               f2 =~     communicate             h  5.457 1.519  3.591
## 7               f1  ~     parents_edu             j  0.081 0.026  3.167
## 8               f2  ~     parents_edu             k  0.004 0.002  2.335
## 9               f1  ~            finc             q  0.177 0.071  2.472
## 10              f2  ~            finc             w  0.008 0.004  1.731
## 11      edu_expect  ~     parents_edu             e -0.006 0.016 -0.381
## 12      edu_expect  ~            finc             r -0.054 0.044 -1.230
## 13      edu_expect  ~              f2             y  1.983 0.740  2.680
## 14      edu_expect  ~              f1             u  0.185 0.093  1.987
## 15  tutor_fees_log ~~  tutor_fees_log                4.174 1.003  4.161
## 16 school_fees_log ~~ school_fees_log                2.176 0.131 16.627
## 17       other_log ~~       other_log                7.130 0.445 16.015
## 18      total_care ~~      total_care                0.523 0.030 17.589
## 19        care_edu ~~        care_edu                0.219 0.069  3.190
## 20     communicate ~~     communicate                0.234 0.060  3.927
## 21      edu_expect ~~      edu_expect                1.770 0.111 16.014
## 22              f1 ~~              f1                2.199 0.997  2.206
## 23              f2 ~~              f2                0.014 0.008  1.844
## 24     parents_edu ~~     parents_edu               14.736 0.000     NA
## 25     parents_edu ~~            finc                0.556 0.000     NA
## 26            finc ~~            finc                1.858 0.000     NA
## 27   total_max_edu :=   e+(j*u)+(k*y) total_max_edu  0.017 0.014  1.180
## 28      total_finc :=   r+(w*y)+(q*u)    total_finc -0.006 0.041 -0.145
## 29         edu_pay :=               u       edu_pay  0.185 0.093  1.987
## 30        edu_care :=               y      edu_care  1.983 0.740  2.680
##    pvalue ci.lower ci.upper
## 1      NA    1.000    1.000
## 2   0.027    0.021    0.358
## 3   0.019    0.068    0.757
## 4      NA    1.000    1.000
## 5   0.000    2.639    9.139
## 6   0.000    2.479    8.435
## 7   0.002    0.031    0.131
## 8   0.020    0.001    0.008
## 9   0.013    0.037    0.317
## 10  0.084   -0.001    0.016
## 11  0.703   -0.038    0.026
## 12  0.219   -0.139    0.032
## 13  0.007    0.533    3.434
## 14  0.047    0.002    0.368
## 15  0.000    2.208    6.141
## 16  0.000    1.919    2.432
## 17  0.000    6.257    8.002
## 18  0.000    0.464    0.581
## 19  0.001    0.085    0.354
## 20  0.000    0.117    0.350
## 21  0.000    1.553    1.986
## 22  0.027    0.245    4.152
## 23  0.065   -0.001    0.029
## 24     NA   14.736   14.736
## 25     NA    0.556    0.556
## 26     NA    1.858    1.858
## 27  0.238   -0.011    0.045
## 28  0.885   -0.086    0.074
## 29  0.047    0.002    0.368
## 30  0.007    0.533    3.434
Est <- parameterEstimates(fit1, ci = FALSE, standardized = TRUE)#est为未标准化的,std.all为标准化后的
subset(Est, lhs == "f2")
##    lhs op         rhs label   est    se     z pvalue std.lv std.all
## 4   f2 =~  total_care     f 1.000 0.000    NA     NA  0.119   0.163
## 5   f2 =~    care_edu     g 5.889 1.658 3.552  0.000  0.703   0.832
## 6   f2 =~ communicate     h 5.457 1.519 3.591  0.000  0.652   0.803
## 8   f2  ~ parents_edu     k 0.004 0.002 2.335  0.020  0.035   0.133
## 10  f2  ~        finc     w 0.008 0.004 1.731  0.084  0.064   0.087
## 23  f2 ~~          f2       0.014 0.008 1.844  0.065  0.972   0.972
##    std.nox
## 4    0.163
## 5    0.832
## 6    0.803
## 8    0.035
## 10   0.064
## 23   0.972
fitMeasures(fit1, c("cfi", "rmsea","nfi","srmr"))
##   cfi rmsea   nfi  srmr 
## 0.947 0.044 0.910 0.048
semPaths(fit1,whatLabels="std.all",style="lisrel",layout = "tree2",
         residuals=TRUE)

只有女生的模型

model2 <- '
   # latent variables
    f1 =~ a*tutor_fees_log + s*school_fees_log + d*other_log
    f2 =~ f*total_care + g*care_edu + h*communicate
    
   # regressions
     f1 ~ j*parents_edu
     f2 ~ k*parents_edu
    
     f1 ~ q*finc
     f2 ~ w*finc
    edu_expect~  e*parents_edu
     edu_expect~  r*finc
     #f1 ~  t*edu_expect
      edu_expect ~ y*f2
      edu_expect ~ u*f1
   # residual covariances
     # max_edu ~~ finc_per
      #total
     total_max_edu := e +  (j*u) + (k*y)
     total_finc := r +  (w*y) + (q*u)
     edu_pay := u
     edu_care := y
'
fit2 <- sem(model2,data=girl )
summary(fit2)
## lavaan 0.6-5 ended normally after 77 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                         21
##                                                       
##   Number of observations                           515
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                49.422
##   Degrees of freedom                                21
##   P-value (Chi-square)                           0.000
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard errors                             Standard
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f1 =~                                               
##     tutr_fs_lg (a)    1.000                           
##     schl_fs_lg (s)    0.176    0.123    1.427    0.154
##     other_log  (d)    0.289    0.227    1.272    0.203
##   f2 =~                                               
##     total_care (f)    1.000                           
##     care_edu   (g)    3.941    0.845    4.663    0.000
##     communicat (h)    3.322    0.641    5.180    0.000
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f1 ~                                                
##     parents_ed (j)    0.068    0.029    2.368    0.018
##   f2 ~                                                
##     parents_ed (k)    0.004    0.003    1.526    0.127
##   f1 ~                                                
##     finc       (q)    0.256    0.087    2.925    0.003
##   f2 ~                                                
##     finc       (w)    0.006    0.007    0.788    0.431
##   edu_expect ~                                        
##     parents_ed (e)    0.035    0.018    1.978    0.048
##     finc       (r)    0.016    0.059    0.274    0.784
##     f2         (y)    0.676    0.322    2.100    0.036
##     f1         (u)    0.149    0.161    0.925    0.355
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .tutor_fees_log    5.272    1.006    5.238    0.000
##    .school_fees_lg    1.807    0.118   15.259    0.000
##    .other_log         7.182    0.463   15.517    0.000
##    .total_care        0.558    0.035   15.833    0.000
##    .care_edu          0.127    0.081    1.572    0.116
##    .communicate       0.262    0.060    4.405    0.000
##    .edu_expect        1.393    0.095   14.691    0.000
##    .f1                1.118    0.957    1.168    0.243
##    .f2                0.036    0.014    2.614    0.009
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     total_max_edu     0.048    0.014    3.410    0.001
##     total_finc        0.058    0.043    1.365    0.172
##     edu_pay           0.149    0.161    0.925    0.355
##     edu_care          0.676    0.322    2.100    0.036
parameterEstimates(fit2)#这里的est是没标注准化的系数,还有置信区间
##                lhs op             rhs         label    est    se      z
## 1               f1 =~  tutor_fees_log             a  1.000 0.000     NA
## 2               f1 =~ school_fees_log             s  0.176 0.123  1.427
## 3               f1 =~       other_log             d  0.289 0.227  1.272
## 4               f2 =~      total_care             f  1.000 0.000     NA
## 5               f2 =~        care_edu             g  3.941 0.845  4.663
## 6               f2 =~     communicate             h  3.322 0.641  5.180
## 7               f1  ~     parents_edu             j  0.068 0.029  2.368
## 8               f2  ~     parents_edu             k  0.004 0.003  1.526
## 9               f1  ~            finc             q  0.256 0.087  2.925
## 10              f2  ~            finc             w  0.006 0.007  0.788
## 11      edu_expect  ~     parents_edu             e  0.035 0.018  1.978
## 12      edu_expect  ~            finc             r  0.016 0.059  0.274
## 13      edu_expect  ~              f2             y  0.676 0.322  2.100
## 14      edu_expect  ~              f1             u  0.149 0.161  0.925
## 15  tutor_fees_log ~~  tutor_fees_log                5.272 1.006  5.238
## 16 school_fees_log ~~ school_fees_log                1.807 0.118 15.259
## 17       other_log ~~       other_log                7.182 0.463 15.517
## 18      total_care ~~      total_care                0.558 0.035 15.833
## 19        care_edu ~~        care_edu                0.127 0.081  1.572
## 20     communicate ~~     communicate                0.262 0.060  4.405
## 21      edu_expect ~~      edu_expect                1.393 0.095 14.691
## 22              f1 ~~              f1                1.118 0.957  1.168
## 23              f2 ~~              f2                0.036 0.014  2.614
## 24     parents_edu ~~     parents_edu               14.171 0.000     NA
## 25     parents_edu ~~            finc                0.457 0.000     NA
## 26            finc ~~            finc                1.544 0.000     NA
## 27   total_max_edu :=   e+(j*u)+(k*y) total_max_edu  0.048 0.014  3.410
## 28      total_finc :=   r+(w*y)+(q*u)    total_finc  0.058 0.043  1.365
## 29         edu_pay :=               u       edu_pay  0.149 0.161  0.925
## 30        edu_care :=               y      edu_care  0.676 0.322  2.100
##    pvalue ci.lower ci.upper
## 1      NA    1.000    1.000
## 2   0.154   -0.066    0.417
## 3   0.203   -0.156    0.734
## 4      NA    1.000    1.000
## 5   0.000    2.285    5.598
## 6   0.000    2.065    4.579
## 7   0.018    0.012    0.124
## 8   0.127   -0.001    0.009
## 9   0.003    0.084    0.427
## 10  0.431   -0.009    0.020
## 11  0.048    0.000    0.070
## 12  0.784   -0.100    0.132
## 13  0.036    0.045    1.307
## 14  0.355   -0.167    0.465
## 15  0.000    3.299    7.244
## 16  0.000    1.575    2.039
## 17  0.000    6.275    8.089
## 18  0.000    0.489    0.627
## 19  0.116   -0.031    0.286
## 20  0.000    0.146    0.379
## 21  0.000    1.207    1.579
## 22  0.243   -0.757    2.993
## 23  0.009    0.009    0.064
## 24     NA   14.171   14.171
## 25     NA    0.457    0.457
## 26     NA    1.544    1.544
## 27  0.001    0.020    0.076
## 28  0.172   -0.025    0.142
## 29  0.355   -0.167    0.465
## 30  0.036    0.045    1.307
Est <- parameterEstimates(fit2, ci = FALSE, standardized = TRUE)#est为未标准化的,std.all为标准化后的
subset(Est, lhs == "f2")
##    lhs op         rhs label   est    se     z pvalue std.lv std.all
## 4   f2 =~  total_care     f 1.000 0.000    NA     NA  0.192   0.248
## 5   f2 =~    care_edu     g 3.941 0.845 4.663  0.000  0.755   0.904
## 6   f2 =~ communicate     h 3.322 0.641 5.180  0.000  0.636   0.779
## 8   f2  ~ parents_edu     k 0.004 0.003 1.526  0.127  0.020   0.076
## 10  f2  ~        finc     w 0.006 0.007 0.788  0.431  0.031   0.038
## 23  f2 ~~          f2       0.036 0.014 2.614  0.009  0.992   0.992
##    std.nox
## 4    0.248
## 5    0.904
## 6    0.779
## 8    0.020
## 10   0.031
## 23   0.992
fitMeasures(fit2, c("cfi", "rmsea","nfi","srmr"))
##   cfi rmsea   nfi  srmr 
## 0.936 0.051 0.896 0.051
semPaths(fit2,whatLabels="std.all",style="lisrel",layout = "tree2",
         residuals=TRUE)

城市的模型

model2 <- '
   # latent variables
    f1 =~ a*tutor_fees_log + s*school_fees_log + d*other_log
    f2 =~ f*total_care + g*care_edu + h*communicate
    
   # regressions
     f1 ~ j*parents_edu
     f2 ~ k*parents_edu
    
     f1 ~ q*finc
     f2 ~ w*finc
    edu_expect~  e*parents_edu
     edu_expect~  r*finc
     #f1 ~  t*edu_expect
      edu_expect ~ y*f2
      edu_expect ~ u*f1
   # residual covariances
     # max_edu ~~ finc_per
      #total
     total_max_edu := e +  (j*u) + (k*y)
     total_finc := r +  (w*y) + (q*u)
     edu_pay := u
     edu_care := y
'
fit2 <- sem(model2,data=city )
## Warning in lav_object_post_check(object): lavaan WARNING: some estimated lv
## variances are negative
summary(fit2)
## lavaan 0.6-5 ended normally after 114 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                         21
##                                                       
##   Number of observations                           136
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                17.262
##   Degrees of freedom                                21
##   P-value (Chi-square)                           0.695
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard errors                             Standard
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f1 =~                                               
##     tutr_fs_lg (a)    1.000                           
##     schl_fs_lg (s)    0.169    0.094    1.801    0.072
##     other_log  (d)   -0.049    0.124   -0.397    0.691
##   f2 =~                                               
##     total_care (f)    1.000                           
##     care_edu   (g)    6.929    4.809    1.441    0.150
##     communicat (h)    6.328    4.369    1.448    0.147
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f1 ~                                                
##     parents_ed (j)    0.249    0.062    3.993    0.000
##   f2 ~                                                
##     parents_ed (k)    0.008    0.006    1.353    0.176
##   f1 ~                                                
##     finc       (q)    0.808    0.268    3.010    0.003
##   f2 ~                                                
##     finc       (w)    0.001    0.010    0.116    0.908
##   edu_expect ~                                        
##     parents_ed (e)    0.061    0.033    1.860    0.063
##     finc       (r)    0.171    0.128    1.336    0.182
##     f2         (y)    0.245    1.209    0.203    0.839
##     f1         (u)   -0.047    0.077   -0.612    0.541
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .tutor_fees_log   15.389    3.869    3.978    0.000
##    .school_fees_lg    2.122    0.275    7.711    0.000
##    .other_log         9.213    1.118    8.238    0.000
##    .total_care        0.564    0.069    8.216    0.000
##    .care_edu          0.177    0.111    1.589    0.112
##    .communicate       0.196    0.094    2.078    0.038
##    .edu_expect        1.548    0.191    8.095    0.000
##    .f1               -4.463    3.430   -1.301    0.193
##    .f2                0.010    0.013    0.731    0.465
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     total_max_edu     0.051    0.025    2.050    0.040
##     total_finc        0.133    0.111    1.200    0.230
##     edu_pay          -0.047    0.077   -0.612    0.541
##     edu_care          0.245    1.209    0.203    0.839
parameterEstimates(fit2)#这里的est是没标注准化的系数,还有置信区间
##                lhs op             rhs         label    est    se      z
## 1               f1 =~  tutor_fees_log             a  1.000 0.000     NA
## 2               f1 =~ school_fees_log             s  0.169 0.094  1.801
## 3               f1 =~       other_log             d -0.049 0.124 -0.397
## 4               f2 =~      total_care             f  1.000 0.000     NA
## 5               f2 =~        care_edu             g  6.929 4.809  1.441
## 6               f2 =~     communicate             h  6.328 4.369  1.448
## 7               f1  ~     parents_edu             j  0.249 0.062  3.993
## 8               f2  ~     parents_edu             k  0.008 0.006  1.353
## 9               f1  ~            finc             q  0.808 0.268  3.010
## 10              f2  ~            finc             w  0.001 0.010  0.116
## 11      edu_expect  ~     parents_edu             e  0.061 0.033  1.860
## 12      edu_expect  ~            finc             r  0.171 0.128  1.336
## 13      edu_expect  ~              f2             y  0.245 1.209  0.203
## 14      edu_expect  ~              f1             u -0.047 0.077 -0.612
## 15  tutor_fees_log ~~  tutor_fees_log               15.389 3.869  3.978
## 16 school_fees_log ~~ school_fees_log                2.122 0.275  7.711
## 17       other_log ~~       other_log                9.213 1.118  8.238
## 18      total_care ~~      total_care                0.564 0.069  8.216
## 19        care_edu ~~        care_edu                0.177 0.111  1.589
## 20     communicate ~~     communicate                0.196 0.094  2.078
## 21      edu_expect ~~      edu_expect                1.548 0.191  8.095
## 22              f1 ~~              f1               -4.463 3.430 -1.301
## 23              f2 ~~              f2                0.010 0.013  0.731
## 24     parents_edu ~~     parents_edu               18.887 0.000     NA
## 25     parents_edu ~~            finc                0.752 0.000     NA
## 26            finc ~~            finc                0.949 0.000     NA
## 27   total_max_edu :=   e+(j*u)+(k*y) total_max_edu  0.051 0.025  2.050
## 28      total_finc :=   r+(w*y)+(q*u)    total_finc  0.133 0.111  1.200
## 29         edu_pay :=               u       edu_pay -0.047 0.077 -0.612
## 30        edu_care :=               y      edu_care  0.245 1.209  0.203
##    pvalue ci.lower ci.upper
## 1      NA    1.000    1.000
## 2   0.072   -0.015    0.353
## 3   0.691   -0.291    0.193
## 4      NA    1.000    1.000
## 5   0.150   -2.497   16.355
## 6   0.147   -2.234   14.890
## 7   0.000    0.127    0.371
## 8   0.176   -0.003    0.018
## 9   0.003    0.282    1.334
## 10  0.908   -0.018    0.020
## 11  0.063   -0.003    0.125
## 12  0.182   -0.080    0.421
## 13  0.839   -2.125    2.616
## 14  0.541   -0.198    0.104
## 15  0.000    7.806   22.972
## 16  0.000    1.583    2.662
## 17  0.000    7.021   11.405
## 18  0.000    0.429    0.698
## 19  0.112   -0.041    0.395
## 20  0.038    0.011    0.380
## 21  0.000    1.173    1.922
## 22  0.193  -11.186    2.259
## 23  0.465   -0.016    0.035
## 24     NA   18.887   18.887
## 25     NA    0.752    0.752
## 26     NA    0.949    0.949
## 27  0.040    0.002    0.100
## 28  0.230   -0.084    0.351
## 29  0.541   -0.198    0.104
## 30  0.839   -2.125    2.616
Est <- parameterEstimates(fit2, ci = FALSE, standardized = TRUE)#est为未标准化的,std.all为标准化后的
## Warning in sqrt(ETA2): 产生了NaNs
## Warning in sqrt(ETA2): 产生了NaNs

## Warning in sqrt(ETA2): 产生了NaNs
subset(Est, lhs == "f2")
##    lhs op         rhs label   est    se     z pvalue std.lv std.all
## 4   f2 =~  total_care     f 1.000 0.000    NA     NA  0.103   0.136
## 5   f2 =~    care_edu     g 6.929 4.809 1.441  0.150  0.715   0.862
## 6   f2 =~ communicate     h 6.328 4.369 1.448  0.147  0.653   0.828
## 8   f2  ~ parents_edu     k 0.008 0.006 1.353  0.176  0.073   0.318
## 10  f2  ~        finc     w 0.001 0.010 0.116  0.908  0.011   0.011
## 23  f2 ~~          f2       0.010 0.013 0.731  0.465  0.898   0.898
##    std.nox
## 4    0.136
## 5    0.862
## 6    0.828
## 8    0.073
## 10   0.011
## 23   0.898
fitMeasures(fit2, c("cfi", "rmsea","nfi","srmr"))
##   cfi rmsea   nfi  srmr 
## 1.000 0.000 0.897 0.057
semPaths(fit2,whatLabels="std.all",style="lisrel",layout = "tree2",
         residuals=TRUE)
## Warning in sqrt(ETA2): 产生了NaNs

## Warning in sqrt(ETA2): 产生了NaNs

## Warning in sqrt(ETA2): 产生了NaNs

农村的模型

model2 <- '
   # latent variables
    f1 =~ a*tutor_fees_log + s*school_fees_log + d*other_log
    f2 =~ f*total_care + g*care_edu + h*communicate
    
   # regressions
     f1 ~ j*parents_edu
     f2 ~ k*parents_edu
    
     f1 ~ q*finc
     f2 ~ w*finc
    edu_expect~  e*parents_edu
     edu_expect~  r*finc
     #f1 ~  t*edu_expect
      edu_expect ~ y*f2
      edu_expect ~ u*f1
   # residual covariances
     # max_edu ~~ finc_per
      #total
     total_max_edu := e +  (j*u) + (k*y)
     total_finc := r +  (w*y) + (q*u)
     edu_pay := u
     edu_care := y
'
fit2 <- sem(model2,data=countryside )
summary(fit2)
## lavaan 0.6-5 ended normally after 80 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of free parameters                         21
##                                                       
##   Number of observations                          1006
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                66.298
##   Degrees of freedom                                21
##   P-value (Chi-square)                           0.000
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard errors                             Standard
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f1 =~                                               
##     tutr_fs_lg (a)    1.000                           
##     schl_fs_lg (s)    0.250    0.117    2.138    0.033
##     other_log  (d)    0.484    0.225    2.151    0.031
##   f2 =~                                               
##     total_care (f)    1.000                           
##     care_edu   (g)    4.792    0.881    5.437    0.000
##     communicat (h)    3.978    0.667    5.966    0.000
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   f1 ~                                                
##     parents_ed (j)    0.019    0.019    1.006    0.314
##   f2 ~                                                
##     parents_ed (k)    0.002    0.002    1.585    0.113
##   f1 ~                                                
##     finc       (q)    0.123    0.052    2.376    0.018
##   f2 ~                                                
##     finc       (w)    0.006    0.004    1.404    0.160
##   edu_expect ~                                        
##     parents_ed (e)    0.015    0.011    1.329    0.184
##     finc       (r)   -0.031    0.033   -0.936    0.349
##     f2         (y)    1.233    0.349    3.534    0.000
##     f1         (u)    0.162    0.088    1.845    0.065
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .tutor_fees_log    3.946    0.712    5.543    0.000
##    .school_fees_lg    1.983    0.101   19.641    0.000
##    .other_log         6.805    0.352   19.315    0.000
##    .total_care        0.536    0.024   22.202    0.000
##    .care_edu          0.146    0.065    2.243    0.025
##    .communicate       0.278    0.046    6.017    0.000
##    .edu_expect        1.631    0.078   21.024    0.000
##    .f1                1.447    0.704    2.056    0.040
##    .f2                0.024    0.008    3.016    0.003
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     total_max_edu     0.021    0.011    1.880    0.060
##     total_finc       -0.003    0.031   -0.111    0.912
##     edu_pay           0.162    0.088    1.845    0.065
##     edu_care          1.233    0.349    3.534    0.000
parameterEstimates(fit2)#这里的est是没标注准化的系数,还有置信区间
##                lhs op             rhs         label    est    se      z
## 1               f1 =~  tutor_fees_log             a  1.000 0.000     NA
## 2               f1 =~ school_fees_log             s  0.250 0.117  2.138
## 3               f1 =~       other_log             d  0.484 0.225  2.151
## 4               f2 =~      total_care             f  1.000 0.000     NA
## 5               f2 =~        care_edu             g  4.792 0.881  5.437
## 6               f2 =~     communicate             h  3.978 0.667  5.966
## 7               f1  ~     parents_edu             j  0.019 0.019  1.006
## 8               f2  ~     parents_edu             k  0.002 0.002  1.585
## 9               f1  ~            finc             q  0.123 0.052  2.376
## 10              f2  ~            finc             w  0.006 0.004  1.404
## 11      edu_expect  ~     parents_edu             e  0.015 0.011  1.329
## 12      edu_expect  ~            finc             r -0.031 0.033 -0.936
## 13      edu_expect  ~              f2             y  1.233 0.349  3.534
## 14      edu_expect  ~              f1             u  0.162 0.088  1.845
## 15  tutor_fees_log ~~  tutor_fees_log                3.946 0.712  5.543
## 16 school_fees_log ~~ school_fees_log                1.983 0.101 19.641
## 17       other_log ~~       other_log                6.805 0.352 19.315
## 18      total_care ~~      total_care                0.536 0.024 22.202
## 19        care_edu ~~        care_edu                0.146 0.065  2.243
## 20     communicate ~~     communicate                0.278 0.046  6.017
## 21      edu_expect ~~      edu_expect                1.631 0.078 21.024
## 22              f1 ~~              f1                1.447 0.704  2.056
## 23              f2 ~~              f2                0.024 0.008  3.016
## 24     parents_edu ~~     parents_edu               13.456 0.000     NA
## 25     parents_edu ~~            finc                0.336 0.000     NA
## 26            finc ~~            finc                1.775 0.000     NA
## 27   total_max_edu :=   e+(j*u)+(k*y) total_max_edu  0.021 0.011  1.880
## 28      total_finc :=   r+(w*y)+(q*u)    total_finc -0.003 0.031 -0.111
## 29         edu_pay :=               u       edu_pay  0.162 0.088  1.845
## 30        edu_care :=               y      edu_care  1.233 0.349  3.534
##    pvalue ci.lower ci.upper
## 1      NA    1.000    1.000
## 2   0.033    0.021    0.479
## 3   0.031    0.043    0.924
## 4      NA    1.000    1.000
## 5   0.000    3.065    6.519
## 6   0.000    2.671    5.286
## 7   0.314   -0.018    0.055
## 8   0.113   -0.001    0.005
## 9   0.018    0.022    0.225
## 10  0.160   -0.002    0.014
## 11  0.184   -0.007    0.038
## 12  0.349   -0.095    0.033
## 13  0.000    0.549    1.916
## 14  0.065   -0.010    0.334
## 15  0.000    2.550    5.341
## 16  0.000    1.785    2.181
## 17  0.000    6.114    7.496
## 18  0.000    0.488    0.583
## 19  0.025    0.018    0.273
## 20  0.000    0.188    0.369
## 21  0.000    1.479    1.784
## 22  0.040    0.068    2.827
## 23  0.003    0.008    0.040
## 24     NA   13.456   13.456
## 25     NA    0.336    0.336
## 26     NA    1.775    1.775
## 27  0.060   -0.001    0.043
## 28  0.912   -0.064    0.057
## 29  0.065   -0.010    0.334
## 30  0.000    0.549    1.916
Est <- parameterEstimates(fit2, ci = FALSE, standardized = TRUE)#est为未标准化的,std.all为标准化后的
subset(Est, lhs == "f2")
##    lhs op         rhs label   est    se     z pvalue std.lv std.all
## 4   f2 =~  total_care     f 1.000 0.000    NA     NA  0.156   0.208
## 5   f2 =~    care_edu     g 4.792 0.881 5.437  0.000  0.745   0.890
## 6   f2 =~ communicate     h 3.978 0.667 5.966  0.000  0.619   0.761
## 8   f2  ~ parents_edu     k 0.002 0.002 1.585  0.113  0.015   0.057
## 10  f2  ~        finc     w 0.006 0.004 1.404  0.160  0.037   0.050
## 23  f2 ~~          f2       0.024 0.008 3.016  0.003  0.994   0.994
##    std.nox
## 4    0.208
## 5    0.890
## 6    0.761
## 8    0.015
## 10   0.037
## 23   0.994
fitMeasures(fit2, c("cfi", "rmsea","nfi","srmr"))
##   cfi rmsea   nfi  srmr 
## 0.940 0.046 0.916 0.044
semPaths(fit2,whatLabels="std.all",style="lisrel",layout = "tree2",
         residuals=TRUE)