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

\title{Correlation \& Distance}
\author{Rui Zhou}
\date{2026-05-26}

\begin{document}
\maketitle

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

「相关」是统计入门绕不开的概念，但实际上有三种相关常被混用：

\begin{itemize}
\tightlist
\item
  \textbf{零阶相关} \(r(X, Y)\)：直接算 \(X\) 和 \(Y\)
  的相关系数，不控制任何其他变量。
\item
  \textbf{偏相关}（partial correlation）\(r(X, Y \mid Z)\)：先把 \(X\)
  和 \(Y\) 各自对 \(Z\) 回归取残差，再算两个残差的相关------这是「控制
  \(Z\) 之后」\(X\) 和 \(Y\) 的纯净相关。
\item
  \textbf{半偏相关 / 部分相关}（part / semi-partial
  correlation）\(r(X, Y \cdot Z)\)：只把 \(Y\) 对 \(Z\)
  回归取残差，\(X\) 不动------这是「\(Z\) 已经解释了 \(Y\)
  那部分，剩下的部分跟 \(X\) 的相关」。多元回归里每个解释变量对 \(R^2\)
  的「独特贡献」用的就是它。
\end{itemize}

\begin{concept}[三种相关的关系]
设回归 $Y = \beta_0 + \beta_1 X + \beta_2 Z + \varepsilon$。则：
$$\text{偏相关} \; r(X, Y \mid Z) = \frac{r_{XY} - r_{XZ} r_{YZ}}{\sqrt{(1-r_{XZ}^2)(1-r_{YZ}^2)}}$$
$$\text{半偏相关}^2 = R^2_\text{full} - R^2_\text{without X}$$

实际意义：\textbf{偏相关}回答「$X$ 单独和 $Y$ 的关系有多强（剔除 $Z$ 的所有影响）」；\textbf{半偏相关}回答「在已有 $Z$ 的模型里，$X$ 能额外解释多少 $Y$」。心理学论文常说「控制人口学变量后……」，里头数学是偏相关。
\end{concept}

第二个主题是\textbf{距离}------这是聚类、PCA、判别分析的基础。最常用的几种：

\begin{itemize}
\tightlist
\item
  \textbf{欧氏距离}
  \(d(\mathbf{x}, \mathbf{y}) = \sqrt{\sum_i (x_i - y_i)^2}\)：直觉对应「直线距离」，对量纲敏感。
\item
  \textbf{马氏距离}（Mahalanobis）：欧氏距离对变量的协方差矩阵做白化校正，自动消除量纲和相关性。
\item
  \textbf{曼哈顿距离} \(\sum_i |x_i - y_i|\)：对离群值更稳健，适合高维。
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(bruceR)}
\FunctionTok{library}\NormalTok{(rstatix)}
\FunctionTok{library}\NormalTok{(tidyverse)}
\FunctionTok{library}\NormalTok{(stargazer)}
\end{Highlighting}
\end{Shaded}

\hypertarget{correlation}{%
\section{Correlation}\label{correlation}}

\hypertarget{bivariate-correlation}{%
\subsection{Bivariate Correlation}\label{bivariate-correlation}}

Here we're to discuss more about bivariate correlation when taking
groups into account. As you can see, the pooled data may interact and
neutralize the effect or relationship of two variables. However, after
we inspect that correlation under each group, surprising outcomes may
spring up!

