University of California, Irvine
PSYCH 10A
title: Assignment 8
author: Jeff Rouder
output: pdf_document
---
# Overview
Previously, we examined the teaching evaluations for 81 courses across 18
questions. Here, each question served as an attribute, and we asked about the
relations among questions. We found that the questions are highly correlated. In
th
...[Show More]
title: Assignment 8
author: Jeff Rouder
output: pdf_document
---
# Overview
Previously, we examined the teaching evaluations for 81 courses across 18
questions. Here, each question served as an attribute, and we asked about the
relations among questions. We found that the questions are highly correlated. In
this sense, they were redundant, they could all be replaced with the a single
question, say, "how well do you like this course."
In this assignment, we continue with data where each unit (person, course, etc.) is
measured on many variables. Again, we would like to understand the structure
underlying the data. Let's look at some more complicated data sets and see if we
can explore the structure.
We will learn two exploratory methods: hierarchical clustering and blocking the
correlation matrix.
# Toy Data Set
I have constructed a 'toy' data set to explore structure. Here is how you load it.
```{r}
library(corrplot)
link <-
"https://raw.githubusercontent.com/rouderj/uciPsych10A-F21/main/a8Toy.dat"
toy <- read.table(url(link),head=T)
toy[1:10,]
```
Dataset `toy` is a wide data set comprised of seven columns. Each column is an
attribute of a person, and there are 100 people in total. The columns are height
(cm), weight (kg), IQ, GPA (grade point average), T1 is the score on a verbal task,
T2 is the score on a math task.
One way we explored structure before was to look at the correlation matrix. Here
it is for these data:
```{r}
corrplot(cor(toy),method="color")
```
This visualization is helpful in that we see that limbo stands out as opposite of
height and weight. What else is there? It is not transparent. Let's improve on
this visualization.
# Distances and Clustering
One approach to understanding structure is to cluster similar attributes. Here is
a **hierarchical cluster plot**:
```{r,echo=F}
toy2<-toy
for (i in 1:dim(toy)[2]){
toy2[,i] <- scale(toy[,i])
}
toy2$limbo <- - toy2$limbo
plot(hclust(dist(t(toy2))),axes=F,
ylab="",xlab="",sub="",main="")
```
Here is what I see
- There are two groups at the coarsest level, *size* (height, weight, limbo), and
*performance* (IQ, GPA, T1, T2)
- For *size*, height and weight are most similar, and limbo is more distant from
either.
- For *performance*, T1 and T2 are most similar, and, the next most similar is
GPA. IQ is more distant from these three.
## Distance, z-scores, Clustering
The key to getting a cluster plot is to figure out the *distance* between
variables. Distance is the opposite of correlation. If two variables are highly
correlated, they are very close together. If they are uncorrelated, they are
distant. If they are negatively correlated, they are even more distance.
In the previous section, we computed the correlation between say height and weight.
The *unstandardized* or *raw* distance between height and weight is
\[
||h-w|| = \sqrt{\sum_i(h_i-w_i)^2},
\]
where $i$ is the $i$th person. Unfortunately, this distance doesn't make that much
sense. The subtraction of a weight from a height doesn't even have a physical
unit.
The appropriate way to use distance is to **standardize** the variables first.
Suppose $x$ is a vector, and $x_i$ is the $i$th member of vector $x$. For example,
if $x$ is $(2,5,4)$, then $x_2=5$.
The standardized scores of $x$, denoted here as $z$ are:
\[
z_i =\frac{x_i-\bar{x}}{s_x},
\]
where $\bar{x}$ is the mean of the vector and $s_x$ is the standard deviation of
the vector.
z-scores are popular and useful in any endeavor that assess differences across
people. The R function to convert a vector into its z-scores is `scale()`.
Q1. Suppose `x<-c(1,4,5,6)`. Let `z` be the standardization (or z-scores) of `x`.
What are the values of `z`? Confirm that the mean of `z` is 0 and the standard
deviation of `z` is 1. HINT: Sometimes, the mean might be a number like
-1.709743e-16. The "e" means exponent, and the number is really $-
1.709743\times10^{-17}$, which is .0000000000000000171. Now, this number is zero
plus some rounding errors. In R, the *machine precision* is about $10^{-16}$ and
anything smaller is indistinguishable from zero.
[Show Less]