options(repos = c(CRAN = "https://cloud.r-project.org"))
#Packages to install (same packages as the moderation code)
install.packages("pastecs")
## Installing package into 'C:/Users/chris/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'pastecs' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\chris\AppData\Local\Temp\RtmpkRJREc\downloaded_packages
install.packages("lm.beta")
## Installing package into 'C:/Users/chris/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'lm.beta' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\chris\AppData\Local\Temp\RtmpkRJREc\downloaded_packages
install.packages("QuantPsyc")
## Installing package into 'C:/Users/chris/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'QuantPsyc' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\chris\AppData\Local\Temp\RtmpkRJREc\downloaded_packages
install.packages("interactions")
## Installing package into 'C:/Users/chris/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'interactions' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\chris\AppData\Local\Temp\RtmpkRJREc\downloaded_packages
install.packages("jtools")
## Installing package into 'C:/Users/chris/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'jtools' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\chris\AppData\Local\Temp\RtmpkRJREc\downloaded_packages
install.packages("multilevel")
## Installing package into 'C:/Users/chris/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'multilevel' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\chris\AppData\Local\Temp\RtmpkRJREc\downloaded_packages
install.packages("boot")
## Installing package into 'C:/Users/chris/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'boot' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\chris\AppData\Local\Temp\RtmpkRJREc\downloaded_packages
library(pastecs)
## Warning: package 'pastecs' was built under R version 4.5.2
library(lm.beta)
## Warning: package 'lm.beta' was built under R version 4.5.2
library(QuantPsyc)
## Warning: package 'QuantPsyc' was built under R version 4.5.2
## Loading required package: boot
## Warning: package 'boot' was built under R version 4.5.2
## Loading required package: dplyr
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:pastecs':
##
## first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
## Loading required package: purrr
## Loading required package: MASS
##
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
##
## select
##
## Attaching package: 'QuantPsyc'
## The following object is masked from 'package:lm.beta':
##
## lm.beta
## The following object is masked from 'package:base':
##
## norm
library(interactions)
## Warning: package 'interactions' was built under R version 4.5.2
library(jtools)
## Warning: package 'jtools' was built under R version 4.5.2
library(multilevel)
## Warning: package 'multilevel' was built under R version 4.5.2
## Loading required package: nlme
##
## Attaching package: 'nlme'
## The following object is masked from 'package:dplyr':
##
## collapse
library(boot)
#Import Data
Data <- read.csv(file.choose(),header=T)
attach(Data)
names(Data)
## [1] "satisfaction_level" "last_evaluation" "number_project"
## [4] "average_monthly_hours" "time_spend_company" "Work_accident"
## [7] "left" "promotion_last_5years" "department"
## [10] "salary"
#Remove scientific notation
options(scipen = 999)
# Mediation with Covariates
#Variables:
#IV: number of projects
#Covariate: time spent at company
#DV: satisfaction
#Mediator: last evaluation
#Dataframe with Mediation Variables
DataFrame_Med <- Data[, c("satisfaction_level", "last_evaluation", "number_project",
"time_spend_company")]
#Descriptive Statistics (mean and SD) and Correlation
stat.desc(DataFrame_Med)
## satisfaction_level last_evaluation number_project
## nbr.val 14999.000000000 14999.000000000 14999.00000000
## nbr.null 0.000000000 0.000000000 0.00000000
## nbr.na 0.000000000 0.000000000 0.00000000
## min 0.090000000 0.360000000 2.00000000
## max 1.000000000 1.000000000 7.00000000
## range 0.910000000 0.640000000 5.00000000
## sum 9191.890000000 10740.810000000 57042.00000000
## median 0.640000000 0.720000000 4.00000000
## mean 0.612833522 0.716101740 3.80305354
## SE.mean 0.002030128 0.001397637 0.01006441
## CI.mean.0.95 0.003979300 0.002739538 0.01972747
## var 0.061817201 0.029298864 1.51928391
## std.dev 0.248630651 0.171169111 1.23259236
## coef.var 0.405706676 0.239029039 0.32410597
## time_spend_company
## nbr.val 14999.00000000
## nbr.null 0.00000000
## nbr.na 0.00000000
## min 2.00000000
## max 10.00000000
## range 8.00000000
## sum 52470.00000000
## median 3.00000000
## mean 3.49823322
## SE.mean 0.01192236
## CI.mean.0.95 0.02336928
## var 2.13199781
## std.dev 1.46013623
## coef.var 0.41739248
cor(DataFrame_Med)
## satisfaction_level last_evaluation number_project
## satisfaction_level 1.0000000 0.1050212 -0.1429696
## last_evaluation 0.1050212 1.0000000 0.3493326
## number_project -0.1429696 0.3493326 1.0000000
## time_spend_company -0.1008661 0.1315907 0.1967859
## time_spend_company
## satisfaction_level -0.1008661
## last_evaluation 0.1315907
## number_project 0.1967859
## time_spend_company 1.0000000
#Assumptions Testing
Assumptions_Med <- lm(satisfaction_level ~ last_evaluation + number_project + time_spend_company)
par(mfrow=c(2,2))
plot(Assumptions_Med)