Take dataset ``car\_sales.sav'' as an example, which you have been quite
similar to ``mtcars''. We're interested in whether or not people value
fuel-efficient cars more.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{carsales }\OtherTok{=} \FunctionTok{import}\NormalTok{(}\StringTok{"car\_sales.sav"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## ✔ Successfully imported: 157 obs. of 26 variables
\end{verbatim}

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

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{ggplot}\NormalTok{(}\AttributeTok{data =}\NormalTok{ carsales, }\AttributeTok{mapping =} \FunctionTok{aes}\NormalTok{(}\AttributeTok{x =}\NormalTok{ mpg,}\AttributeTok{y =}\NormalTok{ sales))}\SpecialCharTok{+}
  \FunctionTok{geom\_point}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\begin{figure}

{\centering \includegraphics[width=0.7\linewidth]{r-correlation-distance_files/figure-latex/unnamed-chunk-3-1} 

}

\caption{Scatter Plot of mpg and sales with ALL Observations}\label{fig:unnamed-chunk-3}
\end{figure}

At first glance, there are apparently some outliers. Moreover, the
scatter points seem to cluster in the bottom-left, possibly a skewed
distribution instead of normal! Nevertheless, those don't bother us to
check the overall correlation first.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{cor}\NormalTok{(sales,mpg,}\AttributeTok{use =} \StringTok{\textquotesingle{}c\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] -0.01668058
\end{verbatim}

The correlation is not high in magnitude. Here, use ``\texttt{cor()}''
for simplicity. Note that for data with missing values, specify
parameter
``\texttt{use\ =\ \textquotesingle{}complete.obs\textquotesingle{}}''
(or ``\texttt{use\ =\ \textquotesingle{}c\textquotesingle{}}'' as
abbreviation) to compute only with complete cases.

Admittedly, ``\texttt{cor.test()}'' will also return the correlation
coefficient, along with outcomes with hypothesis testing. However, the
latter makes it to be complex unnecessarily! (Good thing is that,
``\texttt{cor.test()}'' will automatically wipe out cases with missing
values.)

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{cor.test}\NormalTok{(mpg,sales)}
\end{Highlighting}
\end{Shaded}

Then, we're to deal with outliers and skewness, then to recompute
correlation.

Good news is that, \(\log\) transformation helps when it comes to skewed
distribution, and outliers will get contained under \(\log\)
transformation at the same time.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{cor}\NormalTok{(}\FunctionTok{log}\NormalTok{(mpg),}\FunctionTok{log}\NormalTok{(sales),}\AttributeTok{use =} \StringTok{\textquotesingle{}c\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

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

The correlation coefficient is improved in magnitude, but not
significant after all.

Then, here comes what's the most important point. When faced with a
brand-new data, before conducting any computation and hypothesis
testing, getting an overall picture of the data is the most important
thing to do!

After observation, it's likely that cars whose model are ``Metro'' or
``F-Series'' are special compared to others. Accordingly, we're to group
the data and then recompute correlation.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{sales\_some }\OtherTok{=}\NormalTok{ sales[model}\SpecialCharTok{!=}\StringTok{"Metro"}\SpecialCharTok{\&}\NormalTok{model}\SpecialCharTok{!=}\StringTok{"F{-}Series"}\NormalTok{]}
\NormalTok{mpg\_some }\OtherTok{=}\NormalTok{ mpg[model}\SpecialCharTok{!=}\StringTok{"Metro"}\SpecialCharTok{\&}\NormalTok{model}\SpecialCharTok{!=}\StringTok{"F{-}Series"}\NormalTok{]}
\NormalTok{type\_some }\OtherTok{=} \FunctionTok{as.factor}\NormalTok{(type[model}\SpecialCharTok{!=}\StringTok{"Metro"}\SpecialCharTok{\&}\NormalTok{model}\SpecialCharTok{!=}\StringTok{"F{-}Series"}\NormalTok{])}

\NormalTok{df }\OtherTok{=} \FunctionTok{data.frame}\NormalTok{(sales\_some,mpg\_some,type\_some)}
\NormalTok{df }\SpecialCharTok{\%\textgreater{}\%}
  \FunctionTok{ggplot}\NormalTok{(}\FunctionTok{aes}\NormalTok{(mpg\_some, sales\_some,}\AttributeTok{color =}\NormalTok{ type\_some)) }\SpecialCharTok{+}
  \FunctionTok{geom\_point}\NormalTok{(}\AttributeTok{alpha=}\FloatTok{0.8}\NormalTok{, }\AttributeTok{size=}\FloatTok{0.8}\NormalTok{) }\SpecialCharTok{+}
  \FunctionTok{geom\_smooth}\NormalTok{(}\AttributeTok{method=}\StringTok{"lm"}\NormalTok{,}\AttributeTok{se=}\ConstantTok{FALSE}\NormalTok{) }\SpecialCharTok{+}
  \FunctionTok{ylab}\NormalTok{(}\StringTok{\textquotesingle{}Sales(k)\textquotesingle{}}\NormalTok{) }\SpecialCharTok{+}
  \FunctionTok{xlab}\NormalTok{(}\StringTok{\textquotesingle{}MPG\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{r-correlation-distance_files/figure-latex/unnamed-chunk-7-1.pdf}

Use graphs to think! The categorized data plays an important role here,
and unfolds the underlying correlation. Based on the prior observation,
then check correlation quantitatively.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{cor.test}\NormalTok{(sales\_some[type\_some}\SpecialCharTok{==}\StringTok{"1"}\NormalTok{],mpg\_some[type\_some}\SpecialCharTok{==}\StringTok{"1"}\NormalTok{])}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Pearson's product-moment correlation
## 
## data:  sales_some[type_some == "1"] and mpg_some[type_some == "1"]
## t = 0.070377, df = 38, p-value = 0.9443
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.3011645  0.3217809
## sample estimates:
##        cor 
## 0.01141584
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{cor.test}\NormalTok{(sales\_some[type\_some}\SpecialCharTok{==}\StringTok{"0"}\NormalTok{],mpg\_some[type\_some}\SpecialCharTok{==}\StringTok{"0"}\NormalTok{])}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## 
##  Pearson's product-moment correlation
## 
## data:  sales_some[type_some == "0"] and mpg_some[type_some == "0"]
## t = 2.8695, df = 112, p-value = 0.004915
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.0817139 0.4251484
## sample estimates:
##       cor 
## 0.2616958
\end{verbatim}

Finally, don't forget to

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

\hypertarget{partial-part-correlation}{%
\subsection{Partial \& Part
Correlation}\label{partial-part-correlation}}

\hypertarget{background-knowledge}{%
\subsubsection{Background Knowledge}\label{background-knowledge}}

Above all, I'd like to clarify one concept that we've been familiar with
but without mentioning this concept. Zero-order correlation refers to
the correlation between \(X\) and \(Y\) without controlling other
covariates. Traditionally, Pearson and Spearman correlations are typical
of zero-order 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!

Even with more covariates, conduct \(A\) and \(B\) on all the covariates
and then conpute the correlation with their residual parts. Done!

\hypertarget{examples}{%
\subsubsection{Examples}\label{examples}}

We first man-make a data that vividly shows the differences between
partial and zero-order correlation.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{DV }\OtherTok{=} \FunctionTok{c}\NormalTok{(}\DecValTok{1}\SpecialCharTok{:}\DecValTok{8}\NormalTok{, }\DecValTok{8}\SpecialCharTok{:}\DecValTok{11}\NormalTok{)}
\NormalTok{IV }\OtherTok{=} \FunctionTok{c}\NormalTok{(}\DecValTok{4}\SpecialCharTok{:}\DecValTok{1}\NormalTok{,}\DecValTok{8}\SpecialCharTok{:}\DecValTok{5}\NormalTok{,}\DecValTok{12}\SpecialCharTok{:}\DecValTok{9}\NormalTok{)}
\NormalTok{covariate }\OtherTok{=} \FunctionTok{c}\NormalTok{(}\FunctionTok{rep}\NormalTok{(}\DecValTok{0}\NormalTok{,}\DecValTok{4}\NormalTok{), }\FunctionTok{rep}\NormalTok{(}\DecValTok{4}\NormalTok{,}\DecValTok{4}\NormalTok{), }\FunctionTok{rep}\NormalTok{(}\DecValTok{7}\NormalTok{,}\DecValTok{4}\NormalTok{))}
\NormalTok{studiedData }\OtherTok{=} \FunctionTok{data.frame}\NormalTok{(DV,IV,covariate)}

\NormalTok{studiedData }\SpecialCharTok{|\textgreater{}} \FunctionTok{ggplot}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x =}\NormalTok{ IV,}\AttributeTok{y =}\NormalTok{ DV))}\SpecialCharTok{+}
  \FunctionTok{geom\_point}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\begin{figure}

{\centering \includegraphics[width=0.7\linewidth]{r-correlation-distance_files/figure-latex/unnamed-chunk-12-1} 

}

\caption{Scatter Plot of X and Y}\label{fig:unnamed-chunk-12}
\end{figure}

Clearly, considering zero-order correlation, \(X\) is strictly
negatively-correlated with \(Y\), the covariate is strictly
positively-correlated with \(Y\). However, \(X\) shows partially
positive correlation with \(Y\) overall.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{cor}\NormalTok{(IV,DV)}
\end{Highlighting}
\end{Shaded}

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

If you're to study the partial correlation, things will be different.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(ppcor)}
\FunctionTok{pcor}\NormalTok{(studiedData)}
\end{Highlighting}
\end{Shaded}

\begin{table}[!htbp] \centering 
  \caption{Partial Correlation between Variables} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}} cccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & DV & IV & covariate \\ 
\hline \\[-1.8ex] 
DV & $1$ & $$-$0.972$ & $0.991$ \\ 
IV & $$-$0.972$ & $1$ & $0.993$ \\ 
covariate & $0.991$ & $0.993$ & $1$ \\ 
\hline \\[-1.8ex] 
\end{tabular} 
\end{table}

One more example. 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}[]
\CommentTok{\# example: a covariate can "make" a correlation that should not exist}
\NormalTok{currentData }\OtherTok{\textless{}{-}} \FunctionTok{import}\NormalTok{(}\StringTok{"health\_funding.sav"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## ✔ Successfully imported: 50 obs. of 4 variables
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{attach}\NormalTok{(currentData)}
\NormalTok{studiedData }\OtherTok{=} \FunctionTok{data.frame}\NormalTok{(disease,funding,visits)}
\end{Highlighting}
\end{Shaded}

First of all, check overall zero-order correlation.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{cor}\NormalTok{(studiedData)}
\end{Highlighting}
\end{Shaded}

\begin{table}[!htbp] \centering 
  \caption{Zero-Order Correlation} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}} cccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & disease & funding & visits \\ 
\hline \\[-1.8ex] 
disease & $1$ & $0.737$ & $0.762$ \\ 
funding & $0.737$ & $1$ & $0.964$ \\ 
visits & $0.762$ & $0.964$ & $1$ \\ 
\hline \\[-1.8ex] 
\end{tabular} 
\end{table}

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
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{(studiedData)}\SpecialCharTok{$}\NormalTok{estimate}
\end{Highlighting}
\end{Shaded}

\begin{table}[!htbp] \centering 
  \caption{Partial Correlation} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}} cccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & disease & funding & visits \\ 
