1. Load dat
rm(list=ls())
library(lavaan)
## Warning: package 'lavaan' was built under R version 3.1.3
## This is lavaan 0.5-20
## lavaan is BETA software! Please report any bugs.
setwd("~/Google Drive/work/cost benefit UG/new design with Max/analysis/data")
sub_temp = read.csv("SEM_EG.csv")
  1. Set function vars

  2. Run function

for (i in 1 : length(Emotion_name)){
  model1 = out_model[i]
  model2 = med_model[i]
  model3 = '
  # effets
  indirect_dg  := a*b
  direct_dg := c
  total_dg := direct_dg + indirect_dg
  
  indirect_pi  := a*b + api*b
  direct_pi := c + cpi
  total_pi := direct_pi + indirect_pi
  
  indirect_ig  := a*b + aig*b
  direct_ig := c + cig
  total_ig := direct_ig + indirect_ig
  
  indirect_ug  := a*b + aug*b
  direct_ug := c + cug
  total_ug := direct_ug + indirect_ug
  
  mod_indirect_pi := indirect_pi - indirect_dg
  mod_direct_pi := direct_pi - direct_dg
  mod_total_pi := total_pi - total_dg
  
  mod_indirect_ig := indirect_ig - indirect_dg
  mod_direct_ig := direct_ig - direct_dg
  mod_total_ig := total_ig - total_dg
  
  mod_indirect_ug := indirect_ug - indirect_dg
  mod_direct_ug := direct_ug - direct_dg
  mod_total_ug := total_ug - total_dg
  '

  model = paste (model1, model2, model3, sep = " ")

  fit  <- sem(model, data = sub_temp)
  
  print(Emotion_name[i])
  summary (fit)
}
## [1] "happy"
## lavaan (0.5-20) converged normally after  80 iterations
## 
##   Number of observations                          1008
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic                0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##   eWTR ~                                              
##     gift       (c)    0.002    0.000    5.545    0.000
##     gift_PI  (cpi)    0.000    0.000    1.060    0.289
##     gift_IG  (cig)    0.000    0.000    0.151    0.880
##     gift_UG  (cug)   -0.000    0.000   -0.328    0.743
##     happy      (b)    0.054    0.006    8.701    0.000
##   happy ~                                             
##     gift       (a)    0.022    0.002   13.684    0.000
##     gift_PI  (api)   -0.002    0.002   -0.855    0.392
##     gift_IG  (aig)   -0.002    0.002   -0.870    0.384
##     gift_UG  (aug)   -0.006    0.002   -3.201    0.001
## 
## Variances:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     eWTR              0.029    0.001   22.450    0.000
##     happy             0.751    0.033   22.450    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     indirect_dg       0.001    0.000    7.342    0.000
##     direct_dg         0.002    0.000    5.545    0.000
##     total_dg          0.003    0.000    9.440    0.000
##     indirect_pi       0.001    0.000    7.302    0.000
##     direct_pi         0.002    0.000    7.033    0.000
##     total_pi          0.003    0.000   10.915    0.000
##     indirect_ig       0.001    0.000    7.255    0.000
##     direct_ig         0.002    0.000    5.938    0.000
##     total_ig          0.003    0.000    9.672    0.000
##     indirect_ug       0.001    0.000    6.805    0.000
##     direct_ug         0.002    0.000    5.759    0.000
##     total_ug          0.003    0.000    8.760    0.000
##     mod_indirect_p   -0.000    0.000   -0.851    0.395
##     mod_direct_pi     0.000    0.000    1.060    0.289
##     mod_total_pi      0.000    0.000    0.797    0.426
##     mod_indirect_g   -0.000    0.000   -0.865    0.387
##     mod_direct_ig     0.000    0.000    0.151    0.880
##     mod_total_ig     -0.000    0.000   -0.085    0.933
##     mod_indirect_g   -0.000    0.000   -3.004    0.003
##     mod_direct_ug    -0.000    0.000   -0.328    0.743
##     mod_total_ug     -0.000    0.000   -1.164    0.244
## 
## [1] "grateful"
## lavaan (0.5-20) converged normally after  69 iterations
## 
##   Number of observations                          1008
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic                0.000
##   Degrees of freedom                                 0
##   Minimum Function Value               0.0000000000000
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##   eWTR ~                                              
##     gift       (c)    0.002    0.000    5.532    0.000
##     gift_PI  (cpi)    0.000    0.000    1.095    0.274
##     gift_IG  (cig)    0.000    0.000    0.174    0.862
##     gift_UG  (cug)   -0.000    0.000   -0.213    0.832
##     grateful   (b)    0.059    0.006    9.825    0.000
##   grateful ~                                          
##     gift       (a)    0.021    0.002   12.682    0.000
##     gift_PI  (api)   -0.002    0.002   -0.843    0.399
##     gift_IG  (aig)   -0.002    0.002   -0.847    0.397
##     gift_UG  (aug)   -0.006    0.002   -3.247    0.001
## 
## Variances:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     eWTR              0.028    0.001   22.450    0.000
##     grateful          0.779    0.035   22.450    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     indirect_dg       0.001    0.000    7.767    0.000
##     direct_dg         0.002    0.000    5.532    0.000
##     total_dg          0.003    0.000    9.440    0.000
##     indirect_pi       0.001    0.000    7.698    0.000
##     direct_pi         0.002    0.000    7.073    0.000
##     total_pi          0.003    0.000   10.915    0.000
##     indirect_ig       0.001    0.000    7.635    0.000
##     direct_ig         0.002    0.000    5.950    0.000
##     total_ig          0.003    0.000    9.672    0.000
##     indirect_ug       0.001    0.000    6.935    0.000
##     direct_ug         0.002    0.000    5.868    0.000
##     total_ug          0.003    0.000    8.760    0.000
##     mod_indirect_p   -0.000    0.000   -0.840    0.401
##     mod_direct_pi     0.000    0.000    1.095    0.274
##     mod_total_pi      0.000    0.000    0.797    0.426
##     mod_indirect_g   -0.000    0.000   -0.844    0.399
##     mod_direct_ig     0.000    0.000    0.174    0.862
##     mod_total_ig     -0.000    0.000   -0.085    0.933
##     mod_indirect_g   -0.000    0.000   -3.083    0.002
##     mod_direct_ug    -0.000    0.000   -0.213    0.832
##     mod_total_ug     -0.000    0.000   -1.164    0.244
## 
## [1] "surprised"
## lavaan (0.5-20) converged normally after  72 iterations
## 
##   Number of observations                          1008
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic                0.000
##   Degrees of freedom                                 0
##   Minimum Function Value               0.0000000000000
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##   eWTR ~                                              
##     gift       (c)    0.003    0.000    8.701    0.000
##     gift_PI  (cpi)    0.000    0.000    0.801    0.423
##     gift_IG  (cig)    0.000    0.000    0.025    0.980
##     gift_UG  (cug)   -0.000    0.000   -1.008    0.314
##     surprisd   (b)    0.018    0.006    3.197    0.001
##   surprised ~                                         
##     gift       (a)    0.011    0.002    6.189    0.000
##     gift_PI  (api)   -0.000    0.002   -0.004    0.997
##     gift_IG  (aig)   -0.002    0.002   -1.091    0.275
##     gift_UG  (aug)   -0.003    0.002   -1.598    0.110
## 
## Variances:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     eWTR              0.031    0.001   22.450    0.000
##     surprised         0.935    0.042   22.450    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     indirect_dg       0.000    0.000    2.841    0.005
##     direct_dg         0.003    0.000    8.701    0.000
##     total_dg          0.003    0.000    9.440    0.000
##     indirect_pi       0.000    0.000    2.872    0.004
##     direct_pi         0.003    0.000   10.101    0.000
##     total_pi          0.003    0.000   10.915    0.000
##     indirect_ig       0.000    0.000    2.713    0.007
##     direct_ig         0.003    0.000    9.087    0.000
##     total_ig          0.003    0.000    9.672    0.000
##     indirect_ug       0.000    0.000    2.654    0.008
##     direct_ug         0.003    0.000    8.233    0.000
##     total_ug          0.003    0.000    8.760    0.000
##     mod_indirect_p   -0.000    0.000   -0.004    0.997
##     mod_direct_pi     0.000    0.000    0.801    0.423
##     mod_total_pi      0.000    0.000    0.797    0.426
##     mod_indirect_g   -0.000    0.000   -1.032    0.302
##     mod_direct_ig     0.000    0.000    0.025    0.980
##     mod_total_ig     -0.000    0.000   -0.085    0.933
##     mod_indirect_g   -0.000    0.000   -1.430    0.153
##     mod_direct_ug    -0.000    0.000   -1.008    0.314
##     mod_total_ug     -0.000    0.000   -1.164    0.244
## 
## [1] "angry"
## lavaan (0.5-20) converged normally after  79 iterations
## 
##   Number of observations                          1008
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic                0.000
##   Degrees of freedom                                 0
##   Minimum Function Value               0.0000000000000
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##   eWTR ~                                              
##     gift       (c)    0.003    0.000    7.401    0.000
##     gift_PI  (cpi)    0.000    0.000    0.845    0.398
##     gift_IG  (cig)   -0.000    0.000   -0.023    0.982
##     gift_UG  (cug)   -0.000    0.000   -0.790    0.430
##     angry      (b)   -0.038    0.006   -6.495    0.000
##   angry ~                                             
##     gift       (a)   -0.016    0.002   -9.380    0.000
##     gift_PI  (api)    0.000    0.002    0.154    0.878
##     gift_IG  (aig)    0.001    0.002    0.310    0.757
##     gift_UG  (aug)    0.004    0.002    1.942    0.052
## 
## Variances:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     eWTR              0.030    0.001   22.450    0.000
##     angry             0.856    0.038   22.450    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     indirect_dg       0.001    0.000    5.340    0.000
##     direct_dg         0.003    0.000    7.401    0.000
##     total_dg          0.003    0.000    9.440    0.000
##     indirect_pi       0.001    0.000    5.400    0.000
##     direct_pi         0.003    0.000    8.752    0.000
##     total_pi          0.003    0.000   10.915    0.000
##     indirect_ig       0.001    0.000    5.333    0.000
##     direct_ig         0.003    0.000    7.637    0.000
##     total_ig          0.003    0.000    9.672    0.000
##     indirect_ug       0.000    0.000    4.986    0.000
##     direct_ug         0.002    0.000    7.138    0.000
##     total_ug          0.003    0.000    8.760    0.000
##     mod_indirect_p   -0.000    0.000   -0.154    0.878
##     mod_direct_pi     0.000    0.000    0.845    0.398
##     mod_total_pi      0.000    0.000    0.797    0.426
##     mod_indirect_g   -0.000    0.000   -0.309    0.757
##     mod_direct_ig    -0.000    0.000   -0.023    0.982
##     mod_total_ig     -0.000    0.000   -0.085    0.933
##     mod_indirect_g   -0.000    0.000   -1.860    0.063
##     mod_direct_ug    -0.000    0.000   -0.790    0.430
##     mod_total_ug     -0.000    0.000   -1.164    0.244
## 
## [1] "annoyed"
## lavaan (0.5-20) converged normally after  76 iterations
## 
##   Number of observations                          1008
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic                0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##   eWTR ~                                              
##     gift       (c)    0.002    0.000    6.297    0.000
##     gift_PI  (cpi)    0.000    0.000    0.904    0.366
##     gift_IG  (cig)    0.000    0.000    0.086    0.932
##     gift_UG  (cug)   -0.000    0.000   -0.604    0.546
##     annoyed    (b)   -0.050    0.006   -8.285    0.000
##   annoyed ~                                           
##     gift       (a)   -0.020    0.002  -11.677    0.000
##     gift_PI  (api)    0.001    0.002    0.307    0.759
##     gift_IG  (aig)    0.001    0.002    0.663    0.507
##     gift_UG  (aug)    0.004    0.002    2.290    0.022
## 
## Variances:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     eWTR              0.029    0.001   22.450    0.000
##     annoyed           0.798    0.036   22.450    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     indirect_dg       0.001    0.000    6.757    0.000
##     direct_dg         0.002    0.000    6.297    0.000
##     total_dg          0.003    0.000    9.440    0.000
##     indirect_pi       0.001    0.000    6.811    0.000
##     direct_pi         0.002    0.000    7.634    0.000
##     total_pi          0.003    0.000   10.915    0.000
##     indirect_ig       0.001    0.000    6.684    0.000
##     direct_ig         0.002    0.000    6.636    0.000
##     total_ig          0.003    0.000    9.672    0.000
##     indirect_ug       0.001    0.000    6.337    0.000
##     direct_ug         0.002    0.000    6.196    0.000
##     total_ug          0.003    0.000    8.760    0.000
##     mod_indirect_p   -0.000    0.000   -0.307    0.759
##     mod_direct_pi     0.000    0.000    0.904    0.366
##     mod_total_pi      0.000    0.000    0.797    0.426
##     mod_indirect_g   -0.000    0.000   -0.661    0.509
##     mod_direct_ig     0.000    0.000    0.086    0.932
##     mod_total_ig     -0.000    0.000   -0.085    0.933
##     mod_indirect_g   -0.000    0.000   -2.208    0.027
##     mod_direct_ug    -0.000    0.000   -0.604    0.546
##     mod_total_ug     -0.000    0.000   -1.164    0.244
## 
## [1] "disgust"
## lavaan (0.5-20) converged normally after  82 iterations
## 
##   Number of observations                          1008
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic                0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##   eWTR ~                                              
##     gift       (c)    0.002    0.000    7.146    0.000
##     gift_PI  (cpi)    0.000    0.000    0.898    0.369
##     gift_IG  (cig)    0.000    0.000    0.043    0.966
##     gift_UG  (cug)   -0.000    0.000   -0.732    0.464
##     disgust    (b)   -0.039    0.006   -6.524    0.000
##   disgust ~                                           
##     gift       (a)   -0.018    0.002  -10.328    0.000
##     gift_PI  (api)    0.001    0.002    0.411    0.681
##     gift_IG  (aig)    0.001    0.002    0.628    0.530
##     gift_UG  (aug)    0.004    0.002    2.213    0.027
## 
## Variances:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     eWTR              0.030    0.001   22.450    0.000
##     disgust           0.836    0.037   22.450    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     indirect_dg       0.001    0.000    5.516    0.000
##     direct_dg         0.002    0.000    7.146    0.000
##     total_dg          0.003    0.000    9.440    0.000
##     indirect_pi       0.001    0.000    5.529    0.000
##     direct_pi         0.003    0.000    8.554    0.000
##     total_pi          0.003    0.000   10.915    0.000
##     indirect_ig       0.001    0.000    5.457    0.000
##     direct_ig         0.002    0.000    7.470    0.000
##     total_ig          0.003    0.000    9.672    0.000
##     indirect_ug       0.001    0.000    5.171    0.000
##     direct_ug         0.002    0.000    6.957    0.000
##     total_ug          0.003    0.000    8.760    0.000
##     mod_indirect_p   -0.000    0.000   -0.410    0.682
##     mod_direct_pi     0.000    0.000    0.898    0.369
##     mod_total_pi      0.000    0.000    0.797    0.426
##     mod_indirect_g   -0.000    0.000   -0.625    0.532
##     mod_direct_ig     0.000    0.000    0.043    0.966
##     mod_total_ig     -0.000    0.000   -0.085    0.933
##     mod_indirect_g   -0.000    0.000   -2.095    0.036
##     mod_direct_ug    -0.000    0.000   -0.732    0.464
##     mod_total_ug     -0.000    0.000   -1.164    0.244