% 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{longtable,booktabs,array}
\usepackage{calc} % for calculating minipage widths
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\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}
\renewcommand{\and}{\\}
\usepackage{etoolbox}
\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}
\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={心理统计 II 课程完整笔记：1--13 章 R 实操（期中 + 期末整合）},
  pdfauthor={周睿; 北京大学心理与认知科学学院},
  hidelinks,
  pdfcreator={LaTeX via pandoc}}

\title{心理统计 II 课程完整笔记：1--13 章 R 实操（期中 + 期末整合）}
\author{周睿 \and 北京大学心理与认知科学学院}
\date{2023 春}

\begin{document}
\maketitle

{
\setcounter{tocdepth}{2}
\tableofcontents
}
\hypertarget{ux5f15ux8a00}{%
\section{引言}\label{ux5f15ux8a00}}

本文整合了北大心理与认知科学学院《心理统计 II》2023 春学期所有 13 章 R
实操，是一份\textbf{考前一站式参考手册}。每节都配完整的 R 代码 +
示例数据 + 运行结果（表格、回归输出、模型诊断图、KM
曲线、聚类树状图等）。

\begin{keypoint}[13 章一句话导览]
\textbf{期中（第 1--7 章）}：
\begin{itemize}
\item \textbf{Ch.1--3 Non-Parametric Tests}：当数据不满足正态、连续、独立等参数假设时换用基于秩次的检验（Wilcoxon / Mann--Whitney U / Kruskal--Wallis / McNemar / Kolmogorov--Smirnov / Friedman）。
\item \textbf{Ch.4 ANCOVA + Latin Square}：方差分析的两个扩展——ANCOVA 在 ANOVA 之外引入连续协变量控制混杂；Latin Square 是控制两个 nuisance 因子的实验设计。
\item \textbf{Ch.5 Nested ANOVA + MANOVA}：处理层级结构数据（嵌套），以及多个因变量同时考察组间差异（MANOVA）。
\item \textbf{Ch.6 Logistic Regression}：因变量是二分类时的回归（连续 OLS 不适用），用对数几率（log-odds）建模。
\item \textbf{Ch.7 Multi-Linear Regression}：基础但最常用——用多个解释变量预测连续因变量，注意多重共线性、残差诊断、影响点。
\end{itemize}

\textbf{期末（第 8--13 章）}：
\begin{itemize}
\item \textbf{Ch.8 Moderation \& Mediation}：调节是「$X \to Y$ 的强度受 $Z$ 影响」（交互项），中介是「$X \to M \to Y$」（间接效应 + bootstrap CI）。
\item \textbf{Ch.9 Correlation \& Distance}：区分零阶 / 偏 / 半偏相关，以及聚类前要懂的距离度量（欧氏 / 马氏 / 曼哈顿）。
\item \textbf{Ch.10 PCA / EFA}：两种降维。PCA 是纯几何变换；EFA 假设潜在心理结构驱动观测变量。
\item \textbf{Ch.11 Reliability Analysis}：量表信度，Cronbach $\alpha$、$\omega$、test-retest 等。
\item \textbf{Ch.12 Survival Analysis}：Kaplan--Meier 估计 + Cox 比例风险模型，处理含右删失的时间-事件数据。
\item \textbf{Ch.13 Clustering}：无监督学习入门，层次聚类 vs $k$-means，加上 $k$ 选择的轮廓系数 / elbow / 平行分析。
\end{itemize}
\end{keypoint}

\begin{concept}[这门课的核心脉络]
心理统计 II 的中心问题是：\textbf{当 ANOVA / OLS 的基本假设不满足时，统计推断要怎么变形？}

\begin{itemize}
\item 数据是\textbf{秩次而非真值}（小样本 / 离散）$\Rightarrow$ Ch.1--3 非参数
\item 因子有\textbf{层级结构}或\textbf{多个因变量}并行 $\Rightarrow$ Ch.5 嵌套 / MANOVA
\item 因变量\textbf{不是连续}（二分 / 计数 / 含删失）$\Rightarrow$ Ch.6 Logistic、Ch.12 Survival
\item 想理解\textbf{中间机制}而不只是关联 $\Rightarrow$ Ch.8 中介
\item 变量\textbf{太多需要降维}（高维 / 多重共线）$\Rightarrow$ Ch.10 PCA / EFA
\item 想发现\textbf{隐藏分组}而不是验证已知组 $\Rightarrow$ Ch.13 聚类
\end{itemize}

每一种方法都对应一类「假设违反」的修补——这是把整门课记牢的最稳骨架。
\end{concept}

\textbf{阅读建议}：每个 r-tutorial 单章页（如
\texttt{/research/r-tutorials/r-pca}）有更详细的\textbf{核心概念框}（concept）和\textbf{易踩的坑}（pitfall），想精读某个方法的同学可以单独跳读对应单章。本文是「全景代码菜谱」。

\newpage

\hypertarget{ux7b2cux4e00ux90e8ux5206ux671fux4e2dux8303ux56f417-ux7ae0}{%
\section{第一部分：期中范围（1--7
章）}\label{ux7b2cux4e00ux90e8ux5206ux671fux4e2dux8303ux56f417-ux7ae0}}

\hypertarget{non-parametric-tests}{%
\section{Non-Parametric Tests}\label{non-parametric-tests}}

Parametric statistics assumes data coming from a type of probability
distribution (i.e., the parametric form of the distribution) and makes
inferences about the parameters of the distribution. Estimation is about
estimating the parameters; Hypothesis testing is about deriving the
sampling distribution of a test statistic (e.g., t, F).

Non-parametric statistics uses \textbf{distribution-free} methods which
do not rely on assumptions that the data are drawn from a given
probability distribution (don't rely on the estimation of parameters).

The disadvantage of non-parametric tests is that they're less powerful!
The probability of rejecting the null hypothesis is lower, and also a
higher probability to make type II error when a true effect or
difference exists.

Non-parametric tests are used when:

\begin{itemize}
\tightlist
\item
  Non-normality or skewness
\item
  Outliers
\item
  Small sample sizes Thus don't trust normality assumptions
\item
  \textbf{Ordinal} or \textbf{Nominal} data
\end{itemize}

\hypertarget{one-sample}{%
\subsection{One Sample}\label{one-sample}}

\hypertarget{chi-square-test}{%
\subsubsection{Chi-Square Test}\label{chi-square-test}}

The chi-square test is a statistical hypothesis test that is used to
determine \textbf{if there is a significant association between two
categorical variables}. It is a non-parametric test, which means that it
does not assume any particular distribution of the data.

The test involves comparing the \emph{observed frequencies} of different
categories with the \emph{expected frequencies}, assuming that there is
no association between the two variables. The expected frequencies are
calculated based on the assumption of \textbf{independence} between the
two variables.

The test statistic is calculated as

\[
\begin{aligned}
\chi^2&=\sum^C\frac{(f_o-f_e)^2}{f_e}\\
C=\#\text{Categories},\ &f_o=\text{observed freq}.,\ f_e=\text{expected freq.}
\end{aligned}
\]

Note that the degree of freedom for chi-square statistic does not depend
on the sample size, but only on number of categories.

The resulting value is then compared to the chi-square distribution. If
the calculated chi-square value is greater than the critical value from
the chi-square distribution, then the null hypothesis of independence is
rejected, and it can be concluded that there is a significant
association between the two variables.

Note that for chi-square test, under each category (bin), the sample
size should be no less than 5, otherwise you have to merge some adjacent
categories or use less bins.

\hypertarget{goodness-of-fit}{%
\paragraph{Goodness of Fit}\label{goodness-of-fit}}

The function takes one or more contingency tables as input and produces
a chi-square test result object that contains the test statistic,
degrees of freedom, p-value, and expected frequencies.

Here is the general syntax for using the chisq.test() function:

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{chisq.test}\NormalTok{(x, }\AttributeTok{y =} \ConstantTok{NULL}\NormalTok{, }
           \AttributeTok{correct =} \ConstantTok{TRUE}\NormalTok{, }
           \AttributeTok{p =} \ConstantTok{NULL}\NormalTok{, }\AttributeTok{rescale.p =} \ConstantTok{FALSE}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

The \texttt{x} argument is the contingency table or data that is used to
perform the chi-square test. If \texttt{y} is specified, a test for
independence is performed, and if \texttt{y} is not specified, a
goodness of fit test is performed.

The \texttt{correct} argument is a logical value that indicates whether
to apply a continuity correction to the test statistic. The \texttt{p}
argument specifies the expected probabilities for the goodness of fit
test. The \texttt{rescale.p} argument is a logical value that indicates
whether to rescale the expected probabilities to sum to 1 (if
necessary).

The most useful example for the test of goodness of fit is in linear
models. Suppose with the well-known dataset \texttt{mtcars}, we're to
check the fitting effect between predicted value and the real value,
regressing \texttt{mpg} on \texttt{wt}.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{chisq.test}\NormalTok{(}\FunctionTok{fitted}\NormalTok{(}\FunctionTok{lm}\NormalTok{(mpg}\SpecialCharTok{\textasciitilde{}}\NormalTok{wt,}\AttributeTok{data =}\NormalTok{ mtcars)),}
\NormalTok{           mtcars}\SpecialCharTok{$}\NormalTok{mpg)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Pearson's Chi-squared test
## 
## data:  fitted(lm(mpg ~ wt, data = mtcars)) and mtcars$mpg
## X-squared = 682.67, df = 672, p-value = 0.3792
\end{verbatim}

\hypertarget{independece-test}{%
\paragraph{Independece test}\label{independece-test}}

It's beautiful for the logic under the independence test, and its
relationship with test of goodness of fit.

Here, we've successfully \textbf{transformed a problem of correlation
into a problem of the similarity of distributions}. And such
transformation is interesting and not twisted at all.

Suppose we have data on 500 individuals regarding their smoking habits
and whether they have lung cancer. The data is summarized in the
following contingency table:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{smoking }\OtherTok{\textless{}{-}} \FunctionTok{c}\NormalTok{(}\StringTok{"Never"}\NormalTok{, }\StringTok{"Former"}\NormalTok{, }\StringTok{"Current"}\NormalTok{)}
\NormalTok{cancer }\OtherTok{\textless{}{-}} \FunctionTok{c}\NormalTok{(}\StringTok{"Yes"}\NormalTok{, }\StringTok{"No"}\NormalTok{)}
\NormalTok{observed }\OtherTok{\textless{}{-}} \FunctionTok{matrix}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\DecValTok{50}\NormalTok{, }\DecValTok{200}\NormalTok{, }\DecValTok{100}\NormalTok{, }\DecValTok{100}\NormalTok{, }\DecValTok{50}\NormalTok{, }\DecValTok{0}\NormalTok{),}
                   \AttributeTok{nrow =} \DecValTok{3}\NormalTok{, }\AttributeTok{byrow =} \ConstantTok{TRUE}\NormalTok{,}
                   \AttributeTok{dimnames =} \FunctionTok{list}\NormalTok{(smoking, cancer))}
\NormalTok{observed}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##         Yes  No
## Never    50 200
## Former  100 100
## Current  50   0
\end{verbatim}

In this table, the rows represent the smoking status of the individuals
(\texttt{Never}, \texttt{Former}, \texttt{Current}), and the columns
represent whether they have lung cancer (\texttt{Yes}, \texttt{No}).

Pass a contingency table into the \texttt{chisq.test()} directly and see
the result, testing whether smoking is associated with lung cancer.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{smoke }\OtherTok{=} \FunctionTok{chisq.test}\NormalTok{(observed)}
\NormalTok{smoke}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Pearson's Chi-squared test
## 
## data:  observed
## X-squared = 125, df = 2, p-value < 2.2e-16
\end{verbatim}

Moreover, we can also inspect the \emph{expected} frequencies of the
contingency table using the \texttt{result\$expected} command. This will
give us a matrix of the expected frequencies under the assumption of
independence between smoking and lung cancer.

This will undoubtedly save great efficiency, in case that it's tested by
the teacher, otherwise there's gonna be a huge efficiency loss.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{smoke}\SpecialCharTok{$}\NormalTok{expected}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##         Yes  No
## Never   100 150
## Former   80 120
## Current  20  30
\end{verbatim}

\hypertarget{effect-size}{%
\paragraph{Effect Size}\label{effect-size}}

\texttt{rstatix::cramer\_v()} is a function in R that is used to compute
the Cramer's V statistic, which is a measure of the association between
two categorical variables. Cramer's V is a normalized version of the
chi-square statistic and is often used as a measure of effect size in
chi-square tests.

The parameters of this function is much the same as
\texttt{chisq.test()}.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{rstatix}\SpecialCharTok{::}\FunctionTok{cramer\_v}\NormalTok{(observed)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] 0.5
\end{verbatim}

\includegraphics[width=1\linewidth]{cramer v}

\hypertarget{binomial-test}{%
\subsubsection{Binomial Test}\label{binomial-test}}

The binomial test is a statistical hypothesis test that is used to
determine \textbf{if the proportion of successes in a binary experiment
is significantly different from a hypothesized value}. The test assumes
that the outcome of each trial is either a success or a failure and that
the trials are independent.

In the binomial test, the null hypothesis is that the proportion of
successes is equal to a hypothesized value, and the alternative
hypothesis is that the proportion of successes is different from the
hypothesized value.

In R, the \texttt{binom.test()} function can be used to perform the
binomial test. Here is an example of how to use the function:

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# Generate a vector of binary data}
\NormalTok{toss }\OtherTok{\textless{}{-}} \FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{0}\NormalTok{)}

