% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{ctexart}
\usepackage{amsmath,amssymb}
\usepackage{iftex}
\ifPDFTeX
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
  \usepackage{unicode-math} % this also loads fontspec
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
\usepackage{lmodern}
\ifPDFTeX\else
  % xetex/luatex font selection
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{5}
\usepackage[most]{tcolorbox}
\definecolor{boxidea}{HTML}{6c8aa5}
\definecolor{boxwarn}{HTML}{b58266}
\definecolor{boxtip}{HTML}{8aa085}
\newtcolorbox{concept}[1][]{enhanced, breakable, colback=boxidea!7, colframe=boxidea, coltitle=white, fonttitle=\bfseries, title=#1, arc=2pt, left=8pt, right=8pt, top=4pt, bottom=4pt}
\newtcolorbox{pitfall}[1][]{enhanced, breakable, colback=boxwarn!7, colframe=boxwarn, coltitle=white, fonttitle=\bfseries, title=#1, arc=2pt, left=8pt, right=8pt, top=4pt, bottom=4pt}
\newtcolorbox{keypoint}[1][]{enhanced, breakable, colback=boxtip!7, colframe=boxtip, coltitle=white, fonttitle=\bfseries, title=#1, arc=2pt, left=8pt, right=8pt, top=4pt, bottom=4pt}
\renewcommand{\and}{\\}
\ifLuaTeX
  \usepackage{selnolig}  % disable illegal ligatures
\fi
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\urlstyle{same}
\hypersetup{
  pdftitle={Moderation \& Mediation},
  pdfauthor={Rui Zhou; Spring 2023},
  hidelinks,
  pdfcreator={LaTeX via pandoc}}

\title{Moderation \& Mediation}
\author{Rui Zhou \and Spring 2023}
\date{}

\begin{document}
\maketitle

{
\setcounter{tocdepth}{2}
\tableofcontents
}
\hypertarget{ux8fd9ux7ae0ux5728ux8bb2ux4ec0ux4e48}{%
\section{这章在讲什么}\label{ux8fd9ux7ae0ux5728ux8bb2ux4ec0ux4e48}}

调节（moderation）和中介（mediation）解决的是两个\textbf{不同}问题，初学者经常混。一句话区分：

\begin{itemize}
\tightlist
\item
  \textbf{调节} \(X \xrightarrow{Z} Y\)：\(X\) 对 \(Y\)
  的影响\textbf{取决于第三个变量}
  \(Z\)（年纪小的时候社会支持作用大，年纪大就没那么明显）。回归形式：\(Y = a + b_1 X + b_2 Z + b_3 (X \cdot Z) + \varepsilon\)，关键看
  \(b_3\)（交互项系数）的显著性。
\item
  \textbf{中介} \(X \to M \to Y\)：\(X\) 通过\textbf{一个中间变量} \(M\)
  影响 \(Y\)（学习时间 → 自我效能感 →
  学业成绩）。三步走：\(Y \sim X\)（总效应）、\(M \sim X\)（\(a\)
  路径）、\(Y \sim X + M\)（\(b\) 路径 + 直接效应）。
\end{itemize}

\begin{concept}[两个核心数学量]
\textbf{调节的 simple slope}：当 $Z$ 取某个具体值 $z_0$ 时，$X$ 对 $Y$ 的斜率为 $b_1 + b_3 z_0$。常规做法是在 $Z$ 的均值、$\bar Z \pm 1\,\mathrm{sd}$ 三个点上画斜率，看哪些斜率显著、哪些不显著。

\textbf{中介的间接效应} $a \cdot b$：$X$ 经过 $M$ 影响 $Y$ 的部分。检验显著性首选\textbf{自助法置信区间}（bootstrap）而不是 Sobel 检验——因为 $a \cdot b$ 的抽样分布并不正态，Sobel 功效较低、保守。Hayes 的 PROCESS 宏 / R 的 \texttt{mediation} 包默认就是 bootstrap。
\end{concept}

\textbf{中心化变量}（centering）在调节分析里几乎是必做的：把 \(X\) 和
\(Z\) 各自减去均值再相乘构造交互项，可以缓解 \(X \cdot Z\) 与
\(X\)、\(Z\) 本身的多重共线性，让主效应 \(b_1\)、\(b_2\)
解读为「在另一个变量取均值时的效应」。

\hypertarget{moderation}{%
\section{Moderation}\label{moderation}}

In statistics, moderation refers to a type of \textbf{interaction
effect} between two or more variables in a statistical model.
Specifically, it occurs when the strength or direction of the
relationship between two variables (the predictor and the outcome
variable) changes depending on the level of a third variable (the
moderator). Moderator variable is a variable whose different values
determine the nature of the relationship between two other variables.

\begin{figure}

{\centering \includegraphics[width=1\linewidth]{moderation} 

}

\caption{Moderation}\label{fig:unnamed-chunk-1}
\end{figure}

To test for moderation in a statistical model, researchers typically
include an \textbf{interaction term} between the \emph{predictor} and
the \emph{moderator} variable. If the interaction term is statistically
significant, it suggests that the relationship between the predictor and
outcome variable is different at different levels of the moderator
variable. Moderation in model is an extension for two-way ANOVA, without
limits to the scale of data (categorical or continuous).

Moderation analysis is important in statistics because it allows
researchers to examine the conditions under which a relationship between
two variables is stronger or weaker. This information can be useful for
developing interventions or identifying subgroups of individuals who may
benefit from different types of treatments or interventions.

\hypertarget{fit-moderation-model}{%
\subsection{Fit Moderation Model}\label{fit-moderation-model}}

\hypertarget{null-model}{%
\subsubsection{NULL Model}\label{null-model}}

The data in ``moderation\_data.sav'' contains information about loyalty
to marriage, relationship between partners, and their age. We're
interested in the relationship between age and loyalty, and how the
relationship in marriage moderates it.

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# moderation data}
\NormalTok{currentData }\OtherTok{\textless{}{-}}\NormalTok{ bruceR}\SpecialCharTok{::}\FunctionTok{import}\NormalTok{(}\StringTok{"moderation\_data.sav"}\NormalTok{)}
\FunctionTok{attach}\NormalTok{(currentData)}
\end{Highlighting}
\end{Shaded}

Note that when an interaction term is introduced into the model, better
to centralize the variable for convenience of interpretation! (Even
without centered independent variables, the significance output won't
change.)

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{C\_Relationship }\OtherTok{=}\NormalTok{ Relationship }\SpecialCharTok{{-}} \FunctionTok{mean}\NormalTok{(Relationship)}
\NormalTok{C\_age\_con }\OtherTok{=}\NormalTok{ age\_con }\SpecialCharTok{{-}} \FunctionTok{mean}\NormalTok{(age\_con)}
\end{Highlighting}
\end{Shaded}

The NULL model here is without the interaction term, but to inspect
\texttt{age} and \texttt{Relationship} through MLR.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{model\_0 }\OtherTok{\textless{}{-}} \FunctionTok{lm}\NormalTok{(Loyalty }\SpecialCharTok{\textasciitilde{}}\NormalTok{ C\_Relationship }\SpecialCharTok{+}\NormalTok{ C\_age\_con)}
\NormalTok{bruceR}\SpecialCharTok{::}\FunctionTok{GLM\_summary}\NormalTok{(model\_0)}
\end{Highlighting}
\end{Shaded}

\hypertarget{moderation-model}{%
\subsubsection{Moderation Model}\label{moderation-model}}

Then, add the interaction of moderator and predictor into the MLR model.
(Moderation in essence is a MLR with interaction terms.)

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{model\_moderation }\OtherTok{\textless{}{-}} \FunctionTok{lm}\NormalTok{(Loyalty }\SpecialCharTok{\textasciitilde{}}\NormalTok{ C\_Relationship }\SpecialCharTok{*}\NormalTok{ C\_age\_con)}
\NormalTok{bruceR}\SpecialCharTok{::}\FunctionTok{GLM\_summary}\NormalTok{(model\_moderation)}
\end{Highlighting}
\end{Shaded}

After introducing the interaction term into our model, four things are
of great interest.

\begin{itemize}
\item
  Significance output of interaction term.
\item
  Significance output of original terms in null model.
\item
  Adjusted \(R^2\) compared to null model.
\item
  VIF, in case of multicollinearity.

  The improvement of \(R^2\) indicates that, even after considering the
  added complexity of model, the explanatory power is greater than the
  null.
\end{itemize}

\hypertarget{model-comparison}{%
\subsubsection{Model Comparison}\label{model-comparison}}

The most natural impulse is to compare the moderation model against the
null one. The difference lies in the interaction term. In fact, the
output comes down right to the interaction term.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{anova}\NormalTok{(model\_0, model\_moderation)}
\end{Highlighting}
\end{Shaded}

\hypertarget{after-significant}{%
\subsection{After Significant}\label{after-significant}}

After seeing the moderation (interaction) is significant, we need to
check how it exerts its effect by \textbf{simple main effect}, because a
significant moderation term will affect your interpretation of main
effect.

We're to compute marginal means, where the data is divided into
\(3\times 3\) categories using \(Mean \pm SD\), i.e., with 3 \(X\)
levels and 3 \(M\) levels.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(emmeans)}
\NormalTok{m\_Relationship }\OtherTok{\textless{}{-}} \FunctionTok{mean}\NormalTok{(Relationship, }\AttributeTok{na.rm =} \ConstantTok{TRUE}\NormalTok{)}
\NormalTok{sd\_Relationship }\OtherTok{\textless{}{-}} \FunctionTok{sd}\NormalTok{(Relationship, }\AttributeTok{na.rm =} \ConstantTok{TRUE}\NormalTok{)}
\NormalTok{m\_Age }\OtherTok{\textless{}{-}} \FunctionTok{mean}\NormalTok{(age\_con, }\AttributeTok{na.rm =} \ConstantTok{TRUE}\NormalTok{)}
\NormalTok{sd\_Age }\OtherTok{\textless{}{-}} \FunctionTok{sd}\NormalTok{(age\_con, }\AttributeTok{na.rm =} \ConstantTok{TRUE}\NormalTok{)}

\CommentTok{\# use un{-}centered data to see how the raw looks like in slope analysis.}
\NormalTok{model\_moderation }\OtherTok{\textless{}{-}} \FunctionTok{lm}\NormalTok{(Loyalty }\SpecialCharTok{\textasciitilde{}}\NormalTok{ Relationship }\SpecialCharTok{*}\NormalTok{ age\_con)}

\CommentTok{\# compute Estimated Marginal Means}
\NormalTok{emm }\OtherTok{\textless{}{-}} \FunctionTok{emmeans}\NormalTok{(model\_moderation, }\SpecialCharTok{\textasciitilde{}}\NormalTok{Relationship }\SpecialCharTok{*}\NormalTok{ age\_con, }\AttributeTok{cov.keep =} \DecValTok{3}\NormalTok{, }\AttributeTok{at =} \FunctionTok{list}\NormalTok{(}\AttributeTok{age\_con =} \FunctionTok{c}\NormalTok{(m\_Age }\SpecialCharTok{{-}}
\NormalTok{    sd\_Age, m\_Age, m\_Age }\SpecialCharTok{+}\NormalTok{ sd\_Age), }\AttributeTok{Relationship =} \FunctionTok{c}\NormalTok{(m\_Relationship }\SpecialCharTok{{-}}\NormalTok{ sd\_Relationship,}
\NormalTok{    m\_Relationship, m\_Relationship }\SpecialCharTok{+}\NormalTok{ sd\_Relationship)), }\AttributeTok{level =} \FloatTok{0.95}\NormalTok{)}
\FunctionTok{summary}\NormalTok{(emm)}
\end{Highlighting}
\end{Shaded}

We then do the slope analysis, similar to simple main effect.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{simpleSlope }\OtherTok{\textless{}{-}} \FunctionTok{emtrends}\NormalTok{(model\_moderation, pairwise }\SpecialCharTok{\textasciitilde{}}\NormalTok{ age\_con, }\AttributeTok{var =} \StringTok{"Relationship"}\NormalTok{,}
    \AttributeTok{cov.keep =} \DecValTok{3}\NormalTok{, }\AttributeTok{at =} \FunctionTok{list}\NormalTok{(}\AttributeTok{age\_con =} \FunctionTok{c}\NormalTok{(m\_Age }\SpecialCharTok{{-}}\NormalTok{ sd\_Age, m\_Age, m\_Age }\SpecialCharTok{+}\NormalTok{ sd\_Age)),}
    \AttributeTok{level =} \FloatTok{0.95}\NormalTok{)}
\FunctionTok{summary}\NormalTok{(simpleSlope)}
\end{Highlighting}
\end{Shaded}

Slopes can be visualized.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{emmip}\NormalTok{(model\_moderation, age\_con }\SpecialCharTok{\textasciitilde{}}\NormalTok{ Relationship, }\AttributeTok{cov.keep =} \DecValTok{3}\NormalTok{, }\AttributeTok{at =} \FunctionTok{list}\NormalTok{(}\AttributeTok{Relationship =} \FunctionTok{c}\NormalTok{(m\_Relationship }\SpecialCharTok{{-}}
\NormalTok{    sd\_Relationship, m\_Relationship, m\_Relationship }\SpecialCharTok{+}\NormalTok{ sd\_Relationship), }\AttributeTok{age\_con =} \FunctionTok{c}\NormalTok{(m\_Age }\SpecialCharTok{{-}}
\NormalTok{    sd\_Age, m\_Age, m\_Age }\SpecialCharTok{+}\NormalTok{ sd\_Age)), }\AttributeTok{CIs =} \ConstantTok{TRUE}\NormalTok{, }\AttributeTok{level =} \FloatTok{0.95}\NormalTok{, }\AttributeTok{position =} \StringTok{"jitter"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\hypertarget{appendix-for-moderation}{%
\subsection{Appendix for Moderation}\label{appendix-for-moderation}}

\hypertarget{uncentered-model}{%
\subsubsection{Uncentered Model}\label{uncentered-model}}

One plausible reason for centralization is interpretation, and the other
is possible collinearity.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{model\_nonc }\OtherTok{=} \FunctionTok{lm}\NormalTok{(Loyalty }\SpecialCharTok{\textasciitilde{}}\NormalTok{ Relationship }\SpecialCharTok{+}\NormalTok{ age\_con }\SpecialCharTok{+}\NormalTok{ Relationship }\SpecialCharTok{*}\NormalTok{ age\_con)}
\NormalTok{bruceR}\SpecialCharTok{::}\FunctionTok{GLM\_summary}\NormalTok{(model\_nonc)}
\end{Highlighting}
\end{Shaded}

Even though the uncentered model reports the same results with respect
to significance, the VIF bombed!

\hypertarget{brucerglm_summary}{%
\subsubsection{\texorpdfstring{\texttt{bruceR::GLM\_summary()}}{bruceR::GLM\_summary()}}\label{brucerglm_summary}}

By the way, why do I use \texttt{bruceR::GLM\_summary(model)} here?
Because it will return well-rounded report with standardized and
non-standardized coefficients, 95\% CI for coefficients, significance
output for variables, necessary report of the model. Otherwise, if I'm
to get the 95\% CI of each slope coefficients, I have to construct a new
command like:

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{cbind}\NormalTok{(}\FunctionTok{coef}\NormalTok{(model\_moderation), }\FunctionTok{confint}\NormalTok{(model\_moderation, }\AttributeTok{level =} \FloatTok{0.95}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

\hypertarget{residual-plots}{%
\subsubsection{Residual Plots}\label{residual-plots}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{res.std }\OtherTok{\textless{}{-}} \FunctionTok{rstandard}\NormalTok{(model\_moderation)}
\FunctionTok{plot}\NormalTok{(res.std, }\AttributeTok{ylab =} \StringTok{"Standardized Residuals"}\NormalTok{)}
\NormalTok{car}\SpecialCharTok{::}\FunctionTok{residualPlots}\NormalTok{(model\_moderation)}

\FunctionTok{library}\NormalTok{(ggplot2)}
\FunctionTok{ggplot}\NormalTok{(}\FunctionTok{as.data.frame}\NormalTok{(res.std), }\FunctionTok{aes}\NormalTok{(}\AttributeTok{sample =}\NormalTok{ res.std)) }\SpecialCharTok{+} \FunctionTok{geom\_qq}\NormalTok{() }\SpecialCharTok{+} \FunctionTok{geom\_qq\_line}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

Lastly,

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{detach}\NormalTok{(currentData)}
\end{Highlighting}
\end{Shaded}

\hypertarget{more-on-moderation}{%
\subsubsection{More on Moderation}\label{more-on-moderation}}

There are several limitations to moderation analysis that researchers
should be aware of:

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  Causality cannot be inferred: Moderation analysis can only identify
  statistical relationships between variables, and cannot establish
  causality. To establish causality, researchers need to use
  experimental designs or quasi-experimental designs that include random
  assignment.
\item
  Measurement error: Moderation analysis assumes that all variables are
  measured without error. However, in practice, measurement error is
  often present, which can lead to biased estimates of the moderation
  effect.
\item
  Limited generalizability: Moderation effects may be specific to the
  sample, context, or time period in which the study was conducted, and
  may not generalize to other populations or contexts.
\item
  Sample size requirements: Moderation analysis often requires larger
  sample sizes than simple regression analysis, especially when testing
  for higher-order interactions or when there are multiple moderators.
\item
  Multicollinearity: If the moderator variable is highly correlated with
  the predictor variable, this can lead to multicollinearity, which can
  make it difficult to estimate the moderation effect accurately.
\item
  Model complexity: Adding interaction terms to a statistical model can
  increase its complexity, which can make it more difficult to interpret
  and can lead to overfitting.
\end{enumerate}

Despite these limitations, moderation analysis can provide valuable
insights into the conditions under which a relationship between two
variables is stronger or weaker, and can help researchers develop more
effective interventions or treatments.

\hypertarget{mediation}{%
\section{Mediation}\label{mediation}}

Mediation refers to a causal process in which the relationship between
two variables is explained by a \textbf{third} variable, called a
mediator. Mediation analysis is used to test for the presence and
strength of this causal process.

\begin{figure}

{\centering \includegraphics[width=1\linewidth]{Mediation} 

}

\caption{Moderation}\label{fig:unnamed-chunk-14}
\end{figure}

To test for mediation in a statistical model, researchers typically use
a mediation model, which includes three variables: the independent
variable (X), the mediator variable (M), and the dependent variable (Y).
The mediator variable is assumed to be affected by the independent
variable, and in turn, affects the dependent variable. The strength of
the mediation effect is typically estimated using regression analysis.

Mediation analysis is important in statistics because it allows
researchers to identify the mechanisms through which an independent
variable affects a dependent variable. This can provide insights into
the underlying processes that drive a particular phenomenon and can
inform the development of interventions or treatments that target
specific mediators.

\hypertarget{fit-mediation-model}{%
\subsection{Fit Mediation Model}\label{fit-mediation-model}}

The data in ``mediation\_data.sav'' has information about customers'
satisfaction, relationship with customers and discounts offered. We are
interested in whether discount can mediate the influence of customer
relationship on their satisfaction about some consumer goods.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{currentData }\OtherTok{\textless{}{-}}\NormalTok{ bruceR}\SpecialCharTok{::}\FunctionTok{import}\NormalTok{(}\StringTok{"mediation\_data.sav"}\NormalTok{)}
\FunctionTok{attach}\NormalTok{(currentData)}
\end{Highlighting}
\end{Shaded}

Use Baron and Kenny method to conduct mediation analysis. Three steps
are:

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\tightlist
\item
  IV could predict DV.
\item
  IV could predict Mediator.
\item
  Mediator could predict DV on the presence of IV.
\end{enumerate}

Besides, the predictive power of IV is then reduced,

\begin{itemize}
\tightlist
\item
  If IV is no longer a significant predictor \(\to\) full mediation.
\item
  If IV is still significant but reduced in magitude \(\to\) partial
  mediation.
\end{itemize}

Note that the effect of mediator on DV should primarily be based on IV!

\hypertarget{direct-effect}{%
\subsubsection{Direct Effect}\label{direct-effect}}

Check without any consideration of mediation as a baseline model, which
is the relationship between Y and X.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{model\_0 }\OtherTok{\textless{}{-}} \FunctionTok{lm}\NormalTok{(Satisfaction }\SpecialCharTok{\textasciitilde{}}\NormalTok{ Relationship)}
\NormalTok{bruceR}\SpecialCharTok{::}\FunctionTok{GLM\_summary}\NormalTok{(model\_0)}
\end{Highlighting}
\end{Shaded}

\hypertarget{internal-correlation}{%
\subsubsection{Internal Correlation}\label{internal-correlation}}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# M as a function of X}
\NormalTok{model\_M }\OtherTok{\textless{}{-}} \FunctionTok{lm}\NormalTok{(Discount }\SpecialCharTok{\textasciitilde{}}\NormalTok{ Relationship)}
\NormalTok{bruceR}\SpecialCharTok{::}\FunctionTok{GLM\_summary}\NormalTok{(model\_M)}
\end{Highlighting}
\end{Shaded}

Significant result here is the necessity for further mediation analysis.
If not significant, no need to move forward.

\hypertarget{full-model}{%
\subsubsection{Full Model}\label{full-model}}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# Y as a function of both X and M}
\NormalTok{model\_Y }\OtherTok{\textless{}{-}} \FunctionTok{lm}\NormalTok{(Satisfaction }\SpecialCharTok{\textasciitilde{}}\NormalTok{ Relationship }\SpecialCharTok{+}\NormalTok{ Discount)}
\NormalTok{bruceR}\SpecialCharTok{::}\FunctionTok{GLM\_summary}\NormalTok{(model\_Y)}
\end{Highlighting}
\end{Shaded}

Here, it's a case of partial mediation. And the adjusted \(R^2\) is
greatly improved, indicating that the mediation model is far more
effective than the baseline model.

\hypertarget{mediation-effect}{%
\subsection{Mediation Effect}\label{mediation-effect}}

Mediation analysis is based on both the full model and the
internal-correlation model. Here are more important indicators measuring
the mediation effect:

\begin{itemize}
\tightlist
\item
  ACME, average causal mediation effect, the indirect effect of X on Y.
\item
  ADE: average direct effect.
\item
  Total Effect: the total effect of X on Y, the sum of direct and
  indirect effect.
\item
  Proportion Mediated: how much percentage of indirect effect within the
  total effect.
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(mediation)}
\NormalTok{mediation\_results }\OtherTok{\textless{}{-}} \FunctionTok{mediate}\NormalTok{(model\_M, model\_Y, }\AttributeTok{treat =} \StringTok{"Relationship"}\NormalTok{, }\AttributeTok{mediator =} \StringTok{"Discount"}\NormalTok{,}
    \AttributeTok{boot =} \ConstantTok{TRUE}\NormalTok{, }\AttributeTok{sims =} \DecValTok{500}\NormalTok{)}
\FunctionTok{summary}\NormalTok{(mediation\_results)}
\end{Highlighting}
\end{Shaded}

\hypertarget{appendix-for-mediation}{%
\subsection{Appendix for Mediation}\label{appendix-for-mediation}}

\hypertarget{mediation-causality}{%
\subsubsection{Mediation \& Causality}\label{mediation-causality}}

Mediation analysis can reveal the presence of a causal relationship
between two variables. A causal relationship exists when a change in one
variable (the independent variable) leads to a change in another
variable (the dependent variable). Mediation analysis examines the
causal mechanism that links the independent variable to the dependent
variable through one or more intervening variables (the mediators).

To establish a causal relationship, mediation analysis must satisfy
three conditions:

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  The independent variable (X) must be significantly related to the
  mediator variable (M).
\item
  The mediator variable (M) must be significantly related to the
  dependent variable (Y), after controlling for the effect of the
  independent variable (X).
\item
  The relationship between the independent variable (X) and the
  dependent variable (Y) must be significantly reduced or eliminated
  when the mediator variable (M) is included in the model.
\end{enumerate}

If these conditions are met, it provides evidence that the independent
variable causes changes in the mediator variable, which in turn causes
changes in the dependent variable. This suggests the presence of a
causal relationship between the independent and dependent variables,
mediated by the mediator variable.

It is important to note, however, that mediation analysis alone cannot
establish causality with certainty. Other factors, such as confounding
variables or reverse causality, may also influence the relationship
between variables. Therefore, researchers should use caution when
interpreting the results of mediation analysis and consider other forms
of evidence, such as experimental designs. There are several limitations
to using mediation analysis to establish causality:

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  Directionality: Mediation analysis assumes that the causal
  relationship flows from the independent variable to the mediator
  variable to the dependent variable. However, in some cases, the causal
  relationship may flow in the opposite direction or may be
  bidirectional, making it difficult to determine the direction of
  causality.
\item
  Third variables: Mediation analysis assumes that there are no third
  variables (confounding variables) that are responsible for the
  observed relationship between the independent variable and the
  dependent variable. If there are confounding variables that have not
  been accounted for, the observed mediation effect may be due to these
  variables rather than the mediator variable.
\item
  Measurement error: Mediation analysis assumes that all variables are
  measured without error. However, in practice, measurement error is
  often present, which can lead to biased estimates of the mediation
  effect.
\item
  Sample size requirements: Mediation analysis often requires larger
  sample sizes than simple regression analysis, especially when testing
  for higher-order mediation or when there are multiple mediators.
\item
  Model complexity: Adding mediator variables to a statistical model can
  increase its complexity, which can make it more difficult to interpret
  and can lead to overfitting.
\item
  Nonlinearity: Mediation analysis assumes that the relationships
  between variables are linear. However, in some cases, the
  relationships may be nonlinear, which can lead to biased estimates of
  the mediation effect.
\end{enumerate}

Despite these limitations, mediation analysis can provide valuable
insights into the causal mechanisms that underlie relationships between
variables. However, researchers should use caution when interpreting the
results and consider other forms of evidence, such as experimental
designs, to establish causality more convincingly.

\hypertarget{sobel-test}{%
\subsubsection{Sobel Test}\label{sobel-test}}

It's outdated admittedly. In early periods, the statistic constructed in
Sobel Test was considered to follow a normal distribution, but that was
proved wrong later. Hence, in Sobel Test, the probability of committing
type I error increases before you even know it.

\begin{figure}

{\centering \includegraphics[width=0.8\linewidth]{sobel test} 

}

\caption{Sobel Test}\label{fig:unnamed-chunk-20}
\end{figure}

Sobel Test uses \(z\)-statistic: \[
z=\frac{\hat a\hat b}{\sqrt{\hat a^2s_b^2+\hat b^2s_a^2}}
\]

The code is exhibited below but I don't wanna run it.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(bda)}
\FunctionTok{mediation.test}\NormalTok{(Discount, Relationship, Satisfaction)}
\end{Highlighting}
\end{Shaded}

Lastly, don't forget to

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{detach}\NormalTok{(currentData)}
\end{Highlighting}
\end{Shaded}

\hypertarget{ux5bb9ux6613ux8e29ux7684ux5751}{%
\section{容易踩的坑}\label{ux5bb9ux6613ux8e29ux7684ux5751}}

\begin{pitfall}[调节与中介都容易栽的几件事]
\begin{enumerate}
\item \textbf{把交互显著当作"调节存在"，忘了画 simple slope}：$b_3$ 显著只说明斜率随 $Z$ 变化，但**怎么变**要看 simple slope。可能 $Z$ 高时正向显著、$Z$ 低时不显著（增强效应），也可能两端方向相反（buffering / crossover）。结论完全不一样。
\item \textbf{中介不做 bootstrap CI}：很多老教材停留在 Baron \& Kenny 的「四步法 + Sobel test」，但近 15 年的方法论文献一致推荐 bootstrap CI 检验间接效应。$10000$ 次重抽 + 95\% percentile CI 是标配。
\item \textbf{把中介和调节简单堆叠成"调节中介"模型不查可识别性}：moderated mediation（$X \to M \to Y$ 但 $a$ 或 $b$ 路径被 $Z$ 调节）需要明确指定哪一条路径被调节、$Z$ 在哪个方程里出现。盲目把所有变量塞 PROCESS 模板里跑不一定能识别。建议先画路径图，确认每条箭头都有理论支持再上模型。
\item \textbf{中介的因果方向只来自理论}：$X \to M \to Y$ 跟 $X \to Y$（$M$ 是结果的一部分）在横截面数据下完全等价——区分只能靠时间顺序（纵向设计）或随机干预（实验）。横截面中介系数显著 \textbf{不能}直接得出因果结论。
\end{enumerate}
\end{pitfall}

\end{document}
