library(lavaan)
## This is lavaan 0.6-17
## lavaan is FREE software! Please report any bugs.
#library(semPlot)
library(seminr)
data("PoliticalDemocracy")
str(PoliticalDemocracy)
## 'data.frame':    75 obs. of  11 variables:
##  $ y1: num  2.5 1.25 7.5 8.9 10 7.5 7.5 7.5 2.5 10 ...
##  $ y2: num  0 0 8.8 8.8 3.33 ...
##  $ y3: num  3.33 3.33 10 10 10 ...
##  $ y4: num  0 0 9.2 9.2 6.67 ...
##  $ y5: num  1.25 6.25 8.75 8.91 7.5 ...
##  $ y6: num  0 1.1 8.09 8.13 3.33 ...
##  $ y7: num  3.73 6.67 10 10 10 ...
##  $ y8: num  3.333 0.737 8.212 4.615 6.667 ...
##  $ x1: num  4.44 5.38 5.96 6.29 5.86 ...
##  $ x2: num  3.64 5.06 6.26 7.57 6.82 ...
##  $ x3: num  2.56 3.57 5.22 6.27 4.57 ...
head(PoliticalDemocracy)

model

simple_mm<-constructs(composite("Dm1965",multi_items("x",1:3)),composite("Ind1966",multi_items("y",1:2)),composite("Ind1967",multi_items("y",3:4)),composite("Ind1968",multi_items("y",5:6)),composite("Ind1969",multi_items("y",7:8)))
#structural model
simple_sm<-relationships(paths(from="Ind1967",to=c("Ind1966","Ind1968")),paths(from="Ind1967",to="Dm1965"),paths(from=c("Ind1966","Ind1968"),to="Dm1965"))
#estimate
plot(simple_sm)

mediation analysis in data two