\hline \\[-1.8ex] 
disease & $1$ & $0.013$ & $0.286$ \\ 
funding & $0.013$ & $1$ & $0.920$ \\ 
visits & $0.286$ & $0.920$ & $1$ \\ 
\hline \\[-1.8ex] 
\end{tabular} 
\end{table}

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{(studiedData)}\SpecialCharTok{$}\NormalTok{estimate}
\end{Highlighting}
\end{Shaded}

\begin{table}[!htbp] \centering 
  \caption{Part Correlation} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}} cccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & disease & funding & visits \\ 
\hline \\[-1.8ex] 
disease & $1$ & $0.009$ & $0.193$ \\ 
funding & $0.004$ & $1$ & $0.622$ \\ 
visits & $0.076$ & $0.596$ & $1$ \\ 
\hline \\[-1.8ex] 
\end{tabular} 
\end{table}

And finally,

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

\hypertarget{distance}{%
\section{Distance}\label{distance}}

We need to distinguish between two kinds of distance. One is distance
between cases (or observations), the other is distance between
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 that measures the cosine of the angle between them.
    \item
      It is thus a judgment of orientation and not magnitude.
    \end{itemize}
  \end{itemize}
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{currentData }\OtherTok{=} \FunctionTok{import}\NormalTok{(}\StringTok{"data10{-}04.sav"}\NormalTok{, }\AttributeTok{sheet =} \DecValTok{1}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## ✔ Successfully imported: 12 obs. of 6 variables
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{attach}\NormalTok{(currentData)}
\CommentTok{\# only use the data that should be considered or analyzed}
\NormalTok{studiedData }\OtherTok{=} \FunctionTok{data.frame}\NormalTok{(hgrow,temp,rain,hsun,humi)}
\end{Highlighting}
\end{Shaded}

\hypertarget{correlation-1}{%
\subsection{Correlation}\label{correlation-1}}

Note that 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{(studiedData)}
\end{Highlighting}
\end{Shaded}

\begin{table}[!htbp] \centering 
  \caption{Correlation between Variables} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}} cccccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & hgrow & temp & rain & hsun & humi \\ 
