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
  indirect  := a*b
direct := c
total := c + a*b
  '

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


  fit1  <- sem(model, data = sub_temp)
  
  print(Emotion_name[i])
  summary (fit1)
}
## [1] "happy"
## lavaan (0.5-20) converged normally after  25 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    7.966    0.000
##     happy      (b)    0.054    0.006    8.821    0.000
##   happy ~                                             
##     gift       (a)    0.020    0.001   17.721    0.000
## 
## Variances:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     eWTR              0.029    0.001   22.450    0.000
##     happy             0.759    0.034   22.450    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     indirect          0.001    0.000    7.897    0.000
##     direct            0.002    0.000    7.966    0.000
##     total             0.003    0.000   13.533    0.000
## 
## [1] "grateful"
## lavaan (0.5-20) converged normally after  23 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    8.089    0.000
##     grateful   (b)    0.059    0.006    9.942    0.000
##   grateful ~                                          
##     gift       (a)    0.019    0.001   16.263    0.000
## 
## Variances:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     eWTR              0.028    0.001   22.450    0.000
##     grateful          0.789    0.035   22.450    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     indirect          0.001    0.000    8.483    0.000
##     direct            0.002    0.000    8.089    0.000
##     total             0.003    0.000   13.533    0.000
## 
## [1] "surprised"
## lavaan (0.5-20) converged normally after  20 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   12.413    0.000
##     surprised  (b)    0.019    0.006    3.297    0.001
##   surprised ~                                         
##     gift       (a)    0.010    0.001    7.869    0.000
## 
## Variances:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     eWTR              0.031    0.001   22.450    0.000
##     surprised         0.938    0.042   22.450    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     indirect          0.000    0.000    3.041    0.002
##     direct            0.003    0.000   12.413    0.000
##     total             0.003    0.000   13.533    0.000
## 
## [1] "angry"
## lavaan (0.5-20) converged normally after  22 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.003    0.000   10.413    0.000
##     angry      (b)   -0.039    0.006   -6.601    0.000
##   angry ~                                             
##     gift       (a)   -0.015    0.001  -12.600    0.000
## 
## 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|)
##     indirect          0.001    0.000    5.847    0.000
##     direct            0.003    0.000   10.413    0.000
##     total             0.003    0.000   13.533    0.000
## 
## [1] "annoyed"
## lavaan (0.5-20) converged normally after  21 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.873    0.000
##     annoyed    (b)   -0.050    0.006   -8.397    0.000
##   annoyed ~                                           
##     gift       (a)   -0.018    0.001  -15.566    0.000
## 
## 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|)
##     indirect          0.001    0.000    7.390    0.000
##     direct            0.002    0.000    8.873    0.000
##     total             0.003    0.000   13.533    0.000
## 
## [1] "disgust"
## lavaan (0.5-20) converged normally after  24 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   10.093    0.000
##     disgust    (b)   -0.039    0.006   -6.631    0.000
##   disgust ~                                           
##     gift       (a)   -0.016    0.001  -13.615    0.000
## 
## 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|)
##     indirect          0.001    0.000    5.962    0.000
##     direct            0.002    0.000   10.093    0.000
##     total             0.003    0.000   13.533    0.000