library(flextable)
library(DiagrammeR)

library(lavaan)

library(tidyverse)

At this point, I will construct an overall mediation model.

Diagram

Let’s take a look at the boxes-and-arrows.

grViz("

digraph mediation {
  
  graph [overlap = true,
         rankdir = LR,
         bgcolor = '#222222']
  
  node [shape = box,
        color = wheat,
        fontcolor = wheat]
        
  edge [color = wheat,
        fontcolor = wheat]
  
  disclosure->'affective trust' [label = <a<SUB>aff</SUB>>]
  disclosure->liking [label = 'c&prime;']
  disclosure->'cognitive trust' [label = <a<SUB>cog</SUB>>]
  'affective trust'->liking [label = <b<SUB>aff</SUB>>]
  'cognitive trust'->liking [label = <b<SUB>cog</SUB>>]
  
}
")

Statistical Models

Now, I’ll build the actual data model. My understanding is that I can follow the classic procedure, but control for cognitive trust when testing affective trust—and vice versa.

data <- readRDS(file.path("..", "data", "hq-data.rds"))
formatAsTable <- readRDS("format.rds")

Mediation by Affective Trust

model.aff <- "# measurement model
              aff =~ aff1 + aff2 + aff3 + aff4
              lik =~ lik1 + lik2 + lik3.r + lik4.r

              # structural model
              aff ~ affa*disclose
              lik ~ affb*aff
              lik ~ cprime*disclose
              ind_fx := affa*affb
              tot_fx := affa*affb + cprime"

fit.aff <- model.aff %>%
  sem(data)

fit.aff %>%
  parameterEstimates %>%
  filter(op %in% c("~", ":=")) %>%
  formatAsTable

\(c\prime\) goes to zero…looks like full mediation!

Mediation by Cognitive Trust

model.cog <- "# measurement model
              cog =~ cog1 + cog2 + cog3
              lik =~ lik1 + lik2 + lik3.r + lik4.r

              # structural model
              cog ~ coga*disclose
              lik ~ cogb*cog
              lik ~ cprime*disclose
              ind_fx := coga*cogb
              tot_fx := coga*cogb + cprime"

fit.cog <- model.cog %>%
  sem(data)

fit.cog %>%
  parameterEstimates %>%
  filter(op %in% c("~", ":=")) %>%
  formatAsTable

\(c\prime\) doesn’t go to zero, so we can’t say we have full mediation. But \(a_{cog}\) and \(b_{cog}\) are significant, so I think that means partial mediation.

Mediation by Both

model.tru <- "# measurement model
              aff =~ aff1 + aff2 + aff3 + aff4
              cog =~ cog1 + cog2 + cog3
              lik =~ lik1 + lik2 + lik3.r + lik4.r

              # structural model
              aff ~ affa*disclose
              cog ~ coga*disclose
              lik ~ affb*aff + cogb*cog
              lik ~ cprime*disclose
              ind_fx := affa*affb + coga*cogb
              tot_fx := affa*affb + coga*cogb + cprime"

fit.tru <- model.tru %>%
  sem(data)

fit.tru %>%
  parameterEstimates %>%
  filter(op %in% c("~", ":=")) %>%
  formatAsTable

That’s interesting! Trust doesn’t fully mediate the effect of disclosure on liking—it inconsistently mediates it. That is, disclosure has a significantly negative effect on liking when controlling for trust.

I’m not sure what would explain that. Maybe something like my hypothesis (that disclosure calls attention to negative aspects of ADHD) is true, but cognitive trust was the wrong construct to use. Or it could be that disclosure makes someone trustworthy, but otherwise makes them look bad (annoying, poor social skills, oversharing, that kind of thing). There’s no way to know for sure without more studies.

Also, as predicted, affective trust is a stronger mediator than cognitive trust.

Mediation by Combined Trust

model.com <- "# measurement model
              tru =~ aff1 + aff2 + aff3 + aff4 + cog1 + cog2 + cog3
              lik =~ lik1 + lik2 + lik3.r + lik4.r

              # structural model
              tru ~ a*disclose
              lik ~ b*tru
              lik ~ cprime*disclose
              ind_fx := a*b
              tot_fx := a*b + cprime"

fit.com <- model.com %>%
  sem(data)

fit.com %>%
  parameterEstimates %>%
  formatAsTable
model.efa <- 'tru1 =~ aff1 + aff2 + 0*aff3 + aff4 + cog1 + cog2 + cog3 + cog4.r
              tru2 =~ aff1 + aff2 + aff3 + aff4 + cog1 + 0*cog2 + cog3 + cog4.r'

fit.efa <- model.efa %>%
  cfa(data, std.lv=T, std.ov=T)

fit.efa %>%
  summary
## lavaan 0.6-8 ended normally after 32 iterations
## 
##   Estimator                                       DWLS
##   Optimization method                           NLMINB
##   Number of model parameters                        47
##                                                       
##   Number of observations                           403
##                                                       
## Model Test User Model:
##                                               Standard      Robust
##   Test Statistic                                 6.570      22.623
##   Degrees of freedom                                13          13
##   P-value (Chi-square)                           0.923       0.046
##   Scaling correction factor                                  0.310
##   Shift parameter                                            1.417
##        simple second-order correction                             
## 
## Parameter Estimates:
## 
##   Standard errors                           Robust.sem
##   Information                                 Expected
##   Information saturated (h1) model        Unstructured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   tru1 =~                                             
##     aff1             -0.188    0.050   -3.773    0.000
##     aff2             -0.012    0.045   -0.256    0.798
##     aff3              0.000                           
##     aff4              0.047    0.047    1.018    0.309
##     cog1              0.456    0.048    9.412    0.000
##     cog2              0.954    0.045   21.214    0.000
##     cog3              0.327    0.045    7.274    0.000
##     cog4.r            0.455    0.056    8.190    0.000
##   tru2 =~                                             
##     aff1              0.900    0.028   32.287    0.000
##     aff2              0.840    0.025   34.002    0.000
##     aff3              0.904    0.012   74.995    0.000
##     aff4              0.804    0.026   30.657    0.000
##     cog1              0.468    0.042   11.153    0.000
##     cog2              0.000                           
##     cog3              0.295    0.044    6.679    0.000
##     cog4.r           -0.250    0.053   -4.695    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   tru1 ~~                                             
##     tru2              0.442    0.052    8.424    0.000
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .aff1              0.000                           
##    .aff2              0.000                           
##    .aff3              0.000                           
##    .aff4              0.000                           
##    .cog1              0.000                           
##    .cog2              0.000                           
##    .cog3              0.000                           
##    .cog4.r            0.000                           
##     tru1              0.000                           
##     tru2              0.000                           
## 
## Thresholds:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     aff1|t1          -1.231    0.083  -14.800    0.000
##     aff1|t2          -0.803    0.070  -11.406    0.000
##     aff1|t3          -0.134    0.063   -2.139    0.032
##     aff1|t4           0.829    0.071   11.683    0.000
##     aff2|t1          -1.726    0.111  -15.488    0.000
##     aff2|t2          -1.143    0.080  -14.310    0.000
##     aff2|t3          -0.333    0.064   -5.214    0.000
##     aff2|t4           0.753    0.069   10.848    0.000
##     aff3|t1          -1.726    0.111  -15.488    0.000
##     aff3|t2          -0.959    0.074  -12.932    0.000
##     aff3|t3          -0.172    0.063   -2.735    0.006
##     aff3|t4           0.856    0.072   11.956    0.000
##     aff4|t1          -1.884    0.125  -15.029    0.000
##     aff4|t2          -1.119    0.079  -14.160    0.000
##     aff4|t3          -0.103    0.063   -1.642    0.101
##     aff4|t4           0.847    0.071   11.865    0.000
##     cog1|t1          -2.435    0.208  -11.686    0.000
##     cog1|t2          -1.480    0.095  -15.576    0.000
##     cog1|t3          -0.359    0.064   -5.610    0.000
##     cog1|t4           0.704    0.068   10.281    0.000
##     cog2|t1          -1.884    0.125  -15.029    0.000
##     cog2|t2          -0.874    0.072  -12.137    0.000
##     cog2|t3          -0.326    0.064   -5.115    0.000
##     cog2|t4           0.673    0.068    9.901    0.000
##     cog3|t1          -1.518    0.097  -15.617    0.000
##     cog3|t2          -0.642    0.067   -9.518    0.000
##     cog3|t3           0.140    0.063    2.238    0.025
##     cog3|t4           1.041    0.077   13.606    0.000
##     cog4.r|t1        -1.131    0.079  -14.235    0.000
##     cog4.r|t2        -0.009    0.063   -0.149    0.881
##     cog4.r|t3         0.560    0.066    8.455    0.000
##     cog4.r|t4         1.480    0.095   15.576    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .aff1              0.305                           
##    .aff2              0.302                           
##    .aff3              0.183                           
##    .aff4              0.317                           
##    .cog1              0.385                           
##    .cog2              0.091                           
##    .cog3              0.721                           
##    .cog4.r            0.831                           
##     tru1              1.000                           
##     tru2              1.000                           
## 
## Scales y*:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     aff1              1.000                           
##     aff2              1.000                           
##     aff3              1.000                           
##     aff4              1.000                           
##     cog1              1.000                           
##     cog2              1.000                           
##     cog3              1.000                           
##     cog4.r            1.000
model.cor <- "aff =~ aff1 + aff2 + aff3 + aff4
              cog =~ cog1 + cog2 + cog3 + cog4
              aff ~ cog"

fit.cor <- model.cor %>%
  cfa(data)

fit.cor %>%
  standardizedSolution
##     lhs  op  rhs est.std    se       z pvalue ci.lower ci.upper
## 1   aff  =~ aff1   0.795 0.022  36.076  0.000    0.752    0.838
## 2   aff  =~ aff2   0.839 0.017  49.129  0.000    0.805    0.872
## 3   aff  =~ aff3   0.907 0.012  72.631  0.000    0.883    0.932
## 4   aff  =~ aff4   0.834 0.016  50.642  0.000    0.801    0.866
## 5   cog  =~ cog1   0.930 0.028  33.291  0.000    0.875    0.985
## 6   cog  =~ cog2   0.660 0.036  18.199  0.000    0.589    0.731
## 7   cog  =~ cog3   0.575 0.041  13.967  0.000    0.494    0.655
## 8   cog  =~ cog4  -0.107 0.049  -2.190  0.029   -0.203   -0.011
## 9   aff   ~  cog   0.687 0.033  20.663  0.000    0.622    0.752
## 10 aff1   |   t1  -1.231 0.083 -14.800  0.000   -1.394   -1.068
## 11 aff1   |   t2  -0.803 0.070 -11.406  0.000   -0.941   -0.665
## 12 aff1   |   t3  -0.134 0.063  -2.139  0.032   -0.257   -0.011
## 13 aff1   |   t4   0.829 0.071  11.683  0.000    0.690    0.968
## 14 aff2   |   t1  -1.726 0.111 -15.488  0.000   -1.944   -1.507
## 15 aff2   |   t2  -1.143 0.080 -14.310  0.000   -1.299   -0.986
## 16 aff2   |   t3  -0.333 0.064  -5.214  0.000   -0.458   -0.208
## 17 aff2   |   t4   0.753 0.069  10.848  0.000    0.617    0.889
## 18 aff3   |   t1  -1.726 0.111 -15.488  0.000   -1.944   -1.507
## 19 aff3   |   t2  -0.959 0.074 -12.932  0.000   -1.105   -0.814
## 20 aff3   |   t3  -0.172 0.063  -2.735  0.006   -0.295   -0.049
## 21 aff3   |   t4   0.856 0.072  11.956  0.000    0.716    0.996
## 22 aff4   |   t1  -1.884 0.125 -15.029  0.000   -2.130   -1.638
## 23 aff4   |   t2  -1.119 0.079 -14.160  0.000   -1.274   -0.964
## 24 aff4   |   t3  -0.103 0.063  -1.642  0.101   -0.226    0.020
## 25 aff4   |   t4   0.847 0.071  11.865  0.000    0.707    0.987
## 26 cog1   |   t1  -2.435 0.208 -11.686  0.000   -2.844   -2.027
## 27 cog1   |   t2  -1.480 0.095 -15.576  0.000   -1.666   -1.293
## 28 cog1   |   t3  -0.359 0.064  -5.610  0.000   -0.484   -0.234
## 29 cog1   |   t4   0.704 0.068  10.281  0.000    0.570    0.838
## 30 cog2   |   t1  -1.884 0.125 -15.029  0.000   -2.130   -1.638
## 31 cog2   |   t2  -0.874 0.072 -12.137  0.000   -1.015   -0.733
## 32 cog2   |   t3  -0.326 0.064  -5.115  0.000   -0.451   -0.201
## 33 cog2   |   t4   0.673 0.068   9.901  0.000    0.539    0.806
## 34 cog3   |   t1  -1.518 0.097 -15.617  0.000   -1.708   -1.327
## 35 cog3   |   t2  -0.642 0.067  -9.518  0.000   -0.774   -0.510
## 36 cog3   |   t3   0.140 0.063   2.238  0.025    0.017    0.263
## 37 cog3   |   t4   1.041 0.077  13.606  0.000    0.891    1.191
## 38 cog4   |   t1  -1.480 0.095 -15.576  0.000   -1.666   -1.293
## 39 cog4   |   t2  -0.560 0.066  -8.455  0.000   -0.689   -0.430
## 40 cog4   |   t3   0.009 0.063   0.149  0.881   -0.113    0.132
## 41 cog4   |   t4   1.131 0.079  14.235  0.000    0.975    1.287
## 42 aff1  ~~ aff1   0.368 0.035  10.485  0.000    0.299    0.436
## 43 aff2  ~~ aff2   0.297 0.029  10.354  0.000    0.240    0.353
## 44 aff3  ~~ aff3   0.177 0.023   7.789  0.000    0.132    0.221
## 45 aff4  ~~ aff4   0.305 0.027  11.113  0.000    0.251    0.359
## 46 cog1  ~~ cog1   0.135 0.052   2.590  0.010    0.033    0.237
## 47 cog2  ~~ cog2   0.564 0.048  11.777  0.000    0.470    0.658
## 48 cog3  ~~ cog3   0.670 0.047  14.174  0.000    0.577    0.763
## 49 cog4  ~~ cog4   0.989 0.010  94.258  0.000    0.968    1.009
## 50  aff  ~~  aff   0.528 0.046  11.553  0.000    0.438    0.617
## 51  cog  ~~  cog   1.000 0.000      NA     NA    1.000    1.000
## 52 aff1 ~*~ aff1   1.000 0.000      NA     NA    1.000    1.000
## 53 aff2 ~*~ aff2   1.000 0.000      NA     NA    1.000    1.000
## 54 aff3 ~*~ aff3   1.000 0.000      NA     NA    1.000    1.000
## 55 aff4 ~*~ aff4   1.000 0.000      NA     NA    1.000    1.000
## 56 cog1 ~*~ cog1   1.000 0.000      NA     NA    1.000    1.000
## 57 cog2 ~*~ cog2   1.000 0.000      NA     NA    1.000    1.000
## 58 cog3 ~*~ cog3   1.000 0.000      NA     NA    1.000    1.000
## 59 cog4 ~*~ cog4   1.000 0.000      NA     NA    1.000    1.000
## 60 aff1  ~1        0.000 0.000      NA     NA    0.000    0.000
## 61 aff2  ~1        0.000 0.000      NA     NA    0.000    0.000
## 62 aff3  ~1        0.000 0.000      NA     NA    0.000    0.000
## 63 aff4  ~1        0.000 0.000      NA     NA    0.000    0.000
## 64 cog1  ~1        0.000 0.000      NA     NA    0.000    0.000
## 65 cog2  ~1        0.000 0.000      NA     NA    0.000    0.000
## 66 cog3  ~1        0.000 0.000      NA     NA    0.000    0.000
## 67 cog4  ~1        0.000 0.000      NA     NA    0.000    0.000
## 68  aff  ~1        0.000 0.000      NA     NA    0.000    0.000
## 69  cog  ~1        0.000 0.000      NA     NA    0.000    0.000
model.cfa <- "aff =~ aff1 + aff2 + aff3 + aff4
              cog =~ cog1 + cog2 + cog3 + cog4.r
              lik =~ lik1 + lik2 + lik3.r + lik4.r"

fit.cfa <- model.cfa %>%
  cfa(data)

data <- fit.cfa %>%
  lavPredict %>%
  as_tibble %>%
  mutate(id = row_number()) %>%
  inner_join(data, "id")
data %>%
  saveRDS(file.path("..", "data", "pred-data.rds"))
fit.vif <- lm(lik ~ disclose + aff + cog, data)

library(car)

fit.vif %>%
  vif
## disclose      aff      cog 
## 1.001949 2.538526 2.539831

Output document:

options(knitr.duplicate.label = "allow")
rmarkdown::render("mediation.Rmd",
                  output_dir = file.path("..", "github", "thesis"))