\hline \\[-1.8ex] 
hgrow & $1$ & $0.983$ & $0.709$ & $0.704$ & $0.374$ \\ 
temp & $0.983$ & $1$ & $0.715$ & $0.690$ & $0.292$ \\ 
rain & $0.709$ & $0.715$ & $1$ & $0.702$ & $0.384$ \\ 
hsun & $0.704$ & $0.690$ & $0.702$ & $1$ & $$-$0.051$ \\ 
humi & $0.374$ & $0.292$ & $0.384$ & $$-$0.051$ & $1$ \\ 
\hline \\[-1.8ex] 
\end{tabular} 
\end{table}

\hypertarget{euclidean-distance}{%
\subsection{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 scaled!

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# studiedData \%\textgreater{}\% dist()}
\NormalTok{studiedData  }\SpecialCharTok{\%\textgreater{}\%} 
  \FunctionTok{scale}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%} \FunctionTok{dist}\NormalTok{() }
\end{Highlighting}
\end{Shaded}

\begin{table}[!htbp] \centering 
  \caption{Euclidean Distance between Cases} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}} ccccccccccccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 \\ 
\hline \\[-1.8ex] 
1 & $0$ & $0.925$ & $2.142$ & $3.692$ & $4.790$ & $3.904$ & $4.078$ & $5.793$ & $3.764$ & $2.536$ & $1.782$ & $0.591$ \\ 
2 & $0.925$ & $0$ & $1.288$ & $2.856$ & $4.179$ & $3.338$ & $3.756$ & $5.505$ & $3.576$ & $2.232$ & $1.433$ & $0.923$ \\ 
3 & $2.142$ & $1.288$ & $0$ & $2.149$ & $3.774$ & $3.151$ & $3.962$ & $5.710$ & $3.870$ & $2.675$ & $2.054$ & $2.192$ \\ 
4 & $3.692$ & $2.856$ & $2.149$ & $0$ & $2.558$ & $2.001$ & $3.108$ & $4.535$ & $3.518$ & $2.627$ & $2.509$ & $3.447$ \\ 
5 & $4.790$ & $4.179$ & $3.774$ & $2.558$ & $0$ & $2.307$ & $2.871$ & $2.895$ & $2.724$ & $3.151$ & $3.662$ & $4.501$ \\ 
6 & $3.904$ & $3.338$ & $3.151$ & $2.001$ & $2.307$ & $0$ & $1.265$ & $3.338$ & $1.937$ & $1.636$ & $2.236$ & $3.529$ \\ 
7 & $4.078$ & $3.756$ & $3.962$ & $3.108$ & $2.871$ & $1.265$ & $0$ & $2.696$ & $1.292$ & $1.566$ & $2.448$ & $3.629$ \\ 
8 & $5.793$ & $5.505$ & $5.710$ & $4.535$ & $2.895$ & $3.338$ & $2.696$ & $0$ & $2.761$ & $3.721$ & $4.519$ & $5.333$ \\ 
9 & $3.764$ & $3.576$ & $3.870$ & $3.518$ & $2.724$ & $1.937$ & $1.292$ & $2.761$ & $0$ & $1.654$ & $2.541$ & $3.402$ \\ 
10 & $2.536$ & $2.232$ & $2.675$ & $2.627$ & $3.151$ & $1.636$ & $1.566$ & $3.721$ & $1.654$ & $0$ & $0.967$ & $2.093$ \\ 
11 & $1.782$ & $1.433$ & $2.054$ & $2.509$ & $3.662$ & $2.236$ & $2.448$ & $4.519$ & $2.541$ & $0.967$ & $0$ & $1.374$ \\ 
12 & $0.591$ & $0.923$ & $2.192$ & $3.447$ & $4.501$ & $3.529$ & $3.629$ & $5.333$ & $3.402$ & $2.093$ & $1.374$ & $0$ \\ 
\hline \\[-1.8ex] 
\end{tabular} 
\end{table}

