#loading packages
library(haven) #for importing stata files
library(ggplot2); theme_set(theme_bw()); library(grid) #for data viz
library(lme4) #for multilevel models
## Loading required package: Matrix
library(nlme) #for multilevel models
##
## Attaching package: 'nlme'
## The following object is masked from 'package:lme4':
##
## lmList
library(psych) #for describing data
##
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
library(tidyr); library(dplyr) #for manipulating data
##
## Attaching package: 'tidyr'
## The following objects are masked from 'package:Matrix':
##
## expand, pack, unpack
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:nlme':
##
## collapse
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(broom) #for converting results summary to df
library(forcats) #for factor vars
library(sjPlot) #creates beautiful tables
Variable key:
Example where everything appears to run normally:
################################
# 3 MONTHS POST STIM (MONTH 4) #
################################
#loading in data (already cleaned for mediation w/ 2 rows pp)
data <- read_stata("/Users/samanthasteimle/Library/CloudStorage/Box-Box/Power Packs/Analyses/Mediation/pp_mediation_month4.dta")
#mediation model
model_lme <- lme(fixed = z_pdep_cmis_4 ~ -1 +
dm + dm:poststim_dev_4 + #m as outcome
dy + dy:pdep_dev_4 + dy:poststim_dev_4 , #y as outcome
random = ~ -1 + dm:poststim_dev_4 + dy:pdep_dev_4 + dy:poststim_dev_4 | res_id,
weights = varIdent(form = ~ 1 | dm), #separate sigma^{2}_{e} for each outcome
data = data,
na.action = na.exclude,
control = lmeControl(opt = "optim", maxIter = 200, msMaxIter = 200, niterEM = 50, msMaxEval = 400))
summary(model_lme)
## Linear mixed-effects model fit by REML
## Data: data
## AIC BIC logLik
## 12628.78 12723.24 -6301.389
##
## Random effects:
## Formula: ~-1 + dm:poststim_dev_4 + dy:pdep_dev_4 + dy:poststim_dev_4 | res_id
## Structure: General positive-definite, Log-Cholesky parametrization
## StdDev Corr
## dm:poststim_dev_4 0.2131552 dm:__4 dy:__4
## dy:pdep_dev_4 0.1714188 -0.100
## poststim_dev_4:dy 0.2095951 0.094 0.568
## Residual 0.4686991
##
## Variance function:
## Structure: Different standard deviations per stratum
## Formula: ~1 | dm
## Parameter estimates:
## 0 1
## 1.000000 0.847219
## Fixed effects: z_pdep_cmis_4 ~ -1 + dm + dm:poststim_dev_4 + dy + dy:pdep_dev_4 + dy:poststim_dev_4
## Value Std.Error DF t-value p-value
## dm 0.00252712 0.005712404 10432 0.4423909 0.6582
## dy 0.00148212 0.006716053 10432 0.2206825 0.8253
## dm:poststim_dev_4 -0.05482268 0.022253497 10432 -2.4635535 0.0138
## dy:pdep_dev_4 0.06770067 0.023387424 10432 2.8947465 0.0038
## poststim_dev_4:dy -0.01178432 0.023622402 10432 -0.4988620 0.6179
## Correlation:
## dm dy dm:__4 dy:__4
## dy 0.003
## dm:poststim_dev_4 -0.137 -0.002
## dy:pdep_dev_4 0.000 -0.005 -0.050
## poststim_dev_4:dy -0.003 -0.145 0.058 0.290
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -6.06733777 -0.03713039 0.05722108 0.37449218 4.67261828
##
## Number of Observations: 10582
## Number of Groups: 146
tab_model(model_lme, show.se= TRUE, digits=3,digits.re=3, show.ci = FALSE)
|
Combined var where each duplicate obs = M (pdep_dev) or = Y (cmis_dev) |
|||
|---|---|---|---|
| Predictors | Estimates | std. Error | p |
|
Dummy: Z = M (parent WB dev) |
0.003 | 0.006 | 0.658 |
|
Dummy: Z = Y (child WB dev) |
0.001 | 0.007 | 0.825 |
| dm:poststim_dev_4 | -0.055 | 0.022 | 0.014 |
| dy:pdep_dev_4 | 0.068 | 0.023 | 0.004 |
| poststim_dev_4:dy | -0.012 | 0.024 | 0.618 |
| Random Effects | |||
| σ2 | 0.220 | ||
| τ00 | |||
| τ00 | |||
| τ11 res_id.dy:pdep_dev_4 | 0.029 | ||
| τ11 res_id.poststim_dev_4:dy | 0.044 | ||
| ρ01 | -0.100 | ||
| 0.094 | |||
| ICC | 0.047 | ||
| N res_id | 146 | ||
| Observations | 10582 | ||
| Marginal R2 / Conditional R2 | 0.003 / 0.050 | ||
### Fixed Effects
#pulling out fixed effects info
FE <- fixef(model_lme)
FE
## dm dy dm:poststim_dev_4 dy:pdep_dev_4
## 0.002527116 0.001482115 -0.054822679 0.067700666
## poststim_dev_4:dy
## -0.011784319
#dm = intercept in m model, mean of m when all vars = 0 (should be 0 or close to 0 b/c variables should be person-centered)
#dy = intercept in y model, mean of y when all vars = 0 (should be 0 or close to 0 b/c variables should be person-centered)
#dm:poststim = effect of poststim (x) on parent WB (m) = a
#dy:pdep = effect of parent WB (m) on child WB (y) = b
#poststim:dy = effect of poststim (x) on child WB (y) = c'
# Let's put the parameters into named objects, as these will be useful later.
a <- as.numeric(FE[3])
a
## [1] -0.05482268
b <- as.numeric(FE[4])
b
## [1] 0.06770067
cprime <- as.numeric(FE[5])
cprime
## [1] -0.01178432
### Random Effects
#pulling out random effects info
VarCorr(model_lme)
## res_id = pdLogChol(-1 + dm:poststim_dev_4 + dy:pdep_dev_4 + dy:poststim_dev_4)
## Variance StdDev Corr
## dm:poststim_dev_4 0.04543514 0.2131552 dm:__4 dy:__4
## dy:pdep_dev_4 0.02938442 0.1714188 -0.100
## poststim_dev_4:dy 0.04393011 0.2095951 0.094 0.568
## Residual 0.21967883 0.4686991
# Easier to interpret and work with when placed in objects...
#The *variance* of the *a* paths
sig2_a <- as.numeric(VarCorr(model_lme)["dm:poststim_dev_4", "Variance"])
sig2_a
## [1] 0.04543514
#The *variance* of the *b* paths
sig2_b <- as.numeric(VarCorr(model_lme)["dy:pdep_dev_4", "Variance"])
sig2_b
## [1] 0.02938442
#The variance of the *c'* paths
sig2_cprime <- as.numeric(VarCorr(model_lme)["poststim_dev_4:dy", "Variance"])
sig2_cprime
## [1] 0.04393011
#The residual *variance* of the mediator variable,
sig2_em <- (1.00*as.numeric(VarCorr(model_lme)["Residual", "StdDev"]))^2
sig2_em
## [1] 0.2196788
#The residual *variance* of the outcome variable,
sig2_ey <- (0.847219*as.numeric(VarCorr(model_lme)["Residual", "StdDev"]))^2
sig2_ey
## [1] 0.1576811
#The *covariance* between the $a_{j}$ and $b_{j}$ paths
#We define a function to convert the correlation to a covariance using the variances
cortocov <- function (r, var1, var2) {
cov=r*((var1*var2)^0.5)
return(cov)
}
covajbj <- cortocov(r = as.numeric(VarCorr(model_lme)["dy:pdep_dev_4","Corr"]),
var1 = as.numeric(VarCorr(model_lme)["dm:poststim_dev_4","Variance"]), #same thing as sig2_a
var2 = as.numeric(VarCorr(model_lme)["dy:pdep_dev_4","Variance"])) #same thing as sig2_b
covajbj
## [1] -0.003653882
### Calculating mediated effect (e.g., indirect effect)
indirecteffect <- a*b + covajbj
indirecteffect
## [1] -0.007365414
### Calculating total effect *c*
totaleffect <- cprime + a*b + covajbj
totaleffect
## [1] -0.01914973
### Calculating percent of total effect accounted for by mediated effect
percentmediated <- 100*(indirecteffect/totaleffect)
percentmediated
## [1] 38.46223
### Calculating percent of mediated effect accounted for by ab covariance
percentcovariance <- 100*(covajbj/indirecteffect)
percentcovariance
## [1] 49.60864
Example where % covariance is negative
################################
# 2 MONTHS POST STIM (MONTH 3) #
################################
#loading in data (already cleaned for mediation w/ 2 rows pp)
data <- read_stata("/Users/samanthasteimle/Library/CloudStorage/Box-Box/Power Packs/Analyses/Mediation/pp_mediation_month3.dta")
#mediation model
model_lme <- lme(fixed = z_pdep_cmis_3 ~ -1 +
dm + dm:poststim_dev_3 + #m as outcome
dy + dy:pdep_dev_3 + dy:poststim_dev_3, #y as outcome
random = ~ -1 + dm:poststim_dev_3 + dy:pdep_dev_3 + dy:poststim_dev_3 | res_id,
weights = varIdent(form = ~ 1 | dm), #separate sigma^{2}_{e} for each outcome
data = data,
na.action = na.exclude,
control = lmeControl(opt = "optim", maxIter = 200, msMaxIter = 200, niterEM = 50, msMaxEval = 400))
summary(model_lme)
## Linear mixed-effects model fit by REML
## Data: data
## AIC BIC logLik
## 13689.16 13784.54 -6831.578
##
## Random effects:
## Formula: ~-1 + dm:poststim_dev_3 + dy:pdep_dev_3 + dy:poststim_dev_3 | res_id
## Structure: General positive-definite, Log-Cholesky parametrization
## StdDev Corr
## dm:poststim_dev_3 0.2315666 dm:__3 dy:__3
## dy:pdep_dev_3 0.1578304 0.032
## poststim_dev_3:dy 0.2610409 0.057 0.243
## Residual 0.4707403
##
## Variance function:
## Structure: Different standard deviations per stratum
## Formula: ~1 | dm
## Parameter estimates:
## 0 1
## 1.0000000 0.8456371
## Fixed effects: z_pdep_cmis_3 ~ -1 + dm + dm:poststim_dev_3 + dy + dy:pdep_dev_3 + dy:poststim_dev_3
## Value Std.Error DF t-value p-value
## dm -0.00046251 0.005409342 11209 -0.085502 0.9319
## dy 0.00112221 0.006416011 11209 0.174907 0.8612
## dm:poststim_dev_3 -0.04408929 0.022922379 11209 -1.923417 0.0545
## dy:pdep_dev_3 0.07691126 0.022696994 11209 3.388610 0.0007
## poststim_dev_3:dy -0.03329258 0.026386107 11209 -1.261746 0.2071
## Correlation:
## dm dy dm:__3 dy:__3
## dy 0.001
## dm:poststim_dev_3 -0.066 -0.001
## dy:pdep_dev_3 0.000 -0.002 0.016
## poststim_dev_3:dy -0.001 -0.069 0.040 0.143
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -6.06207976 -0.02255108 0.04214271 0.36610144 4.72238468
##
## Number of Observations: 11358
## Number of Groups: 145
tab_model(model_lme, show.se= TRUE, digits=3,digits.re=3, show.ci = FALSE)
|
Combined var where each duplicate obs = M (pdep_dev) or = Y (cmis_dev) |
|||
|---|---|---|---|
| Predictors | Estimates | std. Error | p |
|
Dummy: Z = M (parent WB dev) |
-0.000 | 0.005 | 0.932 |
|
Dummy: Z = Y (child WB dev) |
0.001 | 0.006 | 0.861 |
| dm:poststim_dev_3 | -0.044 | 0.023 | 0.054 |
| dy:pdep_dev_3 | 0.077 | 0.023 | 0.001 |
| poststim_dev_3:dy | -0.033 | 0.026 | 0.207 |
| Random Effects | |||
| σ2 | 0.222 | ||
| τ00 | |||
| τ00 | |||
| τ11 res_id.dy:pdep_dev_3 | 0.025 | ||
| τ11 res_id.poststim_dev_3:dy | 0.068 | ||
| ρ01 | 0.032 | ||
| 0.057 | |||
| ICC | 0.063 | ||
| N res_id | 145 | ||
| Observations | 11358 | ||
| Marginal R2 / Conditional R2 | 0.004 / 0.066 | ||
### Fixed Effects
#pulling out fixed effects info
FE <- fixef(model_lme)
FE
## dm dy dm:poststim_dev_3 dy:pdep_dev_3
## -0.0004625078 0.0011222078 -0.0440892867 0.0769112570
## poststim_dev_3:dy
## -0.0332925766
#dm = intercept in m model, mean of m when all vars = 0 (should be 0 or close to 0 b/c variables should be person-centered)
#dy = intercept in y model, mean of y when all vars = 0 (should be 0 or close to 0 b/c variables should be person-centered)
#dm:poststim = effect of poststim (x) on parent WB (m) = a
#dy:pdep = effect of parent WB (m) on child WB (y) = b
#poststim:dy = effect of poststim (x) on child WB (y) = c'
# Let's put the parameters into named objects, as these will be useful later.
a <- as.numeric(FE[3])
a
## [1] -0.04408929
b <- as.numeric(FE[4])
b
## [1] 0.07691126
cprime <- as.numeric(FE[5])
cprime
## [1] -0.03329258
### Random Effects
#pulling out random effects info
VarCorr(model_lme)
## res_id = pdLogChol(-1 + dm:poststim_dev_3 + dy:pdep_dev_3 + dy:poststim_dev_3)
## Variance StdDev Corr
## dm:poststim_dev_3 0.05362309 0.2315666 dm:__3 dy:__3
## dy:pdep_dev_3 0.02491044 0.1578304 0.032
## poststim_dev_3:dy 0.06814233 0.2610409 0.057 0.243
## Residual 0.22159639 0.4707403
# Easier to interpret and work with when placed in objects...
#The *variance* of the *a* paths
sig2_a <- as.numeric(VarCorr(model_lme)["dm:poststim_dev_3", "Variance"])
sig2_a
## [1] 0.05362309
#The *variance* of the *b* paths
sig2_b <- as.numeric(VarCorr(model_lme)["dy:pdep_dev_3", "Variance"])
sig2_b
## [1] 0.02491044
#The variance of the *c'* paths
sig2_cprime <- as.numeric(VarCorr(model_lme)["poststim_dev_3:dy", "Variance"])
sig2_cprime
## [1] 0.06814233
#The residual *variance* of the mediator variable,
sig2_em <- (1.00*as.numeric(VarCorr(model_lme)["Residual", "StdDev"]))^2
sig2_em
## [1] 0.2215964
#The residual *variance* of the outcome variable,
sig2_ey <- (0.8456371*as.numeric(VarCorr(model_lme)["Residual", "StdDev"]))^2
sig2_ey
## [1] 0.1584641
#The *covariance* between the $a_{j}$ and $b_{j}$ paths
#We define a function to convert the correlation to a covariance using the variances
cortocov <- function (r, var1, var2) {
cov=r*((var1*var2)^0.5)
return(cov)
}
covajbj <- cortocov(r = as.numeric(VarCorr(model_lme)["dy:pdep_dev_3","Corr"]),
var1 = as.numeric(VarCorr(model_lme)["dm:poststim_dev_3","Variance"]), #same thing as sig2_a
var2 = as.numeric(VarCorr(model_lme)["dy:pdep_dev_3","Variance"])) #same thing as sig2_b
covajbj
## [1] 0.001169544
### Calculating mediated effect (e.g., indirect effect)
indirecteffect <- a*b + covajbj
indirecteffect
## [1] -0.002221418
### Calculating total effect *c*
totaleffect <- cprime + a*b + covajbj
totaleffect
## [1] -0.03551399
### Calculating percent of total effect accounted for by mediated effect
percentmediated <- 100*(indirecteffect/totaleffect)
percentmediated
## [1] 6.255051
### Calculating percent of mediated effect accounted for by ab covariance
percentcovariance <- 100*(covajbj/indirecteffect)
percentcovariance
## [1] -52.64853
Example where % mediated is negative and % covariance > 100%
################################
# 2 MONTHS POST STIM (MONTH 3) #
################################
#loading in data (already cleaned for mediation w/ 2 rows pp)
data <- read_stata("/Users/samanthasteimle/Library/CloudStorage/Box-Box/Power Packs/Analyses/Mediation/pp_mediation_month3.dta")
#mediation model
model_lme <- lme(fixed = z_pdep_csad_3 ~ -1 +
dm + dm:poststim_dev_3 + #m as outcome
dy + dy:pdep_dev_3 + dy:poststim_dev_3, #y as outcome
random = ~ -1 + dm:poststim_dev_3 + dy:pdep_dev_3 + dy:poststim_dev_3 | res_id,
weights = varIdent(form = ~ 1 | dm), #separate sigma^{2}_{e} for each outcome
data = data,
na.action = na.exclude,
control = lmeControl(opt = "optim", maxIter = 200, msMaxIter = 200, niterEM = 50, msMaxEval = 400))
summary(model_lme)
## Linear mixed-effects model fit by REML
## Data: data
## AIC BIC logLik
## 10855.46 10950.85 -5414.73
##
## Random effects:
## Formula: ~-1 + dm:poststim_dev_3 + dy:pdep_dev_3 + dy:poststim_dev_3 | res_id
## Structure: General positive-definite, Log-Cholesky parametrization
## StdDev Corr
## dm:poststim_dev_3 0.2315976 dm:__3 dy:__3
## dy:pdep_dev_3 0.1884799 0.094
## poststim_dev_3:dy 0.1368665 0.309 0.306
## Residual 0.3669883
##
## Variance function:
## Structure: Different standard deviations per stratum
## Formula: ~1 | dm
## Parameter estimates:
## 0 1
## 1.000000 1.084685
## Fixed effects: z_pdep_csad_3 ~ -1 + dm + dm:poststim_dev_3 + dy + dy:pdep_dev_3 + dy:poststim_dev_3
## Value Std.Error DF t-value p-value
## dm -0.00045488 0.005407528 11217 -0.084119 0.9330
## dy 0.00063705 0.004956111 11217 0.128538 0.8977
## dm:poststim_dev_3 -0.04402080 0.022919257 11217 -1.920691 0.0548
## dy:pdep_dev_3 0.08582703 0.022482007 11217 3.817588 0.0001
## poststim_dev_3:dy -0.02687946 0.016118082 11217 -1.667659 0.0954
## Correlation:
## dm dy dm:__3 dy:__3
## dy 0.006
## dm:poststim_dev_3 -0.066 -0.004
## dy:pdep_dev_3 0.000 -0.003 0.056
## poststim_dev_3:dy -0.006 -0.073 0.192 0.180
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -7.95070228 -0.01885931 0.02003931 0.32443362 4.66666233
##
## Number of Observations: 11366
## Number of Groups: 145
tab_model(model_lme, show.se= TRUE, digits=3,digits.re=3, show.ci = FALSE)
|
Combined var where each duplicate obs = M (pdep_dev) or = Y (csad_dev) |
|||
|---|---|---|---|
| Predictors | Estimates | std. Error | p |
|
Dummy: Z = M (parent WB dev) |
-0.000 | 0.005 | 0.933 |
|
Dummy: Z = Y (child WB dev) |
0.001 | 0.005 | 0.898 |
| dm:poststim_dev_3 | -0.044 | 0.023 | 0.055 |
| dy:pdep_dev_3 | 0.086 | 0.022 | <0.001 |
| poststim_dev_3:dy | -0.027 | 0.016 | 0.095 |
| Random Effects | |||
| σ2 | 0.135 | ||
| τ00 | |||
| τ00 | |||
| τ11 res_id.dy:pdep_dev_3 | 0.036 | ||
| τ11 res_id.poststim_dev_3:dy | 0.019 | ||
| ρ01 | 0.094 | ||
| 0.309 | |||
| ICC | 0.073 | ||
| N res_id | 145 | ||
| Observations | 11366 | ||
| Marginal R2 / Conditional R2 | 0.006 / 0.079 | ||
### Fixed Effects
#pulling out fixed effects info
FE <- fixef(model_lme)
FE
## dm dy dm:poststim_dev_3 dy:pdep_dev_3
## -0.0004548758 0.0006370505 -0.0440207991 0.0858270327
## poststim_dev_3:dy
## -0.0268794595
#dm = intercept in m model, mean of m when all vars = 0 (should be 0 or close to 0 b/c variables should be person-centered)
#dy = intercept in y model, mean of y when all vars = 0 (should be 0 or close to 0 b/c variables should be person-centered)
#dm:poststim = effect of poststim (x) on parent WB (m) = a
#dy:pdep = effect of parent WB (m) on child WB (y) = b
#poststim:dy = effect of poststim (x) on child WB (y) = c'
# Let's put the parameters into named objects, as these will be useful later.
a <- as.numeric(FE[3])
a
## [1] -0.0440208
b <- as.numeric(FE[4])
b
## [1] 0.08582703
cprime <- as.numeric(FE[5])
cprime
## [1] -0.02687946
### Random Effects
#pulling out random effects info
VarCorr(model_lme)
## res_id = pdLogChol(-1 + dm:poststim_dev_3 + dy:pdep_dev_3 + dy:poststim_dev_3)
## Variance StdDev Corr
## dm:poststim_dev_3 0.05363746 0.2315976 dm:__3 dy:__3
## dy:pdep_dev_3 0.03552469 0.1884799 0.094
## poststim_dev_3:dy 0.01873244 0.1368665 0.309 0.306
## Residual 0.13468041 0.3669883
# Easier to interpret and work with when placed in objects...
#The *variance* of the *a* paths
sig2_a <- as.numeric(VarCorr(model_lme)["dm:poststim_dev_3", "Variance"])
sig2_a
## [1] 0.05363746
#The *variance* of the *b* paths
sig2_b <- as.numeric(VarCorr(model_lme)["dy:pdep_dev_3", "Variance"])
sig2_b
## [1] 0.03552469
#The variance of the *c'* paths
sig2_cprime <- as.numeric(VarCorr(model_lme)["poststim_dev_3:dy", "Variance"])
sig2_cprime
## [1] 0.01873244
#The residual *variance* of the mediator variable,
sig2_em <- (1.00*as.numeric(VarCorr(model_lme)["Residual", "StdDev"]))^2
sig2_em
## [1] 0.1346804
#The residual *variance* of the outcome variable,
sig2_ey <- (1.084685*as.numeric(VarCorr(model_lme)["Residual", "StdDev"]))^2
sig2_ey
## [1] 0.1584571
#The *covariance* between the $a_{j}$ and $b_{j}$ paths
#We define a function to convert the correlation to a covariance using the variances
cortocov <- function (r, var1, var2) {
cov=r*((var1*var2)^0.5)
return(cov)
}
covajbj <- cortocov(r = as.numeric(VarCorr(model_lme)["dy:pdep_dev_3","Corr"]),
var1 = as.numeric(VarCorr(model_lme)["dm:poststim_dev_3","Variance"]), #same thing as sig2_a
var2 = as.numeric(VarCorr(model_lme)["dy:pdep_dev_3","Variance"])) #same thing as sig2_b
covajbj
## [1] 0.004103242
### Calculating mediated effect (e.g., indirect effect)
indirecteffect <- a*b + covajbj
indirecteffect
## [1] 0.0003250672
### Calculating total effect *c*
totaleffect <- cprime + a*b + covajbj
totaleffect
## [1] -0.02655439
### Calculating percent of total effect accounted for by mediated effect
percentmediated <- 100*(indirecteffect/totaleffect)
percentmediated
## [1] -1.224156
### Calculating percent of mediated effect accounted for by ab covariance
percentcovariance <- 100*(covajbj/indirecteffect)
percentcovariance
## [1] 1262.275