\CommentTok{\# Perform the binomial test}
\FunctionTok{binom.test}\NormalTok{(}\FunctionTok{sum}\NormalTok{(toss),}\FunctionTok{length}\NormalTok{(toss),}\AttributeTok{p =} \FloatTok{0.5}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Exact binomial test
## 
## data:  sum(toss) and length(toss)
## number of successes = 6, number of trials = 10, p-value = 0.7539
## alternative hypothesis: true probability of success is not equal to 0.5
## 95 percent confidence interval:
##  0.2623781 0.8784477
## sample estimates:
## probability of success 
##                    0.6
\end{verbatim}

In this example, we first generate a vector of binary data representing
the outcomes of a binary experiment. We then use the
\texttt{binom.test()} function to perform the binomial test on this
data, specifying the number of successes (\texttt{sum(data)}), the
number of trials (\texttt{length(data)}), and the hypothesized
proportion of successes (\texttt{p\ =\ 0.5}).

The output of the test will include the test statistic, the p-value, and
the confidence interval for the proportion of successes.

\hypertarget{runs-test}{%
\subsubsection{Runs Test}\label{runs-test}}

The runs test is a non-parametric test that can be used to
\textbf{assess the randomness of a set of data}. The test is based on
the number of runs in the data, where a run is defined as a sequence of
consecutive observations with the same sign.

In statistics, a ``run'' is a sequence of consecutive data points that
have the same value or the same sign. A run can be defined for any type
of data, whether it's categorical or continuous.

For example, consider the following sequence of binary data:

1 1 0 1 0 0 0 1 1 1

In this sequence, there are 4 runs: two runs of 1's, one run of 0's, and
one run of 1's.

Another example is a sequence of temperature readings over a period of
time:

20.1 20.3 20.4 20.4 20.5 20.4 20.2 20.0

In this sequence, there is one run of increasing temperatures (from 20.1
to 20.5) followed by one run of decreasing temperatures (from 20.5 to
20.0).

The null hypothesis of the runs test is that the data is random, and the
alternative hypothesis is that the data is not random. The test
statistic is based on the number of runs in the data and is compared to
the expected number of runs under the assumption of randomness.

The expected number of runs can be calculated using the following
formula:

\[
E(R)=\frac{2n_1n_2}{n_1+n_2}+1
\]

where \(n_1\) is the number of positive signs in the data, \(n_2\) is
the number of negative signs in the data.

The test statistic is given by:

\[
z=\frac{R-E(R)}{Var(R)}
\]

where \(R\) is the observed number of runs, \(E(R)\) is the expected
number of runs, and \(Var(R)\) is the variance of the number of runs.
Under the null hypothesis of randomness, the test statistic follows a
standard normal distribution.

Use \texttt{DescTools::RunsTest(data)} in R to determine whether a coin
is randomly tossed or not:

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# Generate a vector of coin toss outcomes (0 = tails, 1 = heads)}
\NormalTok{coin\_tosses }\OtherTok{\textless{}{-}} \FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{0}\NormalTok{, }
                 \DecValTok{0}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{)}

\CommentTok{\# Perform the runs test}
\NormalTok{DescTools}\SpecialCharTok{::}\FunctionTok{RunsTest}\NormalTok{(coin\_tosses)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Runs Test for Randomness
## 
## data:  coin_tosses
## runs = 11, m = 7, n = 9, p-value = 0.3024
## alternative hypothesis: true number of runs is not equal the expected number
\end{verbatim}

\hypertarget{ks-test}{%
\subsubsection{KS Test}\label{ks-test}}

The Kolmogorov-Smirnov (KS) test is a nonparametric statistical test
that is used to compare the distributions of two samples or to test
whether a sample follows a specific distribution. The KS test is based
on the maximum difference between the empirical cumulative distribution
functions (ECDFs) of the two samples.

The KS test can be used to test the null hypothesis that the two samples
are drawn from the same distribution or that a single sample is drawn
from a specific distribution. The test is based on the KS statistic,
which is defined as the maximum vertical difference between the ECDFs of
the two samples.

To perform the KS test in R, you can use the \texttt{ks.test()}
function, which is part of the base R package. Here's the syntax for the
\texttt{ks.test()} function:

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{ks.test}\NormalTok{(x, y, alternative, }
        \AttributeTok{exact =} \ConstantTok{NULL}\NormalTok{, }
        \AttributeTok{conf.level =} \FloatTok{0.95}\NormalTok{, ...)}
\end{Highlighting}
\end{Shaded}

where \texttt{x} and \texttt{y} are the two samples to be compared,
\texttt{alternative} is a character string specifying the alternative
hypothesis (either ``two.sided'', ``less'', or ``greater''),
\texttt{exact} is a logical value indicating whether to use the exact
distribution of the KS statistic (for small sample sizes), and
\texttt{conf.level} is the confidence level for the test.

Here's an example of how to perform the KS test in R using the
\texttt{ks.test()} function:

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# Generate two samples of data from normal distributions}
\FunctionTok{set.seed}\NormalTok{(}\DecValTok{123}\NormalTok{)}
\NormalTok{x }\OtherTok{\textless{}{-}} \FunctionTok{rnorm}\NormalTok{(}\DecValTok{100}\NormalTok{, }\AttributeTok{mean =} \DecValTok{0}\NormalTok{, }\AttributeTok{sd =} \DecValTok{1}\NormalTok{)}
\NormalTok{y }\OtherTok{\textless{}{-}} \FunctionTok{rnorm}\NormalTok{(}\DecValTok{100}\NormalTok{, }\AttributeTok{mean =} \FloatTok{0.8}\NormalTok{, }\AttributeTok{sd =} \DecValTok{1}\NormalTok{)}

\CommentTok{\# Perform the KS test}
\FunctionTok{ks.test}\NormalTok{(x, y)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Asymptotic two-sample Kolmogorov-Smirnov test
## 
## data:  x and y
## D = 0.27, p-value = 0.001365
## alternative hypothesis: two-sided
\end{verbatim}

In this example, we first generate two samples of data from normal
distributions with different means. We then use the \texttt{ks.test()}
function to perform the KS test on the two samples. The function returns
a list containing the test statistic, the p-value, and the alternative
hypothesis.

Moreover, when comparing your data to a specific distribution, you need
to specify the distribution you want to compare your data to using the
\texttt{y} argument, and also provide the parameters of the distribution
using additional arguments \texttt{...}. For example, if you want to
compare your data to a normal distribution, you can provide the mean and
standard deviation of the normal distribution using the mean and sd
arguments, respectively. If you want to compare your data to a uniform
distribution, you can provide the minimum and maximum values of the
uniform distribution using the min and max arguments, respectively.

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# Generate a sample of data}
\FunctionTok{set.seed}\NormalTok{(}\DecValTok{123}\NormalTok{)}
\NormalTok{x }\OtherTok{\textless{}{-}} \FunctionTok{rnorm}\NormalTok{(}\DecValTok{100}\NormalTok{, }\AttributeTok{mean =} \DecValTok{0}\NormalTok{, }\AttributeTok{sd =} \DecValTok{1}\NormalTok{)}

\CommentTok{\# Perform the KS test against a normal distribution}
\FunctionTok{ks.test}\NormalTok{(x, }\StringTok{"pnorm"}\NormalTok{, }\AttributeTok{mean =} \FunctionTok{mean}\NormalTok{(x), }\AttributeTok{sd =} \FunctionTok{sd}\NormalTok{(x))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  x
## D = 0.058097, p-value = 0.8884
## alternative hypothesis: two-sided
\end{verbatim}

\hypertarget{kendalls-tau}{%
\subsubsection{Kendall's Tau}\label{kendalls-tau}}

Kendall's tau is a non-parametric measure of association for
\textbf{ordinal} data that quantifies the strength and
\textbf{direction} of the relationship between two variables. Kendall's
tau is particularly useful when the variables being studied are ordinal
or ranked.

Kendall's tau is based on the difference between the number of
concordant and discordant pairs of observations. A concordant pair is a
pair in which the ranks of both variables are in the same direction
(i.e., both are either increasing or decreasing). A discordant pair is a
pair in which the ranks of the variables are in the opposite direction
(i.e., one is increasing while the other is decreasing).

Kendall's tau coefficient ranges from -1 to +1, with values of -1
indicating a perfect negative association, 0 indicating no association,
and +1 indicating a perfect positive association. The formula for
Kendall's tau is:

\includegraphics[width=1\linewidth]{kendall's tau}

In R, you can calculate Kendall's tau using the \texttt{cor.test()}
functionwith the argument \texttt{method\ =\ "kendall"}. The output of
the test provides the estimated Kendall's tau coefficient, along with
the p-value and confidence interval for the test.

In this example, we have ten companies in the tech industry and their
ranks of revenue for two different years.

\begin{longtable}[]{@{}lll@{}}
\toprule\noalign{}
Company & Year 1 Rank & Year 2 Rank \\
\midrule\noalign{}
\endhead
\bottomrule\noalign{}
\endlastfoot
Apple & 1 & 2 \\
Microsoft & 2 & 1 \\
Amazon & 3 & 3 \\
Alphabet & 4 & 4 \\
Facebook & 5 & 5 \\
Intel & 6 & 6 \\
IBM & 7 & 7 \\
Samsung & 8 & 9 \\
Cisco & 9 & 8 \\
Qualcomm & 10 & 10 \\
\end{longtable}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{rank\_data }\OtherTok{\textless{}{-}} \FunctionTok{data.frame}\NormalTok{(}
  \AttributeTok{Company =} \FunctionTok{c}\NormalTok{(}\StringTok{"Apple"}\NormalTok{, }\StringTok{"Microsoft"}\NormalTok{, }\StringTok{"Amazon"}\NormalTok{, }
              \StringTok{"Alphabet"}\NormalTok{, }\StringTok{"Facebook"}\NormalTok{, }\StringTok{"Intel"}\NormalTok{, }
              \StringTok{"IBM"}\NormalTok{, }\StringTok{"Samsung"}\NormalTok{, }\StringTok{"Cisco"}\NormalTok{, }\StringTok{"Qualcomm"}\NormalTok{),}
  \AttributeTok{Year\_1\_Rank =} \FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{5}\NormalTok{, }\DecValTok{6}\NormalTok{, }\DecValTok{7}\NormalTok{, }\DecValTok{8}\NormalTok{, }\DecValTok{9}\NormalTok{, }\DecValTok{10}\NormalTok{),}
  \AttributeTok{Year\_2\_Rank =} \FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{3}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{5}\NormalTok{, }\DecValTok{6}\NormalTok{, }\DecValTok{7}\NormalTok{, }\DecValTok{9}\NormalTok{, }\DecValTok{8}\NormalTok{, }\DecValTok{10}\NormalTok{)}
\NormalTok{)}
\FunctionTok{cor.test}\NormalTok{(rank\_data}\SpecialCharTok{$}\NormalTok{Year\_1\_Rank, }
\NormalTok{         rank\_data}\SpecialCharTok{$}\NormalTok{Year\_2\_Rank, }
         \AttributeTok{method =} \StringTok{"kendall"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Kendall's rank correlation tau
## 
## data:  rank_data$Year_1_Rank and rank_data$Year_2_Rank
## T = 43, p-value = 2.976e-05
## alternative hypothesis: true tau is not equal to 0
## sample estimates:
##       tau 
## 0.9111111
\end{verbatim}

The high tau and highly significant result indicate that there's a
innegligible great relationship.

\hypertarget{kendalls-w}{%
\subsubsection{Kendall's W}\label{kendalls-w}}

Kendall's W is a measure of agreement among multiple raters or judges
who are assessing the same set of items or objects. Kendall's W is based
on the number of concordant and discordant triples of ratings among the
raters.

Kendall's W ranges from 0 to 1, with values of 0 indicating no agreement
among the raters and 1 indicating perfect agreement. The formula for
Kendall's W is:

\[
W = (n_c - n_d) / (n_c + n_d + n_t)
\] where \(n_c\) is the number of concordant triples, \(n_d\) is the
number of discordant triples, and \(n_t\) is the number of tied triples.

In another form,

\[
W=\frac{\sum_iR_i^2-\frac{(\sum_iR_i)^2}{N}}{\frac1{12}M^2(N^3-N)}
\]

where \(R_i\) is the total ranking of \emph{i}th subject.

Kendall's W can be converted into Spearman correlation.

\[
r_{spearman}=\frac{MW-1}{M-1}
\]

In R, you can calculate Kendall's W using the
\texttt{irr::kendall(ratings)} function, where rating is a \(n*m\)
matrix, \(n\) subjects and \(m\) raters.

Here's an example of how to use the \texttt{irr::kendall()} function to
calculate Kendall's W coefficient:

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# Create a data frame with ratings from three judges for four items}
\NormalTok{ratings }\OtherTok{\textless{}{-}} \FunctionTok{data.frame}\NormalTok{(}
  \AttributeTok{Item =} \FunctionTok{c}\NormalTok{(}\StringTok{"A"}\NormalTok{, }\StringTok{"B"}\NormalTok{, }\StringTok{"C"}\NormalTok{, }\StringTok{"D"}\NormalTok{),}
  \AttributeTok{Judge\_1 =} \FunctionTok{c}\NormalTok{(}\DecValTok{3}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{1}\NormalTok{),}
  \AttributeTok{Judge\_2 =} \FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{3}\NormalTok{),}
  \AttributeTok{Judge\_3 =} \FunctionTok{c}\NormalTok{(}\DecValTok{3}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{4}\NormalTok{))}

\NormalTok{irr}\SpecialCharTok{::}\FunctionTok{kendall}\NormalTok{(}\AttributeTok{ratings =}\NormalTok{ ratings)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##  Kendall's coefficient of concordance W
## 
##  Subjects = 4 
##    Raters = 4 
##         W = 0.225 
## 
##  Chisq(3) = 2.7 
##   p-value = 0.44
\end{verbatim}

Meanwhile, a chi-square test is carried for each subject according to
their sum of rankings. If significant, there exists a difference between
their performance.

Note that there's no signal about the significance about W, only that
about \(\chi^2\).

\hypertarget{two-samples}{%
\subsection{Two Samples}\label{two-samples}}

\hypertarget{related-samples}{%
\subsubsection{Related Samples}\label{related-samples}}

\hypertarget{wilcoxon-signed-ranks-test}{%
\paragraph{Wilcoxon Signed Ranks
Test}\label{wilcoxon-signed-ranks-test}}

The Wilcoxon Signed Ranks test is a nonparametric statistical test used
to determine whether the \textbf{median} of a paired difference between
two related samples is statistically different from zero. It is used
when the data does not meet the assumptions of normality required for a
parametric test like the paired t-test.

The test is performed by first calculating the differences between the
paired observations, then ranking the absolute values of these
differences. The sum of the ranks of the positive differences is then
compared to the sum of the ranks of the negative differences, and take
the smaller one as the statistic called \(T\). The null hypothesis is
that the median difference is zero, while the alternative hypothesis is
that it is not zero.

In R, you can perform the Wilcoxon Signed Ranks test using the
\texttt{wilcox.test()} function. \texttt{wilcox.test()} is a function in
R that performs the \textbf{Wilcoxon rank test}, or the
\textbf{Mann-Whitney U test}. When conducting the former,
\texttt{paired\ =\ T}; the latter, \texttt{paired\ =\ F}. Just specify
the two vectors as the first two parameters.

Here's an example of how to use the \texttt{wilcox.test()} function to
perform the test on two related samples. Suppose we are interested in
comparing the effectiveness of two different weight loss programs. We
have a sample of 10 individuals who have completed both programs. We
measure each individual's weight before and after each program, and we
want to determine whether there is a significant difference in weight
loss between the two programs.

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# Create two related samples}
\NormalTok{weightloss }\OtherTok{\textless{}{-}} \FunctionTok{data.frame}\NormalTok{(}
  \AttributeTok{individual =} \DecValTok{1}\SpecialCharTok{:}\DecValTok{10}\NormalTok{,}
  \AttributeTok{program\_A =} \FunctionTok{c}\NormalTok{(}\DecValTok{185}\NormalTok{, }\DecValTok{190}\NormalTok{, }\DecValTok{200}\NormalTok{, }\DecValTok{175}\NormalTok{, }\DecValTok{195}\NormalTok{, }\DecValTok{180}\NormalTok{, }\DecValTok{170}\NormalTok{, }\DecValTok{185}\NormalTok{, }\DecValTok{195}\NormalTok{, }\DecValTok{200}\NormalTok{),}
  \AttributeTok{program\_B =} \FunctionTok{c}\NormalTok{(}\DecValTok{180}\NormalTok{, }\DecValTok{185}\NormalTok{, }\DecValTok{195}\NormalTok{, }\DecValTok{170}\NormalTok{, }\DecValTok{190}\NormalTok{, }\DecValTok{175}\NormalTok{, }\DecValTok{165}\NormalTok{, }\DecValTok{180}\NormalTok{, }\DecValTok{190}\NormalTok{, }\DecValTok{195}\NormalTok{)}
\NormalTok{)}

\CommentTok{\# Perform the Wilcoxon Signed Ranks test}
\FunctionTok{wilcox.test}\NormalTok{(weightloss}\SpecialCharTok{$}\NormalTok{program\_A,weightloss}\SpecialCharTok{$}\NormalTok{program\_B, }\AttributeTok{paired =} \ConstantTok{TRUE}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  weightloss$program_A and weightloss$program_B
## V = 55, p-value = 0.001904
## alternative hypothesis: true location shift is not equal to 0
\end{verbatim}

Note:

\begin{itemize}
\tightlist
\item
  Faced with tied values, typically use the averge rank.
\item
  Faced with zero difference, typically keep the observation. Deleting
  data is always a concern! Zero difference observation can participate
  in computing \(T\) and influence the degree of freedom.
\end{itemize}

\hypertarget{mcnemar-test}{%
\paragraph{McNemar Test}\label{mcnemar-test}}

In the context of the McNemar Test, the off-diagonal terms refer to the
counts of the two categories that are inconsistent between the two
paired measurements. Specifically, they refer to the counts of the
following two categories:

\begin{itemize}
\item
  False positives (FP): The number of cases where the first measurement
  is negative, but the second measurement is positive.
\item
  False negatives (FN): The number of cases where the first measurement
  is positive, but the second measurement is negative.
\end{itemize}

For example, consider a medical study that measures the presence or
absence of a certain disease in a group of patients before and after
treatment. The 2x2 contingency table for this study might look like
this:

\begin{verbatim}
           After Treatment
           Positive Negative
Before Treatment
Positive      a       b
Negative      c       d
\end{verbatim}

In this table, \texttt{a} represents the number of patients who tested
positive for the disease both before and after treatment (true
positives), \texttt{d} represents the number of patients who tested
negative for the disease both before and after treatment (true
negatives), \texttt{b} represents the number of patients who tested
negative for the disease before treatment but positive after treatment
(false positives), and \texttt{c} represents the number of patients who
tested positive for the disease before treatment but negative after
treatment (false negatives).

The off-diagonal terms in the McNemar Test are used because they
represent the counts of the inconsistent categories between two paired
measurements. These inconsistent categories are the false positives (FP)
and false negatives (FN), which represent the cases where the two
measurements disagree.

In the context of the McNemar Test, the goal is to determine whether
there is a significant difference between the proportions of the
inconsistent categories, namely FP and FN. This is done by comparing the
observed counts of FP and FN to the expected counts under the null
hypothesis of no difference. The expected counts are calculated assuming
that the marginal totals of the contingency table are fixed, which is a
characteristic of paired data.

So the key difference between the Chi-Square Test on a Contingency Table
and the McNemar Test is the question that each test addresses. The
former tests for independence between two categorical variables, while
the latter tests for a significant difference between two correlated
proportions. The two tests have different assumptions and are used in
different contexts, but both use the chi-squared statistic to calculate
the test statistic.

Here's an example of the McNemar Test being used to analyze a medical
experiment in R. In this example, we will examine the effectiveness of a
new treatment for a certain disease compared to a standard treatment.

Suppose we have a sample of 50 patients who have been diagnosed with the
disease. We randomly assign each patient to receive either the new
treatment (Treatment A) or the standard treatment (Treatment B). After
the treatment, we measure whether the patient's symptoms have improved
or not. The results are summarized in the following 2x2 contingency
table:

\begin{verbatim}
           After Treatment
           Improved Not Improved
Before Treatment
Improved        15        10
Not Improved    5        20
\end{verbatim}

In this table, the rows represent the before-treatment condition and the
columns represent the after-treatment condition. The cells represent the
number of patients in each category. For example, 15 patients improved
after receiving Treatment A, while 10 did not improve.

To analyze these data using the McNemar Test in R, we can use the
\texttt{mcnemar.test()} function. Here's the R code:

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# Create a 2x2 contingency table of the data}
\NormalTok{medicine }\OtherTok{\textless{}{-}} \FunctionTok{matrix}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\DecValTok{15}\NormalTok{, }\DecValTok{5}\NormalTok{, }\DecValTok{10}\NormalTok{, }\DecValTok{20}\NormalTok{), }
               \AttributeTok{nrow =} \DecValTok{2}\NormalTok{, }
               \AttributeTok{dimnames =} \FunctionTok{list}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\StringTok{"Improved"}\NormalTok{, }\StringTok{"Not Improved"}\NormalTok{), }
                               \FunctionTok{c}\NormalTok{(}\StringTok{"Improved"}\NormalTok{, }\StringTok{"Not Improved"}\NormalTok{)))}

\CommentTok{\# Perform the McNemar Test}
\FunctionTok{mcnemar.test}\NormalTok{(medicine)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  McNemar's Chi-squared test with continuity correction
## 
## data:  medicine
## McNemar's chi-squared = 1.0667, df = 1, p-value = 0.3017
\end{verbatim}

In this code, we first create a 2x2 contingency table of the data using
the \texttt{matrix()} function. We then use the \texttt{mcnemar.test()}
function with the contingency table as the input. The function returns a
list of information about the test, including the chi-squared statistic,
the degrees of freedom, and the p-value.

In the example above, we cannot reject the null hypothesis that there is
no significant difference between the proportions of patients who
improved after receiving Treatment A compared to Treatment B. McNemar
test is used to compare the proportions of patients who improved after
receiving a new treatment compared to a standard treatment. The output
of the test provides information about the significance of the
difference between the proportions, which can inform decisions about the
effectiveness of the treatments.

\hypertarget{sign-test}{%
\paragraph{Sign Test}\label{sign-test}}

The Sign Test is a non-parametric statistical test that is used to
compare two related samples or matched pairs in which the data are
measured on an ordinal or continuous scale. The test is based on the
distribution of the signs of the differences between the paired
observations.

The Sign Test is used when the data do not meet the assumptions of a
parametric test, such as the paired t-test, which assumes that the data
are normally distributed. The Sign Test is more robust to outliers and
non-normality, and it does not require knowledge of the distribution of
the data.

The Sign Test is a one-sample test, meaning that it compares the
differences between the paired observations to a specified value, such
as zero. The null hypothesis is that the median difference between the
paired observations is equal to zero, while the alternative hypothesis
is that the median difference is not equal to zero.

To perform the Sign Test, we first calculate the differences between the
paired observations. We then assign a sign (+ or -) to each difference,
depending on whether the observation in the first sample is greater or
less than the observation in the second sample. We then count the number
of positive and negative signs, and calculate the test statistic as the
minimum of the two counts. The test statistic follows a binomial
distribution with the number of pairs as the sample size and a
probability of 0.5 under the null hypothesis.

The p-value of the test can be calculated using the \textbf{binomial}
distribution, or it can be \emph{approximated} using a \emph{normal}
approximation. If the p-value is less than the chosen significance
level, the null hypothesis is rejected, and the conclusion is that there
is a significant difference between the two related samples.

The Sign Test can be useful in a variety of applications, including
clinical trials, psychology experiments, and ecological studies. It is a
simple and robust method for comparing two related samples when the data
do not meet the assumptions of a parametric test.

There's no \texttt{sign.test()} in R, but that can be accomplished
``manually'' through \texttt{binomial.test()}, since the statistic
follows such distribution. We only have to assign the signs of positive
or negative according to the differences.

Same with the ``weightloss'' example in the Wilcoxon test, we'll
continue that in the sign test.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{binom.test}\NormalTok{(}\AttributeTok{x =} \FunctionTok{sum}\NormalTok{(weightloss}\SpecialCharTok{$}\NormalTok{program\_A }\SpecialCharTok{\textgreater{}}\NormalTok{ weightloss}\SpecialCharTok{$}\NormalTok{program\_B),}
               \AttributeTok{n =} \FunctionTok{nrow}\NormalTok{(weightloss))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Exact binomial test
## 
## data:  sum(weightloss$program_A > weightloss$program_B) and nrow(weightloss)
## number of successes = 10, number of trials = 10, p-value = 0.001953
## alternative hypothesis: true probability of success is not equal to 0.5
## 95 percent confidence interval:
##  0.6915029 1.0000000
## sample estimates:
## probability of success 
##                      1
\end{verbatim}

Note that the tests for signed ranks of related samples may vary a lot
in terms of their significance level, which largely indicates that the
effectiveness of nonparametric tests may be limited. However, the
\textbf{Wilcoxon test} is generally more recognized.

\hypertarget{independent-samples}{%
\subsubsection{Independent Samples}\label{independent-samples}}

\hypertarget{mann-whitney-u-test}{%
\paragraph{Mann-Whitney U Test}\label{mann-whitney-u-test}}

The Mann-Whitney U Test, is a non-parametric statistical test that is
used to compare the \textbf{medians} of two \textbf{independent}
samples.

The Mann-Whitney U Test is based on the rank sums of the two samples. To
perform the Mann-Whitney U Test, we first combine the two samples and
rank the observations from lowest to highest, assigning the same rank to
ties. We then calculate the sum of the ranks for each sample. The test
statistic is then calculated as the smaller of the two rank sums, \(U\).
The test statistic follows a normal distribution with a mean and
variance that can be calculated from the sample sizes and the \(U\)
statistic. Below is an example.

\includegraphics[width=1\linewidth]{Mann-Whitney U Rank}

The p-value of the test can be calculated using the normal distribution,
or it can be approximated using tables or software. If the p-value is
\textbf{less} than the chosen significance level, the null hypothesis is
rejected, and the conclusion is that there is a significant difference
between the two independent samples.

It is worth noting that the Mann-Whitney U Test is different from the
Wilcoxon Signed-Rank Test. \textbf{The Mann-Whitney U Test is used for
independent samples, while the Wilcoxon Signed-Rank Test is used for
paired samples.}

Here, we assume that in the example provided in the ``weightloss'' part,
the two data in two programs correspond to two groups of people, and
thus independent.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{wilcox.test}\NormalTok{(weightloss}\SpecialCharTok{$}\NormalTok{program\_A,}
\NormalTok{            weightloss}\SpecialCharTok{$}\NormalTok{program\_B,}
            \AttributeTok{paired =}\NormalTok{ F)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  weightloss$program_A and weightloss$program_B
## W = 64, p-value = 0.3029
## alternative hypothesis: true location shift is not equal to 0
\end{verbatim}

\hypertarget{ks-z-test}{%
\paragraph{KS Z Test}\label{ks-z-test}}

Compared to KS test before, this is a more ``general'' version. KS Z
test compares two distributions against each other, while KS test,
strictly speaking, compares a distribution against a theoretical
distribution.

Use the same function \texttt{ks.test()} as before, all others the same,
so \emph{no} need to cover it again here.

Moreover, it's interesting to compare KS test and Mann-Whitney U test.

\begin{itemize}
\tightlist
\item
  The Mann-Whitney test first ranks all the values from low to high, and
  then computes a P value that depends on the discrepancy between the
  mean ranks of the two groups.
\item
  The Kolmogorov-Smirnov test compares the cumulative distribution of
  the two data sets, and computes a \(p\) value that depends on the
  largest.
\end{itemize}

Guidelines for choosing between the two tests.

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\tightlist
\item
  The KS test is sensitive to any differences in the two distributions.
  Substantial differences in shape, spread or median will result in a
  small P value. In contrast, the \textbf{MW test is mostly sensitive to
  changes in the median}.
\item
  The MW test is used more often and is recognized by more people, so
  choose it if you have no idea which to choose.
\item
  The MW test has been extended to handle tied values. The KS test does
  not handle ties so well. If your data are categorical, so has many
  ties, don't choose the KS test.
\item
  Some fields of science tend to prefer the KS test over the MW test. It
  makes sense to follow the traditions of your field.
\end{enumerate}

\hypertarget{moses-extreme-reaction}{%
\paragraph{Moses Extreme Reaction}\label{moses-extreme-reaction}}

\begin{itemize}
\item
  Check the span of the rank of one group when two groups are mixed and
  ranked (up and low 5\% deleted).
\item
  \textbf{Good for distributions with mass on both ends}.

  But less powerful and robust as KS test.
\end{itemize}

In the case of MER, the null hypothesis is that there is no difference
in gene expression levels between individuals with extreme reactions and
those with normal reactions. The alternative hypothesis is that there is
a significant difference in gene expression levels between the two
groups.

The \textbf{range} is tested, so the MER has different
\emph{sensitivity} for different distributions.

Use \texttt{DescTools::MosesTest(x,y,extreme)} to carry the MER. If with
a grouping variable, parameters can be passed through a formula-pattern.
\texttt{extreme} allows you to rule out some extreme values.

In the example provided below, the data record the effect of two kinds
of pills. \texttt{ycss} is the effect, and \texttt{zb} is the grouping
label.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{hypnotic }\OtherTok{\textless{}{-}}\NormalTok{ haven}\SpecialCharTok{::}\FunctionTok{read\_sav}\NormalTok{(}\StringTok{"Data12{-}06.sav"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

The MER is especially useful for medical experiments.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{DescTools}\SpecialCharTok{::}\FunctionTok{MosesTest}\NormalTok{(ycss}\SpecialCharTok{\textasciitilde{}}\NormalTok{zb,}\AttributeTok{data =}\NormalTok{ hypnotic)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Moses Test of Extreme Reactions
## 
## data:  ycss by zb
## S = 15, p-value = 0.6858
## alternative hypothesis: extreme values are more likely in x than in y
\end{verbatim}

\hypertarget{wald-wolfowitz-runs}{%
\paragraph{Wald-Wolfowitz Runs}\label{wald-wolfowitz-runs}}

\begin{itemize}
\tightlist
\item
  Put two groups together and rank them; the group label should mix if
  they are similar.
\item
  \textbf{Good for distributions that differ more on variance than on
  location}.
\end{itemize}

Wald-Wolfowitz Runs is kind of Runs test, but the two-independent-sample
version.

Use the same function as before, \texttt{DescTools::RunsTest(x,y)} to
carry the test!

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{DescTools}\SpecialCharTok{::}\FunctionTok{RunsTest}\NormalTok{(ycss}\SpecialCharTok{\textasciitilde{}}\NormalTok{zb,}\AttributeTok{data =}\NormalTok{ hypnotic)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Wald-Wolfowitz Runs Test
## 
## data:  ycss by zb
## runs = 6, m = 10, n = 10, p-value = 0.03704
## alternative hypothesis: true number of runs is not equal the expected number
\end{verbatim}

\hypertarget{k-samples}{%
\subsection{K Samples}\label{k-samples}}

\hypertarget{dependent-samples}{%
\subsubsection{Dependent Samples}\label{dependent-samples}}

The ``dependent'' here means, a person or an item is tested for \(K\)
times.

\hypertarget{friedman-test}{%
\paragraph{Friedman Test}\label{friedman-test}}

The Friedman test is a non-parametric statistical test used to determine
whether there are \textbf{differences} among two or more groups based on
\textbf{repeated} measures data. It is often used as an alternative to
\emph{repeated measures ANOVA} when the data do not meet the assumptions
of normality or homogeneity of variance.

The Friedman test involves \emph{ranking} the data for each group and
then computing the \emph{sum of the ranks} for each condition or
treatment. The test statistic is calculated as the ratio of the sum of
squares of the ranks to the total sum of squares, and is compared to the
\emph{chi-squared} distribution with degrees of freedom equal to the
number of groups minus one. The Friedman test is appropriate when the
data are measured on an ordinal or continuous scale, but not when the
data are nominal.

If the test statistic is significant, it indicates that there are
differences among the groups. However, the Friedman test does not
identify which groups are different from each other. \emph{Post-hoc
tests}, such as the Wilcoxon signed-rank test or the Dunn's test, can be
used to identify the pairwise differences between the groups.

One of the advantages of the Friedman test is that it is robust, meaning
that it is not affected by outliers or non-normality in the data. It is
also useful for analyzing repeated measures data, where each participant
or object is tested multiple times under different conditions or
treatments.

The statistic is what we calculated as \(\chi^2\) in Kendall's W. No
need to cover here!

Note that no need to pass the pre-ranked data into \texttt{kendall()},
the raw data is fine!

\hypertarget{cochrans-q}{%
\paragraph{Cochran's Q}\label{cochrans-q}}

Cochran's Q test is a non-parametric statistical test used to analyze
the differences between \emph{three or more} \textbf{related}
categorical variables. It is similar to the McNemar test, which is used
to compare two related categorical variables.

The Cochran's Q test is used when we have a set of subjects measured on
a \textbf{binary} (dichotomous) variable at three or more time points or
under three or more conditions. The test determines if there is a
statistically significant difference in the proportion of positive
responses across the different time points or conditions.

The null hypothesis of the Cochran's Q test is that the proportion of
positive responses is the same across all time points or conditions,
while the alternative hypothesis is that at least one time point or
condition has a different proportion of positive responses.

If the Cochran's Q test indicates that there are significant differences
between the time points or conditions, post-hoc tests such as McNemar's
test or Bonferroni correction can be used to determine which time points
or conditions are significantly different from each other.

Suppose we have a courses and a group of 30 students enrolled. We
measure the pass/fail status of each student at three different time
points (midterm, final exam, and overall course grade) to determine if
there is a significant difference in the proportion of passing students
across the different time points.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{midterm }\OtherTok{\textless{}{-}} \FunctionTok{c}\NormalTok{(}\FunctionTok{rep}\NormalTok{(}\StringTok{"Pass"}\NormalTok{, }\DecValTok{5}\NormalTok{), }\FunctionTok{rep}\NormalTok{(}\StringTok{"Fail"}\NormalTok{, }\DecValTok{5}\NormalTok{), }\FunctionTok{rep}\NormalTok{(}\StringTok{"Pass"}\NormalTok{, }\DecValTok{6}\NormalTok{), }\FunctionTok{rep}\NormalTok{(}\StringTok{"Fail"}\NormalTok{, }\DecValTok{4}\NormalTok{), }\FunctionTok{rep}\NormalTok{(}\StringTok{"Pass"}\NormalTok{, }\DecValTok{8}\NormalTok{), }\FunctionTok{rep}\NormalTok{(}\StringTok{"Fail"}\NormalTok{, }\DecValTok{2}\NormalTok{))}
\NormalTok{final }\OtherTok{\textless{}{-}} \FunctionTok{c}\NormalTok{(}\FunctionTok{rep}\NormalTok{(}\StringTok{"Pass"}\NormalTok{, }\DecValTok{8}\NormalTok{), }\FunctionTok{rep}\NormalTok{(}\StringTok{"Fail"}\NormalTok{, }\DecValTok{2}\NormalTok{), }\FunctionTok{rep}\NormalTok{(}\StringTok{"Pass"}\NormalTok{, }\DecValTok{4}\NormalTok{), }\FunctionTok{rep}\NormalTok{(}\StringTok{"Fail"}\NormalTok{, }\DecValTok{6}\NormalTok{), }\FunctionTok{rep}\NormalTok{(}\StringTok{"Pass"}\NormalTok{, }\DecValTok{7}\NormalTok{), }\FunctionTok{rep}\NormalTok{(}\StringTok{"Fail"}\NormalTok{, }\DecValTok{3}\NormalTok{))}
\NormalTok{overall }\OtherTok{\textless{}{-}} \FunctionTok{c}\NormalTok{(}\FunctionTok{rep}\NormalTok{(}\StringTok{"Pass"}\NormalTok{, }\DecValTok{9}\NormalTok{), }\FunctionTok{rep}\NormalTok{(}\StringTok{"Fail"}\NormalTok{, }\DecValTok{1}\NormalTok{), }\FunctionTok{rep}\NormalTok{(}\StringTok{"Pass"}\NormalTok{, }\DecValTok{6}\NormalTok{), }\FunctionTok{rep}\NormalTok{(}\StringTok{"Fail"}\NormalTok{, }\DecValTok{4}\NormalTok{), }\FunctionTok{rep}\NormalTok{(}\StringTok{"Pass"}\NormalTok{, }\DecValTok{8}\NormalTok{), }\FunctionTok{rep}\NormalTok{(}\StringTok{"Fail"}\NormalTok{, }\DecValTok{2}\NormalTok{))}

\NormalTok{df }\OtherTok{\textless{}{-}} \FunctionTok{data.frame}\NormalTok{(midterm, final, overall) }\SpecialCharTok{|\textgreater{}} 
  \FunctionTok{gather}\NormalTok{(}\AttributeTok{key =} \StringTok{\textquotesingle{}exam\textquotesingle{}}\NormalTok{,}\AttributeTok{value =} \StringTok{\textquotesingle{}result\textquotesingle{}}\NormalTok{) }\SpecialCharTok{|\textgreater{}}
  \FunctionTok{mutate}\NormalTok{(}\AttributeTok{stu =} \FunctionTok{rep}\NormalTok{(}\FunctionTok{rep}\NormalTok{(}\DecValTok{1}\SpecialCharTok{:}\DecValTok{30}\NormalTok{),}\DecValTok{3}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

In this dataset, we have three categorical variables (midterm, final,
and overall) measured at three time points for each student. To perform
Cochran's Q test on this dataset, we can use the
\texttt{cochran\_qtest()} function from the \texttt{rstatix} package in
R:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{rstatix}\SpecialCharTok{::}\FunctionTok{cochran\_qtest}\NormalTok{(}\AttributeTok{data =}\NormalTok{ df,}\AttributeTok{formula =}\NormalTok{ result}\SpecialCharTok{\textasciitilde{}}\NormalTok{exam}\SpecialCharTok{|}\NormalTok{stu)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## # A tibble: 1 x 6
##   .y.        n statistic    df     p method          
## * <chr>  <int>     <dbl> <dbl> <dbl> <chr>           
## 1 result    30      4.57     2 0.102 Cochran's Q test
\end{verbatim}

The non-significant result here means that the three exams does not have
differences in difficulty.

\hypertarget{kendalls-w-1}{%
\paragraph{Kendall's W}\label{kendalls-w-1}}

In essence, it's a Friedman Test, no need to cover here.

\hypertarget{independent-samples-1}{%
\subsubsection{Independent Samples}\label{independent-samples-1}}

\hypertarget{kruskalwallis-test}{%
\paragraph{Kruskal--Wallis Test}\label{kruskalwallis-test}}

The Kruskal-Wallis test is a non-parametric statistical test used to
compare \emph{three or more} \textbf{independent} samples to determine
if they come from the same population or not. It is often used as an
alternative to \emph{one-way ANOVA} when the data do not meet the
assumptions of normality or homogeneity of variance.

The Kruskal-Wallis test ranks the data within each group and calculates
the sum of ranks for each group. It then uses these sums to calculate a
test statistic that measures the differences between groups. The null
hypothesis of the Kruskal-Wallis test is that the samples come from the
same population, while the alternative hypothesis is that at least one
sample comes from a different population.

The test is appropriate when the data are measured on an ordinal or
continuous scale, but not when the data are nominal. If the
Kruskal-Wallis test indicates that there are significant differences
between the groups, post-hoc tests such as the Mann-Whitney U test or
Dunn's test can be used to determine which groups are significantly
different from each other.

To perform the Kruskal-Wallis test in R, we can use the
\texttt{kruskal.test()} function. Here's an example:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{group1 }\OtherTok{\textless{}{-}} \FunctionTok{c}\NormalTok{(}\DecValTok{4}\NormalTok{, }\DecValTok{8}\NormalTok{, }\DecValTok{7}\NormalTok{, }\DecValTok{6}\NormalTok{, }\DecValTok{5}\NormalTok{)}
\NormalTok{group2 }\OtherTok{\textless{}{-}} \FunctionTok{c}\NormalTok{(}\DecValTok{3}\NormalTok{, }\DecValTok{7}\NormalTok{, }\DecValTok{6}\NormalTok{, }\DecValTok{5}\NormalTok{, }\DecValTok{4}\NormalTok{)}
\NormalTok{group3 }\OtherTok{\textless{}{-}} \FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{, }\DecValTok{6}\NormalTok{, }\DecValTok{5}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{3}\NormalTok{)}

\FunctionTok{kruskal.test}\NormalTok{(}\FunctionTok{list}\NormalTok{(group1, group2, group3))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Kruskal-Wallis rank sum test
## 
## data:  list(group1, group2, group3)
## Kruskal-Wallis chi-squared = 3.2051, df = 2, p-value = 0.2014
\end{verbatim}

In this example, we have three independent groups with five observations
each. We use the \texttt{list()} function to group the data together,
and then pass the list to the \texttt{kruskal.test()} function. The
output of this code will provide the Kruskal-Wallis test statistic,
degrees of freedom, and p-value.

If the data is consisted of one value column and one group column, then
using the formula-pattern would be more convenient!

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{df }\OtherTok{=} \FunctionTok{data.frame}\NormalTok{(}
  \AttributeTok{value =} \FunctionTok{c}\NormalTok{(group1,group2,group3),}
  \AttributeTok{group =} \FunctionTok{rep}\NormalTok{(}\DecValTok{1}\SpecialCharTok{:}\DecValTok{3}\NormalTok{,}\AttributeTok{each =} \DecValTok{5}\NormalTok{)}
\NormalTok{)}

\FunctionTok{kruskal.test}\NormalTok{(value}\SpecialCharTok{\textasciitilde{}}\NormalTok{group,}\AttributeTok{data =}\NormalTok{ df)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Kruskal-Wallis rank sum test
## 
## data:  value by group
## Kruskal-Wallis chi-squared = 3.2051, df = 2, p-value = 0.2014
\end{verbatim}

\hypertarget{jonckheere-terpstra-trend-test}{%
\paragraph{Jonckheere-Terpstra (Trend)
test}\label{jonckheere-terpstra-trend-test}}

The Jonckheere-Terpstra test, also known as the trend test, is a
non-parametric statistical test used to determine \textbf{if there is a
trend in the ordered levels of a variable across multiple groups}. It is
often used to analyze data where there is an \textbf{ordered}
categorical variable and the groups are ordered in a natural way.

The Jonckheere-Terpstra test ranks the data within each group and
calculates the sum of ranks for each group. It then uses these sums to
calculate a test statistic that measures the trend in the ordered levels
of the variable. The null hypothesis of the Jonckheere-Terpstra test is
that there is no trend in the ordered levels of the variable across the
groups, while the alternative hypothesis is that there is a trend.

The test is appropriate when the data are measured on an ordinal or
continuous scale, but not when the data are nominal. It is commonly used
in fields such as medicine, psychology, and social sciences to analyze
trends in disease severity, performance, or attitudes across different
groups.

If the Jonckheere-Terpstra test indicates that there is a trend in the
ordered levels of the variable, post-hoc tests such as the Wilcoxon
rank-sum test or Dunn's test can be used to determine which groups are
significantly different from each other.

\begin{itemize}
\tightlist
\item
  Only used when the groups are ordered.
\item
  It actually tests about the median between groups.
\item
  it has \textbf{more power} than Kruskal-Wallis.
\end{itemize}

Suppose we want to analyze the trend in intelligence scores across
different age groups. We have four age groups (20-30, 31-40, 41-50, and
51-60) and 10 participants in each group. We measure the intelligence
scores of each participant and want to determine if there is a
significant trend in intelligence scores across the age groups.

Use \texttt{DescTools::JonckheereTerpstraTest()} to carry the test.
\texttt{nperm} is the number of permutations for the reference
distribution. The default is NULL in which case the permutation p-value
is not computed. It's recommended to set \texttt{nperm} to 1000 or
higher if permutation p-value is desired.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{age }\OtherTok{\textless{}{-}} \FunctionTok{c}\NormalTok{(}\FunctionTok{rep}\NormalTok{(}\StringTok{"20{-}30"}\NormalTok{, }\DecValTok{10}\NormalTok{), }\FunctionTok{rep}\NormalTok{(}\StringTok{"31{-}40"}\NormalTok{, }\DecValTok{10}\NormalTok{), }
         \FunctionTok{rep}\NormalTok{(}\StringTok{"41{-}50"}\NormalTok{, }\DecValTok{10}\NormalTok{), }\FunctionTok{rep}\NormalTok{(}\StringTok{"51{-}60"}\NormalTok{, }\DecValTok{10}\NormalTok{))}
\NormalTok{age }\OtherTok{=} \FunctionTok{as.ordered}\NormalTok{(age)}
\NormalTok{intelligence }\OtherTok{\textless{}{-}} \FunctionTok{c}\NormalTok{(}\DecValTok{80}\NormalTok{, }\DecValTok{75}\NormalTok{, }\DecValTok{85}\NormalTok{, }\DecValTok{90}\NormalTok{, }\DecValTok{70}\NormalTok{, }\DecValTok{75}\NormalTok{, }\DecValTok{80}\NormalTok{, }\DecValTok{85}\NormalTok{, }\DecValTok{65}\NormalTok{, }\DecValTok{70}\NormalTok{,}
                   \DecValTok{75}\NormalTok{, }\DecValTok{80}\NormalTok{, }\DecValTok{85}\NormalTok{, }\DecValTok{90}\NormalTok{, }\DecValTok{70}\NormalTok{, }\DecValTok{75}\NormalTok{, }\DecValTok{80}\NormalTok{, }\DecValTok{85}\NormalTok{, }\DecValTok{65}\NormalTok{, }\DecValTok{70}\NormalTok{,}
                   \DecValTok{70}\NormalTok{, }\DecValTok{75}\NormalTok{, }\DecValTok{80}\NormalTok{, }\DecValTok{85}\NormalTok{, }\DecValTok{65}\NormalTok{, }\DecValTok{70}\NormalTok{, }\DecValTok{75}\NormalTok{, }\DecValTok{80}\NormalTok{, }\DecValTok{85}\NormalTok{, }\DecValTok{90}\NormalTok{,}
                   \DecValTok{60}\NormalTok{, }\DecValTok{65}\NormalTok{, }\DecValTok{70}\NormalTok{, }\DecValTok{75}\NormalTok{, }\DecValTok{80}\NormalTok{, }\DecValTok{85}\NormalTok{, }\DecValTok{90}\NormalTok{, }\DecValTok{70}\NormalTok{, }\DecValTok{75}\NormalTok{, }\DecValTok{80}\NormalTok{)}

\NormalTok{df }\OtherTok{\textless{}{-}} \FunctionTok{data.frame}\NormalTok{(age, intelligence)}

\NormalTok{DescTools}\SpecialCharTok{::}\FunctionTok{JonckheereTerpstraTest}\NormalTok{(intelligence}\SpecialCharTok{\textasciitilde{}}\NormalTok{age,}
                                  \AttributeTok{data =}\NormalTok{ df)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Warning in JonckheereTerpstraTest.default(c(80, 75, 85, 90, 70, 75, 80, : Sample size > 100 or data with ties 
##  p-value based on normal approximation. Specify nperm for permutation p-value
\end{verbatim}

\begin{verbatim}
## 
##  Jonckheere-Terpstra test
## 
## data:  intelligence by age
## JT = 276, p-value = 0.5624
## alternative hypothesis: two.sided
\end{verbatim}

The result is slightly calibrated.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{DescTools}\SpecialCharTok{::}\FunctionTok{JonckheereTerpstraTest}\NormalTok{(intelligence}\SpecialCharTok{\textasciitilde{}}\NormalTok{age,}
                                  \AttributeTok{data =}\NormalTok{ df,}
                                  \AttributeTok{nperm =} \DecValTok{5000}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Warning in JonckheereTerpstraTest.default(nperm = nperm, c(80, 75, 85, 90, : Sample size > 100 or data with ties 
##  p-value based on normal approximation. Specify nperm for permutation p-value
\end{verbatim}

\begin{verbatim}
## 
##  Jonckheere-Terpstra test
## 
## data:  intelligence by age
## JT = 276, p-value = 0.5636
## alternative hypothesis: two.sided
\end{verbatim}

\hypertarget{median-test}{%
\paragraph{Median Test}\label{median-test}}

Very simple.

\begin{itemize}
\tightlist
\item
  Count the \# of data \textgreater{} or \textless{} median;
\item
  A Chi-square test on binomial distribution.
\item
  Need a large sample; not preferred.
\end{itemize}

Now that you've ranked your data, why not try other more powerful
non-parametric tests?

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(agricolae)}
\FunctionTok{library}\NormalTok{(irr)}
\FunctionTok{Median.test}\NormalTok{(intelligence,age)}
\end{Highlighting}
\end{Shaded}

\hypertarget{non-parametric-correlation-analysis}{%
\subsection{Non-Parametric Correlation
Analysis}\label{non-parametric-correlation-analysis}}

There are several types of correlation coefficients that are commonly
used in statistics to measure the strength and direction of the
relationship between two variables. The most commonly used correlation
coefficients are Pearson correlation, Spearman correlation,
point-biserial correlation, and phi correlation.

Spearman correlation, point-biserial correlation, and phi correlation
are non-parametric ones.

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  Pearson correlation: Pearson correlation coefficient measures the
  linear relationship between two continuous variables. It ranges from
  -1 to 1, where -1 indicates a perfect negative relationship, 0
  indicates no relationship, and 1 indicates a perfect positive
  relationship. Pearson correlation assumes that the data are normally
  distributed and that there is a linear relationship between the
  variables.
\item
  Spearman correlation: Spearman correlation coefficient measures the
  strength and direction of the monotonic relationship between two
  variables. It ranges from -1 to 1, where -1 indicates a perfect
  negative monotonic relationship, 0 indicates no monotonic
  relationship, and 1 indicates a perfect positive monotonic
  relationship. Spearman correlation does not assume that the data are
  normally distributed and can be used for ordinal or continuous
  variables.
\item
  Point-biserial correlation: Point-biserial correlation coefficient
  measures the relationship between a dichotomous variable and a
  continuous variable. It ranges from -1 to 1, where -1 indicates a
  perfect negative relationship between the dichotomous variable and the
  continuous variable, 0 indicates no relationship, and 1 indicates a
  perfect positive relationship. Point-biserial correlation is used when
  one of the variables is dichotomous and the other is continuous.
\item
  Phi correlation: Phi correlation coefficient measures the relationship
  between two dichotomous variables. It ranges from -1 to 1, where -1
  indicates a perfect negative relationship between the two variables, 0
  indicates no relationship, and 1 indicates a perfect positive
  relationship. Phi correlation is used when both variables are
  dichotomous.
\end{enumerate}

In general, Pearson and Spearman correlations are used for continuous or
ordinal variables, while point-biserial and phi correlations are used
for dichotomous variables. Pearson correlation is most appropriate when
the data are normally distributed and there is a linear relationship
between the variables, while Spearman correlation is more appropriate
when the data are non-normally distributed or the relationship is not
linear. Point-biserial and phi correlations are appropriate when one or
both of the variables are dichotomous.

Moreover, the chi-square independence test is used to determine if there
is a significant association between two categorical variables, while
the phi correlation coefficient is used to measure the strength and
direction of the relationship between two dichotomous variables. The two
tests are different perspectives to view the same problem, either say
correlation or say independence.

All can be dealt with by \texttt{cor.test()}.

What I want to address here is the Phi coefficient. Phi coefficient can
be obtained by \texttt{cor.test()} using the traditional Pearson-way,
and also fine with \texttt{psych::phi(matrix)}, but the latter won't
give you the significance information.

In the example, we want to test the correlation between parents'
political stance and their parenting way. We have the following
contingency table.

\includegraphics[width=1\linewidth]{contingency table}

I'd like to illustrate first with the ``traditional'' way.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{phidt }\OtherTok{=} \FunctionTok{data.frame}\NormalTok{(}
  \AttributeTok{parenting =} \FunctionTok{c}\NormalTok{(}\FunctionTok{rep}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{25}\NormalTok{),}\FunctionTok{rep}\NormalTok{(}\DecValTok{0}\NormalTok{,}\DecValTok{15}\NormalTok{)),}
  \AttributeTok{politics =} \FunctionTok{c}\NormalTok{(}\FunctionTok{rep}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{15}\NormalTok{),}\FunctionTok{rep}\NormalTok{(}\DecValTok{0}\NormalTok{,}\DecValTok{10}\NormalTok{),}\FunctionTok{rep}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{5}\NormalTok{),}\FunctionTok{rep}\NormalTok{(}\DecValTok{0}\NormalTok{,}\DecValTok{10}\NormalTok{))}
\NormalTok{)}
\FunctionTok{cor.test}\NormalTok{(phidt}\SpecialCharTok{$}\NormalTok{parenting,phidt}\SpecialCharTok{$}\NormalTok{politics)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Pearson's product-moment correlation
## 
## data:  phidt$parenting and phidt$politics
## t = 1.6475, df = 38, p-value = 0.1077
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.05797333  0.52729702
## sample estimates:
##       cor 
## 0.2581989
\end{verbatim}

The \texttt{psych::phi(matrix)} is indeed not recommend! But it's last
advantage is that the data is expressed in the easiest way! If just to
get the correlation value, probably go with it.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{phi\_dataset }\OtherTok{=} \FunctionTok{matrix}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\DecValTok{15}\NormalTok{,}\DecValTok{10}\NormalTok{,}\DecValTok{5}\NormalTok{,}\DecValTok{10}\NormalTok{),}\AttributeTok{nrow=}\DecValTok{2}\NormalTok{,}\AttributeTok{ncol=}\DecValTok{2}\NormalTok{)}
\NormalTok{psych}\SpecialCharTok{::}\FunctionTok{phi}\NormalTok{(phi\_dataset)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] 0.26
\end{verbatim}

However, if we carry a chi-square test, that differs a little in its
significance.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{chisq.test}\NormalTok{(phi\_dataset)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  phi_dataset
## X-squared = 1.7067, df = 1, p-value = 0.1914
\end{verbatim}

\hypertarget{ancova}{%
\section{ANCOVA}\label{ancova}}

The data \texttt{df} contains information about miners' vital capacity
as \texttt{vitalcp}, an indicator for their health of lungs; their age
as \texttt{age}, probability related to \texttt{vitalcp}; a dummy
\texttt{time} telling whether or not they have worked as a miner for 10
years, 1 if so, 0 otherwise.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{miner }\OtherTok{=}\NormalTok{ readxl}\SpecialCharTok{::}\FunctionTok{read\_excel}\NormalTok{(}\StringTok{"data09{-}06.xlsx"}\NormalTok{, }\AttributeTok{sheet =} \DecValTok{1}\NormalTok{) }\SpecialCharTok{|\textgreater{}} 
\NormalTok{  rstatix}\SpecialCharTok{::}\FunctionTok{convert\_as\_factor}\NormalTok{(time)}
\end{Highlighting}
\end{Shaded}

\hypertarget{pre-inspection}{%
\subsection{Pre-Inspection}\label{pre-inspection}}

\hypertarget{equal-variance}{%
\subsubsection{Equal Variance}\label{equal-variance}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{miner }\SpecialCharTok{|\textgreater{}} 
\NormalTok{  rstatix}\SpecialCharTok{::}\FunctionTok{levene\_test}\NormalTok{(vitalcp}\SpecialCharTok{\textasciitilde{}}\NormalTok{time)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## # A tibble: 1 x 4
##     df1   df2 statistic     p
##   <int> <int>     <dbl> <dbl>
## 1     1    26      2.44 0.130
\end{verbatim}

\hypertarget{independence-of-covariate-and-treatment}{%
\subsubsection{Independence of Covariate and
Treatment}\label{independence-of-covariate-and-treatment}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{anova}\NormalTok{(}\FunctionTok{aov}\NormalTok{(age}\SpecialCharTok{\textasciitilde{}}\NormalTok{time,}\AttributeTok{data =}\NormalTok{ miner))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Analysis of Variance Table
## 
## Response: age
##           Df  Sum Sq Mean Sq F value  Pr(>F)  
## time       1  202.74 202.741  3.6665 0.06658 .
## Residuals 26 1437.69  55.296                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\hypertarget{homogeniety-of-regression-slopes}{%
\subsubsection{Homogeniety of Regression
Slopes}\label{homogeniety-of-regression-slopes}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{anova}\NormalTok{(}\FunctionTok{aov}\NormalTok{(vitalcp}\SpecialCharTok{\textasciitilde{}}\NormalTok{time}\SpecialCharTok{*}\NormalTok{age,}\AttributeTok{data =}\NormalTok{ miner))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Analysis of Variance Table
## 
## Response: vitalcp
##           Df  Sum Sq Mean Sq F value    Pr(>F)    
## time       1  0.2045  0.2045  0.3595 0.5543807    
## age        1 10.8806 10.8806 19.1264 0.0002044 ***
## time:age   1  0.1023  0.1023  0.1799 0.6752461    
## Residuals 24 13.6532  0.5689                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\hypertarget{ancova-1}{%
\subsection{ANCOVA}\label{ancova-1}}

\hypertarget{fit-the-model}{%
\subsubsection{Fit the Model}\label{fit-the-model}}

It's noteworthy that in R, the more essential the effect variable is,
the earlier it should be put.

The common ``rank'' is as follows.

\begin{itemize}
\tightlist
\item
  covariate
\item
  main effect
\item
  interaction
\end{itemize}

The order that IVs are plugged matters! Since we have the algorithm of
SS related to that.

In the formula, connect the main effect and covariate with sign
\texttt{+}.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{cfit }\OtherTok{=} \FunctionTok{aov}\NormalTok{(vitalcp}\SpecialCharTok{\textasciitilde{}}\NormalTok{age}\SpecialCharTok{+}\NormalTok{time,}\AttributeTok{data =}\NormalTok{ miner)}
\end{Highlighting}
\end{Shaded}

\hypertarget{summary-the-model}{%
\subsubsection{Summary the Model}\label{summary-the-model}}

\hypertarget{ancova-view}{%
\paragraph{ANCOVA View}\label{ancova-view}}

As for the fitted ANCOVA model, say \texttt{model}, plug in
\texttt{model} into \texttt{summary()} or \texttt{anova()} will return
the same and necessary results.

Since \texttt{summary()} is more generally used, I'd like to recommend
\texttt{summary()}!

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# anova(cfit)}
\FunctionTok{summary}\NormalTok{(cfit)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## age          1 10.543  10.543  19.162 0.000187 ***
## time         1  0.542   0.542   0.985 0.330439    
## Residuals   25 13.755   0.550                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\hypertarget{linear-model-view}{%
\paragraph{Linear-Model View}\label{linear-model-view}}

View the ANOVA model as a special case of linear model, with covariate
and main effect as its independent variables as is used above, and
intercept is included.

\texttt{car::Anova(model,type\ =\ \textquotesingle{}III\textquotesingle{})}
can return the SS, miner, \(F\) and \(p\) for each term, including the
intercept. The coefficients, together with the same significance, can be
reported through \texttt{summary(model)}.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{car}\SpecialCharTok{::}\FunctionTok{Anova}\NormalTok{(cfit,}\AttributeTok{type =} \StringTok{\textquotesingle{}3\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Anova Table (Type III tests)
## 
## Response: vitalcp
##             Sum Sq Df F value    Pr(>F)    
## (Intercept) 37.959  1 68.9886 1.177e-08 ***
## age         10.881  1 19.7751 0.0001563 ***
## time         0.542  1  0.9852 0.3304389    
## Residuals   13.755 25                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{summary}\NormalTok{(}\FunctionTok{lm}\NormalTok{(vitalcp}\SpecialCharTok{\textasciitilde{}}\NormalTok{age}\SpecialCharTok{+}\NormalTok{time,}\AttributeTok{data =}\NormalTok{ miner))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = vitalcp ~ age + time, data = miner)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.68343 -0.37405  0.03523  0.44561  1.12263 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  8.27718    0.99654   8.306 1.18e-08 ***
## age         -0.08700    0.01956  -4.447 0.000156 ***
## time2       -0.30033    0.30258  -0.993 0.330439    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7418 on 25 degrees of freedom
## Multiple R-squared:  0.4463, Adjusted R-squared:  0.402 
## F-statistic: 10.07 on 2 and 25 DF,  p-value: 0.0006186
\end{verbatim}

\hypertarget{effect-size-1}{%
\subsubsection{Effect Size}\label{effect-size-1}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{rstatix}\SpecialCharTok{::}\FunctionTok{partial\_eta\_squared}\NormalTok{(cfit)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##        age       time 
## 0.43389844 0.03791233
\end{verbatim}

\hypertarget{post-hoc-test}{%
\subsubsection{Post-Hoc Test}\label{post-hoc-test}}

Library the package \texttt{multcomp} and use the
\texttt{glht(model,\ linfct\ =\ mcp(time\ =\ \textquotesingle{}Tukey\textquotesingle{}))}
within to get the post-hoc model. Then, use \texttt{summary()} to get
its result.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(multcomp)}
\NormalTok{postHoc }\OtherTok{=} \FunctionTok{glht}\NormalTok{(cfit,}\AttributeTok{linfct =} \FunctionTok{mcp}\NormalTok{(}\AttributeTok{time =} \StringTok{\textquotesingle{}Tukey\textquotesingle{}}\NormalTok{))}
\FunctionTok{summary}\NormalTok{(postHoc)}
\end{Highlighting}
\end{Shaded}

Well, the significant intercept term means that the grand mean does
account for some of the variation.

\hypertarget{visualize-the-model}{%
\subsubsection{Visualize the Model}\label{visualize-the-model}}

Library package \texttt{HH}, and then use the function \texttt{ancova()}
to fit the model. Note that all parameters passed into \texttt{ancova()}
is the same as those into \texttt{aov()}.

Mind the order of independent variables, since that does matter!

\texttt{ancova(...)} will not only return the completely same result as
we've seen in \texttt{summary(aov(...))}, but also a graph illustrating
the comprehensive correlation between the dependent variable and
covariate \& main effect.

Note that within \texttt{ancova()}, more underlying functions from that
package \texttt{HH} are loaded, which means you can not get away with
just specifying the package like \texttt{HH::ancova}, but have to
library the package specifically.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(HH)}
\FunctionTok{ancova}\NormalTok{(vitalcp}\SpecialCharTok{\textasciitilde{}}\NormalTok{age}\SpecialCharTok{+}\NormalTok{time , }\AttributeTok{data =}\NormalTok{ miner)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Analysis of Variance Table
## 
## Response: vitalcp
##           Df  Sum Sq Mean Sq F value    Pr(>F)    
## age        1 10.5431 10.5431 19.1617 0.0001871 ***
## time       1  0.5421  0.5421  0.9852 0.3304389    
## Residuals 25 13.7555  0.5502                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-45-1.pdf}

\hypertarget{visual-check}{%
\subsubsection{Visual Check}\label{visual-check}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(ggpubr)}
\FunctionTok{library}\NormalTok{(patchwork)}
\NormalTok{p1 }\OtherTok{=} \FunctionTok{ggscatter}\NormalTok{(miner[miner}\SpecialCharTok{$}\NormalTok{time}\SpecialCharTok{==}\DecValTok{1}\NormalTok{,],}
          \AttributeTok{x=}\StringTok{"age"}\NormalTok{,}\AttributeTok{y=}\StringTok{"vitalcp"}\NormalTok{,}
          \AttributeTok{color=}\StringTok{"red"}\NormalTok{,}\AttributeTok{add=}\StringTok{"reg.line"}\NormalTok{)}
\NormalTok{p2 }\OtherTok{=} \FunctionTok{ggscatter}\NormalTok{(miner[miner}\SpecialCharTok{$}\NormalTok{time}\SpecialCharTok{==}\DecValTok{2}\NormalTok{,],}
          \AttributeTok{x=}\StringTok{"age"}\NormalTok{,}\AttributeTok{y=}\StringTok{"vitalcp"}\NormalTok{,}
          \AttributeTok{color=}\StringTok{"blue"}\NormalTok{,}\AttributeTok{add=}\StringTok{"reg.line"}\NormalTok{)}
\NormalTok{p3 }\OtherTok{=} \FunctionTok{ggscatter}\NormalTok{(}\AttributeTok{data =} \FunctionTok{data.frame}\NormalTok{(}
  \AttributeTok{pred =} \FunctionTok{fitted}\NormalTok{(cfit),}
  \AttributeTok{resid =} \FunctionTok{residuals}\NormalTok{(cfit)}
\NormalTok{),}\AttributeTok{x=}\StringTok{"pred"}\NormalTok{,}\AttributeTok{y=}\StringTok{"resid"}\NormalTok{,}\AttributeTok{color=}\StringTok{"green"}\NormalTok{,}\AttributeTok{add=}\StringTok{"reg.line"}\NormalTok{)}

\NormalTok{(p1}\SpecialCharTok{+}\NormalTok{p2)}\SpecialCharTok{/}\NormalTok{p3}
\end{Highlighting}
\end{Shaded}

\includegraphics[width=1\linewidth]{psy-stat-II-2023_files/figure-latex/unnamed-chunk-46-1}

\hypertarget{report-the-data}{%
\subsection{Report the Data}\label{report-the-data}}

ANCOVA was conducted on \texttt{vitalcp} with \texttt{time} as the main
effect and \texttt{age} as the covariate.

The covariate, age of the workers, was significantly related to the
vital capacity,
\(F(1,25)=19.775, p<0.001, r = 0.665,\eta^2p=0.43389844\). There was no
significant effect of time on the vital capacity, after controlling the
effect of age, \(F(1,25) = 0.965, p=0.33, \eta^2p= 0.03791233\).

\hypertarget{latin-square}{%
\section{Latin Square}\label{latin-square}}

The following data \texttt{yie} has the information about the yields of
vegetables, say \texttt{yield}, and the corresponding conditions of
growing, such as the variety of vegetables, say \texttt{variety} here;
and the location information for \texttt{rep} and \texttt{col}. In the
three treatments, varieties of vegetables are the primary one that we're
interested in, and the other two are secondary variables to be
controlled.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{knitr}\SpecialCharTok{::}\FunctionTok{include\_graphics}\NormalTok{(}\StringTok{\textquotesingle{}Latin Square.png\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{yie }\OtherTok{=}\NormalTok{ readxl}\SpecialCharTok{::}\FunctionTok{read\_xlsx}\NormalTok{(}\StringTok{"veget.xlsx"}\NormalTok{) }\SpecialCharTok{|\textgreater{}} 
\NormalTok{  rstatix}\SpecialCharTok{::}\FunctionTok{convert\_as\_factor}\NormalTok{(rep,col,variety)}
\end{Highlighting}
\end{Shaded}

\hypertarget{fit-the-model-1}{%
\subsection{Fit the Model}\label{fit-the-model-1}}

The model is fitted the same as before for ANOVA. Note that Latin Square
is a special design, where each letter (level) appears only once in each
row and each column.

Here, though the factors differ in importance, the order that they're
plugged in does not matter!

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{latinfit }\OtherTok{=} \FunctionTok{aov}\NormalTok{(yield}\SpecialCharTok{\textasciitilde{}}\NormalTok{rep}\SpecialCharTok{+}\NormalTok{col}\SpecialCharTok{+}\NormalTok{variety,}
           \AttributeTok{data =}\NormalTok{ yie)}
\end{Highlighting}
\end{Shaded}

\hypertarget{summary-the-model-1}{%
\subsection{Summary the Model}\label{summary-the-model-1}}

\hypertarget{anova-view}{%
\subsubsection{ANOVA View}\label{anova-view}}

Basically, \texttt{summary(model)} and \texttt{anova(model)} will return
the same result.

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# summary(latinfit)}
\FunctionTok{anova}\NormalTok{(latinfit)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Analysis of Variance Table
## 
## Response: yield
##           Df Sum Sq Mean Sq F value  Pr(>F)  
## rep        5  4.460  0.8919  0.6461 0.66553  
## col        5  1.695  0.3389  0.2455 0.94034  
## variety    5 21.563  4.3126  3.1242 0.01477 *
## Residuals 56 77.302  1.3804                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\hypertarget{linear-model-view-1}{%
\subsubsection{Linear-Model View}\label{linear-model-view-1}}

ANOVA (even Latin Square design) is a special case of linear model.
Similarly, use
\texttt{car::Anova(model,type\ =\ \textquotesingle{}III\textquotesingle{})}
to see the intercept term, with other terms same with the results from
the functions mentioned above.

However, if one grouping variable has multiple levels, the linear model
may seems not so simple, because each level will sit at a term. In this
case, traditional view of ANOVA seems ``better''.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{car}\SpecialCharTok{::}\FunctionTok{Anova}\NormalTok{(latinfit, }\AttributeTok{type =} \StringTok{\textquotesingle{}III\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Anova Table (Type III tests)
## 
## Response: yield
##              Sum Sq Df  F value  Pr(>F)    
## (Intercept) 1342.92  1 972.8543 < 2e-16 ***
## rep            4.46  5   0.6461 0.66553    
## col            1.69  5   0.2455 0.94034    
## variety       21.56  5   3.1242 0.01477 *  
## Residuals     77.30 56                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{summary}\NormalTok{(}\FunctionTok{lm}\NormalTok{(yield}\SpecialCharTok{\textasciitilde{}}\NormalTok{rep}\SpecialCharTok{+}\NormalTok{col}\SpecialCharTok{+}\NormalTok{variety,}
           \AttributeTok{data =}\NormalTok{ yie))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = yield ~ rep + col + variety, data = yie)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.4000 -0.9833 -0.0250  0.9167  2.2000 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.728e+01  5.539e-01  31.191  < 2e-16 ***
## rep2        -1.917e-01  4.797e-01  -0.400  0.69097    
## rep3         1.667e-01  4.797e-01   0.347  0.72954    
## rep4         8.333e-02  4.797e-01   0.174  0.86270    
## rep5        -3.333e-01  4.797e-01  -0.695  0.48996    
## rep6        -5.500e-01  4.797e-01  -1.147  0.25639    
## col2         1.667e-01  4.797e-01   0.347  0.72954    
## col3         1.583e-01  4.797e-01   0.330  0.74255    
## col4         4.583e-01  4.797e-01   0.956  0.34340    
## col5         3.917e-01  4.797e-01   0.817  0.41764    
## col6         2.000e-01  4.797e-01   0.417  0.67829    
## variety2     4.500e-01  4.797e-01   0.938  0.35218    
## variety3     1.083e-01  4.797e-01   0.226  0.82213    
## variety4    -7.401e-17  4.797e-01   0.000  1.00000    
## variety5     1.517e+00  4.797e-01   3.162  0.00253 ** 
## variety6    -2.266e-17  4.797e-01   0.000  1.00000    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.175 on 56 degrees of freedom
## Multiple R-squared:  0.2639, Adjusted R-squared:  0.06676 
## F-statistic: 1.339 on 15 and 56 DF,  p-value: 0.2112
\end{verbatim}

\hypertarget{post-hoc-test-1}{%
\subsection{Post-Hoc Test}\label{post-hoc-test-1}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{TukeyHSD}\NormalTok{(latinfit)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = yield ~ rep + col + variety, data = yie)
## 
## $rep
##            diff       lwr       upr     p adj
## 2-1 -0.19166667 -1.606952 1.2236185 0.9986162
## 3-1  0.16666667 -1.248619 1.5819518 0.9992958
## 4-1  0.08333333 -1.331952 1.4986185 0.9999768
## 5-1 -0.33333333 -1.748619 1.0819518 0.9817860
## 6-1 -0.55000000 -1.965285 0.8652852 0.8595512
## 3-2  0.35833333 -1.056952 1.7736185 0.9749576
## 4-2  0.27500000 -1.140285 1.6902852 0.9923856
## 5-2 -0.14166667 -1.556952 1.2736185 0.9996812
## 6-2 -0.35833333 -1.773619 1.0569518 0.9749576
## 4-3 -0.08333333 -1.498619 1.3319518 0.9999768
## 5-3 -0.50000000 -1.915285 0.9152852 0.9013962
## 6-3 -0.71666667 -2.131952 0.6986185 0.6693358
## 5-4 -0.41666667 -1.831952 0.9986185 0.9523845
## 6-4 -0.63333333 -2.048619 0.7819518 0.7725796
## 6-5 -0.21666667 -1.631952 1.1986185 0.9975120
## 
## $col
##             diff        lwr      upr     p adj
## 2-1  0.166666667 -1.2486185 1.581952 0.9992958
## 3-1  0.158333333 -1.2569518 1.573619 0.9994513
## 4-1  0.458333333 -0.9569518 1.873619 0.9298025
## 5-1  0.391666667 -1.0236185 1.806952 0.9632783
## 6-1  0.200000000 -1.2152852 1.615285 0.9983024
## 3-2 -0.008333333 -1.4236185 1.406952 1.0000000
## 4-2  0.291666667 -1.1236185 1.706952 0.9900234
## 5-2  0.225000000 -1.1902852 1.640285 0.9970236
## 6-2  0.033333333 -1.3819518 1.448619 0.9999998
## 4-3  0.300000000 -1.1152852 1.715285 0.9886570
## 5-3  0.233333333 -1.1819518 1.648619 0.9964650
## 6-3  0.041666667 -1.3736185 1.456952 0.9999993
## 5-4 -0.066666667 -1.4819518 1.348619 0.9999923
## 6-4 -0.258333333 -1.6736185 1.156952 0.9943019
## 6-5 -0.191666667 -1.6069518 1.223619 0.9986162
## 
## $variety
##           diff          lwr        upr     p adj
## 2-1  0.4500000 -0.965285169  1.8652852 0.9347773
## 3-1  0.1083333 -1.306951836  1.5236185 0.9999148
## 4-1  0.0000000 -1.415285169  1.4152852 1.0000000
## 5-1  1.5166667  0.101381497  2.9319518 0.0289344
## 6-1  0.0000000 -1.415285169  1.4152852 1.0000000
## 3-2 -0.3416667 -1.756951836  1.0736185 0.9796809
## 4-2 -0.4500000 -1.865285169  0.9652852 0.9347773
## 5-2  1.0666667 -0.348618503  2.4819518 0.2434519
## 6-2 -0.4500000 -1.865285169  0.9652852 0.9347773
## 4-3 -0.1083333 -1.523618503  1.3069518 0.9999148
## 5-3  1.4083333 -0.006951836  2.8236185 0.0518445
## 6-3 -0.1083333 -1.523618503  1.3069518 0.9999148
## 5-4  1.5166667  0.101381497  2.9319518 0.0289344
## 6-4  0.0000000 -1.415285169  1.4152852 1.0000000
## 6-5 -1.5166667 -2.931951836 -0.1013815 0.0289344
\end{verbatim}

\hypertarget{nested-anova}{%
\section{Nested ANOVA}\label{nested-anova}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{nestedData }\OtherTok{=}\NormalTok{ readxl}\SpecialCharTok{::}\FunctionTok{read\_excel}\NormalTok{(}\StringTok{"nested anova.xlsx"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\hypertarget{visualization}{%
\subsection{visualization}\label{visualization}}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# we should eyeball the data first to make sense of the test.}
\CommentTok{\# create boxplots to visualize the data}
\FunctionTok{ggplot}\NormalTok{(nestedData, }\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\FunctionTok{factor}\NormalTok{(location), }\AttributeTok{y=}\NormalTok{DV)) }\SpecialCharTok{+} 
  \FunctionTok{geom\_boxplot}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-54-1.pdf}

\hypertarget{nested-anova-1}{%
\subsection{nested ANOVA}\label{nested-anova-1}}

Theoretically, the order of computing the variance sum of squares
matters, thus appointing the
\texttt{summation\ =\ \textquotesingle{}I\textquotesingle{}}.

However, here the summation type does not matter.

Note that the result of the main effect of the group is wrong. We can
manually make it straight from the SS of the group and the subgroup.

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# note that nested anova can use error type I and III.}
\CommentTok{\# aov() has a default type I to process factors in an sequential order}
\CommentTok{\# here type I and III don\textquotesingle{}t differ, since effectively we have no orders}
\NormalTok{aov\_model }\OtherTok{=} \FunctionTok{aov}\NormalTok{(DV }\SpecialCharTok{\textasciitilde{}} \FunctionTok{factor}\NormalTok{(method)}\SpecialCharTok{/}\FunctionTok{factor}\NormalTok{(location), }
                \AttributeTok{data =}\NormalTok{ nestedData,}\AttributeTok{summation=}\StringTok{"I"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Warning: In lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
##  extra argument 'summation' will be disregarded
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{summary}\NormalTok{(aov\_model)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##                                 Df Sum Sq Mean Sq F value   Pr(>F)    
## factor(method)                   2  665.7   332.8   255.7 1.45e-10 ***
## factor(method):factor(location)  9 1720.7   191.2   146.9 6.98e-11 ***
## Residuals                       12   15.6     1.3                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\hypertarget{error-adjustment}{%
\subsubsection{error adjustment}\label{error-adjustment}}

An alternative refinement is to adjust the error term.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{aov.result2 }\OtherTok{=} \FunctionTok{aov}\NormalTok{(DV }\SpecialCharTok{\textasciitilde{}} \FunctionTok{factor}\NormalTok{(method) }\SpecialCharTok{+} 
                    \FunctionTok{Error}\NormalTok{(}\FunctionTok{factor}\NormalTok{(method)}\SpecialCharTok{:}\FunctionTok{factor}\NormalTok{(location)),}
                  \AttributeTok{data =}\NormalTok{ nestedData)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Warning in aov(DV ~ factor(method) + Error(factor(method):factor(location)), :
## Error() model is singular
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{summary}\NormalTok{(aov.result2)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Error: factor(method):factor(location)
##                Df Sum Sq Mean Sq F value Pr(>F)
## factor(method)  2  665.7   332.8   1.741   0.23
## Residuals       9 1720.7   191.2               
## 
## Error: Within
##           Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 12  15.62   1.302
\end{verbatim}

\hypertarget{comparison-with-one-way}{%
\subsubsection{comparison with one-way}\label{comparison-with-one-way}}

If we wrongly ignore the nested factor and use one-way ANOVA, the
results would be different

Note that there's only one group factor, therefore, the type of
summation does not matter.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{aov\_model }\OtherTok{=} \FunctionTok{aov}\NormalTok{(DV }\SpecialCharTok{\textasciitilde{}} \FunctionTok{factor}\NormalTok{(method), }
                \AttributeTok{data =}\NormalTok{ nestedData,}\AttributeTok{summation=}\StringTok{"III"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Warning: In lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
##  extra argument 'summation' will be disregarded
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{summary}\NormalTok{(aov\_model)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##                Df Sum Sq Mean Sq F value Pr(>F)  
## factor(method)  2  665.7   332.8   4.026 0.0331 *
## Residuals      21 1736.3    82.7                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{aov\_model }\OtherTok{=} \FunctionTok{aov}\NormalTok{(DV }\SpecialCharTok{\textasciitilde{}} \FunctionTok{factor}\NormalTok{(method), }
                \AttributeTok{data =}\NormalTok{ nestedData)}
\FunctionTok{summary}\NormalTok{(aov\_model)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##                Df Sum Sq Mean Sq F value Pr(>F)  
## factor(method)  2  665.7   332.8   4.026 0.0331 *
## Residuals      21 1736.3    82.7                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\hypertarget{manova}{%
\section{MANOVA}\label{manova}}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# salary data example}
\NormalTok{salaryData }\OtherTok{=}\NormalTok{ readxl}\SpecialCharTok{::}\FunctionTok{read\_xlsx}\NormalTok{(}\StringTok{"example.xlsx"}\NormalTok{)}
\NormalTok{DV }\OtherTok{=} \FunctionTok{cbind}\NormalTok{(salaryData}\SpecialCharTok{$}\NormalTok{salary, salaryData}\SpecialCharTok{$}\NormalTok{salbegin)}
\end{Highlighting}
\end{Shaded}

\hypertarget{visualization-1}{%
\subsection{Visualization}\label{visualization-1}}

Note that in order to generate the boxplot according to the
``categorical'' data, ``factor'' type should be designated in the
mapping parameter.

If not ``factored'', the scatter plot works fine with grouped effect,
but the boxplot fails.

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# eyeball the data}
\FunctionTok{library}\NormalTok{(gridExtra)}
\NormalTok{p1 }\OtherTok{=} \FunctionTok{ggplot}\NormalTok{(salaryData, }\FunctionTok{aes}\NormalTok{(}\AttributeTok{x =} \FunctionTok{factor}\NormalTok{(jobcat), }\AttributeTok{y =}\NormalTok{ salary)) }\SpecialCharTok{+} 
  \FunctionTok{geom\_boxplot}\NormalTok{(}\AttributeTok{outlier.shape =} \ConstantTok{NA}\NormalTok{) }\SpecialCharTok{+} \FunctionTok{geom\_jitter}\NormalTok{(}\AttributeTok{width =} \FloatTok{0.2}\NormalTok{) }\SpecialCharTok{+} 
  \FunctionTok{theme}\NormalTok{(}\AttributeTok{legend.position=}\StringTok{"top"}\NormalTok{)}
\NormalTok{p2 }\OtherTok{=} \FunctionTok{ggplot}\NormalTok{(salaryData, }\FunctionTok{aes}\NormalTok{(}\AttributeTok{x =} \FunctionTok{factor}\NormalTok{(jobcat), }\AttributeTok{y =}\NormalTok{ salbegin)) }\SpecialCharTok{+} 
  \FunctionTok{geom\_boxplot}\NormalTok{(}\AttributeTok{outlier.shape =} \ConstantTok{NA}\NormalTok{) }\SpecialCharTok{+} \FunctionTok{geom\_jitter}\NormalTok{(}\AttributeTok{width =} \FloatTok{0.2}\NormalTok{) }\SpecialCharTok{+} 
  \FunctionTok{theme}\NormalTok{(}\AttributeTok{legend.position=}\StringTok{"top"}\NormalTok{)}
\FunctionTok{grid.arrange}\NormalTok{(p1, p2, }\AttributeTok{ncol=}\DecValTok{2}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-60-1.pdf}

We can also eyeball the DVs.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{car}\SpecialCharTok{::}\FunctionTok{scatterplot}\NormalTok{(salary }\SpecialCharTok{\textasciitilde{}}\NormalTok{ salbegin, }\AttributeTok{data=}\NormalTok{salaryData, }\AttributeTok{ellipse=}\ConstantTok{TRUE}\NormalTok{, }
  \AttributeTok{smooth=}\FunctionTok{list}\NormalTok{(}\AttributeTok{style=}\StringTok{"lines"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-61-1.pdf}

\hypertarget{asumptions}{%
\subsection{Asumptions}\label{asumptions}}

\hypertarget{normality}{%
\subsubsection{Normality}\label{normality}}

\hypertarget{pipe-friendly-preferred}{%
\paragraph{pipe-friendly (Preferred)}\label{pipe-friendly-preferred}}

Note that \texttt{mshapiro\_test()} is from the package
\texttt{rstatix}, one step further from \texttt{tidyverse}.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{salaryData }\SpecialCharTok{|\textgreater{}}\NormalTok{ dplyr}\SpecialCharTok{::}\FunctionTok{select}\NormalTok{(salary,salbegin) }\SpecialCharTok{|\textgreater{}} 
\NormalTok{  rstatix}\SpecialCharTok{::}\FunctionTok{mshapiro\_test}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## # A tibble: 1 x 2
##   statistic  p.value
##       <dbl>    <dbl>
## 1     0.707 6.13e-28
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{salaryData }\SpecialCharTok{|\textgreater{}} \FunctionTok{group\_by}\NormalTok{(jobcat) }\SpecialCharTok{|\textgreater{}} 
  \FunctionTok{shapiro\_test}\NormalTok{(salary,salbegin)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## # A tibble: 6 x 4
##   jobcat variable statistic        p
##    <dbl> <chr>        <dbl>    <dbl>
## 1      1 salary       0.882 4.57e-16
## 2      1 salbegin     0.931 6.10e-12
## 3      2 salary       0.818 2.86e- 4
## 4      2 salbegin     0.499 1.64e- 8
## 5      3 salary       0.929 1.72e- 4
## 6      3 salbegin     0.860 2.14e- 7
\end{verbatim}

\hypertarget{classical-not-preferred}{%
\paragraph{classical (Not Preferred)}\label{classical-not-preferred}}

Not preferred. It's not efficient to do this way.

The specific function \texttt{mshapiro.test()} requires to library the
package \texttt{mvnormtest}, which surprisingly has a single function!
Moreover, it requires the data to be transposed before being processed.

The most annoying part is that the data has to be packed purposefully.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{mvnormtest}\SpecialCharTok{::}\FunctionTok{mshapiro.test}\NormalTok{(}\FunctionTok{t}\NormalTok{(DV))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Shapiro-Wilk normality test
## 
## data:  Z
## W = 0.70689, p-value < 2.2e-16
\end{verbatim}

\hypertarget{homogeniety-of-variance-covariance}{%
\subsubsection{Homogeniety of Variance \&
Covariance}\label{homogeniety-of-variance-covariance}}

Using the Box's M-test and Levene's Test.

\hypertarget{pipe-friendly-preferred-1}{%
\paragraph{pipe-friendly (Preferred)}\label{pipe-friendly-preferred-1}}

Using \texttt{box\_m()} from the package \texttt{rstatix} to test the
homogeniety of covariance matrices, in which the first parameter is the
DVs needed to be analyzed (No group variable! This can be achieved
through \texttt{select()}), and the second is the \texttt{group}
parameter specifying the group variable.

Moreover, the function \texttt{heplots::boxM()} is helpful. Instead of
taking the formula to specify the parameters, using the ``traditional''
way to write \texttt{group\ =\ ...} works pretty well combined with
pipe-friendly environment!

The drawback of the functions is that they \textbf{cannot search column
variables automatically}, and that should be specified
\textbf{manually}. And more annoying one is that, when encountering with
NAs, an error will be raised by \texttt{rstatix::box\_m()}, while that's
fine with \texttt{heplots::boxM()}.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{salaryData }\SpecialCharTok{|\textgreater{}}\NormalTok{ dplyr}\SpecialCharTok{::}\FunctionTok{select}\NormalTok{(salary,salbegin) }\SpecialCharTok{|\textgreater{}}
\NormalTok{  rstatix}\SpecialCharTok{::}\FunctionTok{box\_m}\NormalTok{(}\AttributeTok{group =}\NormalTok{ salaryData}\SpecialCharTok{$}\NormalTok{jobcat)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## # A tibble: 1 x 4
##   statistic   p.value parameter method                                          
##       <dbl>     <dbl>     <dbl> <chr>                                           
## 1      509. 1.14e-106         6 Box's M-test for Homogeneity of Covariance Matr~
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{salaryData }\SpecialCharTok{|\textgreater{}}\NormalTok{ dplyr}\SpecialCharTok{::}\FunctionTok{select}\NormalTok{(salary,salbegin) }\SpecialCharTok{|\textgreater{}} 
\NormalTok{  heplots}\SpecialCharTok{::}\FunctionTok{boxM}\NormalTok{(}\AttributeTok{group =}\NormalTok{ salaryData}\SpecialCharTok{$}\NormalTok{jobcat)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Box's M-test for Homogeneity of Covariance Matrices
## 
## data:  dplyr::select(salaryData, salary, salbegin)
## Chi-Sq (approx.) = 508.68, df = 6, p-value < 2.2e-16
\end{verbatim}

Do not forget that the assumption of homogeniety of variance for each DV
is needed either! However, the traditional \texttt{levene\_test()} (or
its variants) can deal with one DV at a time, which is tedious when we
have many DVs to check.

Hopefully, we have \texttt{heplots::leveneTests(data,group)} to deal
with that issue. Combined with pipe-friendly procedures, that's
fabulous!

One noteworthy flaw is that the
\texttt{heplots::leveneTests(data,group)} cannot search column variables
like that in the ``tidyverse'' environment.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{salaryData }\SpecialCharTok{|\textgreater{}}\NormalTok{  dplyr}\SpecialCharTok{::}\FunctionTok{select}\NormalTok{(salbegin,salary) }\SpecialCharTok{|\textgreater{}} 
\NormalTok{  heplots}\SpecialCharTok{::}\FunctionTok{leveneTests}\NormalTok{(}\AttributeTok{group =}\NormalTok{ salaryData}\SpecialCharTok{$}\NormalTok{jobcat)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Warning in leveneTest.default(x, group = group, center = center, ...): group
## coerced to factor.

## Warning in leveneTest.default(x, group = group, center = center, ...): group
## coerced to factor.
\end{verbatim}

\begin{verbatim}
## Levene's Tests for Homogeneity of Variance (center = median)
## 
##          df1 df2 F value    Pr(>F)    
## salbegin   2 471  68.862 < 2.2e-16 ***
## salary     2 471  51.189 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\hypertarget{classical}{%
\paragraph{classical}\label{classical}}

The most annoying part is that the data has to be packed purposefully.

An alternative way to use \texttt{heplots::boxM()} has been mentioned
above. We can renovate the efficiency to carry this test by replacing
the ``formula'' specifying-pattern with the ``group'' one.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{heplots}\SpecialCharTok{::}\FunctionTok{boxM}\NormalTok{(}\FunctionTok{cbind}\NormalTok{(salary, salbegin) }\SpecialCharTok{\textasciitilde{}} \FunctionTok{factor}\NormalTok{(jobcat), }
              \AttributeTok{data =}\NormalTok{ salaryData)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Box's M-test for Homogeneity of Covariance Matrices
## 
## data:  Y
## Chi-Sq (approx.) = 508.68, df = 6, p-value < 2.2e-16
\end{verbatim}

\hypertarget{manova-1}{%
\subsection{MANOVA}\label{manova-1}}

Use the function \texttt{manova()}, which is from the baseR, no special
need to library packages.

Similarly, the most annoying part is that the \textbf{DVs and IVs has to
be packed} previously.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{jobcat }\OtherTok{=}\NormalTok{ salaryData}\SpecialCharTok{$}\NormalTok{jobcat}
\NormalTok{fit }\OtherTok{=} \FunctionTok{manova}\NormalTok{(DV }\SpecialCharTok{\textasciitilde{}}\NormalTok{ jobcat)}
\end{Highlighting}
\end{Shaded}

As before, simply use \texttt{summary()} of the fitted model to see the
results.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{summary}\NormalTok{(fit)  }\CommentTok{\#for MANOVA}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##            Df  Pillai approx F num Df den Df    Pr(>F)    
## jobcat      1 0.62914    399.5      2    471 < 2.2e-16 ***
## Residuals 472                                             
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\hypertarget{effect-size-2}{%
\subsubsection{Effect Size}\label{effect-size-2}}

Checking the effect size is also indispensable.

Use \texttt{eta\_squared()} from the package \texttt{effectsize}, which
contains plenty of useful functions computing effect size under various
scenarios.

Note that there's also an \texttt{eta\_squared()} from the package
\texttt{rstatix()}, but that can only deal with univariate ANOVA, it
would fail when encountering MANOVA models.

Also note that the package \texttt{effectsize} is incorporated in
\texttt{bruceR}!

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{effectsize}\SpecialCharTok{::}\FunctionTok{eta\_squared}\NormalTok{(fit)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## # Effect Size for ANOVA (Type I)
## 
## Parameter | Eta2 (partial) |       95% CI
## -----------------------------------------
## jobcat    |           0.63 | [0.59, 1.00]
## 
## - One-sided CIs: upper bound fixed at [1.00].
\end{verbatim}

\hypertarget{univariate-anova}{%
\subsubsection{Univariate ANOVA}\label{univariate-anova}}

Needed if MANOVA reports significant results.

\texttt{summary.aov()} will report the univariate ANOVA respectively.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{summary.aov}\NormalTok{(fit)  }\CommentTok{\#for indDVidual anovas}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##  Response 1 :
##              Df     Sum Sq    Mean Sq F value    Pr(>F)    
## jobcat        1 8.3933e+10 8.3933e+10  733.86 < 2.2e-16 ***
## Residuals   472 5.3983e+10 1.1437e+08                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response 2 :
##              Df     Sum Sq    Mean Sq F value    Pr(>F)    
## jobcat        1 1.6687e+10 1.6687e+10  624.44 < 2.2e-16 ***
## Residuals   472 1.2614e+10 2.6724e+07                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\hypertarget{post-hoc-tests}{%
\subsubsection{Post-Hoc Tests}\label{post-hoc-tests}}

Note that the post-hoc tests here are essentially for the univariate
ANOVA.

Note that the \texttt{games\_howell\_test()} is different from
\texttt{tukey\_hsd()}.

Note that for many DVs to take the post-hoc tests simultaneously, first
\texttt{gather()} the targeted columns, and then take the
\textbf{grouped} data for the test.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{salaryData }\SpecialCharTok{\%\textgreater{}\%} \FunctionTok{gather}\NormalTok{(}\AttributeTok{key =} \StringTok{"var"}\NormalTok{, }
                      \AttributeTok{value =} \StringTok{"value"}\NormalTok{, }
\NormalTok{                      salary, salbegin) }\SpecialCharTok{|\textgreater{}} 
  \FunctionTok{group\_by}\NormalTok{(var) }\SpecialCharTok{|\textgreater{}}
  \FunctionTok{games\_howell\_test}\NormalTok{(value}\SpecialCharTok{\textasciitilde{}}\NormalTok{jobcat) }
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## # A tibble: 6 x 9
##   var      .y.   group1 group2 estimate conf.low conf.high    p.adj p.adj.signif
## * <chr>    <chr> <chr>  <chr>     <dbl>    <dbl>     <dbl>    <dbl> <chr>       
## 1 salary   value 1      2         3100.    1746.     4455. 1.22e- 6 ****        
## 2 salary   value 1      3        36139.   31302.    40977. 4.25e-10 ****        
## 3 salary   value 2      3        33039.   28196.    37881. 4.21e-10 ****        
## 4 salbegin value 1      2          982.     256.     1707. 6   e- 3 **          
## 5 salbegin value 1      3        16162.   13539.    18784. 2.74e-10 ****        
## 6 salbegin value 2      3        15180.   12514.    17846. 4.77e-10 ****
\end{verbatim}

\hypertarget{logistic-regression}{%
\section{Logistic Regression}\label{logistic-regression}}

Logistic Regression is for fitting a regression \textbf{curve} to data
where the DV is \textbf{dichotomous}. Meanwhile, the IV(s) can take on
any form, such as binary, categorical and continuous.

The goal of logistic regression is to model the probability of a certain
event (or say, a specific input). However, probability \(p\) is not used
directly here. Use \textbf{Odds} instead.

\[
Odds=\frac{p}{1-p}
\]

The logistic function used as such a predictor is as follows.

\[
p(z)=\frac1{1+e^{-z}}=\frac{e^{z}}{1+e^{z}}
\]

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-73-1.pdf}

The logistic function performs well that it's the C.D.F of a normal
distribution! Or take derivative of the logistic function, beautiful and
unique traits of the normal distribution can be well applied.

In the logistic function, \(z=\alpha+\beta x\), which can also be
extended to multiple IVs, i.e.~\(z = \beta_0+\beta_1+...+\beta_n x\).

Obviously, if the coefficient of IV is positive, then the probability
will increase with IV. If not significantly different from zero, then
probability won't significantly be affected by it.

Take a step further, the coefficient \(\beta\) is interpreted through
the term ``\textbf{Odds Ratio}'', where

\[
Odds\ Ratio = \frac{adds(x_i+1)}{odds(x_i)}=e^{\beta_i}
\]

The odds ratio is the effect size of corresponding IV \(x_i\), which
means one unit increase in x would induce the odds to change to
\(e^{\beta_i}\) times of itself. The direction given by it coincides
with previous discussion of the sign of coefficients and direction of
odds' change. Then, if \(\beta_i\) is statistically differently from 0
(through \(z\) test or Wald test, equivalent), then the odds ratio is
significantly different from 1, and 1 doesn't fall in its CI.

Furthermore, given an observation, the CI of probability can be
obtained.

All above seems strange at first glance. However, the focus on odds
isn't odd. From the logistic function, doing some transformation, we can
get the logit function.

\[
Logit(p)=\ln(\frac{p}{1-p})=\alpha+\beta x
\]

The logit function is linear and the response variable has much to do
with odds!

However, the linear regression line here isn't obtained
``traditionally'' as we did. Maximum Likelihood Estimation (MLE) is
used, instead of Ordinary Least Squares (OLS).

The following picture can help understand why MLE.

\includegraphics[width=1\linewidth]{Logit transformation}

For real data, the observed outcome can be either 0 or 1. However, that
won't make any sense considering linear transforming that into
\(Logit(p)=\ln(\frac{p}{1-p})\), either \(-\infty\) or \(+\infty\).
Thus, OLS doesn't work here.

Alternatively, MLE is used here to estimate a ``good'' fit. Given an
arbitrary regression line, each point is projected onto the line
according to its \textbf{\(x\)} value(s). Then the goodness of such fit
is rated by its ``likelihood'' with the reality, which means better
model should explain and predict the data better， thus depicting the
world better.

The likelihood used here is as follows.

\[
\begin{aligned}
\text{L}(data) & =\Pi_{i=1}^n [p(x_i)^{y_i}(1-p(x_i))^{1-y_i}]\\
\text{LL}(data)& =\sum_{i=1}^n [{y_i}\times p(x_i)+{1-y_i}\times(1-p(x_i))]
\end{aligned}
\]

Starting from a seemingly ``arbitrary'' line (the initial parameter
should be specified or guessed by human, and that matters when it comes
to efficiency.), the algorithm is smart enough that it'll change the
parameters repeatedly until for a direction with higher likelihood, and
ultimately find the maximized one and the corresponding parameters as
the solution.

Finally, the fitted result can be evaluated through four aspects.

\includegraphics[width=1\linewidth]{evaluation of logitstic model}

\[
\begin{aligned}
Accuracy&=\frac{TP+TN}{TP+TN+FP+FN}\\
Precision&=\frac{TP}{TP+FP}\\
Sensitivity&=\frac{TP}{TP+FN}\\
Specificity&=\frac{TN}{TN+FP}
\end{aligned}
\]

\begin{itemize}
\tightlist
\item
  Accuracy is the rate that the model can predict true results,
  \textbf{either positive or negative}.
\item
  Precision is the rate that the model can predict true positive
  results, \textbf{within reported positive ones}.
\item
  Sensitivity is the rate that the model can predict true positive
  results, \textbf{within actual positive ones}.
\item
  Specificity is the rate that the model can predict the true
  \textbf{negative} results, \textbf{within actual negative ones}.
\end{itemize}

How many parameters the mdoel should have in order to have an effective
and efficient prediction? More parameters added into a certain model
will increase accuracy for the present data (sometimes even won't),
however, such complicated model may lose generalizability and lose its
ability to predict. Well, on the other hand, simple model may not have
high ability to explain or predict the data. We should find a
compromise.

\includegraphics[width=1\linewidth]{fitting model}

Generally speaking, for models evaluated by MLE, the comparison of MLEs
under different hypothesis can be applied to model comparison.

\[
\begin{aligned}
H_0&:\theta=\theta_0\\
H_1&:\theta=\theta_1\\
\Lambda(y)&=\frac{L(\theta_1|y)}{L(\theta_2|y)}
\end{aligned}\\
If\ \Lambda(y)>c,\ reject\ H_0
\]

Based on above, here to introduce the nested hypothesis test.
Apparently, the nested model is simpler, we're to check necessity of the
addition of other IV(s).

Suppose Model 0 (\(M_0\)) is nested in Model 1 (\(M_1\)). \(M_0\) has
\(m\) parameters, and \(M_1\) has \(n\). Define

\[
\Delta(y) = 2\ln(L(\hat{\theta_1}|y)-2\ln(L(\hat{\theta_0}|y))
\]

Here factor ``2'' is to scale the statistic to make it that

\[
\Delta(y)\sim \chi_{n-m}^2
\]

Hence, if \(\Delta(y)\) exceeds the critical value, reject \(M_0\) and
recognize that \(M_1\) is simpler but not worse (i.e.~general and
powerful).

Often, a model with intercept and predictors (B) is compared to an
intercept-only model to test whether the predictors contribute something
over and above the intercept-only.

Note that considering sample size when evaluating goodness of fit,
\(\chi^2\) statistics are heavily influenced by sample size so that with
a very large sample, even a tiny difference will be significant. If the
sample size is large and the \(\chi^2\) is significant, this may not be
important.

Coming back, there're three ways for model fitting.

\begin{itemize}
\tightlist
\item
  Enter: all theoretically-based variables are added at once.
\item
  Forward Selection: one parameter at a time, significant ones kept.
  Used for exploratory purposes.
\item
  Backward Elimination: full model first, eliminate one by one. Used for
  finding a concise model.
\end{itemize}

All three way should be based on the significance of \(\chi^2\) test.

Meanwhile, there're more concepts or evaluations for model fitting,
indicating goodness of fit.

\begin{itemize}
\tightlist
\item
  Nagelkerke \(r^2\): between 0 and 1, similar to \(r^2\), preferred.
\item
  Hosmer and Lemeshow (HL) test: compare the observed frequency and the
  predicted frequency, an alternative to model \(\chi^2\) test. If NOT
  significant, good!
\end{itemize}

\hypertarget{fit-the-model-2}{%
\subsection{Fit the Model}\label{fit-the-model-2}}

Using the data \texttt{logisticData}, we're to predict whether or not to
have cancer through logistic regression model, with \texttt{age},
\texttt{pathsize} and \texttt{pathscat} (3 types) as predictors.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{logisticData }\OtherTok{\textless{}{-}}\NormalTok{ readxl}\SpecialCharTok{::}\FunctionTok{read\_xlsx}\NormalTok{(}\StringTok{"Data11{-}02.xlsx"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

Observations with \texttt{pathscat\ ==\ 99} are invalid and should be
eliminated.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{logisticData }\OtherTok{=}\NormalTok{ logisticData }\SpecialCharTok{|\textgreater{}} \FunctionTok{filter}\NormalTok{(pathscat }\SpecialCharTok{!=} \DecValTok{99}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

Then, set dummy variables.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{logisticData }\OtherTok{=} \FunctionTok{data.frame}\NormalTok{(}\FunctionTok{model.matrix}\NormalTok{(}
    \SpecialCharTok{\textasciitilde{}}\FunctionTok{factor}\NormalTok{(pathscat),logisticData)) }\SpecialCharTok{|\textgreater{}} 
\NormalTok{  dplyr}\SpecialCharTok{::}\FunctionTok{select}\NormalTok{(}\DecValTok{2}\SpecialCharTok{:}\DecValTok{3}\NormalTok{) }\SpecialCharTok{|\textgreater{}} 
  \FunctionTok{rename}\NormalTok{(}
    \AttributeTok{pathscat2 =}\NormalTok{ factor.pathscat}\FloatTok{.2}\NormalTok{,}
    \AttributeTok{pathscat3 =}\NormalTok{ factor.pathscat}\FloatTok{.3}
\NormalTok{  ) }\SpecialCharTok{|\textgreater{}}
  \FunctionTok{cbind}\NormalTok{(logisticData)}
\end{Highlighting}
\end{Shaded}

Setting dummies is quite important when having categorical data, and for
more details, see appendix.

Use
\texttt{glm(formula,\ data,\ family\ =\ binomial(\textquotesingle{}logit\textquotesingle{}))}
to get the logistic model. Here \texttt{glm()} stands for ``Generalized
Linear Model''.

As for the model, others are the same with what we've learned.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fit }\OtherTok{=} \FunctionTok{glm}\NormalTok{(ln\_yesno }\SpecialCharTok{\textasciitilde{}}\NormalTok{ age }\SpecialCharTok{+}\NormalTok{ pathsize }\SpecialCharTok{+}\NormalTok{ pathscat2 }\SpecialCharTok{+}\NormalTok{ pathscat3,}
          \AttributeTok{data =}\NormalTok{ logisticData,}
          \AttributeTok{family =} \FunctionTok{binomial}\NormalTok{(}\StringTok{\textquotesingle{}logit\textquotesingle{}}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

Note that \texttt{\textquotesingle{}logit\textquotesingle{}} is the
default for
\texttt{binomial(\textquotesingle{}logit\textquotesingle{}))}.

Not that in \texttt{lm()}, categorical variables can be thrown in
directly, without setting dummies tediously. But the categorical
variable should be ``factor'' or ``character'', not ``numeric''.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{summary}\NormalTok{(fit)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## glm(formula = ln_yesno ~ age + pathsize + pathscat2 + pathscat3, 
##     family = binomial("logit"), data = logisticData)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.9676  -0.7400  -0.6134  -0.4497   2.1834  
## 
## Coefficients:
##              Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -0.583306   0.391314  -1.491   0.1361    
## age         -0.024646   0.005764  -4.276  1.9e-05 ***
## pathsize     0.423857   0.130885   3.238   0.0012 ** 
## pathscat2   -0.122002   0.266498  -0.458   0.6471    
## pathscat3    0.184969   0.845896   0.219   0.8269    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 1216.7  on 1120  degrees of freedom
## Residual deviance: 1151.8  on 1116  degrees of freedom
## AIC: 1161.8
## 
## Number of Fisher Scoring iterations: 4
\end{verbatim}

The logistic regression model is different from the traditional OLS
model; there's no F value and multiple R squared. Instead, deviance of
the designated model and the null model are reported at bottom, no test
represented here. In fact, the \textbf{deviance} is 2 times the
\textbf{Log-Likelihood} taking the absolute value. and the larger the
AIC, the prettier the model comparison.

The effect size for each \(x_i\) is \(e^{\beta_i}\), which can be
obtained through a matrix. Interestingly, the \texttt{exp()} can be
exerted to a data frame and all values within.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{exp}\NormalTok{(}\FunctionTok{cbind}\NormalTok{(}\AttributeTok{OR =} \FunctionTok{coef}\NormalTok{(fit), }\FunctionTok{confint}\NormalTok{(fit)))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Waiting for profiling to be done...
\end{verbatim}

\begin{verbatim}
##                    OR     2.5 %    97.5 %
## (Intercept) 0.5580501 0.2582678 1.1992024
## age         0.9756550 0.9645972 0.9866601
## pathsize    1.5278427 1.1832781 1.9780602
## pathscat2   0.8851466 0.5234879 1.4893971
## pathscat3   1.2031815 0.2348608 6.7095766
\end{verbatim}

\hypertarget{goodness-of-fit-1}{%
\subsection{Goodness of Fit}\label{goodness-of-fit-1}}

\hypertarget{each-predictor}{%
\subsubsection{Each Predictor}\label{each-predictor}}

\hypertarget{separately}{%
\paragraph{Separately}\label{separately}}

Use \texttt{aod::wald.test(Sigma,\ b,\ Terms)} to carry the Wald test
for each coefficient.

\begin{itemize}
\item
  \texttt{Sigma}: a var-cov matrix of the model, extracted from
  \texttt{vcov(model)}.
\item
  \texttt{b}: a vector of coefficients with var-cov matrix
  \texttt{Sigma}, extracted from \texttt{coef(model)}.
\item
  \texttt{Terms}: an optional integer vector specifying which
  coefficients should be jointly tested, like choosing the variable from
  the var-cov matrix.
\end{itemize}

The result of significance of the Wald test is the same as is reported
in \texttt{summary(model)}.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{aod}\SpecialCharTok{::}\FunctionTok{wald.test}\NormalTok{(}
  \AttributeTok{Sigma =} \FunctionTok{vcov}\NormalTok{(fit),}
  \AttributeTok{b =} \FunctionTok{coef}\NormalTok{(fit), }
  \AttributeTok{Terms =} \DecValTok{2}\SpecialCharTok{:}\DecValTok{2}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Wald test:
## ----------
## 
## Chi-squared test:
## X2 = 18.3, df = 1, P(> X2) = 1.9e-05
\end{verbatim}

\hypertarget{forward-selection}{%
\paragraph{Forward Selection}\label{forward-selection}}

Each predictor is tested sequentially, corresponding to ``forward
selection''.

Note that if you didn't set dummies encountering with categorical
variables, fine with the linear model, but here goes wrong!

Note that \texttt{test\ =\ \textquotesingle{}Chisq\textquotesingle{}}
with capital `C', otherwise an error will be raised.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{anova}\NormalTok{(fit,}\AttributeTok{test =} \StringTok{\textquotesingle{}Chisq\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Analysis of Deviance Table
## 
## Model: binomial, link: logit
## 
## Response: ln_yesno
## 
## Terms added sequentially (first to last)
## 
## 
##           Df Deviance Resid. Df Resid. Dev  Pr(>Chi)    
## NULL                       1120     1216.7              
## age        1   32.315      1119     1184.3 1.311e-08 ***
## pathsize   1   32.024      1118     1152.3 1.523e-08 ***
## pathscat2  1    0.510      1117     1151.8    0.4750    
## pathscat3  1    0.048      1116     1151.8    0.8265    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\hypertarget{overall-model}{%
\subsubsection{Overall Model}\label{overall-model}}

The deviance is the absolute value of 2 times the log-likelihood, the
difference of the deviance of the null model and the designated model is
the statistic used for the model comparison, which follows a \(\chi^2\)
distribution with its degree of freedom as the difference of the number
of parameter in two models.

\[
\Delta(y)=deviance_{null}-deviance\sim \chi^2_n,n=\#IV
\]

Therefore, we can even carry the model test manually.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{with}\NormalTok{(fit, null.deviance }\SpecialCharTok{{-}}\NormalTok{ deviance)}
\FunctionTok{with}\NormalTok{(fit, df.null }\SpecialCharTok{{-}}\NormalTok{ df.residual)}
\FunctionTok{with}\NormalTok{(fit, }\FunctionTok{pchisq}\NormalTok{(null.deviance }\SpecialCharTok{{-}}\NormalTok{ deviance, }
\NormalTok{                 df.null }\SpecialCharTok{{-}}\NormalTok{ df.residual, }
                 \AttributeTok{lower.tail =} \ConstantTok{FALSE}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

The result of model comparison is highly significant, meaning that the
designated model can explain far more variation of the data than the
null one.

But better to use
\texttt{anova(model1,null-model,test\ =\ \textquotesingle{}Chisq\textquotesingle{})}.
Same result.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{nullfit }\OtherTok{=} \FunctionTok{glm}\NormalTok{(ln\_yesno }\SpecialCharTok{\textasciitilde{}} \DecValTok{1}\NormalTok{,}
          \AttributeTok{data =}\NormalTok{ logisticData,}
          \AttributeTok{family =} \FunctionTok{binomial}\NormalTok{(}\StringTok{\textquotesingle{}logit\textquotesingle{}}\NormalTok{))}
\FunctionTok{anova}\NormalTok{(fit,nullfit,}\AttributeTok{test =} \StringTok{\textquotesingle{}Chisq\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Analysis of Deviance Table
## 
## Model 1: ln_yesno ~ age + pathsize + pathscat2 + pathscat3
## Model 2: ln_yesno ~ 1
##   Resid. Df Resid. Dev Df Deviance  Pr(>Chi)    
## 1      1116     1151.8                          
## 2      1120     1216.7 -4  -64.897 2.705e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\hypertarget{model-fitting}{%
\subsubsection{Model Fitting}\label{model-fitting}}

The designated model wins, but that unnecessarily means it can explains
the data well. Then we're to take the test of goodness of fit, checking
the observed and predicted frequencies. No significance is wanted here.

Methods for such model fitting varies. Here we use HL-test. Use
\texttt{ResourceSelection::hoslem.test(x\ =\ observed\_values,y\ =\ expected\_values,g\ =\ 10)},
where \texttt{g} is the number of bins to calculate quantiles and
\texttt{x} \& \texttt{y} cannot be reversed.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{ResourceSelection}\SpecialCharTok{::}\FunctionTok{hoslem.test}\NormalTok{(}\AttributeTok{x =}\NormalTok{ logisticData}\SpecialCharTok{$}\NormalTok{ln\_yesno, }
                               \AttributeTok{y =} \FunctionTok{fitted}\NormalTok{(fit), }
                               \AttributeTok{g =} \DecValTok{10}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Hosmer and Lemeshow goodness of fit (GOF) test
## 
## data:  logisticData$ln_yesno, fitted(fit)
## X-squared = 8.9727, df = 8, p-value = 0.3446
\end{verbatim}

\emph{Note that the bins set should not be so dense that the sample size
within each bin will fall short.}

\hypertarget{fitting-quality}{%
\subsubsection{Fitting Quality}\label{fitting-quality}}

\hypertarget{log-likelihood}{%
\paragraph{Log-Likelihood}\label{log-likelihood}}

Log-likelihood can be obtained using \texttt{logLik(model)} directly,
with \texttt{df} telling parameters in the model (including the
intercept).

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{logLik}\NormalTok{(fit)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 'log Lik.' -575.8852 (df=5)
\end{verbatim}

Log-likelihood can be obtained through \emph{deviance}, which is 2 times
the log-likelihood, and you can get that using \texttt{model\$deviance}.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fit}\SpecialCharTok{$}\NormalTok{deviance}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] 1151.77
\end{verbatim}

\hypertarget{r-squares}{%
\paragraph{R Squares}\label{r-squares}}

Obtain all 5 R Squares by \texttt{pscl::pR2(model)}.

Hint:

\begin{itemize}
\tightlist
\item
  \texttt{r2CU} is the Nagelkerke R2;
\item
  \texttt{r2ML} is the Cox\&Snell R2;
\item
  \texttt{llh} is the log-likelihood of the designated model;
\item
  \texttt{llhNull} is the log-likelihood of the null model.
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{pscl}\SpecialCharTok{::}\FunctionTok{pR2}\NormalTok{(fit)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## fitting null model for pseudo-r2
\end{verbatim}

\begin{verbatim}
##           llh       llhNull            G2      McFadden          r2ML 
## -575.88522800 -608.33388920   64.89732239    0.05334022    0.05624846 
##          r2CU 
##    0.08494006
\end{verbatim}

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

You can compare one model against another using
\texttt{anova(model1,model2,test\ =\ \textquotesingle{}Chisq\textquotesingle{})}.

For example, it's natural to think of an alternative model from what
we've fitted originally. Just keep all significant IV(s), and we get a
new model to be compared against.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fit2 }\OtherTok{=} \FunctionTok{glm}\NormalTok{(ln\_yesno }\SpecialCharTok{\textasciitilde{}}\NormalTok{ age }\SpecialCharTok{+}\NormalTok{ pathsize,}
           \AttributeTok{data =}\NormalTok{ logisticData,}
           \AttributeTok{family =} \FunctionTok{binomial}\NormalTok{(}\StringTok{"logit"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{anova}\NormalTok{(fit,fit2,}\AttributeTok{test =} \StringTok{\textquotesingle{}Chisq\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Analysis of Deviance Table
## 
## Model 1: ln_yesno ~ age + pathsize + pathscat2 + pathscat3
## Model 2: ln_yesno ~ age + pathsize
##   Resid. Df Resid. Dev Df Deviance Pr(>Chi)
## 1      1116     1151.8                     
## 2      1118     1152.3 -2 -0.55846   0.7564
\end{verbatim}

The significance is shown under \texttt{Pr(\textgreater{}Chi)}. And
\texttt{Df} shows the degree of freedom of the certain \(\chi^2\)
distribution. \texttt{Deviance} shows the \(\chi^2\) statistic value.

In this case, a simpler model does not perform statistically worse and
is considered better.

\hypertarget{classification-table}{%
\subsubsection{Classification Table}\label{classification-table}}

It's the last step of logistic regression. Just see how well the model
works when putting that into practice!

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{pred\_y }\OtherTok{=} \FunctionTok{round}\NormalTok{(}\FunctionTok{predict}\NormalTok{(fit,}\AttributeTok{type=}\StringTok{\textquotesingle{}response\textquotesingle{}}\NormalTok{),}\DecValTok{0}\NormalTok{)}

\NormalTok{classification\_df }\OtherTok{=} \FunctionTok{data.frame}\NormalTok{(}
  \AttributeTok{observed\_y =}\NormalTok{ logisticData}\SpecialCharTok{$}\NormalTok{ln\_yesno,}
  \AttributeTok{predicted\_y =} \FunctionTok{round}\NormalTok{(pred\_y,}\DecValTok{0}\NormalTok{))}

\FunctionTok{xtabs}\NormalTok{(}\SpecialCharTok{\textasciitilde{}}\NormalTok{predicted\_y}\SpecialCharTok{+}\NormalTok{observed\_y,}\AttributeTok{data=}\NormalTok{classification\_df)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##            observed_y
## predicted_y   0   1
##           0 846 246
##           1  14  15
\end{verbatim}

You can also use the \texttt{caret::confusionMatrix()} to make a
classification table, and get all the performance metrics conveniently.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(caret)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Attaching package: 'caret'
\end{verbatim}

\begin{verbatim}
## The following object is masked from 'package:survival':
## 
##     cluster
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{classtab }\OtherTok{=} \FunctionTok{confusionMatrix}\NormalTok{(}\FunctionTok{factor}\NormalTok{(pred\_y),}\FunctionTok{factor}\NormalTok{(logisticData}\SpecialCharTok{$}\NormalTok{ln\_yesno))}
\NormalTok{classtab}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   0   1
##          0 846 246
##          1  14  15
##                                           
##                Accuracy : 0.7681          
##                  95% CI : (0.7422, 0.7925)
##     No Information Rate : 0.7672          
##     P-Value [Acc > NIR] : 0.4884          
##                                           
##                   Kappa : 0.0597          
##                                           
##  Mcnemar's Test P-Value : <2e-16          
##                                           
##             Sensitivity : 0.98372         
##             Specificity : 0.05747         
##          Pos Pred Value : 0.77473         
##          Neg Pred Value : 0.51724         
##              Prevalence : 0.76717         
##          Detection Rate : 0.75468         
##    Detection Prevalence : 0.97413         
##       Balanced Accuracy : 0.52060         
##                                           
##        'Positive' Class : 0               
## 
\end{verbatim}

\hypertarget{report-results}{%
\subsection{Report Results}\label{report-results}}

A logistic regression analysis was conducted to predict the occurrence
of cancer using cancer category, age and tumor size as predictors. A
test of the full model against a constant-only model was statistically
significant, indicating that the predictors as a set reliably
distinguished between cancer and non-cancer
(\(χ^2(4)= 64.897, p < .001\)).

Nagelkerke's R2 of \(.085\) indicated a weak relationship between
prediction and grouping. Prediction success overall was 76.8\% (98.4\%
for detecting a non-cancer and 5.7\% for cancer). The Wald criterion
demonstrated that only age and tumor size made a significant
contribution to prediction (p \textless{} .001 and p =.001,
respectively). Cancer category was not a significant predictor. EXP(B)
value indicates that when tumor size is raised by one unit the odds
ratio is 1.5 times as large and therefore the tumor are 1.5 more times
likely to be cancer.

\hypertarget{multi-linear-regression}{%
\section{Multi-Linear Regression}\label{multi-linear-regression}}

The data \texttt{df} presented in MLR contains information about salary,
beginning salary, education year, job category, previous working
experience and so on. The relationship between salary and beginning
salary, education, to name a few, is of interest.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{df }\OtherTok{=}\NormalTok{ readxl}\SpecialCharTok{::}\FunctionTok{read\_excel}\NormalTok{(}\StringTok{"data02{-}01.xlsx"}\NormalTok{, }\AttributeTok{sheet =} \DecValTok{1}\NormalTok{)}
\FunctionTok{attach}\NormalTok{(df)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## The following objects are masked _by_ .GlobalEnv:
## 
##     age, jobcat
\end{verbatim}

\hypertarget{pre-inspection-1}{%
\subsection{Pre-Inspection}\label{pre-inspection-1}}

\hypertarget{linear-relationship}{%
\subsubsection{linear relationship}\label{linear-relationship}}

Use \texttt{cor()} to carry the correlation test, the default method of
which is
\texttt{method\ =\ \textquotesingle{}pearson\textquotesingle{}}. The
result will be presented as a correlation matrix.

However, obviously the correlation matrix won't give you information
about p-value (or say, significance).

Good thing is that the check of linear relationship is the first and
rough step for further regression, maybe other more information (about
p-value or CIs) is unnecessary. Another good thing is that the
\texttt{cor()} is from base R, no extra requirement to library packages.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{df }\SpecialCharTok{|\textgreater{}}\NormalTok{ dplyr}\SpecialCharTok{::}\FunctionTok{select}\NormalTok{(salary,educ,salbegin,jobtime,prevexp) }\SpecialCharTok{|\textgreater{}} \FunctionTok{cor}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##               salary        educ    salbegin      jobtime      prevexp
## salary    1.00000000  0.66055891  0.88011747  0.084092267 -0.097466926
## educ      0.66055891  1.00000000  0.63319565  0.047378777 -0.252352521
## salbegin  0.88011747  0.63319565  1.00000000 -0.019753475  0.045135627
## jobtime   0.08409227  0.04737878 -0.01975347  1.000000000  0.002978134
## prevexp  -0.09746693 -0.25235252  0.04513563  0.002978134  1.000000000
\end{verbatim}

Well, it's clumsy to compress the target data (variables) into a matrix
to take the test and get the correlation matrix. Just fine to know a bit
about that.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{attach}\NormalTok{(df)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## The following objects are masked _by_ .GlobalEnv:
## 
##     age, jobcat
\end{verbatim}

\begin{verbatim}
## The following objects are masked from df (pos = 3):
## 
##     age, bdate, educ, gender, id, jobcat, jobtime, minority, prevexp,
##     salary, salbegin
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# first create an empty matrix, primarily filled with NA}
\NormalTok{m }\OtherTok{=} \FunctionTok{matrix}\NormalTok{(}\AttributeTok{nrow=}\FunctionTok{nrow}\NormalTok{(salaryData),}\AttributeTok{ncol=}\DecValTok{5}\NormalTok{)}
\NormalTok{m[,}\DecValTok{1}\NormalTok{]}\OtherTok{=}\NormalTok{salary}
\NormalTok{m[,}\DecValTok{2}\NormalTok{]}\OtherTok{=}\NormalTok{educ}
\NormalTok{m[,}\DecValTok{3}\NormalTok{] }\OtherTok{=}\NormalTok{ salbegin}
\NormalTok{m[,}\DecValTok{4}\NormalTok{]}\OtherTok{=}\NormalTok{jobtime  }
\NormalTok{m[,}\DecValTok{5}\NormalTok{]}\OtherTok{=}\NormalTok{prevexp}
\CommentTok{\#check the correlation matrix}
\FunctionTok{cor}\NormalTok{(m)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##             [,1]        [,2]        [,3]         [,4]         [,5]
## [1,]  1.00000000  0.66055891  0.88011747  0.084092267 -0.097466926
## [2,]  0.66055891  1.00000000  0.63319565  0.047378777 -0.252352521
## [3,]  0.88011747  0.63319565  1.00000000 -0.019753475  0.045135627
## [4,]  0.08409227  0.04737878 -0.01975347  1.000000000  0.002978134
## [5,] -0.09746693 -0.25235252  0.04513563  0.002978134  1.000000000
\end{verbatim}

Additionally, I'd like to use \texttt{bruceR::Corr()} for the
correlation matrix, which is visible for both in amount and in
significance. Moreover, it's fancy!

For simplicity, the two decimals by default is well enough. If more
accuracy is needed, you can set the parameter \texttt{digits\ =}.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{df }\SpecialCharTok{|\textgreater{}}\NormalTok{ dplyr}\SpecialCharTok{::}\FunctionTok{select}\NormalTok{(salary,educ,salbegin,jobtime,prevexp) }\SpecialCharTok{|\textgreater{}} 
\NormalTok{  bruceR}\SpecialCharTok{::}\FunctionTok{Corr}\NormalTok{(}\AttributeTok{digits =} \DecValTok{2}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-98-1.pdf}

\begin{verbatim}
## Correlation matrix is displayed in the RStudio `Plots` Pane.
## 
## Pearson's r and 95% confidence intervals:
## ────────────────────────────────────────────────────
##                       r       [95% CI]     p       N
## ────────────────────────────────────────────────────
## salary-educ        0.66 [ 0.61,  0.71] <.001 *** 474
## salary-salbegin    0.88 [ 0.86,  0.90] <.001 *** 474
## salary-jobtime     0.08 [-0.01,  0.17]  .067 .   474
## salary-prevexp    -0.10 [-0.19, -0.01]  .034 *   474
## educ-salbegin      0.63 [ 0.58,  0.68] <.001 *** 474
## educ-jobtime       0.05 [-0.04,  0.14]  .303     474
## educ-prevexp      -0.25 [-0.33, -0.17] <.001 *** 474
## salbegin-jobtime  -0.02 [-0.11,  0.07]  .668     474
## salbegin-prevexp   0.05 [-0.05,  0.13]  .327     474
## jobtime-prevexp    0.00 [-0.09,  0.09]  .948     474
## ────────────────────────────────────────────────────
\end{verbatim}

\hypertarget{multicollinearity}{%
\subsubsection{multicollinearity}\label{multicollinearity}}

Use \texttt{car::vif(model)} and get the result directly.

For the convenience of narration, check of multicollinearity is
presented here. Most of the time, the check is a ``post-hoc'' one, after
obtaining the fitted model.

VIF less than 2 is ideal, and less than 5 is the bottom line.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fit }\OtherTok{=} \FunctionTok{lm}\NormalTok{(salary }\SpecialCharTok{\textasciitilde{}}\NormalTok{ educ }\SpecialCharTok{+}\NormalTok{ salbegin }\SpecialCharTok{+}\NormalTok{ jobtime }\SpecialCharTok{+}\NormalTok{ prevexp,}\AttributeTok{data =}\NormalTok{ df)}
\NormalTok{car}\SpecialCharTok{::}\FunctionTok{vif}\NormalTok{(fit)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##     educ salbegin  jobtime  prevexp 
## 1.937010 1.813582 1.007613 1.155814
\end{verbatim}

\hypertarget{fit-the-model-3}{%
\subsection{Fit the Model}\label{fit-the-model-3}}

\hypertarget{basic-summary}{%
\subsubsection{basic summary}\label{basic-summary}}

Firstly, use \texttt{lm()} to get the linear model.

Subsequently, use \texttt{summary()} to get the detailed result.

Period.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fit }\OtherTok{=} \FunctionTok{lm}\NormalTok{(salary }\SpecialCharTok{\textasciitilde{}}\NormalTok{ educ }\SpecialCharTok{+}\NormalTok{ salbegin }\SpecialCharTok{+}\NormalTok{ jobtime }\SpecialCharTok{+}\NormalTok{ prevexp,}\AttributeTok{data =}\NormalTok{ df)}
\FunctionTok{summary}\NormalTok{(fit)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = salary ~ educ + salbegin + jobtime + prevexp, data = df)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -29600  -4119  -1246   2642  46079 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1.615e+04  3.255e+03  -4.961 9.84e-07 ***
## educ         6.699e+02  1.656e+02   4.045 6.11e-05 ***
## salbegin     1.768e+00  5.873e-02  30.111  < 2e-16 ***
## jobtime      1.615e+02  3.425e+01   4.715 3.19e-06 ***
## prevexp     -1.730e+01  3.528e+00  -4.904 1.30e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 7465 on 469 degrees of freedom
## Multiple R-squared:  0.8105, Adjusted R-squared:  0.8089 
## F-statistic: 501.5 on 4 and 469 DF,  p-value: < 2.2e-16
\end{verbatim}

It's noteworthy that \texttt{bruceR::print\_table(model)} can spring up
the regression result as a decent table, and can be exported to a
\texttt{.doc} file.

However, fine with \texttt{summary()}, decency doesn't matter much here,
and the \texttt{summary()} can give more information about the model
itself that is essential, such F value and multiple R squared.

\hypertarget{coefficients}{%
\subsubsection{coefficients}\label{coefficients}}

Use \texttt{coefficients(model)} from base R to see the coefficients for
each IV.

Furthermore, use \texttt{confint(model,level\ =\ 0.95)} from base R to
see the CIs (95\% by default) for each coefficients.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{coefficients}\NormalTok{(fit)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##   (Intercept)          educ      salbegin       jobtime       prevexp 
## -16149.671204    669.913570      1.768427    161.485642    -17.303251
\end{verbatim}

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

\begin{verbatim}
##                     2.5 %       97.5 %
## (Intercept) -22546.784635 -9752.557773
## educ           344.511104   995.316035
## salbegin         1.653019     1.883835
## jobtime         94.190179   228.781104
## prevexp        -24.236663   -10.369839
\end{verbatim}

Multiple R squared is reported through \texttt{summary()}.

By definition, Multiple R squared is defined as the square of the
correlation between estimated (fitted) value and real values.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{cor}\NormalTok{(salary,fit}\SpecialCharTok{$}\NormalTok{fitted.values)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] 0.9002721
\end{verbatim}

\hypertarget{model-comparison-1}{%
\subsubsection{model comparison}\label{model-comparison-1}}

Compare the model with the ``zero'' model, formally called the null
model. The null model has no IV but with intercept, which is the mean.
The null model uses the mean to predict the data or explain the
variation of values.

Get the null model by formula \texttt{DV\textasciitilde{}1}.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fit2 }\OtherTok{=} \FunctionTok{lm}\NormalTok{(salary }\SpecialCharTok{\textasciitilde{}} \DecValTok{1}\NormalTok{ )}
\end{Highlighting}
\end{Shaded}

Then use \texttt{anova(model1,model2)} to compare the two models.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{anova}\NormalTok{(fit,fit2)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Analysis of Variance Table
## 
## Model 1: salary ~ educ + salbegin + jobtime + prevexp
## Model 2: salary ~ 1
##   Res.Df        RSS Df   Sum of Sq      F    Pr(>F)    
## 1    469 2.6137e+10                                    
## 2    473 1.3792e+11 -4 -1.1178e+11 501.45 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

If we think further, the F test for the MLR model is to test with null
hypothesis that all the coefficients of all the IVs are zero. Therefore,
the model comparison by essence coincides with the F test! Obviously,
the F value ``copies'' that in the \texttt{summary()} result.

\hypertarget{standard-mlr}{%
\subsection{Standard MLR}\label{standard-mlr}}

Transform the formula with each variable embedded into the function
\texttt{scale()}, no other adjustments!

Under standard MLR, the coefficients are standardized ones.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fit\_std }\OtherTok{=} \FunctionTok{lm}\NormalTok{(}\FunctionTok{scale}\NormalTok{(salary) }\SpecialCharTok{\textasciitilde{}} \FunctionTok{scale}\NormalTok{(educ) }\SpecialCharTok{+} \FunctionTok{scale}\NormalTok{(salbegin) }\SpecialCharTok{+} 
               \FunctionTok{scale}\NormalTok{(jobtime) }\SpecialCharTok{+} \FunctionTok{scale}\NormalTok{(prevexp),}
             \AttributeTok{data =}\NormalTok{ df)}
\end{Highlighting}
\end{Shaded}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{summary}\NormalTok{(fit\_std)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = scale(salary) ~ scale(educ) + scale(salbegin) + 
##     scale(jobtime) + scale(prevexp), data = df)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.73349 -0.24121 -0.07298  0.15475  2.69850 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     -1.129e-16  2.008e-02   0.000        1    
## scale(educ)      1.132e-01  2.798e-02   4.045 6.11e-05 ***
## scale(salbegin)  8.151e-01  2.707e-02  30.111  < 2e-16 ***
## scale(jobtime)   9.515e-02  2.018e-02   4.715 3.19e-06 ***
## scale(prevexp)  -1.060e-01  2.161e-02  -4.904 1.30e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4372 on 469 degrees of freedom
## Multiple R-squared:  0.8105, Adjusted R-squared:  0.8089 
## F-statistic: 501.5 on 4 and 469 DF,  p-value: < 2.2e-16
\end{verbatim}

\hypertarget{semi--partial-for-x}{%
\subsubsection{(semi-) partial for x}\label{semi--partial-for-x}}

(Semi-) partial correlation indicates the variance accounted for by the
certain IV specifically, which is especially intriguing and important in
standard MlR.

Use \texttt{pcor()} from the package \texttt{ppcor} to compute the
partial correlation for IVs.

Use \texttt{spcor()} from the package \texttt{ppcor} to compute the
semi-partial correlation for IVs.

Throw the regressed variables (both IVs \& DV) into the functions above,
and period.

The results will include estimated value of correlation and its p-value.
However, the drawback is that the returned matrix does not include
rownames and colnames, making it reading-unfriendly. However, we only
care about the (semi-) partial correlation between DV and all IVs, which
means only the very one row or column is enough.

Therefore, DV goes first! Only check the first row or column!

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# partial correlation}
\NormalTok{df }\SpecialCharTok{|\textgreater{}}\NormalTok{ dplyr}\SpecialCharTok{::}\FunctionTok{select}\NormalTok{(salary,educ,salbegin,jobtime,prevexp) }\SpecialCharTok{|\textgreater{}} 
\NormalTok{  ppcor}\SpecialCharTok{::}\FunctionTok{pcor}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## $estimate
##              salary        educ   salbegin     jobtime     prevexp
## salary    1.0000000  0.18362578  0.8118323  0.21275206 -0.22085427
## educ      0.1836258  1.00000000  0.2347785  0.04216897 -0.30912243
## salbegin  0.8118323  0.23477855  1.0000000 -0.21340370  0.33602452
## jobtime   0.2127521  0.04216897 -0.2134037  1.00000000  0.07981028
## prevexp  -0.2208543 -0.30912243  0.3360245  0.07981028  1.00000000
## 
## $p.value
##                 salary         educ      salbegin      jobtime      prevexp
## salary    0.000000e+00 6.105604e-05 1.163309e-111 3.186348e-06 1.296712e-06
## educ      6.105604e-05 0.000000e+00  2.550384e-07 3.611647e-01 6.903840e-12
## salbegin 1.163309e-111 2.550384e-07  0.000000e+00 2.967847e-06 6.775217e-14
## jobtime   3.186348e-06 3.611647e-01  2.967847e-06 0.000000e+00 8.358673e-02
## prevexp   1.296712e-06 6.903840e-12  6.775217e-14 8.358673e-02 0.000000e+00
## 
## $statistic
##             salary       educ  salbegin    jobtime   prevexp
## salary    0.000000  4.0454629 30.110719  4.7153987 -4.904006
## educ      4.045463  0.0000000  5.230663  0.9140415 -7.039248
## salbegin 30.110719  5.2306629  0.000000 -4.7305296  7.726346
## jobtime   4.715399  0.9140415 -4.730530  0.0000000  1.733935
## prevexp  -4.904006 -7.0392480  7.726346  1.7339351  0.000000
## 
## $n
## [1] 474
## 
## $gp
## [1] 3
## 
## $method
## [1] "pearson"
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# part or semi{-}partial correlation}
\NormalTok{df }\SpecialCharTok{|\textgreater{}}\NormalTok{ dplyr}\SpecialCharTok{::}\FunctionTok{select}\NormalTok{(salary,educ,salbegin,jobtime,prevexp) }\SpecialCharTok{|\textgreater{}} 
\NormalTok{  ppcor}\SpecialCharTok{::}\FunctionTok{spcor}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## $estimate
##              salary        educ   salbegin     jobtime     prevexp
## salary    1.0000000  0.08132013  0.6052725  0.09478688 -0.09857818
## educ      0.1319374  1.00000000  0.1705911  0.02981024 -0.22957564
## salbegin  0.6028339  0.10472088  1.0000000 -0.09470792  0.15468589
## jobtime   0.2119468  0.04108416 -0.2126269  1.00000000  0.07793657
## prevexp  -0.2054291 -0.29487448  0.3236570  0.07263463  1.00000000
## 
## $p.value
##                salary         educ     salbegin    jobtime      prevexp
## salary   0.000000e+00 7.788628e-02 2.067811e-48 0.03975543 3.244186e-02
## educ     4.126643e-03 0.000000e+00 1.994428e-04 0.51867959 4.739749e-07
## salbegin 6.161045e-48 2.303088e-02 0.000000e+00 0.03992148 7.559174e-04
## jobtime  3.477648e-06 3.736610e-01 3.230041e-06 0.00000000 9.112418e-02
## prevexp  6.973073e-06 6.655125e-11 6.011570e-13 0.11543038 0.000000e+00
## 
## $statistic
##             salary       educ  salbegin    jobtime   prevexp
## salary    0.000000  1.7669539 16.466993  2.0620275 -2.145298
## educ      2.882488  0.0000000  3.749348  0.6458697 -5.108220
## salbegin 16.362654  2.2804165  0.000000 -2.0602942  3.390753
## jobtime   4.696710  0.8904871 -4.712493  0.0000000  1.692976
## prevexp  -4.545809 -6.6830784  7.407988  1.5771712  0.000000
## 
## $n
## [1] 474
## 
## $gp
## [1] 3
## 
## $method
## [1] "pearson"
\end{verbatim}

\hypertarget{goodness-of-fit-2}{%
\subsection{Goodness of Fit}\label{goodness-of-fit-2}}

\hypertarget{fitted-values-real-values}{%
\subsubsection{fitted values \& real
values}\label{fitted-values-real-values}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fitted\_y }\OtherTok{=} \FunctionTok{fitted}\NormalTok{(fit) }\CommentTok{\# predicted values}
\FunctionTok{plot}\NormalTok{(df}\SpecialCharTok{$}\NormalTok{salary,fitted\_y,}\StringTok{\textquotesingle{}p\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-108-1.pdf}

\hypertarget{residual}{%
\subsubsection{residual}\label{residual}}

If the model fits well, then the residual should follow a normal
distribution.

Fitted model's residuals can be obtained through
\texttt{residuals(model)} or \texttt{model\$residuals}. This works the
same for other attributes of a linear model, such as coefficients.

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# hist(fit$residuals)}
\FunctionTok{hist}\NormalTok{(}\FunctionTok{residuals}\NormalTok{(fit))}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-109-1.pdf}

\hypertarget{plot-more}{%
\subsubsection{plot more}\label{plot-more}}

Use \texttt{plot(model)} to get multiple plots reflecting the goodness
of fit from many perspectives.

Specify parameter \texttt{which\ =\ vector\_of\_range} to designate the
plots to show, 6 the most. \texttt{which\ =\ 1:4} by default.

Following the order, the six plots are:

\begin{itemize}
\tightlist
\item
  Residuals \& Fitted Values

  \begin{itemize}
  \tightlist
  \item
    test the homoscedascitiy assumption
  \end{itemize}
\item
  QQ Plot for Residuals

  \begin{itemize}
  \tightlist
  \item
    test the Gaussian assumption
  \end{itemize}
\item
  Square Root of Standardized Residuals \& Fitted Value

  \begin{itemize}
  \tightlist
  \item
    similar to the first one
  \end{itemize}
\item
  Cook's Distance

  \begin{itemize}
  \tightlist
  \item
    check outliers
  \end{itemize}
\item
  Standardized Residuals \& Leverage

  \begin{itemize}
  \tightlist
  \item
    See if influential points have huge residual, if so, possibly
    outlier!
  \end{itemize}
\item
  Cook's Distance \& Leverage

  \begin{itemize}
  \tightlist
  \item
    Large in both, possibly outlier!
  \end{itemize}
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{plot}\NormalTok{(fit, }\AttributeTok{which=}\DecValTok{1}\SpecialCharTok{:}\DecValTok{6}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-110-1.pdf}
\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-110-2.pdf}
\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-110-3.pdf}
\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-110-4.pdf}
\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-110-5.pdf}
\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-110-6.pdf}

There's another way to do the model checking. Use
\texttt{performance::check\_model(model)}.

The default option is to check all the ``vif'', ``qq'', ``normality'',
``linearity'', ``ncv'', ``homogeneity'', ``outliers'', ``reqq'',
``pp\_check'', ``binned\_residuals'' and ``overdispersion'', which can
be specified through \texttt{check\ =\ specifying\_vector}. Here I'll
take the check of outliers as an example.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{performance}\SpecialCharTok{::}\FunctionTok{check\_model}\NormalTok{(fit,}\AttributeTok{check =} \StringTok{\textquotesingle{}outliers\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-111-1.pdf}

Do not forget to \texttt{detach()} the attached data.

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

\hypertarget{appendix}{%
\section{Appendix}\label{appendix}}

\hypertarget{brucermanova}{%
\subsection{\texorpdfstring{\texttt{bruceR::MANOVA()}}{bruceR::MANOVA()}}\label{brucermanova}}

\texttt{bruceR::MANOVA()} is an old friend. What's new here is that, for
covariate(s), just specify the parameter \texttt{covariate}. If single,
a character is good; if multiple, compress charactors into a factor.

Mind the differences between \texttt{covariate} and \texttt{between}.
The latter should put interaction(s) into consideration.

For the ANCOVA example above,

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{ancfit }\OtherTok{=}\NormalTok{ bruceR}\SpecialCharTok{::}\FunctionTok{MANOVA}\NormalTok{(}\AttributeTok{data =}\NormalTok{ miner, }
                        \AttributeTok{dv =} \StringTok{\textquotesingle{}vitalcp\textquotesingle{}}\NormalTok{, }
                        \AttributeTok{between =} \StringTok{\textquotesingle{}time\textquotesingle{}}\NormalTok{,}
                        \AttributeTok{covariate =} \StringTok{\textquotesingle{}age\textquotesingle{}}\NormalTok{ )}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Warning: Numerical variables NOT centered on 0 (matters if variable in interaction):
##    age
\end{verbatim}

\begin{verbatim}
## 
## ====== ANOVA (Between-Subjects Design) ======
## 
## Descriptives:
## ────────────────────────
##  "time"  Mean    S.D.  n
## ────────────────────────
##   time1 3.949 (1.198) 12
##   time2 4.122 (0.768) 16
## ────────────────────────
## Total sample size: N = 28
## 
## ANOVA Table:
## Dependent variable(s):      vitalcp
## Between-subjects factor(s): time
## Within-subjects factor(s):  –
## Covariate(s):               age
## ────────────────────────────────────────────────────────────────────
##           MS   MSE df1 df2      F     p     η²p [90% CI of η²p]  η²G
## ────────────────────────────────────────────────────────────────────
## time   0.542 0.550   1  25  0.985  .330       .038 [.000, .218] .038
## age   10.881 0.550   1  25 19.775 <.001 ***   .442 [.196, .617] .442
## ────────────────────────────────────────────────────────────────────
## MSE = mean square error (the residual variance of the linear model)
## η²p = partial eta-squared = SS / (SS + SSE) = F * df1 / (F * df1 + df2)
## ω²p = partial omega-squared = (F - 1) * df1 / (F * df1 + df2 + 1)
## η²G = generalized eta-squared (see Olejnik & Algina, 2003)
## Cohen’s f² = η²p / (1 - η²p)
## 
## Levene’s Test for Homogeneity of Variance:
## ─────────────────────────────────────────
##              Levene’s F df1 df2     p    
## ─────────────────────────────────────────
## DV: vitalcp       2.701   1  26  .112    
## ─────────────────────────────────────────
\end{verbatim}

For the Latin Square design's example above,

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{latfit }\OtherTok{=}\NormalTok{ bruceR}\SpecialCharTok{::}\FunctionTok{MANOVA}\NormalTok{(}\AttributeTok{data =}\NormalTok{ yie, }
                        \AttributeTok{dv =} \StringTok{\textquotesingle{}yield\textquotesingle{}}\NormalTok{, }
                        \AttributeTok{covariate =} \FunctionTok{c}\NormalTok{(}\StringTok{\textquotesingle{}rep\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}col\textquotesingle{}}\NormalTok{),}
                        \AttributeTok{between =} \StringTok{\textquotesingle{}variety\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## ====== ANOVA (Between-Subjects Design) ======
## 
## Descriptives:
## ────────────────────────────
##  "variety"   Mean    S.D.  n
## ────────────────────────────
##   variety1 17.367 (1.026) 12
##   variety2 17.817 (1.206) 12
##   variety3 17.475 (1.189) 12
##   variety4 17.367 (1.251) 12
##   variety5 18.883 (1.093) 12
##   variety6 17.367 (0.953) 12
## ────────────────────────────
## Total sample size: N = 72
## 
## ANOVA Table:
## Dependent variable(s):      yield
## Between-subjects factor(s): variety
## Within-subjects factor(s):  –
## Covariate(s):               rep, col
## ─────────────────────────────────────────────────────────────────────
##             MS   MSE df1 df2     F     p     η²p [90% CI of η²p]  η²G
## ─────────────────────────────────────────────────────────────────────
## variety  4.313 1.380   5  56 3.124  .015 *     .218 [.029, .331] .218
## rep      0.892 1.380   5  56 0.646  .666       .055 [.000, .100] .055
## col      0.339 1.380   5  56 0.246  .940       .021 [.000, .008] .021
## ─────────────────────────────────────────────────────────────────────
## MSE = mean square error (the residual variance of the linear model)
## η²p = partial eta-squared = SS / (SS + SSE) = F * df1 / (F * df1 + df2)
## ω²p = partial omega-squared = (F - 1) * df1 / (F * df1 + df2 + 1)
## η²G = generalized eta-squared (see Olejnik & Algina, 2003)
## Cohen’s f² = η²p / (1 - η²p)
## 
## Levene’s Test for Homogeneity of Variance:
## ───────────────────────────────────────
##            Levene’s F df1 df2     p    
## ───────────────────────────────────────
## DV: yield       0.241   5  66  .943    
## ───────────────────────────────────────
\end{verbatim}

\hypertarget{set-dummies}{%
\subsection{Set Dummies}\label{set-dummies}}

\hypertarget{model.matrix}{%
\subsubsection{\texorpdfstring{\texttt{model.matrix()}}{model.matrix()}}\label{model.matrix}}

To set dummies in R, you can use the ``factor'' function to convert
categorical variables into a series of binary variables (i.e., dummies)
that can be used in a regression model.

Here's an example of how to set dummies in R:

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# load the mtcars dataset}
\FunctionTok{data}\NormalTok{(mtcars)}

\CommentTok{\# convert the gear variable to a factor variable}
\NormalTok{mtcars}\SpecialCharTok{$}\NormalTok{gear }\OtherTok{\textless{}{-}} \FunctionTok{factor}\NormalTok{(mtcars}\SpecialCharTok{$}\NormalTok{gear)}

\CommentTok{\# create dummies for the gear variable}
\NormalTok{dummies }\OtherTok{\textless{}{-}} \FunctionTok{model.matrix}\NormalTok{(}\SpecialCharTok{\textasciitilde{}}\NormalTok{gear }\SpecialCharTok{{-}} \DecValTok{1}\NormalTok{, }\AttributeTok{data =}\NormalTok{ mtcars)}

\CommentTok{\# view the dummies}
\FunctionTok{head}\NormalTok{(dummies)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##                   gear3 gear4 gear5
## Mazda RX4             0     1     0
## Mazda RX4 Wag         0     1     0
## Datsun 710            0     1     0
## Hornet 4 Drive        1     0     0
## Hornet Sportabout     1     0     0
## Valiant               1     0     0
\end{verbatim}

In this example, we first load the mtcars dataset and convert the
\texttt{gear} variable to a factor variable using the \texttt{factor()}
function. This converts the \texttt{gear} variable \textbf{from a
numeric variable to a categorical variable, which can then be converted
to dummies}.

Next, we use the \texttt{model.matrix()} function to create dummies for
the \texttt{gear} variable. The ``\textasciitilde{}\texttt{gear} - 1''
formula tells R to create dummies for the \texttt{gear} variable, but to
\textbf{exclude the intercept term} (i.e., the constant term).

Finally, we view the dummies using the \texttt{head()} function. The
output shows a matrix with three columns, corresponding to the three
possible values of the \texttt{gear} variable (3, 4, and 5), and a row
for each observation in the mtcars dataset. Each element in the matrix
is either 0 or 1, representing whether or not the corresponding
observation has a particular value of the \texttt{gear} variable.

Note that dummies can also be created for multiple categorical variables
simultaneously using the ``cbind'' function. For example, if you have
two categorical variables ``\texttt{gear}'' and ``cyl'', you can create
dummies for both variables using the following code:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{dummies }\OtherTok{\textless{}{-}} \FunctionTok{cbind}\NormalTok{(}\FunctionTok{model.matrix}\NormalTok{(}\SpecialCharTok{\textasciitilde{}}\NormalTok{gear }\SpecialCharTok{{-}} \DecValTok{1}\NormalTok{, }\AttributeTok{data =}\NormalTok{ mtcars), }
                 \FunctionTok{model.matrix}\NormalTok{(}\SpecialCharTok{\textasciitilde{}}\NormalTok{cyl }\SpecialCharTok{{-}} \DecValTok{1}\NormalTok{, }\AttributeTok{data =}\NormalTok{ mtcars))}

\FunctionTok{head}\NormalTok{(dummies)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##                   gear3 gear4 gear5 cyl
## Mazda RX4             0     1     0   6
## Mazda RX4 Wag         0     1     0   6
## Datsun 710            0     1     0   4
## Hornet 4 Drive        1     0     0   6
## Hornet Sportabout     1     0     0   8
## Valiant               1     0     0   6
\end{verbatim}

This will create a matrix with columns for each possible value of the
\texttt{gear} variable and each possible value of the \texttt{cyl}
variable, and a row for each observation in the mtcars dataset.

Note that for a categorical variable with \(n\) levels, you can only
plug in \(n-1\) of the dummies into the regression data, in case of
multicollinearity.

\hypertarget{fastdummiesdummy_cols}{%
\subsubsection{\texorpdfstring{\texttt{fastDummies::dummy\_cols()}}{fastDummies::dummy\_cols()}}\label{fastdummiesdummy_cols}}

Pipe-friendly functions used to create dummies for categorical variables
include

\begin{itemize}
\tightlist
\item
  \texttt{fastDummies::dummy\_cols()}
\item
  \texttt{caret::dummyVars()}
\end{itemize}

Here's an example of how to use the \texttt{dummy\_cols()} function to
create dummies for the \texttt{gear} and \texttt{cyl} variable in the
\texttt{mtcars} dataset:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{mtcars }\SpecialCharTok{\%\textgreater{}\%}
  \FunctionTok{mutate}\NormalTok{(}\AttributeTok{gear =} \FunctionTok{factor}\NormalTok{(gear),}
         \AttributeTok{cyl =} \FunctionTok{factor}\NormalTok{(cyl)) }\SpecialCharTok{\%\textgreater{}\%}
\NormalTok{  fastDummies}\SpecialCharTok{::}\FunctionTok{dummy\_cols}\NormalTok{(}
    \AttributeTok{select\_columns =} \FunctionTok{c}\NormalTok{(}\StringTok{"gear"}\NormalTok{,}\StringTok{\textquotesingle{}cyl\textquotesingle{}}\NormalTok{), }
    \AttributeTok{remove\_first\_dummy =} \ConstantTok{TRUE}\NormalTok{) }\SpecialCharTok{|\textgreater{}}
  \FunctionTok{head}\NormalTok{() }\SpecialCharTok{|\textgreater{}}
\NormalTok{  dplyr}\SpecialCharTok{::}\FunctionTok{select}\NormalTok{(}\FunctionTok{contains}\NormalTok{(}\StringTok{\textquotesingle{}gear\textquotesingle{}}\NormalTok{),}\FunctionTok{contains}\NormalTok{(}\StringTok{\textquotesingle{}cyl\textquotesingle{}}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##   gear gear_4 gear_5 cyl cyl_6 cyl_8
## 1    4      1      0   6     1     0
## 2    4      1      0   6     1     0
## 3    4      1      0   4     0     0
## 4    3      0      0   6     1     0
## 5    3      0      0   8     0     1
## 6    3      0      0   6     1     0
\end{verbatim}

In this example, we use the \texttt{mutate} function to convert the
\texttt{gear} and \texttt{cyl} variable to a factor variable (it works
fine with \texttt{dummy\_cols()} even skip this step, but wrong with
\texttt{dummyVars()}). We then use the \texttt{dummy\_cols()} function
to create dummies for the \texttt{gear} and \texttt{cyl} variable. The
``select\_columns'' parameter specifies the column(s) to create dummies
for (packed in a vector), and the \texttt{remove\_first\_dummy}
parameter specifies whether or not to remove the first dummy (i.e., the
constant term).

The output of the \texttt{dummy\_cols()} function is a new data frame
with the dummies added as new columns. This can be piped into other
functions for further analysis or modeling.

\hypertarget{caretdummyvars}{%
\subsubsection{\texorpdfstring{\texttt{caret::dummyVars()}}{caret::dummyVars()}}\label{caretdummyvars}}

Usage of this function will be clear after a vivid example.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(caret)}
\FunctionTok{dummyVars}\NormalTok{( }\SpecialCharTok{\textasciitilde{}}\NormalTok{ gear }\SpecialCharTok{+} \FunctionTok{factor}\NormalTok{(cyl), }\AttributeTok{data =}\NormalTok{ mtcars) }\SpecialCharTok{\%\textgreater{}\%} 
  \FunctionTok{predict}\NormalTok{(mtcars) }\SpecialCharTok{|\textgreater{}} \FunctionTok{head}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##                   gear.3 gear.4 gear.5 factor(cyl)4 factor(cyl)6 factor(cyl)8
## Mazda RX4              0      1      0            0            1            0
## Mazda RX4 Wag          0      1      0            0            1            0
## Datsun 710             0      1      0            1            0            0
## Hornet 4 Drive         1      0      0            0            1            0
## Hornet Sportabout      1      0      0            0            0            1
## Valiant                1      0      0            0            1            0
\end{verbatim}

In summary, the \texttt{predict()} function is used to apply the dummies
that were created by the \texttt{dummyVars()} function to a new dataset.
It is a way to use the same set of dummies that were created for one
dataset to create dummies for a different dataset that contains the same
categorical variable as the original dataset.

Note that the ``dummyVars()'' function can also be used to create
dummies for multiple categorical variables simultaneously using the
``+'' operator in the formula.

Note that categorical variable should be converted to factor type
before.

\hypertarget{prediction}{%
\subsection{Prediction}\label{prediction}}

\hypertarget{basic-usage}{%
\subsubsection{Basic Usage}\label{basic-usage}}

In R, the \texttt{predict()} function is used to generate predicted
values. It can be used with various types of models (such as linear
regression, logistic regression, decision trees, etc.) and can generate
corresponding predicted values based on input of new data.

Here are the basic steps to use the \texttt{predict()} function:

\begin{itemize}
\item
  First, pass the model object to the \texttt{predict()} function.

  If no more parameters are specified, then the \texttt{predict(model)}
  will return the fitted values of such model. You can image that as
  passing all IV(s) into the parameter \texttt{newdata} to make
  predictions. However, you can get the fitted values through
  \texttt{model\$fitted.values} and \texttt{fitted(fit)}. The folllowing
  three lines of command will give you the completely same result.
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{predict}\NormalTok{(fit)}
\NormalTok{fit}\SpecialCharTok{$}\NormalTok{fitted.values}
\FunctionTok{fitted}\NormalTok{(fit)}
\end{Highlighting}
\end{Shaded}

\begin{itemize}
\item
  Next, pass the new data wanted to predict to the \texttt{predict()}
  function.

  This new data should be a data frame or matrix, with the number of
  \textbf{columns} matching the number of predictor variables used in
  the model.

  In our example presented in ``Logistic Regression'' above, the model
  has four predictor variables. All the four predictors should be
  incorporated into the data frame. In case of problems of variables'
  order, the colnames should correspond with names used in the model.
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fit }\OtherTok{=} \FunctionTok{glm}\NormalTok{(ln\_yesno }\SpecialCharTok{\textasciitilde{}}\NormalTok{ age }\SpecialCharTok{+}\NormalTok{ pathsize }\SpecialCharTok{+}\NormalTok{ pathscat2 }\SpecialCharTok{+}\NormalTok{ pathscat3,}
          \AttributeTok{data =}\NormalTok{ logisticData,}
          \AttributeTok{family =} \FunctionTok{binomial}\NormalTok{(}\StringTok{\textquotesingle{}logit\textquotesingle{}}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

For instance, we're to predict a case with \texttt{pathscat\ ==\ 2},
\texttt{pathsize\ ==\ 2.0}, \texttt{age\ ==\ 66} and another case with
\texttt{pathscat\ ==\ 1}, \texttt{pathsize\ ==\ 0.5},
\texttt{age\ ==\ 20}. Accordingly, create the data frame containing the
conditions of the two cases.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{conditions }\OtherTok{=} \FunctionTok{data.frame}\NormalTok{(}
  \AttributeTok{pathscat2 =} \FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{0}\NormalTok{),}
  \AttributeTok{pathscat3 =} \FunctionTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{,}\DecValTok{0}\NormalTok{),}
  \AttributeTok{pathsize =} \FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{,.}\DecValTok{5}\NormalTok{),}
  \AttributeTok{age =} \FunctionTok{c}\NormalTok{(}\DecValTok{66}\NormalTok{,}\DecValTok{20}\NormalTok{)}
\NormalTok{)}

\FunctionTok{predict}\NormalTok{(fit,}\AttributeTok{newdata =}\NormalTok{ conditions,}\AttributeTok{type =} \StringTok{\textquotesingle{}response\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##         1         2 
## 0.1847871 0.2964413
\end{verbatim}

\texttt{predict(model,newdata)} will return a vector containing the
estimated predicted values. If you have multiple predictor variables,
the predicted values will be a matrix, with each row representing a new
predicted value.

\hypertarget{type}{%
\subsubsection{\texorpdfstring{\texttt{type}}{type}}\label{type}}

The \texttt{type} parameter in the \texttt{predict()} function in R is
used to specify the type of prediction that you want to generate. The
possible values for the \texttt{type} parameter depend on the type of
model that you are using. The default value of \texttt{type} is
\texttt{response}, which is typically the most appropriate option for
most models.

For example, if you are using a linear regression model, the
\texttt{type} parameter can be set to \texttt{response} or
\texttt{terms}. If you set \texttt{type} to \texttt{response}, the
prediction() function will return the predicted values of the dependent
variable. If you set \texttt{type} to ``terms'', the function will
return the predicted values of the individual terms in the model (i.e.,
the predicted values for each predictor variable).

Similarly, if you are using a logistic regression model, the
\texttt{type} parameter can be set to \texttt{response}, \texttt{terms},
or \texttt{link}. If you set \texttt{type} to \texttt{response}, the
\texttt{predict()} function will return the predicted
\textbf{probabilities} of the binary outcome variable. If you set
\texttt{type} to \texttt{terms}, the function will return the predicted
values of the individual terms in the model. If you set \texttt{type} to
\texttt{link}, the function will return the predicted values on the
\textbf{log-odds} scale.

In generalized linear models (GLMs), such as logistic regression or
Poisson regression, the \texttt{link} scale refers to the scale used in
modeling the relationship between the predictor variables and the
response variable. The link function maps the predicted values to the
scale of the response variable. For example, in logistic regression, the
link function is typically the logit function, which maps the
probability of a binary outcome to the log odds of that outcome.

When you set \texttt{type} to \texttt{link} in the prediction()
function, it returns the predicted values on the link scale, which may
be different from the scale of the response variable.

On the other hand, when you set \texttt{type} to \texttt{response} in
the prediction() function, it returns the predicted values on the scale
of the response variable. For example, in logistic regression, the
predicted values would be the probabilities of the binary outcome.

The choice between \texttt{link} and \texttt{response} depends on the
purpose of the analysis. If you want to compare the predicted values for
different values of the predictor variables on the scale of the response
variable, then \texttt{response} is the appropriate option. If you want
to perform further analysis or transformation on the predicted values,
such as calculating odds ratios or performing hypothesis tests, then
\texttt{link} is the appropriate option.

Back to our example, we can see it more clearly.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{predict}\NormalTok{(fit,}\AttributeTok{newdata =}\NormalTok{ conditions,}\AttributeTok{type =} \StringTok{\textquotesingle{}response\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##         1         2 
## 0.1847871 0.2964413
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{predict}\NormalTok{(fit,}\AttributeTok{newdata =}\NormalTok{ conditions,}\AttributeTok{type =} \StringTok{\textquotesingle{}link\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##          1          2 
## -1.4842449 -0.8643023
\end{verbatim}

\hypertarget{classification-table-1}{%
\subsection{Classification Table}\label{classification-table-1}}

\texttt{confusionMatrix()} is a function from the \texttt{caret} package
in R that creates a confusion matrix and computes various performance
metrics for classification models. The function takes at least two
arguments: \texttt{data} and \texttt{reference}.The \texttt{data}
argument is a vector of predicted class labels, while the
\texttt{reference} argument is a vector of actual class labels. The two
vectors must have the same length.

If necessary, also specify \texttt{positive} and \texttt{dnn}.
\texttt{positive} will tell the factor level that corresponds to a
``positive'' result. If there are only two factor levels, the first
level will be used as the ``positive'' result. \texttt{dnn} reads a
character vector of dimnames for the table.

When you call \texttt{confusionMatrix()} with these arguments, it
produces a confusion matrix that shows the number of true positives,
true negatives, false positives, and false negatives for the
\textbf{classification model}.

Based on this confusion matrix, the function then calculates several
performance metrics, including accuracy, precision, sensitivity (also
known as recall), specificity, F1 score, and Kappa statistic. In
addition to these metrics, the confusionMatrix() function can also
compute various other statistics, such as positive predictive value
(PPV), negative predictive value (NPV), and the area under the ROC curve
(AUC).

More specifically,

\begin{itemize}
\item
  Accuracy: The proportion of correct predictions out of the total
  number of predictions.
\item
  Precision: The proportion of true positives (i.e., correct positive
  predictions) out of the total number of positive predictions.
\item
  Sensitivity/Recall: The proportion of true positives out of the total
  number of actual positives (i.e., the proportion of positive cases
  that were correctly identified).
\item
  Specificity: The proportion of true negatives (i.e., correct negative
  predictions) out of the total number of actual negatives (i.e., the
  proportion of negative cases that were correctly identified).
\item
  F1 score: The harmonic mean of precision and recall, which provides a
  balanced measure of both metrics. It ranges from 0 to 1, with higher
  values indicating better performance.
\item
  Kappa statistic: A measure of agreement between the predicted and
  actual class labels that accounts for the proportion of agreement that
  is expected by chance alone. It ranges from -1 to 1, with higher
  values indicating better agreement.
\item
  Positive predictive value (PPV): The proportion of true positives out
  of the total number of positive predictions.
\item
  Negative predictive value (NPV): The proportion of true negatives out
  of the total number of negative predictions.
\item
  Area under the ROC curve (AUC): A measure of the model's ability to
  distinguish between positive and negative cases across all possible
  thresholds. It ranges from 0.5 (indicating no discrimination) to 1
  (indicating perfect discrimination).
\end{itemize}

Use the example provided above in ``\emph{Logistic Regression}'' to show
the function!

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fit }\OtherTok{=} \FunctionTok{glm}\NormalTok{(ln\_yesno }\SpecialCharTok{\textasciitilde{}}\NormalTok{ age }\SpecialCharTok{+}\NormalTok{ pathsize }\SpecialCharTok{+}\NormalTok{ pathscat2 }\SpecialCharTok{+}\NormalTok{ pathscat3,}
          \AttributeTok{data =}\NormalTok{ logisticData,}
          \AttributeTok{family =} \FunctionTok{binomial}\NormalTok{(}\StringTok{\textquotesingle{}logit\textquotesingle{}}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{caret}\SpecialCharTok{::}\FunctionTok{confusionMatrix}\NormalTok{(}
  \AttributeTok{data =} \FunctionTok{factor}\NormalTok{(}\FunctionTok{round}\NormalTok{(}\FunctionTok{fitted}\NormalTok{(fit),}\DecValTok{0}\NormalTok{)),}
  \AttributeTok{reference  =} \FunctionTok{factor}\NormalTok{(logisticData}\SpecialCharTok{$}\NormalTok{ln\_yesno),}
  \AttributeTok{positive =} \StringTok{\textquotesingle{}1\textquotesingle{}}\NormalTok{,}
  \AttributeTok{dnn =} \FunctionTok{c}\NormalTok{(}\StringTok{\textquotesingle{}predicted\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}actual\textquotesingle{}}\NormalTok{)}
\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Confusion Matrix and Statistics
## 
##          actual
## predicted   0   1
##         0 846 246
##         1  14  15
##                                           
##                Accuracy : 0.7681          
##                  95% CI : (0.7422, 0.7925)
##     No Information Rate : 0.7672          
##     P-Value [Acc > NIR] : 0.4884          
##                                           
##                   Kappa : 0.0597          
##                                           
##  Mcnemar's Test P-Value : <2e-16          
##                                           
##             Sensitivity : 0.05747         
##             Specificity : 0.98372         
##          Pos Pred Value : 0.51724         
##          Neg Pred Value : 0.77473         
##              Prevalence : 0.23283         
##          Detection Rate : 0.01338         
##    Detection Prevalence : 0.02587         
##       Balanced Accuracy : 0.52060         
##                                           
##        'Positive' Class : 1               
## 
\end{verbatim}

\hypertarget{attach-and-detach}{%
\subsection{Attach and Detach}\label{attach-and-detach}}

We can use the function \texttt{attach()} to use variables in the
database conveniently. After \texttt{attach()}, column variables can be
used directly without \texttt{\$}. (R will do the searching work behind
for you!)

However, sometimes names of the variables will conflict with each other
and cause problems. Not recommended from my perspective, because you'll
not know what database the variable is from, especially when you're
trying to check the codes. Combined with tube-friendly and
formula-pattern genre, this won't be any more trouble.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{attach}\NormalTok{(df)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## The following objects are masked _by_ .GlobalEnv:
## 
##     age, jobcat
\end{verbatim}

\begin{verbatim}
## The following objects are masked from df (pos = 3):
## 
##     age, bdate, educ, gender, id, jobcat, jobtime, minority, prevexp,
##     salary, salbegin
\end{verbatim}

After attaching a database, remember to \texttt{detach()} that finally,
in case of conflicts with other variables.

\hypertarget{other-examples}{%
\subsection{Other Examples}\label{other-examples}}

\hypertarget{logistic-regression-1}{%
\subsubsection{Logistic Regression}\label{logistic-regression-1}}

A teacher wants to investigate the factors that affect the performance
of a certain course. Among them, he wants to test whether the time spent
by students on course learning per week (time, in hours), whether they
have taken a prerequisite course (pre, 0-not taken, 1-taken), and
whether they attend the course regularly (attendance, 0-not attend,
1-attend) have an impact on whether the final grade is excellent (grade,
0-not excellent, 1-excellent). The data is recorded in ``grade.sav''.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{pupils }\OtherTok{=}\NormalTok{ haven}\SpecialCharTok{::}\FunctionTok{read\_sav}\NormalTok{(}\StringTok{\textquotesingle{}grade.sav\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\hypertarget{regression-model}{%
\paragraph{Regression Model}\label{regression-model}}

Take \texttt{grade} as the response variable, with \texttt{pre},
\texttt{attendance} and \texttt{time} as independent variables to carry
logistic regression.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{lfit }\OtherTok{=} \FunctionTok{glm}\NormalTok{(grade}\SpecialCharTok{\textasciitilde{}}\NormalTok{pre}\SpecialCharTok{+}\NormalTok{attendance}\SpecialCharTok{+}\NormalTok{time,}\AttributeTok{data =}\NormalTok{ pupils,}
           \AttributeTok{family =} \FunctionTok{binomial}\NormalTok{(}\StringTok{\textquotesingle{}logit\textquotesingle{}}\NormalTok{))}

\FunctionTok{summary}\NormalTok{(lfit)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## glm(formula = grade ~ pre + attendance + time, family = binomial("logit"), 
##     data = pupils)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.8026  -0.5564   0.2268   0.6624   2.0829  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  -4.9147     2.2019  -2.232 0.025615 *  
## pre           2.2420     0.8585   2.611 0.009017 ** 
## attendance    3.1925     0.9109   3.505 0.000457 ***
## time          0.2606     0.1765   1.476 0.139871    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 81.516  on 62  degrees of freedom
## Residual deviance: 53.424  on 59  degrees of freedom
## AIC: 61.424
## 
## Number of Fisher Scoring iterations: 5
\end{verbatim}

The logistic model, together with coefficient and significance for each
IV is included from the R code output above. Nevertheless, I'd like to
report that formally below.

The logistic model is as follows.

\[
\text{Logit}(p)=-4.9147+2.2420\times pre+3.1925\times attendance+0.2606\times time
\]

The coefficients of \texttt{pre} and \texttt{attendance} are
significant, while that of \texttt{time} is not significant. Related
information is presented on the table below.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{bruceR}\SpecialCharTok{::}\FunctionTok{print\_table}\NormalTok{(lfit)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## ──────────────────────────────────────────────
##              Estimate    S.E.      z     p    
## ──────────────────────────────────────────────
## (Intercept)    -4.915 (2.202) -2.232  .026 *  
## pre             2.242 (0.859)  2.611  .009 ** 
## attendance      3.193 (0.911)  3.505 <.001 ***
## time            0.261 (0.177)  1.476  .140    
## ──────────────────────────────────────────────
\end{verbatim}

The significant variables have influence on the odds statistically, with
positive ones indicating positively-correlated relationship. On the
other hand, the one that isn't significant doesn't prove to be
statistically influential on the odds.

In the case given, it's estimated that one unit increment in
\texttt{pre} will induce 0.299 unit increment in \(\text{Logit}(p)\) and
the influence is significant (\(z=2.611,p<.01\)), and one unit increment
in \texttt{pre} will induce 0.299 unit increment in \(\text{Logit}(p)\)
and its influence is highly significant (\(z=3.505,p<.001)\).
\texttt{time} is estimated to entail 0.033 unit increment in
\(\text{Logit}(p)\) for every unit increment, but that's not significant
(\(z = 3.505,p=0.140\)).

\hypertarget{odds-ratio}{%
\paragraph{Odds Ratio}\label{odds-ratio}}

Odds Ratio is the effect size of each independent variable, which is
\(e^{\beta_i}\), where \(\beta_i\) is the coefficient of IV \(x_i\).

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{exp}\NormalTok{(}\FunctionTok{cbind}\NormalTok{(}\AttributeTok{OddsRatio =} \FunctionTok{coef}\NormalTok{(lfit),}\FunctionTok{confint}\NormalTok{(lfit)))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##                OddsRatio        2.5 %      97.5 %
## (Intercept)  0.007337845 5.941785e-05   0.4256995
## pre          9.412000659 2.074149e+00  69.1689029
## attendance  24.350432282 5.040915e+00 200.7150914
## time         1.297725148 9.338253e-01   1.9055477
\end{verbatim}

Odds ratio indicates one unit increase in an IV will cause the odds to
change to how much times of itself. Odds ratio that is greater than 1
means the increase in the IV will cause the odds (or the probability) to
increase.

In this example, the odds ratios for \texttt{pre}, \texttt{attendance}
and \texttt{time} are 9.412000659, 24.350432282 and 1.297725148
respectively. All of them tend to have positive correlation with DV.

Moreover, if we're to check the 95\% CI to make a statistical inference,
\(1\) is not included with \texttt{pre} and \texttt{attendence}, and the
fact corresponds with their significance as is mentioned above. However,
\(1\) falls within 95\% CI of \texttt{time}, and that reminds me of the
non-significant result.

\hypertarget{against-null-model}{%
\paragraph{Against NULL Model}\label{against-null-model}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{with}\NormalTok{(lfit, null.deviance }\SpecialCharTok{{-}}\NormalTok{ deviance)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] 28.09241
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{with}\NormalTok{(lfit, df.null }\SpecialCharTok{{-}}\NormalTok{ df.residual)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] 3
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{with}\NormalTok{(lfit, }\FunctionTok{pchisq}\NormalTok{(null.deviance }\SpecialCharTok{{-}}\NormalTok{ deviance, }
\NormalTok{                 df.null }\SpecialCharTok{{-}}\NormalTok{ df.residual, }
                 \AttributeTok{lower.tail =} \ConstantTok{FALSE}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] 3.473385e-06
\end{verbatim}

Model comparison between the regressed model and the null one showed a
significant result, \(\chi_3^2=28.09241,p<.001\), indicating that the
model performs much better in prediction.

\hypertarget{r-squared}{%
\paragraph{R Squared}\label{r-squared}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{pscl}\SpecialCharTok{::}\FunctionTok{pR2}\NormalTok{(lfit)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## fitting null model for pseudo-r2
\end{verbatim}

\begin{verbatim}
##         llh     llhNull          G2    McFadden        r2ML        r2CU 
## -26.7118943 -40.7580991  28.0924094   0.3446236   0.3597594   0.4956710
\end{verbatim}

The Nagelkerke \(r^2\) is 0.4956710, indicating that the fitted model
can account for 48.70\% of variation of the data, which is great!

\hypertarget{goodness-of-fit-3}{%
\paragraph{Goodness of Fit}\label{goodness-of-fit-3}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{ResourceSelection}\SpecialCharTok{::}\FunctionTok{hoslem.test}\NormalTok{(}\AttributeTok{x =}\NormalTok{ pupils}\SpecialCharTok{$}\NormalTok{grade, }
                               \AttributeTok{y =} \FunctionTok{fitted}\NormalTok{(lfit), }
                               \AttributeTok{g =} \DecValTok{5}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Hosmer and Lemeshow goodness of fit (GOF) test
## 
## data:  pupils$grade, fitted(lfit)
## X-squared = 1.6552, df = 3, p-value = 0.6469
\end{verbatim}

The Hosmer and Lemeshow goodness of fit (GOF) test reports that a
non-significant result, enumerating all possible numbers of bins to
carry such test (from 3 to 9). The number of bins presented here is the
one that returns the lowest \(p\) value, but to my happiness it's far
away from significant, which is 0.6469 and with \(\chi_3^2=1.6552\).

\emph{Note that the bins set should not be so dense that the sample size
within each bin will fall short.}

The non-significant result here indicates that there's no
statistically-significant differences between the fitted values and the
observed values, and the model fits well with the real world!

\hypertarget{prediction-1}{%
\paragraph{Prediction}\label{prediction-1}}

Use the logistic model to predict the outcomes of the two cases.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{stu }\OtherTok{=} \FunctionTok{data.frame}\NormalTok{(}
  \AttributeTok{pre =}\NormalTok{ (}\FunctionTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{,}\DecValTok{1}\NormalTok{)),}
  \AttributeTok{attendance =} \FunctionTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{,}\DecValTok{1}\NormalTok{),}
  \AttributeTok{time =} \FunctionTok{c}\NormalTok{(}\DecValTok{8}\NormalTok{,}\DecValTok{16}\NormalTok{)}
\NormalTok{)}

\NormalTok{predictions }\OtherTok{=} \FunctionTok{predict}\NormalTok{(lfit,}
                      \AttributeTok{newdata =}\NormalTok{ stu,}
                      \AttributeTok{interval =} \StringTok{\textquotesingle{}confidence\textquotesingle{}}\NormalTok{, }
                      \AttributeTok{type =} \StringTok{\textquotesingle{}response\textquotesingle{}}\NormalTok{)}
\NormalTok{predictions}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##          1          2 
## 0.05573454 0.99089361
\end{verbatim}

\emph{Note that the \texttt{predict()} for logistic regression will
return probability \(p\), instead of \(\text{Logit}(p)\) (DV of the
model)!}

Therefore, as for getting excellence on this course, student a has the
probability of 0.05573454, while student b has the probability of
0.99089361.

\hypertarget{summarize-the-result}{%
\paragraph{Summarize the Result}\label{summarize-the-result}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{pred\_y }\OtherTok{=} \FunctionTok{round}\NormalTok{(}\FunctionTok{predict}\NormalTok{(lfit,}\AttributeTok{type=}\StringTok{\textquotesingle{}response\textquotesingle{}}\NormalTok{),}\DecValTok{0}\NormalTok{)}

\NormalTok{classification\_df }\OtherTok{=} \FunctionTok{data.frame}\NormalTok{(}
  \AttributeTok{observed\_y =}\NormalTok{ pupils}\SpecialCharTok{$}\NormalTok{grade,}
  \AttributeTok{predicted\_y =} \FunctionTok{round}\NormalTok{(pred\_y,}\DecValTok{0}\NormalTok{))}

\FunctionTok{xtabs}\NormalTok{(}\SpecialCharTok{\textasciitilde{}}\NormalTok{predicted\_y}\SpecialCharTok{+}\NormalTok{observed\_y,}\AttributeTok{data=}\NormalTok{classification\_df)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##            observed_y
## predicted_y  0  1
##           0 12  4
##           1 10 37
\end{verbatim}

From the classification, we can clearly see that

\[
\begin{aligned}
Accuracy&=\frac{TP+TN}{TP+TN+FP+FN}=\frac{37+12}{37+12+4+10}=\frac{7}{9}\approx77.8\%\\
Precision&=\frac{TP}{TP+FP}=\frac{37}{37+10}=\frac{37}{47}\approx 78.7\%\\
Sensitivity&=\frac{TP}{TP+FN}=\frac{37}{37+12}=\frac{37}{49}\approx 75.5\%\\
Specificity&=\frac{TN}{TN+FP}=\frac{12}{12+4}=\frac{3}{4}=75.0\%
\end{aligned}
\]

\hypertarget{report-the-model}{%
\paragraph{Report the Model}\label{report-the-model}}

A logistic regression analysis was carried to predict the excellence
using prerequisite's requirement, time spent each week and attendance as
predictors. The logistic model is as follows.

\[
\text{Logit}(p)=-4.9147+2.2420\times pre+3.1925\times attendance+0.2606\times time
\]

where predictor \texttt{pre} and \texttt{attendance} are significantly
influential (\(z_{pre}=2.611,p<.01;z_{attendance}=3.505,p<.001\)), while
predictor \texttt{time} wasn't (\(z_{time} = 3.505,p=0.140\)). Odds
ratio, which is effect size, can predict that when \texttt{pre} is from
0 to 1, the odds is 9.412 as large, and when \texttt{attendance} is from
0 to 1, the odds becomes 24.350 as large, both significant. The finding
firmly tell that to cover the prerequisites and to attend the class are
extraordinarily important when it comes to the excellence of the score!
However, the odds ratio for \texttt{time} is 1.298 and not significant,
meaning its influence is not statistically proved. Jointly,
prerequisites and attendance is far more important than time invested,
if the first two cannot be satisfied, expanding your time budget on this
course may result in vain.

As for the model, Nagelkerke's \(R^2\) of \(0.4956710\) indicated a
great relationship between predictors and the outcome. Hosmer and
Lemeshow goodness of fit (GOF) test was conducted, showing a great
goodness of fit, with \(p\) no less than 0.6469 and \(\chi_3^2=1.6552\).
Moreover, the regressed model was compared against the intercept-only
null model, returning a highly significant result, implying that the
model's better performance in prediction. Overall, the model has an
accuracy of 77.8\%, a precision of 78.7\%, a sensitivity of 75.5\%, and
a specificity of 75.0\%.

\hypertarget{mlr}{%
\subsubsection{MLR}\label{mlr}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{heart }\OtherTok{=} \FunctionTok{read.csv}\NormalTok{(}\StringTok{\textquotesingle{}heart.csv\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\hypertarget{fit-the-model-4}{%
\paragraph{Fit the Model}\label{fit-the-model-4}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{mfit }\OtherTok{=} \FunctionTok{lm}\NormalTok{(}\FunctionTok{scale}\NormalTok{(heart.disease)}\SpecialCharTok{\textasciitilde{}}\FunctionTok{scale}\NormalTok{(biking)}\SpecialCharTok{+}\FunctionTok{scale}\NormalTok{(smoking),}
          \AttributeTok{data =}\NormalTok{ heart)}
\end{Highlighting}
\end{Shaded}

\hypertarget{summary-the-model-2}{%
\paragraph{Summary the Model}\label{summary-the-model-2}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{summary}\NormalTok{(mfit)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = scale(heart.disease) ~ scale(biking) + scale(smoking), 
##     data = heart)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.47658 -0.09762  0.00792  0.09671  0.42283 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    -6.119e-17  6.410e-03    0.00        1    
## scale(biking)  -9.403e-01  6.418e-03 -146.53   <2e-16 ***
## scale(smoking)  3.234e-01  6.418e-03   50.39   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1431 on 495 degrees of freedom
## Multiple R-squared:  0.9796, Adjusted R-squared:  0.9795 
## F-statistic: 1.19e+04 on 2 and 495 DF,  p-value: < 2.2e-16
\end{verbatim}

It's predicted that if \texttt{biking} changes for one standard
deviation, then \texttt{heart.disease} is to change for -0.940 standard
deviation, and that if \texttt{smoking} changes for one standard
deviation, then \texttt{heart.disease} is to change for 0.323 standard
deviation. Meanwhile, coefficients of both \texttt{biking} and
\texttt{smoking} are highly significant with \(p<.001\).

\hypertarget{goodness-of-fit-4}{%
\paragraph{Goodness of Fit}\label{goodness-of-fit-4}}

After qualitative analysis, now move on to quantitative works.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{result }\OtherTok{=} \FunctionTok{summary}\NormalTok{(mfit)}
\NormalTok{result}\SpecialCharTok{$}\NormalTok{adj.r.squared}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] 0.9795351
\end{verbatim}

\(Adjusted\ R^2\) of 0.9795 indicates a highly awesome goodness of fit
and its ability to explain the variation of the data!

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{cor}\NormalTok{(}\AttributeTok{x =} \FunctionTok{fitted}\NormalTok{(mfit), }\AttributeTok{y =}\NormalTok{ heart}\SpecialCharTok{$}\NormalTok{heart.disease)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] 0.9897563
\end{verbatim}

The correlation between the fitted values and the observed (real) values
is as high as 0.9897563, which is exactly the square root of \(R^2\).
Jointly speaking, the model has great credibility in prediction.

\hypertarget{vif}{%
\paragraph{VIF}\label{vif}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{car}\SpecialCharTok{::}\FunctionTok{vif}\NormalTok{(mfit)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##  scale(biking) scale(smoking) 
##       1.000229       1.000229
\end{verbatim}

VIFs of \texttt{biking} and \texttt{smoking} are 1.000229 and 1.000229
respectively, far less than 5, showing that there should be no worries
about multicollinearity.

\hypertarget{plots-of-residuals}{%
\paragraph{Plots of Residuals}\label{plots-of-residuals}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow =} \FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{2}\NormalTok{))}
\FunctionTok{plot}\NormalTok{(mfit,}\AttributeTok{which =} \FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{3}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-141-1.pdf}

From the first plot, it's shown that there's no obvious tendency between
the fitted values and the residuals.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow =} \FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{2}\NormalTok{))}
\FunctionTok{plot}\NormalTok{(mfit,}\AttributeTok{which =} \DecValTok{2}\NormalTok{)}
\FunctionTok{hist}\NormalTok{(mfit}\SpecialCharTok{$}\NormalTok{residuals)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-142-1.pdf}

In QQ plot, the scatter points of residuals closely center around the
line offered by normal distribution. In the histogram of residuals, a
clear prototype of normal distribution is depicted. What's more, move on
from qualitative analysis to the quantitative.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{shapiro.test}\NormalTok{(mfit}\SpecialCharTok{$}\NormalTok{residuals)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Shapiro-Wilk normality test
## 
## data:  mfit$residuals
## W = 0.997, p-value = 0.4935
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{mfit}\SpecialCharTok{$}\NormalTok{residuals }\SpecialCharTok{|\textgreater{}} \FunctionTok{mean}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] 2.305236e-18
\end{verbatim}

From the Shapiro test, \(W = 0.9969963,p=0.4935143\), indicating that
the residuals from the fitted model follow a normal distribution.
Moreover, the mean is computed to be (extremely close to) zero.

Therefore, we can safely say that the residuals of the fitted model do
follow a normal distribution.

Based on what we've discussed, we can conclude confidently that the
model fits the data well!

\hypertarget{select}{%
\subsection{\texorpdfstring{\texttt{select()}}{select()}}\label{select}}

\texttt{select()} is one of the most useful functions in ``tidyverse''
environment. However, \texttt{select()} is incorporated in two packages,
``dplyr'' (or ``tidyverse'') and ``MASS''. When the two are both loaded,
then there is going be a clash!

To solve this annoying problem, decorate every \texttt{select()} as
\texttt{dplyr::select}, or change the settings of ``library''
essentially.

\newpage

\hypertarget{ux7b2cux4e8cux90e8ux5206ux671fux672bux8303ux56f4813-ux7ae0}{%
\section{第二部分：期末范围（8--13
章）}\label{ux7b2cux4e8cux90e8ux5206ux671fux672bux8303ux56f4813-ux7ae0}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(bruceR)}
\FunctionTok{library}\NormalTok{(tidyverse)}
\FunctionTok{library}\NormalTok{(ggplot2)}
\end{Highlighting}
\end{Shaded}

\hypertarget{mlr-1}{%
\section{MLR}\label{mlr-1}}

The \texttt{job} contains information about salary, beginning salary,
education year, job category, previous working experience and so on. The
relationship between salary and beginning salary, education, to name a
few, is of interest.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{job }\OtherTok{=} \FunctionTok{import}\NormalTok{(}\StringTok{"data02{-}01.xlsx"}\NormalTok{, }\AttributeTok{sheet =} \DecValTok{1}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## ✔ Successfully imported: 474 obs. of 11 variables
\end{verbatim}

\hypertarget{pre-inspection-2}{%
\subsection{Pre-Inspection}\label{pre-inspection-2}}

\hypertarget{correlation}{%
\subsubsection{Correlation}\label{correlation}}

\begin{itemize}
\tightlist
\item
  \texttt{bruceR::Corr()}: correlation matrix, the default method of
  which is
  \texttt{method\ =\ \textquotesingle{}pearson\textquotesingle{}}.
\item
  \texttt{ppcor::spcor()}: semi-partial correlation for IVs.
\item
  \texttt{ppcor::pcor()}: partial correlation.
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{job }\SpecialCharTok{|\textgreater{}}\NormalTok{ dplyr}\SpecialCharTok{::}\FunctionTok{select}\NormalTok{(salary,educ,salbegin,jobtime,prevexp) }\SpecialCharTok{|\textgreater{}} \FunctionTok{Corr}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-146-1.pdf}

\begin{verbatim}
## Correlation matrix is displayed in the RStudio `Plots` Pane.
## 
## Pearson's r and 95% confidence intervals:
## ────────────────────────────────────────────────────
##                       r       [95% CI]     p       N
## ────────────────────────────────────────────────────
## salary-educ        0.66 [ 0.61,  0.71] <.001 *** 474
## salary-salbegin    0.88 [ 0.86,  0.90] <.001 *** 474
## salary-jobtime     0.08 [-0.01,  0.17]  .067 .   474
## salary-prevexp    -0.10 [-0.19, -0.01]  .034 *   474
## educ-salbegin      0.63 [ 0.58,  0.68] <.001 *** 474
## educ-jobtime       0.05 [-0.04,  0.14]  .303     474
## educ-prevexp      -0.25 [-0.33, -0.17] <.001 *** 474
## salbegin-jobtime  -0.02 [-0.11,  0.07]  .668     474
## salbegin-prevexp   0.05 [-0.05,  0.13]  .327     474
## jobtime-prevexp    0.00 [-0.09,  0.09]  .948     474
## ────────────────────────────────────────────────────
\end{verbatim}

More will be introduced in \emph{Correlation} chapter.

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# partial correlation}
\NormalTok{job }\SpecialCharTok{|\textgreater{}}\NormalTok{ dplyr}\SpecialCharTok{::}\FunctionTok{select}\NormalTok{(salary,educ,salbegin,jobtime,prevexp) }\SpecialCharTok{|\textgreater{}} 
\NormalTok{  ppcor}\SpecialCharTok{::}\FunctionTok{pcor}\NormalTok{()}

\CommentTok{\# part or semi{-}partial correlation}
\NormalTok{job }\SpecialCharTok{|\textgreater{}}\NormalTok{ dplyr}\SpecialCharTok{::}\FunctionTok{select}\NormalTok{(salary,educ,salbegin,jobtime,prevexp) }\SpecialCharTok{|\textgreater{}} 
\NormalTok{  ppcor}\SpecialCharTok{::}\FunctionTok{spcor}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\hypertarget{multicollinearity-1}{%
\subsubsection{Multicollinearity}\label{multicollinearity-1}}

Use \texttt{car::vif(model)} and get the result directly.

VIF less than 2 is ideal, and less than 5 is the bottom line.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fit }\OtherTok{=} \FunctionTok{lm}\NormalTok{(salary }\SpecialCharTok{\textasciitilde{}}\NormalTok{ educ }\SpecialCharTok{+}\NormalTok{ salbegin }\SpecialCharTok{+}\NormalTok{ jobtime }\SpecialCharTok{+}\NormalTok{ prevexp,}\AttributeTok{data =}\NormalTok{ job)}
\NormalTok{car}\SpecialCharTok{::}\FunctionTok{vif}\NormalTok{(fit)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
    educ salbegin  jobtime  prevexp 
1.937010 1.813582 1.007613 1.155814 
\end{verbatim}

\hypertarget{fit-the-model-5}{%
\subsection{Fit the Model}\label{fit-the-model-5}}

Firstly, use \texttt{lm()} to get the linear model. Subsequently, use
\texttt{summary()} to get the detailed result. Period.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{jobfit }\OtherTok{=} \FunctionTok{lm}\NormalTok{(salary }\SpecialCharTok{\textasciitilde{}}\NormalTok{ educ }\SpecialCharTok{+}\NormalTok{ salbegin }\SpecialCharTok{+}\NormalTok{ jobtime }\SpecialCharTok{+}\NormalTok{ prevexp,}\AttributeTok{data =}\NormalTok{ job)}
\FunctionTok{summary}\NormalTok{(jobfit)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}

Call:
lm(formula = salary ~ educ + salbegin + jobtime + prevexp, data = job)

Residuals:
   Min     1Q Median     3Q    Max 
-29600  -4119  -1246   2642  46079 

Coefficients:
              Estimate Std. Error t value Pr(>|t|)    
(Intercept) -1.615e+04  3.255e+03  -4.961 9.84e-07 ***
educ         6.699e+02  1.656e+02   4.045 6.11e-05 ***
salbegin     1.768e+00  5.873e-02  30.111  < 2e-16 ***
jobtime      1.615e+02  3.425e+01   4.715 3.19e-06 ***
prevexp     -1.730e+01  3.528e+00  -4.904 1.30e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 7465 on 469 degrees of freedom
Multiple R-squared:  0.8105,    Adjusted R-squared:  0.8089 
F-statistic: 501.5 on 4 and 469 DF,  p-value: < 2.2e-16
\end{verbatim}

\hypertarget{coefficients-ci}{%
\subsubsection{Coefficients \& CI}\label{coefficients-ci}}

Use \texttt{coefficients(model)} or \texttt{model\$coefficients} to see
the coefficients for each IV.

Furthermore, use \texttt{confint(model,level\ =\ 0.95)} to see the CIs
(95\% by default) for each coefficient.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{jobfit}\SpecialCharTok{$}\NormalTok{coefficients}
\end{Highlighting}
\end{Shaded}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{coefficients}\NormalTok{(jobfit)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##   (Intercept)          educ      salbegin       jobtime       prevexp 
## -16149.671204    669.913570      1.768427    161.485642    -17.303251
\end{verbatim}

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

\begin{verbatim}
##                     2.5 %       97.5 %
## (Intercept) -22546.784635 -9752.557773
## educ           344.511104   995.316035
## salbegin         1.653019     1.883835
## jobtime         94.190179   228.781104
## prevexp        -24.236663   -10.369839
\end{verbatim}

\hypertarget{r-squared-1}{%
\subsubsection{R Squared}\label{r-squared-1}}

Multiple R squared is reported through \texttt{summary()}.

By definition, Multiple R squared is defined as the square of the
correlation between fitted value and real values.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{cor}\NormalTok{(job}\SpecialCharTok{$}\NormalTok{salary,jobfit}\SpecialCharTok{$}\NormalTok{fitted.values)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
[1] 0.9002721
\end{verbatim}

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

Compare the model with the ``zero'' model, so-called the null model. The
null model has no IV but intercept, which is the mean. The null model
uses the mean to predict the data or explain the variation of values.
Get the null model by formula \texttt{DV\textasciitilde{}1}.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{jobfit2 }\OtherTok{=} \FunctionTok{lm}\NormalTok{(salary }\SpecialCharTok{\textasciitilde{}} \DecValTok{1}\NormalTok{,}\AttributeTok{data =}\NormalTok{ job)}
\end{Highlighting}
\end{Shaded}

Then use \texttt{anova(model1,model2)} to compare the two models.

F test for the MLR model is to test against the null hypothesis that all
coefficients of IVs are zero. Therefore, the model comparison by essence
coincides with the F test!

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{anova}\NormalTok{(jobfit,jobfit2)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
Analysis of Variance Table

Model 1: salary ~ educ + salbegin + jobtime + prevexp
Model 2: salary ~ 1
  Res.Df        RSS Df   Sum of Sq      F    Pr(>F)    
1    469 2.6137e+10                                    
2    473 1.3792e+11 -4 -1.1178e+11 501.45 < 2.2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\hypertarget{standard-mlr-1}{%
\subsection{Standard MLR}\label{standard-mlr-1}}

Transform the formula with each variable embedded into the function
\texttt{scale()}, no other adjustments!

Under standard MLR, the coefficients are standardized ones.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fit\_std }\OtherTok{=} \FunctionTok{lm}\NormalTok{(}\FunctionTok{scale}\NormalTok{(salary) }\SpecialCharTok{\textasciitilde{}} \FunctionTok{scale}\NormalTok{(educ) }\SpecialCharTok{+} \FunctionTok{scale}\NormalTok{(salbegin) }\SpecialCharTok{+} \FunctionTok{scale}\NormalTok{(jobtime) }\SpecialCharTok{+} \FunctionTok{scale}\NormalTok{(prevexp)}
\NormalTok{             ,}\AttributeTok{data =}\NormalTok{ job)}
\end{Highlighting}
\end{Shaded}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{summary}\NormalTok{(fit\_std)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}

Call:
lm(formula = scale(salary) ~ scale(educ) + scale(salbegin) + 
    scale(jobtime) + scale(prevexp), data = job)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.73349 -0.24121 -0.07298  0.15475  2.69850 

Coefficients:
                  Estimate Std. Error t value Pr(>|t|)    
(Intercept)     -1.129e-16  2.008e-02   0.000        1    
scale(educ)      1.132e-01  2.798e-02   4.045 6.11e-05 ***
scale(salbegin)  8.151e-01  2.707e-02  30.111  < 2e-16 ***
scale(jobtime)   9.515e-02  2.018e-02   4.715 3.19e-06 ***
scale(prevexp)  -1.060e-01  2.161e-02  -4.904 1.30e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.4372 on 469 degrees of freedom
Multiple R-squared:  0.8105,    Adjusted R-squared:  0.8089 
F-statistic: 501.5 on 4 and 469 DF,  p-value: < 2.2e-16
\end{verbatim}

\hypertarget{plots-for-goodness-of-fit}{%
\subsection{Plots for Goodness of Fit}\label{plots-for-goodness-of-fit}}

\hypertarget{fitted-real-values}{%
\subsubsection{Fitted \& Real Values}\label{fitted-real-values}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fitted\_salary }\OtherTok{=} \FunctionTok{fitted}\NormalTok{(jobfit) }\CommentTok{\# predicted values}
\FunctionTok{plot}\NormalTok{(job}\SpecialCharTok{$}\NormalTok{salary,fitted\_salary,}\StringTok{\textquotesingle{}p\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-157-1.pdf}

\hypertarget{residuals}{%
\subsubsection{Residuals}\label{residuals}}

If the model fits well, the residuals should follow a normal
distribution.

Fitted model's residuals can be obtained through
\texttt{residuals(model)} or \texttt{model\$residuals}.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{hist}\NormalTok{(}\FunctionTok{residuals}\NormalTok{(fit))}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-158-1.pdf}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# hist(fit$residuals)}
\end{Highlighting}
\end{Shaded}

\hypertarget{more-plots}{%
\subsubsection{More Plots}\label{more-plots}}

Use \texttt{plot(model)} to get multiple plots reflecting the goodness
of fit from many perspectives.

Specify parameter \texttt{which\ =\ vector\_of\_range} to designate the
plots to show, 6 the most. \texttt{which\ =\ 1:4} by default.

Following the order, the six plots are:

\begin{itemize}
\tightlist
\item
  Residuals \& Fitted Values

  \begin{itemize}
  \tightlist
  \item
    test the homoscedascitiy assumption
  \end{itemize}
\item
  QQ Plot for Residuals

  \begin{itemize}
  \tightlist
  \item
    test the Gaussian assumption
  \end{itemize}
\item
  Square Root of Standardized Residuals \& Fitted Value

  \begin{itemize}
  \tightlist
  \item
    similar to the first one
  \end{itemize}
\item
  Cook's Distance

  \begin{itemize}
  \tightlist
  \item
    check outliers
  \end{itemize}
\item
  Standardized Residuals \& Leverage

  \begin{itemize}
  \tightlist
  \item
    See if influential points have huge residual, if so, possibly
    outlier!
  \end{itemize}
\item
  Cook's Distance \& Leverage

  \begin{itemize}
  \tightlist
  \item
    Large in both, possibly outlier!
  \end{itemize}
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow =} \FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{3}\NormalTok{))}
\FunctionTok{plot}\NormalTok{(fit, }\AttributeTok{which=}\DecValTok{1}\SpecialCharTok{:}\DecValTok{6}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-159-1.pdf}

\hypertarget{moderation-mediation}{%
\section{Moderation \& Mediation}\label{moderation-mediation}}

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

The data in ``\texttt{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}[]
\NormalTok{marriage }\OtherTok{\textless{}{-}}\NormalTok{ bruceR}\SpecialCharTok{::}\FunctionTok{import}\NormalTok{(}\StringTok{"moderation\_data.sav"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## ✔ Successfully imported: 25 obs. of 8 variables
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{attach}\NormalTok{(marriage)}
\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. However, \textbf{VIF} suffers with uncentered data.

\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)}
\FunctionTok{summary}\NormalTok{(model\_0)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = Loyalty ~ C_Relationship + C_age_con)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.2008 -0.7192  0.2991  0.8225  3.1450 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    11.63320    0.28539   40.76  < 2e-16 ***
## C_Relationship  0.81162    0.19277    4.21 0.000361 ***
## C_age_con      -0.02447    0.03949   -0.62 0.541850    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.427 on 22 degrees of freedom
## Multiple R-squared:  0.4496, Adjusted R-squared:  0.3996 
## F-statistic: 8.985 on 2 and 22 DF,  p-value: 0.001404
\end{verbatim}

\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)}
\FunctionTok{summary}\NormalTok{(model\_moderation)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = Loyalty ~ C_Relationship * C_age_con)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.2137 -0.7480  0.2958  0.8216  2.3732 
## 
## Coefficients:
##                           Estimate Std. Error t value Pr(>|t|)    
## (Intercept)              1.162e+01  2.646e-01  43.900  < 2e-16 ***
## C_Relationship           7.550e-01  1.806e-01   4.181 0.000421 ***
## C_age_con                3.173e-04  3.837e-02   0.008 0.993481    
## C_Relationship:C_age_con 5.626e-02  2.617e-02   2.150 0.043378 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.322 on 21 degrees of freedom
## Multiple R-squared:  0.5489, Adjusted R-squared:  0.4844 
## F-statistic: 8.517 on 3 and 21 DF,  p-value: 0.0006803
\end{verbatim}

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-3}{%
\subsubsection{Model Comparison}\label{model-comparison-3}}

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}

\begin{verbatim}
## Analysis of Variance Table
## 
## Model 1: Loyalty ~ C_Relationship + C_age_con
## Model 2: Loyalty ~ C_Relationship * C_age_con
##   Res.Df    RSS Df Sum of Sq      F  Pr(>F)  
## 1     22 44.796                              
## 2     21 36.715  1    8.0803 4.6217 0.04338 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\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, }
\NormalTok{                                  m\_Relationship}\SpecialCharTok{+}\NormalTok{sd\_Relationship)), }
               \AttributeTok{level =} \FloatTok{0.95}\NormalTok{)}
\FunctionTok{summary}\NormalTok{(emm)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##  Relationship age_con emmean    SE df lower.CL upper.CL
##          3.14    24.9  11.10 0.518 21    10.02     12.2
##          4.65    24.9  11.61 0.389 21    10.80     12.4
##          6.16    24.9  12.13 0.633 21    10.81     13.4
##          3.14    32.2  10.47 0.379 21     9.69     11.3
##          4.65    32.2  11.62 0.265 21    11.07     12.2
##          6.16    32.2  12.76 0.381 21    11.96     13.5
##          3.14    39.6   9.85 0.501 21     8.81     10.9
##          4.65    39.6  11.62 0.386 21    10.82     12.4
##          6.16    39.6  13.39 0.565 21    12.21     14.6
## 
## Confidence level used: 0.95
\end{verbatim}

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

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{simpleSlope }\OtherTok{\textless{}{-}} \FunctionTok{emtrends}\NormalTok{(model\_moderation, }
\NormalTok{                        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{)}
\NormalTok{simpleSlope}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## $emtrends
##  age_con Relationship.trend    SE df lower.CL upper.CL
##     24.9              0.340 0.283 21   -0.249    0.928
##     32.2              0.755 0.181 21    0.379    1.130
##     39.6              1.170 0.244 21    0.662    1.678
## 
## Confidence level used: 0.95 
## 
## $contrasts
##  contrast                                          estimate    SE df t.ratio
##  age_con24.8616533017213 - age_con32.24              -0.415 0.193 21  -2.150
##  age_con24.8616533017213 - age_con39.6183466982787   -0.830 0.386 21  -2.150
##  age_con32.24 - age_con39.6183466982787              -0.415 0.193 21  -2.150
##  p.value
##   0.1039
##   0.1039
##   0.1039
## 
## P value adjustment: tukey method for comparing a family of 3 estimates
\end{verbatim}

Slopes can be visualized.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{emmip}\NormalTok{(model\_moderation, }
\NormalTok{      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,}
\NormalTok{                         m\_Relationship,}
\NormalTok{                         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}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-167-1.pdf}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# emmip(emm)  \# original Rmd had bare emm without formula — skipped}
\end{Highlighting}
\end{Shaded}

\hypertarget{residual-plots}{%
\subsection{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{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-168-1.pdf}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{car}\SpecialCharTok{::}\FunctionTok{residualPlots}\NormalTok{(model\_moderation)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-168-2.pdf}

\begin{verbatim}
##              Test stat Pr(>|Test stat|)
## Relationship   -0.2597           0.7977
## age_con        -0.5863           0.5642
## Tukey test     -1.0147           0.3102
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\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}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-168-3.pdf}

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

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 magnitude \(\to\) partial
  mediation.
\end{itemize}

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

The data in ``\texttt{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{consumer }\OtherTok{\textless{}{-}}\NormalTok{ bruceR}\SpecialCharTok{::}\FunctionTok{import}\NormalTok{(}\StringTok{"mediation\_data.sav"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## ✔ Successfully imported: 40 obs. of 3 variables
\end{verbatim}

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

\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}[]
\CommentTok{\# Regress Y only on X}
\NormalTok{model\_0 }\OtherTok{\textless{}{-}} \FunctionTok{lm}\NormalTok{(Satisfaction }\SpecialCharTok{\textasciitilde{}}\NormalTok{ Relationship,}\AttributeTok{data =}\NormalTok{ consumer)}
\FunctionTok{summary}\NormalTok{(model\_0)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = Satisfaction ~ Relationship, data = consumer)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.50746  0.02041  0.02041  0.02041  0.29254 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    2.6189     0.3069   8.534 2.30e-10 ***
## Relationship   0.4721     0.0651   7.252 1.12e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1805 on 38 degrees of freedom
## Multiple R-squared:  0.5806, Adjusted R-squared:  0.5695 
## F-statistic:  52.6 on 1 and 38 DF,  p-value: 1.123e-08
\end{verbatim}

\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,}\AttributeTok{data =}\NormalTok{ consumer)}
\FunctionTok{summary}\NormalTok{(model\_M)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = Discount ~ Relationship, data = consumer)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.56953 -0.00450  0.01718  0.01718  0.43047 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   2.91634    0.39477   7.388 7.40e-09 ***
## Relationship  0.41330    0.08374   4.935 1.62e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2322 on 38 degrees of freedom
## Multiple R-squared:  0.3906, Adjusted R-squared:  0.3746 
## F-statistic: 24.36 on 1 and 38 DF,  p-value: 1.624e-05
\end{verbatim}

Significant result here is the necessity for further mediation analysis.
If not, 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,}\AttributeTok{data =}\NormalTok{ consumer)}
\FunctionTok{summary}\NormalTok{(model\_Y)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Call:
## lm(formula = Satisfaction ~ Relationship + Discount, data = consumer)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.192183  0.000099  0.007817  0.007817  0.143511 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   0.48089    0.16148   2.978   0.0051 ** 
## Relationship  0.16913    0.02811   6.016 5.97e-07 ***
## Discount      0.73313    0.04251  17.244  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.06084 on 37 degrees of freedom
## Multiple R-squared:  0.9536, Adjusted R-squared:  0.9511 
## F-statistic: 380.1 on 2 and 37 DF,  p-value: < 2.2e-16
\end{verbatim}

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. 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{\textquotesingle{}Relationship\textquotesingle{}}\NormalTok{, }
                             \AttributeTok{mediator=}\StringTok{\textquotesingle{}Discount\textquotesingle{}}\NormalTok{,}
                             \AttributeTok{boot=}\ConstantTok{TRUE}\NormalTok{, }\AttributeTok{sims=}\DecValTok{500}\NormalTok{)}
\FunctionTok{summary}\NormalTok{(mediation\_results)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Causal Mediation Analysis 
## 
## Nonparametric Bootstrap Confidence Intervals with the Percentile Method
## 
##                Estimate 95% CI Lower 95% CI Upper p-value    
## ACME              0.303        0.122         0.50  <2e-16 ***
## ADE               0.169        0.106         0.20   0.004 ** 
## Total Effect      0.472        0.297         0.65  <2e-16 ***
## Prop. Mediated    0.642        0.400         0.81  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Sample Size Used: 40 
## 
## 
## Simulations: 500
\end{verbatim}

\hypertarget{correlation-distance}{%
\section{Correlation \& Distance}\label{correlation-distance}}

\hypertarget{partial-part-correlation}{%
\subsection{Partial \& Part
Correlation}\label{partial-part-correlation}}

Zero-order correlation refers to the correlation between \(X\) and \(Y\)
without controlling other covariates, such as Pearson and Spearman
correlation.

We've discussed partial and part (or, semi-partial) correlation in
multilinear regression. Take a step further, we can consider those under
the most general cases, i.e., without the setting of MLR but some
variables.

Suppose we have three variables, called \(X,Y\) and \(Z\), where \(X\)
is the predictor, \(Y\) is the dependent variable, and \(Z\) is the
covariate. Noteworthy that the assumption of the roles of \(X,Y,Z\)
don't matter much, only for accommodating the setting.

\begin{center}\includegraphics[width=0.6\linewidth]{partial & part correlation} \end{center}

Note that, in the general case, \(Y\) is not represented as the whole
area in the picture, but just an equal role as a circle.

If the correlation of \(X\) and \(Y\) is of our great interest, \[
\begin{aligned}
r_x&=\frac {a+c}{a+b+c+d}\\
pr_x&=\frac{a}{a+d}\\
spr_x&=\frac{a}{a+b+c+d}
\end{aligned}
\]

\begin{itemize}
\item
  Partial: Holding \(Z\) from BOTH \(X\) and \(Y\), i.e., removing the
  effect of \(Z\).

  Good for knowing the \textbf{variance uncounted by other control
  variable(s)}.
\item
  Semi-Partial: Holding \(Z\) from \(X\) only.

  Good for knowing the \textbf{unique contribution of \(X\)}.
\item
  Zero-Order: No extra constraint.
\end{itemize}

One more enlightening way to think about partial correlation is through
multilinear regression.

\begin{center}\includegraphics[width=0.4\linewidth]{MLR partial corr} \end{center}

If we're interested in the partial correlation between \(A\) and \(B\),
with \(C\) as the covariate. Then, what we need to do is partial out the
effect of \(C\) on BOTH \(A\) and \(B\), and this goal can be
accomplished through step-by-step linear regression.

Specifically, first we conduct a simple linear regression of \(A\) on
\(C\), and the residual part is that of \(A\) which doesn't intersect
with \(C\). Similarly, then followed by a SLR of \(B\) on \(C\), same
with the meaning of residual part. For the two residual parts, they
share it in common that they're clean of \(C\). Lastly, compute the
correlation between \(A\) and \(B\) directly, and that's what we want!

\hypertarget{pcor-spcor}{%
\subsubsection{\texorpdfstring{\texttt{pcor()} \&
\texttt{spcor()}}{pcor() \& spcor()}}\label{pcor-spcor}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(ppcor)}
\end{Highlighting}
\end{Shaded}

A popular radio talk show host has just received the latest government
study on public health care funding and has uncovered a startling fact:
As health care funding increases, disease rates also increase! Cities
that spend more actually seem to be worse off than cities that spend
less. Then, shall we just cut the funding and save people's health?

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{health }\OtherTok{\textless{}{-}} \FunctionTok{import}\NormalTok{(}\StringTok{"health\_funding.sav"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}\NormalTok{ dplyr}\SpecialCharTok{::}\FunctionTok{select}\NormalTok{(}\SpecialCharTok{{-}}\DecValTok{4}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## ✔ Successfully imported: 50 obs. of 4 variables
\end{verbatim}

First of all, check overall zero-order correlation.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{cor}\NormalTok{(health)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##           funding   disease    visits
## funding 1.0000000 0.7370609 0.9643374
## disease 0.7370609 1.0000000 0.7619660
## visits  0.9643374 0.7619660 1.0000000
\end{verbatim}

It seems astonishing that ``\texttt{funding}'' is greatly correlated
with ``\texttt{disease}''! May it offer the plausible reason to cut the
funding? Remember, we still have the covariate ``\texttt{visits}''. One
\textbf{alternative explanation} might be that, offered with more
funding, people may be more likely to pay visits to health care, and
thus more diseases are detected, which should have been detected but not
due to wealth state.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{pcor}\NormalTok{(health)}\SpecialCharTok{$}\NormalTok{estimate}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##            funding    disease    visits
## funding 1.00000000 0.01323488 0.9201361
## disease 0.01323488 1.00000000 0.2861809
## visits  0.92013611 0.28618088 1.0000000
\end{verbatim}

It's astonishing that, considering the partial correlation,
``\texttt{funding}'' almost has no correlation with ``\texttt{disease}''
in essence!

Additionally, we can consider the part correlation.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{spcor}\NormalTok{(health)}\SpecialCharTok{$}\NormalTok{estimate}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##             funding     disease    visits
## funding 1.000000000 0.003502953 0.6218522
## disease 0.008571132 1.000000000 0.1934086
## visits  0.595895776 0.075745172 1.0000000
\end{verbatim}

Note that part correlations from rows differ from those from columns.

\hypertarget{distance}{%
\subsection{Distance}\label{distance}}

We need to distinguish between two kinds of distance. One is distance
between \textbf{cases} (or observations), the other is distance between
\textbf{variables}. There are traditionally three ways to measure
distance, either from the perspective of dissimilarity or similarity.

\begin{itemize}
\tightlist
\item
  Dissimilarity

  \begin{itemize}
  \tightlist
  \item
    Euclidean distance
  \end{itemize}

  \[d(\vec p,\vec q)=\sqrt{\sum_{i=1}^n(p_i-q_i)^2}\]

  \begin{itemize}
  \tightlist
  \item
    Chebychev distance
  \end{itemize}

  A vector space where the distance between two vectors is the greatest
  of their differences along any coordinate dimension.
  \[D(\vec x,\vec y)=\max_i|x_i-y_i|\]
\item
  Similarity

  \begin{itemize}
  \tightlist
  \item
    Cosine similarity

    \begin{itemize}
    \tightlist
    \item
      A measure of similarity between two non-zero vectors of an inner
      product space, a judgment of orientation but not magnitude.
    \end{itemize}
  \end{itemize}
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{climate }\OtherTok{=} \FunctionTok{import}\NormalTok{(}\StringTok{"data10{-}04.sav"}\NormalTok{, }\AttributeTok{sheet =} \DecValTok{1}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%} 
\NormalTok{  dplyr}\SpecialCharTok{::}\FunctionTok{select}\NormalTok{(hgrow,temp,rain,hsun,humi)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## ✔ Successfully imported: 12 obs. of 6 variables
\end{verbatim}

\hypertarget{correlation-1}{%
\subsubsection{Correlation}\label{correlation-1}}

Note that \emph{correlation itself is a measure of distance}. However,
correlation only applies for \textbf{distance between variables}, not
for distance between cases. (If you insist to test the correlation
between cases, easy, just transpose your data!)

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{cor}\NormalTok{(climate)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##           hgrow      temp      rain        hsun        humi
## hgrow 1.0000000 0.9833871 0.7093700  0.70442944  0.37357335
## temp  0.9833871 1.0000000 0.7148209  0.69049008  0.29198319
## rain  0.7093700 0.7148209 1.0000000  0.70184182  0.38432585
## hsun  0.7044294 0.6904901 0.7018418  1.00000000 -0.05093789
## humi  0.3735734 0.2919832 0.3843259 -0.05093789  1.00000000
\end{verbatim}

\hypertarget{euclidean-distance}{%
\subsubsection{Euclidean Distance}\label{euclidean-distance}}

Then, we're interested in Euclidean distance \textbf{between cases} by
default. Use ``\texttt{dist(x,\ method\ =\ "euclidean")}''. Mind that
the distance makes sense only if the data is \textbf{scaled}!

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# climate \%\textgreater{}\% dist()}
\NormalTok{climate  }\SpecialCharTok{\%\textgreater{}\%} 
  \FunctionTok{scale}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%} \FunctionTok{dist}\NormalTok{() }
\end{Highlighting}
\end{Shaded}

If the Euclidean distance between \textbf{variables} is of interest,
then \emph{transpose} your data.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{climate  }\SpecialCharTok{\%\textgreater{}\%} 
  \FunctionTok{scale}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%} \FunctionTok{t}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%} \FunctionTok{dist}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

It's noteworthy that, the Euclidean distance result returned by
``\texttt{dist()}'' is a unique type of ``\texttt{dist}'', which is
incompatible with some other types. Luckily, use
``\texttt{as.matrix()}'' to transform that into a matrix (with the
diagonal and upper right triangle filled automatically).

\hypertarget{cosine-similarity}{%
\subsubsection{Cosine Similarity}\label{cosine-similarity}}

Cosine similarity is for \textbf{between cases}. Also, if similarity
between variables is of interest, be flexible with the data.

``\texttt{lsa::cosine()}'' deals with cosine similarity \textbf{between
variables} (column vectors) by default.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{climate }\SpecialCharTok{\%\textgreater{}\%} 
  \FunctionTok{as.matrix.data.frame}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%} \FunctionTok{t}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%} 
\NormalTok{  lsa}\SpecialCharTok{::}\FunctionTok{cosine}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\hypertarget{pca}{%
\section{PCA}\label{pca}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(psych)}
\end{Highlighting}
\end{Shaded}

The Big-Five scale ``bfi'' data is used here.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{bfi }\OtherTok{=}\NormalTok{ bfi }\SpecialCharTok{\%\textgreater{}\%}\NormalTok{ dplyr}\SpecialCharTok{::}\FunctionTok{select}\NormalTok{(}\DecValTok{1}\SpecialCharTok{:}\DecValTok{25}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%} \FunctionTok{drop\_na}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\hypertarget{prerequisites}{%
\subsection{Prerequisites}\label{prerequisites}}

\hypertarget{bartlett-test-of-sphericity-for-correlation-matrix}{%
\subsubsection{Bartlett Test of Sphericity for Correlation
Matrix}\label{bartlett-test-of-sphericity-for-correlation-matrix}}

Firstly, generate a correlation matrix. Then, compare it against the
identity matrix. Significant result is desired.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{bfi\_cor }\OtherTok{=} \FunctionTok{cor}\NormalTok{(bfi)}
\FunctionTok{cortest.bartlett}\NormalTok{(bfi\_cor,}\AttributeTok{n =} \FunctionTok{nrow}\NormalTok{(bfi\_cor))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## $chisq
## [1] 110.9584
## 
## $p.value
## [1] 1
## 
## $df
## [1] 300
\end{verbatim}

Note that \textbf{reversely-coded} items are critical for explanatory
factor analysis. If any, make sure they are detected and
correctly-coded. To check this, you can view through correlation matrix
plot, or through reliability analysis (``\texttt{reliability()}'' will
inform you of reversely-coded items.)

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{corrplot}\SpecialCharTok{::}\FunctionTok{corrplot}\NormalTok{(bfi\_cor)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-189-1.pdf}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{jmv}\SpecialCharTok{::}\FunctionTok{reliability}\NormalTok{(}\AttributeTok{data =}\NormalTok{ bfi,}\AttributeTok{vars =} \FunctionTok{colnames}\NormalTok{(bfi))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  RELIABILITY ANALYSIS
## 
##  Scale Reliability Statistics 
##  ──────────────────────────── 
##             Cronbach's α   
##  ──────────────────────────── 
##    scale       0.5246608   
##  ──────────────────────────── 
##    Note. items 'A1',
##    'C4', 'C5', 'E1',
##    'E2', 'N1', 'N2',
##    'N3', 'N4', 'N5',
##    'O2', 'O4', and 'O5'
##    correlate negatively
##    with the total scale
##    and probably should be
##    reversed
\end{verbatim}

\hypertarget{kmo-for-sampling-adequacy}{%
\subsubsection{KMO for Sampling
Adequacy}\label{kmo-for-sampling-adequacy}}

0.8 and higher are great, 0.7 is acceptable, 0.6 is mediocre, less than
0.5 is unacceptable.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{KMO}\NormalTok{(bfi\_cor)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Kaiser-Meyer-Olkin factor adequacy
## Call: KMO(r = bfi_cor)
## Overall MSA =  0.85
## MSA for each item = 
##   A1   A2   A3   A4   A5   C1   C2   C3   C4   C5   E1   E2   E3   E4   E5   N1 
## 0.75 0.84 0.87 0.88 0.90 0.84 0.80 0.85 0.83 0.86 0.84 0.88 0.90 0.88 0.89 0.78 
##   N2   N3   N4   N5   O1   O2   O3   O4   O5 
## 0.78 0.86 0.89 0.86 0.86 0.78 0.84 0.77 0.76
\end{verbatim}

\hypertarget{no.-of-factors}{%
\subsection{No.~of Factors}\label{no.-of-factors}}

\begin{itemize}
\tightlist
\item
  To determine the number of factors, scree and parallel are two
  methods.
\item
  Scree is obsolete; parallel suggests 3 factors here.
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{scree}\NormalTok{(bfi)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-191-1.pdf}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{fa.parallel}\NormalTok{(bfi)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-191-2.pdf}

\begin{verbatim}
## Parallel analysis suggests that the number of factors =  6  and the number of components =  5
\end{verbatim}

\hypertarget{factor-analysis}{%
\subsection{Factor Analysis}\label{factor-analysis}}

Use ``\texttt{psych::fa(r,\ nfactors,\ rotate,\ fm)}''.

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

Take a look: without selecting the number of factors, without rotation.

The Proportion Var and Cumulative Var are the variance that can be
explained by factors.

``SS loadings'' is the sum of squared loadings for each factor;
\textbf{\textgreater1} means the factor is worth keeping.

The model can be printed with the following results:

\begin{itemize}
\tightlist
\item
  Standardized loadings (pattern matrix) based upon correlation matrix

  \begin{itemize}
  \tightlist
  \item
    SS loadings

    \begin{itemize}
    \tightlist
    \item
      The eigenvalues, the sum of the squared loadings for each factor.
    \item
      \textbf{Better to be \textgreater1}.
    \end{itemize}
  \item
    Proportion Var

    \begin{itemize}
    \tightlist
    \item
      The overall variance (for all variables/items) that can be
      explained by each factor.
    \end{itemize}
  \item
    Cumulative Var
  \item
    Proportion Explained

    \begin{itemize}
    \tightlist
    \item
      The relative importance of each factor.
    \end{itemize}
  \item
    Cumulative Proportion
  \end{itemize}
\item
  Data frame with loading matrix, communality, uniqueness, and item
  complexity.
\end{itemize}

\textbf{Standardized loadings (pattern matrix) based upon correlation
matrix} is the most important part!

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fa\_none }\OtherTok{=} \FunctionTok{fa}\NormalTok{(bfi,}\AttributeTok{nfactors =} \FunctionTok{ncol}\NormalTok{(bfi),}
             \AttributeTok{fm=}\StringTok{\textquotesingle{}minres\textquotesingle{}}\NormalTok{,}\AttributeTok{rotate=}\StringTok{\textquotesingle{}none\textquotesingle{}}\NormalTok{)  }
\NormalTok{fa\_none}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Factor Analysis using method =  minres
## Call: fa(r = bfi, nfactors = ncol(bfi), rotate = "none", fm = "minres")
## Standardized loadings (pattern matrix) based upon correlation matrix
##      MR1   MR2   MR3   MR4   MR5   MR6   MR7   MR8   MR9  MR10  MR11  MR12
## A1 -0.23 -0.02  0.14 -0.01 -0.42  0.31  0.07 -0.06  0.02  0.00  0.00  0.04
## A2  0.48  0.30 -0.19  0.15  0.38 -0.22  0.14  0.15 -0.11  0.03 -0.04  0.06
## A3  0.54  0.31 -0.25  0.12  0.29  0.00  0.11 -0.13  0.10 -0.02 -0.04 -0.13
## A4  0.43  0.12 -0.12  0.30  0.17  0.02  0.00 -0.22 -0.01  0.24  0.00  0.15
## A5  0.59  0.19 -0.27  0.06  0.18  0.14  0.01 -0.05  0.16 -0.14  0.06 -0.10
## C1  0.35  0.14  0.47  0.10 -0.02  0.13 -0.08  0.18  0.18 -0.02 -0.03  0.03
## C2  0.34  0.20  0.52  0.27  0.02  0.19 -0.09  0.05  0.01  0.24 -0.04 -0.10
## C3  0.32  0.06  0.37  0.30  0.02  0.02  0.06  0.12 -0.11 -0.18  0.03 -0.02
## C4 -0.48  0.11 -0.48 -0.20  0.03  0.27  0.09  0.04 -0.09  0.05  0.03  0.04
## C5 -0.51  0.16 -0.32 -0.29  0.13  0.09 -0.01  0.26  0.14  0.15 -0.08 -0.09
## E1 -0.42 -0.19  0.30  0.12  0.30  0.23  0.23 -0.15  0.06 -0.05 -0.10  0.02
## E2 -0.62 -0.04  0.24  0.06  0.33  0.11  0.12 -0.01  0.03  0.03  0.19 -0.03
## E3  0.53  0.33 -0.13 -0.19 -0.09  0.20  0.03 -0.10 -0.10 -0.09  0.02 -0.07
## E4  0.61  0.18 -0.34  0.11 -0.21  0.15 -0.15  0.03  0.15 -0.03  0.02  0.14
## E5  0.52  0.30  0.10 -0.06 -0.24 -0.03  0.23  0.16 -0.12  0.05 -0.07  0.00
## N1 -0.44  0.64  0.04  0.09 -0.27 -0.14  0.13 -0.11  0.02  0.03  0.04 -0.02
## N2 -0.43  0.65  0.10  0.04 -0.22 -0.21  0.16  0.01  0.15  0.00  0.09 -0.01
## N3 -0.41  0.62  0.04  0.06 -0.02 -0.02 -0.15 -0.10  0.01 -0.09 -0.14  0.06
## N4 -0.53  0.42  0.08 -0.04  0.23  0.06 -0.14 -0.02 -0.07 -0.07 -0.23 -0.02
## N5 -0.35  0.44 -0.02  0.24  0.13  0.06 -0.27  0.02 -0.16  0.00  0.18 -0.03
## O1  0.33  0.20  0.19 -0.38  0.02  0.16  0.12 -0.03 -0.09  0.00 -0.02  0.08
## O2 -0.20  0.07 -0.29  0.41 -0.05  0.17  0.09  0.16 -0.02 -0.01  0.03  0.04
## O3  0.41  0.29  0.13 -0.46  0.03  0.12 -0.04 -0.05 -0.06  0.07  0.09 -0.08
## O4 -0.07  0.26  0.18 -0.24  0.32  0.08  0.01  0.12  0.05 -0.08  0.09  0.19
## O5 -0.21 -0.05 -0.26  0.45 -0.13  0.21  0.07  0.05 -0.08 -0.03 -0.04 -0.04
##     MR13  MR14  MR15  MR16  MR17  MR18  MR19  MR20  MR21  MR22  MR23  MR24 MR25
## A1  0.04  0.18  0.06 -0.02 -0.05  0.01 -0.01  0.04  0.01  0.03  0.00 -0.01    0
## A2  0.08  0.06  0.03 -0.08 -0.01  0.06  0.00  0.02  0.04 -0.01  0.00  0.00    0
## A3  0.01 -0.04  0.01  0.05 -0.09  0.04 -0.06  0.01 -0.03  0.03  0.00 -0.01    0
## A4  0.00  0.06  0.00  0.07 -0.01 -0.04  0.03  0.01 -0.01  0.00  0.02  0.00    0
## A5  0.00  0.10 -0.04 -0.03  0.06 -0.10  0.02 -0.02  0.01  0.00 -0.01  0.01    0
## C1  0.08 -0.01 -0.12  0.01 -0.11  0.03 -0.02  0.00  0.00 -0.02  0.02  0.01    0
## C2 -0.06 -0.05  0.07 -0.03  0.03 -0.01  0.01 -0.03 -0.01  0.00 -0.02  0.00    0
## C3  0.10  0.02  0.15  0.08 -0.01 -0.05  0.03 -0.01 -0.01 -0.01  0.01  0.00    0
## C4  0.12 -0.04  0.00 -0.04 -0.08 -0.03  0.03 -0.04 -0.04 -0.02  0.00  0.00    0
## C5  0.01  0.06  0.05  0.07  0.03 -0.01  0.00 -0.01  0.03  0.00  0.01  0.00    0
## E1  0.02 -0.06 -0.01 -0.13  0.03  0.00  0.00 -0.02  0.03  0.00  0.01  0.00    0
## E2  0.00  0.08 -0.07  0.08  0.04  0.07  0.03  0.03 -0.02 -0.02 -0.02  0.00    0
## E3 -0.15  0.01  0.00  0.03 -0.01  0.06  0.03 -0.01  0.01 -0.04  0.02 -0.01    0
## E4  0.07 -0.04  0.03 -0.05  0.10  0.05 -0.01  0.02 -0.01 -0.02 -0.01 -0.01    0
## E5 -0.04  0.05 -0.16 -0.03  0.04 -0.04  0.03  0.00 -0.02  0.02  0.00 -0.01    0
## N1  0.06 -0.08 -0.02  0.05 -0.01 -0.08 -0.05  0.02  0.04 -0.03 -0.01  0.00    0
## N2 -0.02  0.00  0.07 -0.06  0.05  0.05  0.03 -0.02 -0.03  0.01  0.02  0.01    0
## N3 -0.02  0.04 -0.02  0.04 -0.06  0.05  0.06 -0.04  0.02  0.01 -0.03  0.00    0
## N4 -0.01  0.02  0.01 -0.02  0.06 -0.03 -0.02  0.06 -0.04 -0.01  0.01  0.00    0
## N5  0.03  0.04 -0.06 -0.07  0.00  0.00 -0.05 -0.02  0.01  0.02  0.02  0.00    0
## O1  0.01  0.03  0.02  0.06  0.07  0.03 -0.10 -0.04  0.00  0.00 -0.01  0.01    0
## O2 -0.19 -0.03  0.05 -0.04 -0.06 -0.02 -0.03  0.03  0.00  0.00 -0.01  0.01    0
## O3  0.07 -0.10  0.03 -0.05 -0.02  0.00  0.05  0.06  0.02  0.02 -0.01  0.01    0
## O4 -0.09 -0.09  0.01  0.05  0.00 -0.05  0.01  0.01  0.01  0.03  0.01 -0.01    0
## O5  0.06 -0.12 -0.06  0.10  0.08  0.05  0.02  0.00  0.02  0.03  0.01  0.00    0
##      h2   u2 com
## A1 0.39 0.61 3.6
## A2 0.66 0.34 5.0
## A3 0.62 0.38 3.7
## A4 0.48 0.52 4.6
## A5 0.59 0.41 2.8
## C1 0.50 0.50 3.7
## C2 0.63 0.37 4.0
## C3 0.45 0.55 4.9
## C4 0.64 0.36 3.6
## C5 0.63 0.37 4.4
## E1 0.58 0.42 6.0
## E2 0.65 0.35 2.5
## E3 0.56 0.44 3.2
## E4 0.69 0.31 3.0
## E5 0.57 0.43 3.6
## N1 0.77 0.23 2.7
## N2 0.78 0.22 2.8
## N3 0.63 0.37 2.3
## N4 0.62 0.38 3.2
## N5 0.54 0.46 4.7
## O1 0.40 0.60 4.6
## O2 0.41 0.59 4.3
## O3 0.55 0.45 3.8
## O4 0.36 0.64 6.1
## O5 0.43 0.57 3.8
## 
##                        MR1  MR2  MR3  MR4  MR5  MR6  MR7  MR8  MR9 MR10 MR11
## SS loadings           4.74 2.40 1.70 1.35 1.11 0.62 0.39 0.35 0.24 0.24 0.20
## Proportion Var        0.19 0.10 0.07 0.05 0.04 0.02 0.02 0.01 0.01 0.01 0.01
## Cumulative Var        0.19 0.29 0.35 0.41 0.45 0.48 0.49 0.51 0.52 0.53 0.53
## Proportion Explained  0.34 0.17 0.12 0.10 0.08 0.04 0.03 0.02 0.02 0.02 0.01
## Cumulative Proportion 0.34 0.50 0.63 0.72 0.80 0.84 0.87 0.89 0.91 0.93 0.94
##                       MR12 MR13 MR14 MR15 MR16 MR17 MR18 MR19 MR20 MR21 MR22
## SS loadings           0.16 0.13 0.12 0.10 0.09 0.07 0.05 0.03 0.02 0.01 0.01
## Proportion Var        0.01 0.01 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
## Cumulative Var        0.54 0.54 0.55 0.55 0.56 0.56 0.56 0.56 0.56 0.56 0.57
## Proportion Explained  0.01 0.01 0.01 0.01 0.01 0.01 0.00 0.00 0.00 0.00 0.00
## Cumulative Proportion 0.95 0.96 0.97 0.98 0.99 0.99 0.99 1.00 1.00 1.00 1.00
##                       MR23 MR24 MR25
## SS loadings           0.00 0.00 0.00
## Proportion Var        0.00 0.00 0.00
## Cumulative Var        0.57 0.57 0.57
## Proportion Explained  0.00 0.00 0.00
## Cumulative Proportion 1.00 1.00 1.00
## 
## Mean item complexity =  3.9
## Test of the hypothesis that 25 factors are sufficient.
## 
## df null model =  300  with the objective function =  7.48 with Chi Square =  18146.07
## df of  the model are -25  and the objective function was  0 
## 
## The root mean square of the residuals (RMSR) is  0 
## The df corrected root mean square of the residuals is  NA 
## 
## The harmonic n.obs is  2436 with the empirical chi square  0  with prob <  NA 
## The total n.obs was  2436  with Likelihood Chi Square =  0  with prob <  NA 
## 
## Tucker Lewis Index of factoring reliability =  1.017
## Fit based upon off diagonal values = 1
## Measures of factor score adequacy             
##                                                    MR1  MR2 MR3  MR4  MR5  MR6
## Correlation of (regression) scores with factors   0.96 0.94 0.9 0.86 0.86 0.77
## Multiple R square of scores with factors          0.93 0.88 0.8 0.73 0.73 0.60
## Minimum correlation of possible factor scores     0.85 0.75 0.6 0.47 0.47 0.20
##                                                    MR7   MR8   MR9  MR10  MR11
## Correlation of (regression) scores with factors   0.71  0.67  0.62  0.59  0.59
## Multiple R square of scores with factors          0.50  0.44  0.38  0.35  0.34
## Minimum correlation of possible factor scores     0.00 -0.11 -0.24 -0.30 -0.31
##                                                    MR12  MR13  MR14  MR15  MR16
## Correlation of (regression) scores with factors    0.51  0.47  0.45  0.43  0.42
## Multiple R square of scores with factors           0.26  0.22  0.20  0.18  0.17
## Minimum correlation of possible factor scores     -0.48 -0.56 -0.60 -0.64 -0.65
##                                                    MR17  MR18  MR19  MR20  MR21
## Correlation of (regression) scores with factors    0.39  0.36  0.27  0.21  0.19
## Multiple R square of scores with factors           0.15  0.13  0.07  0.05  0.04
## Minimum correlation of possible factor scores     -0.70 -0.74 -0.85 -0.91 -0.93
##                                                    MR22  MR23  MR24 MR25
## Correlation of (regression) scores with factors    0.15  0.10  0.05    0
## Multiple R square of scores with factors           0.02  0.01  0.00    0
## Minimum correlation of possible factor scores     -0.95 -0.98 -1.00   -1
\end{verbatim}

For illustration purpose:

\begin{itemize}
\tightlist
\item
  If using 25 factors (here only 25 items), we can see how loading and
  proportion of variance decrease with factors;
\item
  The cumulative variance explained will approach 1.
\end{itemize}

Scores, or the coefficient for the model:
\(Factors = scores*X+\varepsilon\) where \(X\) are the original data,
\(Factors\) are the obtained latent variables, \(scores\) are their
coefficients.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{head}\NormalTok{(fa\_none}\SpecialCharTok{$}\NormalTok{scores)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##             MR1        MR2        MR3        MR4        MR5         MR6
## [1,] -0.9626360 -0.9588661 -1.4054098  0.4759941 -0.8015550 -0.43231916
## [2,] -0.1390885  0.1084315 -0.6963273 -0.2378928 -0.5391072  0.05982153
## [3,] -0.3348634  0.4224894  0.3075130 -0.3589328 -0.8904569 -0.09589599
## [4,] -0.6125530 -0.4145437 -1.1996091  0.3418352 -0.2270343  0.98903364
## [5,] -0.1559778 -0.5521422 -0.2218671  0.2608480 -1.0992961  0.16307467
## [6,]  1.1302479  0.7788624  0.7855226  0.2839649 -0.9010212  0.03915730
##             MR7         MR8        MR9       MR10       MR11        MR12
## [1,]  0.4218875  0.33649546 -0.1776481  0.2520133  0.5236582  0.02549219
## [2,] -1.5204589  0.08899538 -0.4270758 -0.8301846 -0.4759909 -1.51574630
## [3,]  0.2469924  0.07177995  0.6214508  0.5948036  0.4869344 -0.58456895
## [4,]  1.2248993 -0.31491131  0.9663665  0.4122710 -0.7407253 -0.99481003
## [5,] -0.9427302 -0.24649398 -0.6914795 -0.7686449 -0.4167629 -0.59352826
## [6,]  0.4974892  0.64567163  0.2967707  0.2815885  0.3121439  0.11768794
##             MR13        MR14         MR15        MR16         MR17        MR18
## [1,] -0.65557522 -0.16413109  0.150318429 -0.51002543  0.100235548 -0.27031025
## [2,] -0.08241697 -0.08283261 -0.001249183 -0.04638787 -0.295174589 -0.03465558
## [3,] -0.14606000  0.45539975  0.220009474  0.27571184 -0.181852697  0.18798816
## [4,]  0.19190363 -0.09312211  0.257157316 -0.42900268 -0.041377111  0.29575845
## [5,] -0.45669848  0.14139336 -0.278256908 -0.13307298 -0.001875009 -0.30444354
## [6,] -0.37216733  0.73609907  0.701941763 -0.43639326 -0.428401601 -0.06485289
##            MR19         MR20        MR21        MR22         MR23         MR24
## [1,]  0.1362685 -0.082227379 -0.03603757 -0.01820091 -0.008749894  0.009569002
## [2,] -0.3614771 -0.074749964 -0.03486963 -0.14930817  0.164475662  0.002198851
## [3,]  0.1632381  0.057890828  0.09244331  0.20478642 -0.036201997 -0.066984670
## [4,]  0.4032549 -0.004110559 -0.38316119  0.18234705  0.210808208  0.029328607
## [5,]  0.6039341 -0.125366947 -0.21772004 -0.07448912  0.039062175  0.033158942
## [6,]  0.1335250  0.137412524  0.09439241  0.08965242  0.250201940 -0.044897119
##               MR25
## [1,] -4.469031e-15
## [2,] -6.221992e-15
## [3,]  1.227396e-15
## [4,]  4.065705e-15
## [5,] -5.304509e-15
## [6,]  4.235866e-15
\end{verbatim}

Communality, the variance that can be explained by factors, which should
be high if the model is good.

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# communality}
\FunctionTok{apply}\NormalTok{(fa\_none}\SpecialCharTok{$}\NormalTok{loadings}\SpecialCharTok{\^{}}\DecValTok{2}\NormalTok{,}\DecValTok{1}\NormalTok{,sum)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##        A1        A2        A3        A4        A5        C1        C2        C3 
## 0.3943710 0.6632439 0.6199889 0.4787753 0.5939516 0.4954832 0.6324677 0.4484558 
##        C4        C5        E1        E2        E3        E4        E5        N1 
## 0.6362078 0.6263201 0.5758485 0.6532971 0.5583746 0.6854708 0.5666877 0.7738224 
##        N2        N3        N4        N5        O1        O2        O3        O4 
## 0.7798513 0.6340109 0.6203578 0.5405542 0.4042214 0.4110534 0.5488778 0.3609073 
##        O5 
## 0.4338612
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# uniqueness}
\DecValTok{1} \SpecialCharTok{{-}} \FunctionTok{apply}\NormalTok{(fa\_none}\SpecialCharTok{$}\NormalTok{loadings}\SpecialCharTok{\^{}}\DecValTok{2}\NormalTok{,}\DecValTok{1}\NormalTok{,sum)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##        A1        A2        A3        A4        A5        C1        C2        C3 
## 0.6056290 0.3367561 0.3800111 0.5212247 0.4060484 0.5045168 0.3675323 0.5515442 
##        C4        C5        E1        E2        E3        E4        E5        N1 
## 0.3637922 0.3736799 0.4241515 0.3467029 0.4416254 0.3145292 0.4333123 0.2261776 
##        N2        N3        N4        N5        O1        O2        O3        O4 
## 0.2201487 0.3659891 0.3796422 0.4594458 0.5957786 0.5889466 0.4511222 0.6390927 
##        O5 
## 0.5661388
\end{verbatim}

\hypertarget{reduced-model}{%
\subsubsection{Reduced Model}\label{reduced-model}}

According to scree plot and SS loadings for factors, 5 is suggested.
Note that restrictions on factor number will have practical effect on
the model, instead of just trimming the model to the reduced one.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fa5\_none }\OtherTok{=} \FunctionTok{fa}\NormalTok{(bfi,}\DecValTok{5}\NormalTok{,}\AttributeTok{rotate=}\StringTok{\textquotesingle{}none\textquotesingle{}}\NormalTok{)  }
\NormalTok{fa5\_none}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Factor Analysis using method =  minres
## Call: fa(r = bfi, nfactors = 5, rotate = "none")
## Standardized loadings (pattern matrix) based upon correlation matrix
##      MR1   MR2   MR3   MR4   MR5   h2   u2 com
## A1 -0.22 -0.02  0.13  0.00 -0.37 0.20 0.80 1.9
## A2  0.47  0.29 -0.18  0.13  0.34 0.46 0.54 3.1
## A3  0.53  0.30 -0.25  0.11  0.30 0.54 0.46 2.9
## A4  0.42  0.11 -0.12  0.27  0.16 0.30 0.70 2.5
## A5  0.58  0.18 -0.26  0.04  0.17 0.47 0.53 1.8
## C1  0.34  0.13  0.45  0.12 -0.02 0.35 0.65 2.2
## C2  0.34  0.20  0.48  0.27  0.02 0.45 0.55 2.9
## C3  0.32  0.05  0.35  0.31  0.03 0.32 0.68 3.0
## C4 -0.47  0.10 -0.45 -0.21  0.03 0.48 0.52 2.5
## C5 -0.49  0.15 -0.29 -0.28  0.11 0.44 0.56 2.6
## E1 -0.41 -0.18  0.26  0.11  0.26 0.35 0.65 3.1
## E2 -0.62 -0.04  0.23  0.06  0.32 0.55 0.45 1.9
## E3  0.53  0.33 -0.12 -0.18 -0.09 0.44 0.56 2.2
## E4  0.60  0.17 -0.33  0.10 -0.19 0.54 0.46 2.0
## E5  0.51  0.29  0.09 -0.04 -0.23 0.41 0.59 2.1
## N1 -0.44  0.64  0.03  0.10 -0.27 0.68 0.32 2.2
## N2 -0.42  0.62  0.08  0.05 -0.20 0.61 0.39 2.1
## N3 -0.41  0.61  0.03  0.07 -0.02 0.54 0.46 1.8
## N4 -0.53  0.42  0.08 -0.04  0.22 0.51 0.49 2.3
## N5 -0.35  0.41 -0.03  0.21  0.12 0.35 0.65 2.7
## O1  0.33  0.20  0.20 -0.36  0.01 0.32 0.68 3.2
## O2 -0.20  0.06 -0.29  0.37 -0.04 0.27 0.73 2.6
## O3  0.41  0.30  0.15 -0.45  0.01 0.47 0.53 3.0
## O4 -0.06  0.26  0.18 -0.23  0.30 0.25 0.75 3.7
## O5 -0.20 -0.06 -0.27  0.41 -0.11 0.30 0.70 2.5
## 
##                        MR1  MR2  MR3  MR4  MR5
## SS loadings           4.60 2.27 1.55 1.22 0.96
## Proportion Var        0.18 0.09 0.06 0.05 0.04
## Cumulative Var        0.18 0.27 0.34 0.39 0.42
## Proportion Explained  0.43 0.21 0.15 0.12 0.09
## Cumulative Proportion 0.43 0.65 0.79 0.91 1.00
## 
## Mean item complexity =  2.5
## Test of the hypothesis that 5 factors are sufficient.
## 
## df null model =  300  with the objective function =  7.48 with Chi Square =  18146.07
## df of  the model are 185  and the objective function was  0.64 
## 
## The root mean square of the residuals (RMSR) is  0.03 
## The df corrected root mean square of the residuals is  0.04 
## 
## The harmonic n.obs is  2436 with the empirical chi square  1131.9  with prob <  1.1e-135 
## The total n.obs was  2436  with Likelihood Chi Square =  1538.52  with prob <  8.6e-212 
## 
## Tucker Lewis Index of factoring reliability =  0.877
## RMSEA index =  0.055  and the 90 % confidence intervals are  0.052 0.057
## BIC =  95.87
## Fit based upon off diagonal values = 0.98
## Measures of factor score adequacy             
##                                                    MR1  MR2  MR3  MR4  MR5
## Correlation of (regression) scores with factors   0.95 0.91 0.85 0.81 0.80
## Multiple R square of scores with factors          0.90 0.83 0.73 0.66 0.64
## Minimum correlation of possible factor scores     0.80 0.66 0.46 0.33 0.28
\end{verbatim}

\hypertarget{factor-rotation}{%
\subsubsection{Factor Rotation}\label{factor-rotation}}

Factor loadings are listed for each variable. The square of it is the
variance accounted for. Unrotated factors are typically not very
interpretable (most factors are correlated with many variables), though
it has largest variance explained. The hope is that all these
correlation coefficients are close to (+/-)1 or 0, so we can easily
interpret the meanings of these components (factors). We will accomplish
this goal through rotation.

Factors are rotated to make them more meaningful and easier to interpret
(each variable is associated with a minimal number of factors). The most
popular rotational method is Varimax rotations, which uses orthogonal
rotations yielding uncorrelated factors/components. Varimax attempts to
maximize the variance that a single factor can account for (across all
variables). This enhances the interpretability of the factors. (i.e.,
kind of simplification of interpretation) However, the loadings and
complexity for each variable will change, while communality and
uniqueness won't change; SS loadings for each factors must change, while
cumulative variance accounted for will not change.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fa5\_rot }\OtherTok{=} \FunctionTok{fa}\NormalTok{(bfi,}\AttributeTok{nfactors =} \DecValTok{5}\NormalTok{,}\AttributeTok{rotate =} \StringTok{\textquotesingle{}varimax\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\hypertarget{factor-diagram}{%
\subsection{Factor Diagram}\label{factor-diagram}}

Use ``\texttt{fa.diagram()}'' to show how variables relate to the
factors. Note \texttt{fa.diagram()} use r = 0.3 as a cut off, you can
change it.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{fa.diagram}\NormalTok{(fa5\_rot)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-197-1.pdf}

\hypertarget{reliability-analysis}{%
\section{Reliability Analysis}\label{reliability-analysis}}

Use \texttt{reliability()} function in package ``\texttt{jmv}'' to
conduct reliability analysis. Note that ``\texttt{psych}'' package has a
\texttt{reliability()} function too. don't load the wrong package.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(}\StringTok{"jmv"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

The following data is from a scale designed for athletes. However, in
reliability analysis, the practical meanings of items don't matter that
much compared to factor analysis.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{athletes }\OtherTok{=} \FunctionTok{import}\NormalTok{(}\StringTok{"data15{-}01.xlsx"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%} 
\NormalTok{  dplyr}\SpecialCharTok{::}\FunctionTok{select}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\StringTok{\textquotesingle{}x1\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x2\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x4\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x10\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x13\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x39\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x41\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x45\textquotesingle{}}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## ✔ Successfully imported: 312 obs. of 54 variables
\end{verbatim}

\hypertarget{reliability}{%
\subsection{Reliability}\label{reliability}}

In \texttt{reliability()}, the most important parameters are:

\begin{itemize}
\tightlist
\item
  \texttt{data}: the data as a data frame
\item
  \texttt{vars}: a vector of strings \emph{naming} the variables of
  interest in data
\item
  \texttt{alphaScale}: TRUE (default) or FALSE, provide Cronbach's alpha
\item
  \texttt{corPlot}: TRUE or FALSE (default), provide a correlation plot
\item
  \texttt{revItems}: a vector containing strings naming the varibales
  that are reverse scaled
\item
  \texttt{alphaItems}: TRUE or FALSE (default), provide what the
  Cronbach's alpha would be if the item was dropped
\item
  \texttt{itemRestCor}: TRUE or FALSE (default), provide item-rest
  correlations
\end{itemize}

Other parameters:

\begin{itemize}
\tightlist
\item
  \texttt{meanItems}: TRUE or FALSE (default), provide item means
\item
  \texttt{sdItems}: TRUE or FALSE (default), provide item standard
  deviations
\item
  \texttt{omegaScale}: TRUE or FALSE (default), provide McDonald's omega
\item
  \texttt{omegaItems}: TRUE or FALSE (default), provide what the
  McDonald's omega would be if the item was dropped
\item
  \texttt{meanScale}: TRUE or FALSE (default), provide the mean
\item
  \texttt{sdScale}: TRUE or FALSE (default), provide the standard
  deviation
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{RA\_results }\OtherTok{=} \FunctionTok{reliability}\NormalTok{(athletes,}
                         \AttributeTok{vars =} \FunctionTok{c}\NormalTok{(}\StringTok{\textquotesingle{}x1\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x2\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x4\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x10\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x13\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x39\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x41\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x45\textquotesingle{}}\NormalTok{),}
                         \AttributeTok{meanScale=}\ConstantTok{TRUE}\NormalTok{,}
                         \AttributeTok{sdScale=}\ConstantTok{TRUE}\NormalTok{,}
                         \AttributeTok{corPlot=}\ConstantTok{TRUE}\NormalTok{,}
                         \AttributeTok{alphaItems=}\ConstantTok{TRUE}\NormalTok{,}
                         \AttributeTok{meanItems=}\ConstantTok{TRUE}\NormalTok{,}
                         \AttributeTok{sdItems=}\ConstantTok{TRUE}\NormalTok{,}
                         \AttributeTok{itemRestCor=}\ConstantTok{TRUE}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

For reversely-coded items, the \texttt{reliability()} will return with a
``Note'' to inform you of those, if any. Those reversely-coded items can
also be detected through \texttt{Corplot}, which will correlate
negatively with right-ordered ones.

The \texttt{reliability()} function returns an object with

\begin{itemize}
\tightlist
\item
  \texttt{items}: Item Reliability Statistics, with mean, sd, item-rest
  correlation, (if-removed) Cronbach's \(\alpha\), etc. Results on
  item-level.
\item
  \texttt{scale}: Scale Reliability Statistics, with mean, sd,
  Cronbach's \(\alpha\), etc. Results on scale-level.
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{RA\_results}\SpecialCharTok{$}\NormalTok{items}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Item Reliability Statistics                                               
##  ───────────────────────────────────────────────────────────────────────── 
##           mean        sd           item-rest correlation    Cronbach's α   
##  ───────────────────────────────────────────────────────────────────────── 
##    x1     4.019231    0.8819093              0.101491299     0.102859793   
##    x2     3.416667    1.0786340              0.009362630     0.155679950   
##    x4     3.092949    1.1202671              0.117490345     0.081986674   
##    x10    3.548077    1.0837574              0.246701216    -0.008887225   
##    x13    3.663462    1.2077769              0.015286062     0.154828393   
##    x39    2.759615    1.3761533             -0.057467371     0.219073269   
##    x41    3.028846    1.1658876              0.016813511     0.152730592   
##    x45    2.336538    1.4408637              0.030066097     0.148840877   
##  ─────────────────────────────────────────────────────────────────────────
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{RA\_results}\SpecialCharTok{$}\NormalTok{scale}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Scale Reliability Statistics                       
##  ────────────────────────────────────────────────── 
##             mean        sd           Cronbach's α   
##  ────────────────────────────────────────────────── 
##    scale    3.233173    0.4466260       0.1439260   
##  ────────────────────────────────────────────────── 
##    Note. items 'x39', 'x41', and 'x45'
##    correlate negatively with the total scale
##    and probably should be reversed
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{RA\_results\_rv }\OtherTok{=} \FunctionTok{reliability}\NormalTok{(athletes,}
                            \AttributeTok{vars =} \FunctionTok{c}\NormalTok{(}\StringTok{\textquotesingle{}x1\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x2\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x4\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x10\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x13\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x39\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x41\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x45\textquotesingle{}}\NormalTok{),}
                            \AttributeTok{meanScale=}\ConstantTok{TRUE}\NormalTok{,}\AttributeTok{sdScale=}\ConstantTok{TRUE}\NormalTok{,}\AttributeTok{corPlot=}\ConstantTok{TRUE}\NormalTok{,}\AttributeTok{alphaItems=}\ConstantTok{TRUE}\NormalTok{,}
                            \AttributeTok{meanItems=}\ConstantTok{TRUE}\NormalTok{,}\AttributeTok{sdItems=}\ConstantTok{TRUE}\NormalTok{,}\AttributeTok{itemRestCor=}\ConstantTok{TRUE}\NormalTok{,}
                            \AttributeTok{revItems=}\FunctionTok{c}\NormalTok{(}\StringTok{\textquotesingle{}x39\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x41\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x45\textquotesingle{}}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

\hypertarget{nonadditivity-test}{%
\subsection{Nonadditivity Test}\label{nonadditivity-test}}

Nonadditivity test paves the way for reliability test, which checks
whether the item factor interact with the subject factor. Nonadditivity
test is conducted by \texttt{agricolae::nonadditivity()}.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(agricolae)}
\end{Highlighting}
\end{Shaded}

The data for nonadditivity test should be in \textbf{long-format}.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{athletes\_long }\OtherTok{=}\NormalTok{ athletes }\SpecialCharTok{\%\textgreater{}\%} \FunctionTok{mutate}\NormalTok{(}\AttributeTok{x39 =} \DecValTok{6}\SpecialCharTok{{-}}\NormalTok{x39,}
                       \AttributeTok{x41 =} \DecValTok{6}\SpecialCharTok{{-}}\NormalTok{x41,}
                       \AttributeTok{x45=} \DecValTok{6}\SpecialCharTok{{-}}\NormalTok{x45) }\SpecialCharTok{\%\textgreater{}\%} 
  \FunctionTok{pivot\_longer}\NormalTok{(}\AttributeTok{cols =} \FunctionTok{everything}\NormalTok{(),}
                    \AttributeTok{names\_prefix =} \StringTok{\textquotesingle{}x\textquotesingle{}}\NormalTok{,}
                    \AttributeTok{names\_to =} \StringTok{\textquotesingle{}item\textquotesingle{}}\NormalTok{,}
                    \AttributeTok{values\_to =} \StringTok{\textquotesingle{}score\textquotesingle{}}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%} 
  \FunctionTok{mutate}\NormalTok{(}\AttributeTok{subj =} \FunctionTok{rep}\NormalTok{(}\DecValTok{1}\SpecialCharTok{:}\FunctionTok{nrow}\NormalTok{(athletes),}\AttributeTok{each =} \FunctionTok{ncol}\NormalTok{(athletes)))}
\end{Highlighting}
\end{Shaded}

For nonadditivity test, first construct a model without interaction
term. Then, the \texttt{nonadditivity()} receives four parameters.

\begin{itemize}
\tightlist
\item
  \texttt{y}: Answer of the experimental unit, which is the score for
  each item.
\item
  \texttt{factor1}: \textbf{subject} factor (first treatment applied to
  each experimental unit).
\item
  \texttt{factor2}: \textbf{item} factor (second treatment applied to
  each experimental unit).
\item
  \texttt{df}: Degrees of freedom of the experimental error
\item
  \texttt{MSerror}: Means square error of the experimental
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{model\_nonadd }\OtherTok{=} \FunctionTok{lm}\NormalTok{(score }\SpecialCharTok{\textasciitilde{}}\NormalTok{ subj}\SpecialCharTok{+}\FunctionTok{as.factor}\NormalTok{(item),athletes\_long)}
\NormalTok{MSerror }\OtherTok{=} \FunctionTok{deviance}\NormalTok{(model\_nonadd)}\SpecialCharTok{/}\FunctionTok{df.residual}\NormalTok{(model\_nonadd)}
\NormalTok{res }\OtherTok{=}  \FunctionTok{with}\NormalTok{(athletes\_long, }\FunctionTok{nonadditivity}\NormalTok{(score,subj,item, }\FunctionTok{df.residual}\NormalTok{(model\_nonadd), MSerror))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
## Tukey's test of nonadditivity
## score 
## 
## P : -55.72362
## Q : 330.7249 
## 
## Analysis of Variance Table
## 
## Response: residual
##                 Df Sum Sq Mean Sq F value   Pr(>F)   
## Nonadditivity    1    9.4  9.3888  6.7735 0.009307 **
## Residuals     2486 3445.9  1.3861                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}

\hypertarget{icc}{%
\subsection{ICC}\label{icc}}

Note that package ``\texttt{psych}'' is used here. ICC is also available
in other packages (``\texttt{psy}'', ``\texttt{irr}''), but the
functions would be used quite differently.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(psych)}
\end{Highlighting}
\end{Shaded}

Note that whenever you use the data to carry reliability analysis, the
reversely-coded items should be corrected.

In \texttt{ICC()}, specify parameter \texttt{check.keys\ =\ TRUE} to
reverse those items that do not correlate with total score (with
alerting message and reversed items). Note that this is not done by
default.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{ICC\_results }\OtherTok{=} \FunctionTok{ICC}\NormalTok{(athletes,}\AttributeTok{check.keys =}\NormalTok{ T)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Some items were negatively correlated with total scale and were automatically reversed.
##  This is indicated by a negative sign for the variable name.
\end{verbatim}

\begin{verbatim}
##  reversed items
##    x39 x41 x45
\end{verbatim}

\texttt{ICC()} can be designated to an object, and the most important
returned values are:

\begin{itemize}
\tightlist
\item
  \texttt{results}: A matrix of 6 rows and 8 columns, including the
  ICCs, \(F\) test, \(p\) values, and confidence limits
\item
  \texttt{stats}: The anova statistics (converted from lmer if using
  lmer)
\end{itemize}

Types of ICC are:

\begin{itemize}
\tightlist
\item
  ICC1: single random factor
\item
  ICC2: two-way random factor
\item
  ICC3: two-way mixed factor
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{ICC\_results}\SpecialCharTok{$}\NormalTok{results  }
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##                          type       ICC        F df1  df2           p
## Single_raters_absolute   ICC1 0.1030658 1.919272 311 2184 7.30497e-17
## Single_random_raters     ICC2 0.1115472 2.097898 311 2177 1.26596e-21
## Single_fixed_raters      ICC3 0.1206760 2.097898 311 2177 1.26596e-21
## Average_raters_absolute ICC1k 0.4789692 1.919272 311 2184 7.30497e-17
## Average_random_raters   ICC2k 0.5011019 2.097898 311 2177 1.26596e-21
## Average_fixed_raters    ICC3k 0.5233324 2.097898 311 2177 1.26596e-21
##                         lower bound upper bound
## Single_raters_absolute   0.07308939   0.1382257
## Single_random_raters     0.08104023   0.1471864
## Single_fixed_raters      0.08910136   0.1575172
## Average_raters_absolute  0.38681209   0.5620130
## Average_random_raters    0.41366008   0.5799578
## Average_fixed_raters     0.43900148   0.5993178
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{ICC\_results}\SpecialCharTok{$}\NormalTok{stats  }\CommentTok{\#the ANOVA results}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##           subjects       Judges    Residual
## df    3.110000e+02 7.000000e+00 2177.000000
## SumSq 8.002308e+02 2.578900e+02 2670.108979
## MS    2.573089e+00 3.684144e+01    1.226508
## F     2.097898e+00 3.003765e+01          NA
## p     1.265960e-21 7.229148e-40          NA
\end{verbatim}

\hypertarget{survival-analysis}{%
\section{Survival Analysis}\label{survival-analysis}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(survival)}
\FunctionTok{library}\NormalTok{(survminer)}
\FunctionTok{library}\NormalTok{(ggfortify)}
\end{Highlighting}
\end{Shaded}

Dataset ``lung'' in package ``survival'' is of interest, which contains
the information about survival in patients with advanced lung cancer
from the North Central Cancer Treatment Group. Variables include:

\begin{itemize}
\tightlist
\item
  time: Survival time in days
\item
  status: censoring status \texttt{1=censored}, \texttt{2=dead}
\item
  sex: \texttt{Male=1} \texttt{Female=2}
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{attach}\NormalTok{(lung)}
\end{Highlighting}
\end{Shaded}

\hypertarget{survival-model}{%
\subsection{Survival Model}\label{survival-model}}

\hypertarget{survival-object}{%
\subsubsection{Survival Object}\label{survival-object}}

\texttt{survival::Surv()} creates a survival object, with censored cases
marked with ``\texttt{+}'' following up censoring time and death cases
simply showing the death time. The result is usually used as a response
variable in a model formula. Two most important parameters:

\begin{itemize}
\tightlist
\item
  \texttt{time} :For right censored data, this is the follow up time.
  For interval data, the first argument is the starting time for the
  interval.
\item
  \texttt{event}: The status indicator, normally \texttt{0=alive},
  \texttt{1=dead}. Other choices are \texttt{TRUE/FALSE}
  (\texttt{TRUE\ =\ death}) or \texttt{1/2} (\texttt{2=death}).
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{survobj }\OtherTok{=} \FunctionTok{Surv}\NormalTok{(time, status}\SpecialCharTok{==}\DecValTok{2}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\hypertarget{overall-model-1}{%
\subsubsection{Overall Model}\label{overall-model-1}}

Use \texttt{survival::survfit(formula,data)} to fit the model. For the
\emph{overall} survival analysis, just specify the null hypothesis with
\texttt{Surv\textasciitilde{}1}, i.e., NO groups.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{sfit }\OtherTok{\textless{}{-}} \FunctionTok{survfit}\NormalTok{(}\FunctionTok{Surv}\NormalTok{(time, status}\SpecialCharTok{==}\DecValTok{2}\NormalTok{)}\SpecialCharTok{\textasciitilde{}}\DecValTok{1}\NormalTok{, }\AttributeTok{data=}\NormalTok{lung)}
\end{Highlighting}
\end{Shaded}

The fitted survival model itself will tell you the results, with
observations, events, median and 95\% CI.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{sfit}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Call: survfit(formula = Surv(time, status == 2) ~ 1, data = lung)
## 
##        n events median 0.95LCL 0.95UCL
## [1,] 228    165    310     285     363
\end{verbatim}

If more details are needed, i.e., the instantaneous events and estimates
of survival, std.err, 95\% CI, just \texttt{summary(sfit)}. Moreover, if
\texttt{time} is designated, then the \texttt{summary()} function will
return the \emph{estimated} results.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{summary}\NormalTok{(sfit,}\AttributeTok{time =} \DecValTok{200}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Call: survfit(formula = Surv(time, status == 2) ~ 1, data = lung)
## 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##   200    144      72     0.68  0.0311        0.622        0.744
\end{verbatim}

\hypertarget{grouped-model}{%
\subsubsection{Grouped Model}\label{grouped-model}}

If grouping is of interest, specify it in the formula with
\texttt{Surv\textasciitilde{}group}.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{sfit\_sex }\OtherTok{=} \FunctionTok{survfit}\NormalTok{(}\FunctionTok{Surv}\NormalTok{(time,status}\SpecialCharTok{==}\DecValTok{2}\NormalTok{)}\SpecialCharTok{\textasciitilde{}}\NormalTok{sex)}
\NormalTok{sfit\_sex}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Call: survfit(formula = Surv(time, status == 2) ~ sex)
## 
##         n events median 0.95LCL 0.95UCL
## sex=1 138    112    270     212     310
## sex=2  90     53    426     348     550
\end{verbatim}

It's ideal that the 95\% CI of groups doesn't intersect, and a
significant result can be obtained instantly. However, that's often not
the case; take a step further and use Cox regression to see exactly.

\hypertarget{cox-regression}{%
\subsection{Cox Regression}\label{cox-regression}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{coxph}\NormalTok{(}\FunctionTok{Surv}\NormalTok{(time, status}\SpecialCharTok{==}\DecValTok{2}\NormalTok{)}\SpecialCharTok{\textasciitilde{}}\NormalTok{sex)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Call:
## coxph(formula = Surv(time, status == 2) ~ sex)
## 
##        coef exp(coef) se(coef)      z       p
## sex -0.5310    0.5880   0.1672 -3.176 0.00149
## 
## Likelihood ratio test=10.63  on 1 df, p=0.001111
## n= 228, number of events= 165
\end{verbatim}

where ``\texttt{exp(coef)}'' is the HR (hazard ratio)， which measures
the relative risk between two groups.

Theoretically, it's better only with binary variables in Cox regression.
However, for categorical variables with more than 2 levels, use
``\texttt{as.factor(cat\_var)}'' with ``\texttt{model\ =\ TRUE}'' to
generate corresponding dummy variables. For continuous variables,
they'll be included into the regression model as usual, but the model
will be plotted with expected value of continuous variables.

The simpler version of such comparison can be accomplished by
``\texttt{survival::survdiff()}''. Both of the functions have the same
formula. From ``\texttt{survdiff()}'', we can get the intermediate
results when computing the log rank, or the chi-square test. However,
this wouldn't return the coefficient. Jointly speaking, the combined
results of two functions emphasize the fact that the log rank test
(named Mantel-Haenszel test), is a chi-square test in essence.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{surv\_diff }\OtherTok{=} \FunctionTok{survdiff}\NormalTok{(}\FunctionTok{Surv}\NormalTok{(time, status}\SpecialCharTok{==}\DecValTok{2}\NormalTok{)}\SpecialCharTok{\textasciitilde{}}\NormalTok{sex)}
\NormalTok{surv\_diff}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Call:
## survdiff(formula = Surv(time, status == 2) ~ sex)
## 
##         N Observed Expected (O-E)^2/E (O-E)^2/V
## sex=1 138      112     91.6      4.55      10.3
## sex=2  90       53     73.4      5.68      10.3
## 
##  Chisq= 10.3  on 1 degrees of freedom, p= 0.001
\end{verbatim}

\hypertarget{surv-plots}{%
\subsection{Surv Plots}\label{surv-plots}}

Use ``\texttt{ggsurvplot(model,data)}'' to visualize the survival
analysis. Note that if the ``\texttt{data}'' wasn't passed into
\texttt{survfit()}, an error will be raised.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{ggsurvplot}\NormalTok{(sfit\_sex,}\AttributeTok{data =}\NormalTok{ lung,}
           \AttributeTok{conf.int=}\ConstantTok{TRUE}\NormalTok{, }\AttributeTok{pval=}\ConstantTok{TRUE}\NormalTok{, }
           \AttributeTok{risk.table=}\StringTok{\textquotesingle{}abs\_pct\textquotesingle{}}\NormalTok{, }\CommentTok{\# risk table below the Kaplan{-}Meier curve(s)}
           \AttributeTok{legend.labs=}\FunctionTok{c}\NormalTok{(}\StringTok{"Male"}\NormalTok{, }\StringTok{"Female"}\NormalTok{), }\AttributeTok{legend.title=}\StringTok{"Sex"}\NormalTok{,}
           \AttributeTok{title=}\StringTok{"Kaplan{-}Meier Curve for Lung Cancer Survival"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-220-1.pdf}

Moreover, the parameter ``\texttt{fun}'' is quite useful, which will
define a transformation of the survival curve.

\begin{itemize}
\tightlist
\item
  ``pct'': for survival probability in percentage, \textbf{by default}.
\item
  ``event'': plots cumulative events, \(f(y)=1-y\).
\item
  ``cumhaz'': plots the cumulative hazard function, \(f(y)=-\log(y)\).
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{ggsurvplot}\NormalTok{(sfit\_sex,}\AttributeTok{data =}\NormalTok{ lung,}
           \AttributeTok{conf.int=}\ConstantTok{TRUE}\NormalTok{, }\AttributeTok{pval=}\ConstantTok{TRUE}\NormalTok{, }
           \CommentTok{\# risk.table=\textquotesingle{}abs\_pct\textquotesingle{},}
           \AttributeTok{legend.labs=}\FunctionTok{c}\NormalTok{(}\StringTok{"Male"}\NormalTok{, }\StringTok{"Female"}\NormalTok{), }\AttributeTok{legend.title=}\StringTok{"Sex"}\NormalTok{,}
           \AttributeTok{fun =} \StringTok{\textquotesingle{}event\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-221-1.pdf}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{ggsurvplot}\NormalTok{(sfit\_sex,}\AttributeTok{data =}\NormalTok{ lung,}
           \AttributeTok{conf.int=}\ConstantTok{TRUE}\NormalTok{, }\AttributeTok{pval=}\ConstantTok{TRUE}\NormalTok{, }
           \CommentTok{\# risk.table=\textquotesingle{}abs\_pct\textquotesingle{},}
           \AttributeTok{legend.labs=}\FunctionTok{c}\NormalTok{(}\StringTok{"Male"}\NormalTok{, }\StringTok{"Female"}\NormalTok{), }\AttributeTok{legend.title=}\StringTok{"Sex"}\NormalTok{,}
           \AttributeTok{fun =} \StringTok{\textquotesingle{}cumhaz\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-222-1.pdf}

\hypertarget{clustering}{%
\section{Clustering}\label{clustering}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(parameters)}
\FunctionTok{library}\NormalTok{(}\StringTok{"factoextra"}\NormalTok{) }
\end{Highlighting}
\end{Shaded}

\hypertarget{hierarchical-clustering}{%
\subsection{Hierarchical Clustering}\label{hierarchical-clustering}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{automobile }\OtherTok{=} \FunctionTok{import}\NormalTok{(}\StringTok{"data13{-}01.xlsx"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%} 
\NormalTok{  dplyr}\SpecialCharTok{::}\FunctionTok{select}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\StringTok{\textquotesingle{}type\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}price\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}engine\_s\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}horsepow\textquotesingle{}}\NormalTok{,}
           \StringTok{\textquotesingle{}wheelbas\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}width\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}length\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}curb\_wgt\textquotesingle{}}\NormalTok{,}
           \StringTok{\textquotesingle{}fuel\_cap\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}mpg\textquotesingle{}}\NormalTok{)) }\SpecialCharTok{\%\textgreater{}\%} \FunctionTok{drop\_na}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%} \FunctionTok{scale}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## ✔ Successfully imported: 157 obs. of 27 variables
\end{verbatim}

Use ``\texttt{hclust()}'' from base R and transmit a
\textbf{dissimilarity} structure produced by ``\texttt{dist()}''.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{distMat }\OtherTok{=} \FunctionTok{dist}\NormalTok{(automobile, }\AttributeTok{method =} \StringTok{"euclidean"}\NormalTok{) }\CommentTok{\# Euclidean distance matrix}
\NormalTok{hc }\OtherTok{=} \FunctionTok{hclust}\NormalTok{(distMat)  }\CommentTok{\# hierarchical clustering, using the default method}
\end{Highlighting}
\end{Shaded}

Use ``\texttt{plot(hclust\_obj)}'' to visualize the hierarchical
clustering. Note that you should specify ``\texttt{hang\ =\ -1}'' to
ensure all nodes touch the same horizontal line and looks decent. If the
sample isn't that large, set ``\texttt{labels\ =\ TRUE}'' (by default)
to see the corresponding observations.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{plot}\NormalTok{(hc,}\AttributeTok{labels=}\ConstantTok{FALSE}\NormalTok{,}\AttributeTok{hang =} \SpecialCharTok{{-}}\DecValTok{1}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-226-1.pdf}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{plot}\NormalTok{(hc,}\AttributeTok{labels=}\ConstantTok{FALSE}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-226-2.pdf}

Moreover, you can draw colored dendrogram with grouping.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{fviz\_dend}\NormalTok{(hc,}\AttributeTok{k=}\DecValTok{3}\NormalTok{, }\AttributeTok{rect =}\NormalTok{ T)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-227-1.pdf}

Also, you can also check how many members for each cluster and make a
frequency table.Note that the grouping might change if we change cluster
number.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{memberLabels }\OtherTok{=} \FunctionTok{cutree}\NormalTok{(hc,}\DecValTok{3}\NormalTok{)}
\FunctionTok{table}\NormalTok{(memberLabels)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## memberLabels
##  1  2  3 
## 71 71 10
\end{verbatim}

Then, we can get the centers of cluters by ``\texttt{mean}'' summary
statistics. Use ``\texttt{aggregate()}'' to achieve that.

\begin{itemize}
\tightlist
\item
  \texttt{x}: usually a data frame.
\item
  \texttt{by}: a list of grouping elements, each \textbf{as long} as the
  variables in the data frame x. The elements are coerced to factors
  before use.
\item
  \texttt{FUN}: a function to compute the summary statistics which can
  be applied to all data subsets.
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{aggregate}\NormalTok{(automobile,}\FunctionTok{list}\NormalTok{(memberLabels),mean)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##   Group.1       type      price   engine_s   horsepow   wheelbas      width
## 1       1 -0.4362472 -0.4985325 -0.7231739 -0.6579479 -0.6259158 -0.6459818
## 2       2  0.5201409  0.1193177  0.5017194  0.3293009  0.6601116  0.5979645
## 3       3 -0.5956452  2.6924245  1.5723270  2.3333938 -0.2427902  0.3409229
##        length   curb_wgt   fuel_cap        mpg
## 1 -0.65105171 -0.7853229 -0.7200528  0.6620751
## 2  0.65553921  0.7236353  0.6270241 -0.5592928
## 3 -0.03186123  0.4379815  0.6605042 -0.7297543
\end{verbatim}

Moreover, we can use Silhouette plot to check whether some cases are
outliers.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(cluster)}
\FunctionTok{plot}\NormalTok{(}\FunctionTok{silhouette}\NormalTok{(}\FunctionTok{cutree}\NormalTok{(hc,}\DecValTok{3}\NormalTok{), distMat))}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-230-1.pdf}

\hypertarget{non-hierarchical-clustering-k-means}{%
\subsection{Non-Hierarchical Clustering
(K-Means)}\label{non-hierarchical-clustering-k-means}}

\hypertarget{computation}{%
\subsubsection{Computation}\label{computation}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{athlete }\OtherTok{=} \FunctionTok{import}\NormalTok{(}\StringTok{"data13{-}02.xlsx"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}\NormalTok{ dplyr}\SpecialCharTok{::}\FunctionTok{select}\NormalTok{(}\SpecialCharTok{{-}}\DecValTok{1}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## ✔ Successfully imported: 10 obs. of 4 variables
\end{verbatim}

\begin{itemize}
\tightlist
\item
  \texttt{x}: numeric matrix-like data
\item
  \texttt{centers}: either the number of clusters, say kk, or a set of
  initial (distinct) cluster centres. If a number, a random set of
  (distinct) rows in x is chosen as the initial centres.
\item
  \texttt{iter.max}: the maximum number of iterations allowed.
\item
  \texttt{nstart}: if centers is a number, how many random sets should
  be chosen. Trying several random starts is often recommended.
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{kmean\_results }\OtherTok{=} \FunctionTok{kmeans}\NormalTok{(athlete, }\AttributeTok{centers =} \DecValTok{4}\NormalTok{, }
                       \AttributeTok{iter.max =} \DecValTok{10}\NormalTok{, }\AttributeTok{nstart =} \DecValTok{1}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\texttt{kmeans()} returns an object of class ``\texttt{kmeans}'', a list
with at least the following components:

\begin{itemize}
\tightlist
\item
  \texttt{cluster} : A vector of integers (from 1:k) indicating the
  cluster to which each point is allocated.
\item
  \texttt{centers}: A matrix of cluster centres.
\item
  \texttt{totss}: The total sum of squares.
\item
  \texttt{withinss}: Vector of within-cluster sum of squares, one
  component per cluster.
\item
  \texttt{tot.withinss} :Total within-cluster sum of squares,
  i.e.~sum(withinss).
\item
  \texttt{betweenss}: The between-cluster sum of squares,
  i.e.~\texttt{totss}-\texttt{tot.withinss}.
\item
  \texttt{size}: The number of points in each cluster.
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{kmean\_results}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## K-means clustering with 4 clusters of sizes 2, 2, 3, 3
## 
## Cluster means:
##         x1       x2       x3
## 1 120.5000 19.00000 44.50000
## 2 124.5000 20.00000 44.50000
## 3 121.0000 17.00000 41.66667
## 4 121.6667 18.33333 43.00000
## 
## Clustering vector:
##  [1] 2 4 3 2 4 1 3 4 3 1
## 
## Within cluster sum of squares by cluster:
## [1] 1.000000 1.000000 2.666667 1.333333
##  (between_SS / total_SS =  88.4 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"
\end{verbatim}

\hypertarget{visualization-2}{%
\subsection{Visualization}\label{visualization-2}}

Use \texttt{factoextra::fviz\_cluster(object,\ data)}. For more details
about useful parameters, see the help document. Remember that
\texttt{object} and \texttt{data} are two most indispensable parameters.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{fviz\_cluster}\NormalTok{(kmean\_results, }\AttributeTok{data =}\NormalTok{ athlete)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-234-1.pdf}

\hypertarget{no.-of-clusters}{%
\subsection{No.~of Clusters}\label{no.-of-clusters}}

\hypertarget{within-cluster-ss}{%
\subsubsection{Within-Cluster SS}\label{within-cluster-ss}}

Looking for the ``elbow'' where the SS starts to slow down its drop.
Here suggests 2 or 3.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{fviz\_nbclust}\NormalTok{(automobile, kmeans, }\AttributeTok{method =} \StringTok{"wss"}\NormalTok{, }\AttributeTok{print.summary=}\ConstantTok{TRUE}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-235-1.pdf}

\hypertarget{silhouette-method}{%
\subsubsection{Silhouette Method}\label{silhouette-method}}

Looking for the value that sets the highest average silhouette width,
which indicates the optimal number of clusters.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{fviz\_nbclust}\NormalTok{(automobile, kmeans, }\AttributeTok{method =} \StringTok{"silhouette"}\NormalTok{,}\AttributeTok{print.summary=}\ConstantTok{TRUE}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-236-1.pdf}

\hypertarget{majority-rule}{%
\subsubsection{Majority Rule}\label{majority-rule}}

\texttt{n\_clusters()} is the main function to find out the optimal
numbers of clusters present in the data based on the maximum consensus
of a large number of methods.Note that the \texttt{n\_clusters()}
function will do the normalization automatically (by default).

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{n\_clusters}\NormalTok{(}\FunctionTok{data.frame}\NormalTok{(automobile))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## # Method Agreement Procedure:
## 
## The choice of 2 clusters is supported by 8 (27.59%) methods out of 29 (Elbow, Silhouette, Ch, Ratkowsky, Mcclain, SDindex, Mixture (VEV), Mixture (EEV)).
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{plot}\NormalTok{(}\FunctionTok{n\_clusters}\NormalTok{(}\FunctionTok{data.frame}\NormalTok{(automobile)))}
\end{Highlighting}
\end{Shaded}

\includegraphics{psy-stat-II-2023_files/figure-latex/unnamed-chunk-237-1.pdf}

\hypertarget{two-step-clustering}{%
\subsubsection{Two Step Clustering}\label{two-step-clustering}}

Two-Step Clustering refers to using hierarchical methods first to obtain
the number of clusters and their centroids. Then use these as inputs to
run non-hierarchical analysis.

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# two step clustering in R, not required}
\FunctionTok{library}\NormalTok{(prcr)}
\NormalTok{d }\OtherTok{\textless{}{-}}\NormalTok{ pisaUSA15}
\NormalTok{m3 }\OtherTok{\textless{}{-}} \FunctionTok{create\_profiles\_cluster}\NormalTok{(d,}
\NormalTok{                              broad\_interest, enjoyment, instrumental\_mot, self\_efficacy,}
                              \AttributeTok{n\_profiles =} \DecValTok{3}\NormalTok{)}
\FunctionTok{summary}\NormalTok{(m3)}
\end{Highlighting}
\end{Shaded}


\end{document}