If the Euclidean distance between \textbf{variables} is of interest,
then \emph{transpose} your data.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{dist }\OtherTok{=}\NormalTok{ studiedData  }\SpecialCharTok{\%\textgreater{}\%} 
  \FunctionTok{scale}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%} \FunctionTok{t}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%} \FunctionTok{dist}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\begin{table}[!htbp] \centering 
  \caption{Euclidean Distance between Variables} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}} cccccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & hgrow & temp & rain & hsun & humi \\ 
\hline \\[-1.8ex] 
hgrow & $0$ & $0.605$ & $2.529$ & $2.550$ & $3.712$ \\ 
temp & $0.605$ & $0$ & $2.505$ & $2.609$ & $3.947$ \\ 
rain & $2.529$ & $2.505$ & $0$ & $2.561$ & $3.680$ \\ 
hsun & $2.550$ & $2.609$ & $2.561$ & $0$ & $4.808$ \\ 
humi & $3.712$ & $3.947$ & $3.680$ & $4.808$ & $0$ \\ 
\hline \\[-1.8ex] 
\end{tabular} 
\end{table}

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}{%
\subsection{Cosine Similarity}\label{cosine-similarity}}

Also, we can see the cosine similarity \textbf{between cases}.

Remember, if you're to study the distance between cases, transpose your
matrix of data; if you're to study the distance between variables, throw
the matrix of data into ``\texttt{lsa::cosine()}'' directly.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{studiedData }\SpecialCharTok{\%\textgreater{}\%} 
  \FunctionTok{as.matrix.data.frame}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%} \FunctionTok{t}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%} 
