1. Load data
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_moderated_by_deciders.csv")

2.Set variables

  1. Run SEM model interacted by Deciders/Recipients
for (i in 1 : length(Emotion_name)){
  model1 = out_model[i]
  model2 = med_model[i]
  model3 = '
  # effets
  indirect0  := a*b
  direct0 := c
  total0 := direct0 + indirect0
  
  indirect1  := a*b + aw*b
  direct1 := c + cw
  total1 := direct1 + indirect1
  
  mod_indirect := indirect1 - indirect0
  mod_direct := direct1 - direct0
  mod_total := total1 - total0
  '

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


  fit2  <- sem(model, data = sub_temp)
  
  print(Emotion_name[i])
  summary (fit2)
}
## [1] "happy"
## lavaan (0.5-20) converged normally after  36 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    6.942    0.000
##     gft_dcdrs (cw)    0.000    0.000    0.218    0.828
##     happy      (b)    0.054    0.006    8.814    0.000
##   happy ~                                             
##     gift       (a)    0.021    0.001   16.629    0.000
##     gft_dcdrs (aw)   -0.003    0.001   -2.330    0.020
## 
## Variances:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     eWTR              0.029    0.001   22.450    0.000
##     happy             0.755    0.034   22.450    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     indirect0         0.001    0.000    7.788    0.000
##     direct0           0.002    0.000    6.942    0.000
##     total0            0.003    0.000   11.999    0.000
##     indirect1         0.001    0.000    7.510    0.000
##     direct1           0.002    0.000    7.336    0.000
##     total1            0.003    0.000   11.594    0.000
##     mod_indirect     -0.000    0.000   -2.253    0.024
##     mod_direct        0.000    0.000    0.218    0.828
##     mod_total        -0.000    0.000   -0.413    0.680
## 
## [1] "grateful"
## lavaan (0.5-20) converged normally after  44 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.489    0.000
##     gft_dcdrs (cw)    0.000    0.000    1.059    0.289
##     grateful   (b)    0.060    0.006    9.995    0.000
##   grateful ~                                          
##     gift       (a)    0.022    0.001   16.676    0.000
##     gft_dcdrs (aw)   -0.006    0.001   -4.778    0.000
## 
## Variances:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     eWTR              0.028    0.001   22.450    0.000
##     grateful          0.772    0.034   22.450    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     indirect0         0.001    0.000    8.573    0.000
##     direct0           0.002    0.000    6.489    0.000
##     total0            0.003    0.000   11.999    0.000
##     indirect1         0.001    0.000    7.677    0.000
##     direct1           0.002    0.000    7.839    0.000
##     total1            0.003    0.000   11.594    0.000
##     mod_indirect     -0.000    0.000   -4.311    0.000
##     mod_direct        0.000    0.000    1.059    0.289
##     mod_total        -0.000    0.000   -0.413    0.680
## 
## [1] "surprised"
## lavaan (0.5-20) converged normally after  44 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   10.810    0.000
##     gft_dcdrs (cw)   -0.000    0.000   -0.082    0.935
##     surprised  (b)    0.019    0.006    3.272    0.001
##   surprised ~                                         
##     gift       (a)    0.012    0.001    8.478    0.000
##     gft_dcdrs (aw)   -0.005    0.001   -3.231    0.001
## 
## Variances:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     eWTR              0.031    0.001   22.450    0.000
##     surprised         0.929    0.041   22.450    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     indirect0         0.000    0.000    3.053    0.002
##     direct0           0.003    0.000   10.810    0.000
##     total0            0.003    0.000   11.999    0.000
##     indirect1         0.000    0.000    2.786    0.005
##     direct1           0.003    0.000   10.956    0.000
##     total1            0.003    0.000   11.594    0.000
##     mod_indirect     -0.000    0.000   -2.299    0.022
##     mod_direct       -0.000    0.000   -0.082    0.935
##     mod_total        -0.000    0.000   -0.413    0.680
## 
## [1] "angry"
## lavaan (0.5-20) converged normally after  44 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    9.290    0.000
##     gft_dcdrs (cw)   -0.000    0.000   -0.223    0.824
##     angry      (b)   -0.039    0.006   -6.592    0.000
##   angry ~                                             
##     gift       (a)   -0.016    0.001  -11.457    0.000
##     gft_dcdrs (aw)    0.001    0.001    0.959    0.338
## 
## Variances:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     eWTR              0.030    0.001   22.450    0.000
##     angry             0.860    0.038   22.450    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     indirect0         0.001    0.000    5.714    0.000
##     direct0           0.003    0.000    9.290    0.000
##     total0            0.003    0.000   11.999    0.000
##     indirect1         0.001    0.000    5.585    0.000
##     direct1           0.002    0.000    9.168    0.000
##     total1            0.003    0.000   11.594    0.000
##     mod_indirect     -0.000    0.000   -0.949    0.343
##     mod_direct       -0.000    0.000   -0.223    0.824
##     mod_total        -0.000    0.000   -0.413    0.680
## 
## [1] "annoyed"
## lavaan (0.5-20) converged normally after  44 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    8.024    0.000
##     gft_dcdrs (cw)   -0.000    0.000   -0.282    0.778
##     annoyed    (b)   -0.050    0.006   -8.391    0.000
##   annoyed ~                                           
##     gift       (a)   -0.018    0.001  -13.839    0.000
##     gft_dcdrs (aw)    0.001    0.001    0.550    0.583
## 
## Variances:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     eWTR              0.029    0.001   22.450    0.000
##     annoyed           0.803    0.036   22.450    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     indirect0         0.001    0.000    7.175    0.000
##     direct0           0.002    0.000    8.024    0.000
##     total0            0.003    0.000   11.999    0.000
##     indirect1         0.001    0.000    7.097    0.000
##     direct1           0.002    0.000    7.819    0.000
##     total1            0.003    0.000   11.594    0.000
##     mod_indirect     -0.000    0.000   -0.548    0.583
##     mod_direct       -0.000    0.000   -0.282    0.778
##     mod_total        -0.000    0.000   -0.413    0.680
## 
## [1] "disgust"
## lavaan (0.5-20) converged normally after  41 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    9.088    0.000
##     gft_dcdrs (cw)   -0.000    0.000   -0.313    0.754
##     disgust    (b)   -0.039    0.006   -6.625    0.000
##   disgust ~                                           
##     gift       (a)   -0.016    0.001  -12.123    0.000
##     gft_dcdrs (aw)    0.001    0.001    0.520    0.603
## 
## Variances:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     eWTR              0.030    0.001   22.450    0.000
##     disgust           0.841    0.037   22.450    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     indirect0         0.001    0.000    5.814    0.000
##     direct0           0.002    0.000    9.088    0.000
##     total0            0.003    0.000   11.999    0.000
##     indirect1         0.001    0.000    5.755    0.000
##     direct1           0.002    0.000    8.847    0.000
##     total1            0.003    0.000   11.594    0.000
##     mod_indirect     -0.000    0.000   -0.519    0.604
##     mod_direct       -0.000    0.000   -0.313    0.754
##     mod_total        -0.000    0.000   -0.413    0.680