#Mediation (Path a)
Med1.1 <- lm(last_evaluation ~ number_project + time_spend_company, data = Data)
summary(Med1.1)
##
## Call:
## lm(formula = last_evaluation ~ number_project + time_spend_company,
## data = Data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.49858 -0.12485 -0.00476 0.12740 0.37963
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.5115932 0.0048624 105.214 <0.0000000000000002 ***
## number_project 0.0467249 0.0010812 43.215 <0.0000000000000002 ***
## time_spend_company 0.0076642 0.0009127 8.397 <0.0000000000000002 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.16 on 14996 degrees of freedom
## Multiple R-squared: 0.1261, Adjusted R-squared: 0.126
## F-statistic: 1082 on 2 and 14996 DF, p-value: < 0.00000000000000022
#Mediation Path B and c’)
Med1.2 <- lm(satisfaction_level ~ number_project + last_evaluation + time_spend_company, data =
Data)
summary(Med1.2)
##
## Call:
## lm(formula = satisfaction_level ~ number_project + last_evaluation +
## time_spend_company, data = Data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.62420 -0.19154 0.02245 0.19690 0.59039
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.620334 0.009683 64.06 <0.0000000000000002 ***
## number_project -0.038241 0.001732 -22.08 <0.0000000000000002 ***
## last_evaluation 0.265490 0.012334 21.52 <0.0000000000000002 ***
## time_spend_company -0.014918 0.001382 -10.80 <0.0000000000000002 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2417 on 14995 degrees of freedom
## Multiple R-squared: 0.05514, Adjusted R-squared: 0.05495
## F-statistic: 291.7 on 3 and 14995 DF, p-value: < 0.00000000000000022
#Mediation (Total Effect/Path c)
Med1.3<- lm(satisfaction_level ~ number_project + time_spend_company, data = Data)
summary(Med1.3)
##
## Call:
## lm(formula = satisfaction_level ~ number_project + time_spend_company,
## data = Data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.56288 -0.21584 0.02865 0.20744 0.49186
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.756157 0.007457 101.405 <0.0000000000000002 ***
## number_project -0.025836 0.001658 -15.581 <0.0000000000000002 ***
## time_spend_company -0.012884 0.001400 -9.204 <0.0000000000000002 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2454 on 14996 degrees of freedom
## Multiple R-squared: 0.02594, Adjusted R-squared: 0.02581
## F-statistic: 199.7 on 2 and 14996 DF, p-value: < 0.00000000000000022
#Sobel Bootstrapping
#create your own function (copy code below). If needed, change your dataset name.
indirectsaved = function (dataset, random) {d = Data[random, ]
apath = lm(last_evaluation ~ number_project + time_spend_company, data = d)
bpath = lm(satisfaction_level ~ number_project + last_evaluation + time_spend_company, data = d)
indirect = apath$coefficients[2]*bpath$coefficients[3]
return(indirect)}
#bootstrap results (effect, bias, SE)
bootresults = boot(data = Data,
statistic = indirectsaved, R = 1000)
bootresults
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = Data, statistic = indirectsaved, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 0.012405 0.00003540007 0.0006498412
#bootstrap confidence interval
boot.ci(bootresults, conf = .95, type ="norm")
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 1000 bootstrap replicates
##
## CALL :
## boot.ci(boot.out = bootresults, conf = 0.95, type = "norm")
##
## Intervals :
## Level Normal
## 95% ( 0.0111, 0.0136 )
## Calculations and Intervals on Original Scale