\NormalTok{  lsa}\SpecialCharTok{::}\FunctionTok{cosine}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%} 
  \FunctionTok{stargazer}\NormalTok{(}\AttributeTok{title =} \StringTok{\textquotesingle{}Cosine Similarity between Cases\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{table}[!htbp] \centering 
  \caption{Cosine Similarity between Cases} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}} cccccccccccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
$1$ & $0.985$ & $0.968$ & $0.886$ & $0.626$ & $0.886$ & $0.851$ & $0.632$ & $0.643$ & $0.934$ & $0.970$ & $0.994$ \\ 
$0.985$ & $1$ & $0.994$ & $0.948$ & $0.609$ & $0.901$ & $0.846$ & $0.616$ & $0.597$ & $0.931$ & $0.995$ & $0.997$ \\ 
$0.968$ & $0.994$ & $1$ & $0.974$ & $0.664$ & $0.934$ & $0.881$ & $0.671$ & $0.639$ & $0.950$ & $0.990$ & $0.986$ \\ 
$0.886$ & $0.948$ & $0.974$ & $1$ & $0.662$ & $0.927$ & $0.860$ & $0.669$ & $0.600$ & $0.913$ & $0.954$ & $0.925$ \\ 
$0.626$ & $0.609$ & $0.664$ & $0.662$ & $1$ & $0.876$ & $0.929$ & $1.000$ & $0.984$ & $0.847$ & $0.571$ & $0.609$ \\ 
$0.886$ & $0.901$ & $0.934$ & $0.927$ & $0.876$ & $1$ & $0.989$ & $0.882$ & $0.852$ & $0.991$ & $0.890$ & $0.894$ \\ 
$0.851$ & $0.846$ & $0.881$ & $0.860$ & $0.929$ & $0.989$ & $1$ & $0.934$ & $0.920$ & $0.982$ & $0.828$ & $0.847$ \\ 
$0.632$ & $0.616$ & $0.671$ & $0.669$ & $1.000$ & $0.882$ & $0.934$ & $1$ & $0.984$ & $0.853$ & $0.580$ & $0.616$ \\ 
$0.643$ & $0.597$ & $0.639$ & $0.600$ & $0.984$ & $0.852$ & $0.920$ & $0.984$ & $1$ & $0.843$ & $0.556$ & $0.610$ \\ 
$0.934$ & $0.931$ & $0.950$ & $0.913$ & $0.847$ & $0.991$ & $0.982$ & $0.853$ & $0.843$ & $1$ & $0.916$ & $0.932$ \\ 
$0.970$ & $0.995$ & $0.990$ & $0.954$ & $0.571$ & $0.890$ & $0.828$ & $0.580$ & $0.556$ & $0.916$ & $1$ & $0.990$ \\ 
$0.994$ & $0.997$ & $0.986$ & $0.925$ & $0.609$ & $0.894$ & $0.847$ & $0.616$ & $0.610$ & $0.932$ & $0.990$ & $1$ \\ 
\hline \\[-1.8ex] 
\end{tabular} 
\end{table}

And finally,

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

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

\begin{pitfall}[相关与距离的常见误区]
\begin{enumerate}
\item \textbf{把相关当因果}：$r(X, Y) = 0.8$ 不代表 $X \to Y$，可能 $Y \to X$、$Z \to X, Y$、或纯巧合。教科书例子：「冰淇淋销量」和「溺水率」高相关——其实是「天气」同时影响两者。
\item \textbf{Pearson $r$ 用在非线性关系上}：$r$ 只衡量\textbf{线性}相关。两个变量是 $Y = X^2$ 的关系，$r$ 能算到 0。先画散点图再算 $r$。
\item \textbf{偏相关解释成「$Z$ 不存在时的相关」}：偏相关是「$Z$ 取固定值时」$X$ 和 $Y$ 的关系，不是「假如 $Z$ 不存在」。这两件事很不一样——前者前提是 $Z$ 仍然影响 $X$ 和 $Y$，只是在每个 $Z$ 水平上分别看。
\item \textbf{欧氏距离不标准化就跑}：跟聚类一节同理，量纲差异会让距离被某一两个大量级变量主导。用 \texttt{scale()} 标准化是最简方案；用马氏距离能自动校正。
\end{enumerate}
\end{pitfall}

\end{document}
