-
Notifications
You must be signed in to change notification settings - Fork 0
/
201_pca.Rmd
59 lines (35 loc) · 1.11 KB
/
201_pca.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# (PART\*) Data Reduction {-}
# Principal Component Analysis
<!-- Discussion of the model goes here -->
```{r}
pca <- function(data, scale=T, center=T) {
data <- data[complete.cases(data),]
if (center) mat <- apply(data, 2, function(x) x - mean(x)) else mat <- data
if (scale) for(i in 1:ncol(mat)) mat[,i] <- mat[,i] / sd(data[,i])
decomp <- svd(mat)
var <- (decomp$d^2)/(nrow(mat)-1)
loadings <- decomp$v
colnames(loadings) <- paste0("PC", 1:ncol(loadings))
rownames(loadings) <- colnames(mat)
return(list(var=var, loadings=loadings))
}
```
Testing the function:
```{r}
# Dataset of English constituency vote shares from UK GE2019
library(parlitools)
df <- subset(bes_2019,
subset=bes_2019$country=="England",
select=c("con_19", "lab_19", "ld_19", "brexit_19", "green_19", "other_19"))
df <- apply(df, 2, function(x) ifelse(is.na(x), 0, x))
# With centering and rescaling
pc <- prcomp(df, scale.=T)
pc$sdev^2
pc$rotation
pca(df)
# Without centering and rescaling
pc <- prcomp(df, center=F, scale.=F)
pc$sdev^2
pc$rotation
pca(df, center=F, scale=F)
```