options(repos = c(CRAN = "https://cloud.r-project.org"))
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\RtmpERxyMS\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\RtmpERxyMS\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\RtmpERxyMS\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\RtmpERxyMS\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\RtmpERxyMS\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\RtmpERxyMS\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\RtmpERxyMS\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)
library(readxl)
#Import Data
Data <-read_xlsx(file.choose())
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)
#Moderation with Covariates
#Variables:
#IV: average monthly hours
#Covariate: last evaluation
#DV: satisfaction level
#Moderator: salary
#Center IVs and Moderator
Data$salary.center <- meanCenter(salary)
Data$last.eval.center <- meanCenter(last_evaluation)
Data$avg.center <- meanCenter(average_monthly_hours)
attach(Data)
## The following objects are masked from Data (pos = 3):
## 
##     average_monthly_hours, department, last_evaluation, left,
##     number_project, promotion_last_5years, salary, satisfaction_level,
##     time_spend_company, Work_accident
#assumptions test
#DataFrame with Moderation Variables
DataFrame <- Data[, c("satisfaction_level", "last_evaluation", "average_monthly_hours", "salary")]
#Descriptive Statistics (mean and SD) and Correlation
stat.desc(DataFrame)
##              satisfaction_level last_evaluation average_monthly_hours
## nbr.val         14999.000000000 14999.000000000         14999.0000000
## nbr.null            0.000000000     0.000000000             0.0000000
## nbr.na              0.000000000     0.000000000             0.0000000
## min                 0.090000000     0.360000000            96.0000000
## max                 1.000000000     1.000000000           310.0000000
## range               0.910000000     0.640000000           214.0000000
## sum              9191.890000000 10740.810000000       3015554.0000000
## median              0.640000000     0.720000000           200.0000000
## mean                0.612833522     0.716101740           201.0503367
## SE.mean             0.002030128     0.001397637             0.4077973
## CI.mean.0.95        0.003979300     0.002739538             0.7993325
## var                 0.061817201     0.029298864          2494.3131748
## std.dev             0.248630651     0.171169111            49.9430994
## coef.var            0.405706676     0.239029039             0.2484109
##                      salary
## nbr.val      14999.00000000
## nbr.null         0.00000000
## nbr.na           0.00000000
## min              1.00000000
## max              3.00000000
## range            2.00000000
## sum          23919.00000000
## median           2.00000000
## mean             1.59470631
## SE.mean          0.00520275
## CI.mean.0.95     0.01019803
## var              0.40600211
## std.dev          0.63718295
## coef.var         0.39956131
cor(DataFrame)
##                       satisfaction_level last_evaluation average_monthly_hours
## satisfaction_level            1.00000000      0.10502121          -0.020048113
## last_evaluation               0.10502121      1.00000000           0.339741800
## average_monthly_hours        -0.02004811      0.33974180           1.000000000
## salary                        0.05002248     -0.01300152          -0.002241879
##                             salary
## satisfaction_level     0.050022481
## last_evaluation       -0.013001516
## average_monthly_hours -0.002241879
## salary                 1.000000000
#Assumptions Testing
Assumptions_Mod <- lm(satisfaction_level ~ last_evaluation + average_monthly_hours + salary)
par(mfrow=c(2,2))
plot(Assumptions_Mod)

#Moderation Analysis
Mod <- lm(satisfaction_level ~ avg.center + salary.center + avg.center:salary.center + last.eval.center ,
          data = Data)
summary(Mod)
## 
## Call:
## lm(formula = satisfaction_level ~ avg.center + salary.center + 
##     avg.center:salary.center + last.eval.center, data = Data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.58298 -0.15955  0.02843  0.19662  0.46346 
## 
## Coefficients:
##                             Estimate  Std. Error t value             Pr(>|t|)
## (Intercept)               0.61284414  0.00201251 304.517 < 0.0000000000000002
## avg.center               -0.00031235  0.00004285  -7.289    0.000000000000329
## salary.center             0.02018091  0.00315898   6.388    0.000000000172534
## last.eval.center          0.18619064  0.01251955  14.872 < 0.0000000000000002
## avg.center:salary.center  0.00014885  0.00006416   2.320               0.0204
##                             
## (Intercept)              ***
## avg.center               ***
## salary.center            ***
## last.eval.center         ***
## avg.center:salary.center *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2465 on 14994 degrees of freedom
## Multiple R-squared:  0.01755,    Adjusted R-squared:  0.01729 
## F-statistic: 66.95 on 4 and 14994 DF,  p-value: < 0.00000000000000022
confint(Mod)
##                                   2.5 %        97.5 %
## (Intercept)               0.60889937416  0.6167889074
## avg.center               -0.00039634813 -0.0002283504
## salary.center             0.01398893538  0.0263728936
## last.eval.center          0.16165079550  0.2107304885
## avg.center:salary.center  0.00002308887  0.0002746072
#Moderation Simple Slopes
Mod2 <- lm(satisfaction_level ~ avg.center + salary.center + avg.center:salary.center, data = Data)
ModSlopes <- sim.slopes(Mod2, salary.center)
ModSlopes
##                INT          Slope            SE           LCL            UCL
## at zHigh 0.6252951 -0.00003371294 0.00005892912 -0.0001492212  0.00008179534
## at zMean 0.6128406 -0.00009679998 0.00004062352 -0.0001764270 -0.00001717293
## at zLow  0.6003861 -0.00015988703 0.00005665883 -0.0002709453 -0.00004882881
#Moderation Simple Slopes Plot
interact_plot(Mod2, pred = avg.center, modx = salary.center)
## Warning: -0.637182950469582 is outside the observed range of salary.center