library(seminr)
#loading the data
corp_rep_data<-corp_rep_data
#measurement model
mm<-constructs(composite("QUAL",multi_items("qual_",1:8),weights=mode_B),composite("PERF",multi_items("perf_",1:5),weights=mode_B),composite("CSOR",multi_items("csor_",1:5),weights=mode_B),composite("ATTR",multi_items("attr_",1:3),weights=mode_B),composite("COMP",multi_items("comp_",1:3)),composite("LIKE",multi_items("like_",1:3)),composite("CUSA",single_item("cusa")),composite("CUSL",multi_items("cusl_",1:3)))
#structural model
sm<-relationships(paths(from=c("QUAL","PERF","CSOR","ATTR"),to=c("COMP","LIKE")),paths(from=c("COMP","LIKE"),to=c("CUSA","CUSL")),paths(from=c("CUSA"),to=c("CUSL")))
#estimate
estimate<-estimate_pls(data=corp_rep_data,measurement_model=mm,structural_model=sm,missing=mean_replacement,missing_value="-99")
## Generating the seminr model
## All 344 observations are valid.
#summary
summary<-summary(estimate)
summary
## 
## Results from  package seminr (2.3.2)
## 
## Path Coefficients:
##         COMP  LIKE  CUSA  CUSL
## R^2    0.631 0.558 0.292 0.562
## AdjR^2 0.627 0.552 0.288 0.558
## QUAL   0.430 0.380     .     .
## PERF   0.295 0.117     .     .
## CSOR   0.059 0.178     .     .
## ATTR   0.086 0.167     .     .
## COMP       .     . 0.146 0.006
## LIKE       .     . 0.436 0.344
## CUSA       .     .     . 0.505
## 
## Reliability:
##      alpha  rhoC   AVE  rhoA
## QUAL 0.878 0.894 0.518 1.000
## PERF 0.747 0.824 0.488 1.000
## CSOR 0.816 0.854 0.545 1.000
## ATTR 0.600 0.770 0.540 1.000
## COMP 0.776 0.869 0.688 0.786
## LIKE 0.831 0.899 0.747 0.836
## CUSA 1.000 1.000 1.000 1.000
## CUSL 0.831 0.899 0.748 0.839
## 
## Alpha, rhoC, and rhoA should exceed 0.7 while AVE should exceed 0.5
plot(estimate)
#to find if the paths are significant we are going to do bootstrapping
#bootstrap
boot<-bootstrap_model(seminr_model=estimate,nboot=1000,cores=parallel::detectCores(),seed=123)
## Bootstrapping model using seminr...
## SEMinR Model successfully bootstrapped
#summary
boot_summary<-summary(boot,alpha=0.05)
#inspect total indirect effects
boot_summary$total_indirect_effects
## NULL
#inspect indirect effects
specific_effect_significance(boot,from="COMP",through="CUSA",to="CUSL",alpha=0.05)
##  Original Est. Bootstrap Mean   Bootstrap SD        T Stat.        2.5% CI 
##     0.07350093     0.07443445     0.03651814     2.01272396     0.00296709 
##       97.5% CI 
##     0.14620781
specific_effect_significance(boot,from="LIKE",through="CUSA",to="CUSL",alpha=0.05)
##  Original Est. Bootstrap Mean   Bootstrap SD        T Stat.        2.5% CI 
##     0.22001302     0.21993527     0.03687955     5.96571802     0.14917504 
##       97.5% CI 
##     0.29318977
#inspect the direct effects
summary$paths
##         COMP  LIKE  CUSA  CUSL
## R^2    0.631 0.558 0.292 0.562
## AdjR^2 0.627 0.552 0.288 0.558
## QUAL   0.430 0.380     .     .
## PERF   0.295 0.117     .     .
## CSOR   0.059 0.178     .     .
## ATTR   0.086 0.167     .     .
## COMP       .     . 0.146 0.006
## LIKE       .     . 0.436 0.344
## CUSA       .     .     . 0.505
#inspect the confidence intervals for direct effects
boot_summary$bootstrapped_paths
##                Original Est. Bootstrap Mean Bootstrap SD T Stat. 2.5% CI
## QUAL  ->  COMP         0.430          0.431        0.065   6.603   0.303
## QUAL  ->  LIKE         0.380          0.384        0.067   5.699   0.253
## PERF  ->  COMP         0.295          0.301        0.064   4.611   0.173
## PERF  ->  LIKE         0.117          0.123        0.073   1.613  -0.011
## CSOR  ->  COMP         0.059          0.059        0.054   1.084  -0.044
## CSOR  ->  LIKE         0.178          0.177        0.056   3.205   0.065
## ATTR  ->  COMP         0.086          0.084        0.055   1.565  -0.018
## ATTR  ->  LIKE         0.167          0.165        0.065   2.573   0.034
## COMP  ->  CUSA         0.146          0.147        0.071   2.047   0.007
## COMP  ->  CUSL         0.006          0.006        0.055   0.104  -0.104
## LIKE  ->  CUSA         0.436          0.435        0.062   7.069   0.312
## LIKE  ->  CUSL         0.344          0.343        0.056   6.175   0.231
## CUSA  ->  CUSL         0.505          0.505        0.042  12.074   0.420
##                97.5% CI
## QUAL  ->  COMP    0.552
## QUAL  ->  LIKE    0.514
## PERF  ->  COMP    0.422
## PERF  ->  LIKE    0.261
## CSOR  ->  COMP    0.165
## CSOR  ->  LIKE    0.282
## ATTR  ->  COMP    0.194
## ATTR  ->  LIKE    0.291
## COMP  ->  CUSA    0.281
## COMP  ->  CUSL    0.115
## LIKE  ->  CUSA    0.555
## LIKE  ->  CUSL    0.449
## CUSA  ->  CUSL    0.586
#all the paths are statistically significant,they all exceeded 1.96 mark
#calculate the sign of p1*p2*p3
summary$paths["LIKE","CUSL"]*summary$paths["LIKE","CUSA"]*summary$paths["CUSA","CUSL"]
## [1] 0.07569007
summary$paths["COMP","CUSL"]*summary$paths["COMP","CUSA"]*summary$paths["CUSA","CUSL"]
## [1] 0.0004163559