-
Notifications
You must be signed in to change notification settings - Fork 0
/
PCA.R
35 lines (32 loc) · 1.13 KB
/
PCA.R
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
require(ggplot2, quietly = TRUE)
library(ggfortify, quietly = TRUE)
# http://rpubs.com/sinhrks/basics
require(GGally, quietly = TRUE)
require(reshape2, quietly = TRUE)
require(dplyr, quietly = TRUE)
library(knitr, quietly = TRUE)
library(cluster)
library(ggdendro)
theme_set(theme_bw())
emendas <- read.csv("emendas_area_parlamentar.csv")
emendas <-subset(emendas, !is.na(NOME_PARLAMENTAR))
emendas2 <- log(emendas[,2:18]+1)
emendas2$NOME_PARLAMENTAR <- emendas$NOME_PARLAMENTAR
emendas3 <- emendas2[c(13, 14, 2)]
row.names(emendas3) = emendas2$NOME_PARLAMENTAR
pr.out = prcomp(emendas3, scale = TRUE)
kable(pr.out$rotation)
autoplot(pr.out, label = TRUE, label.size = 2, shape = TRUE,
loadings = TRUE, loadings.colour = 'blue',
loadings.label = TRUE, loadings.label.size = 3)
# Porcentagem da variância explicada:
plot_pve <- function(prout){
pr.var <- pr.out$sdev^2
pve <- pr.var / sum(pr.var)
df = data.frame(x = 1:NROW(pve), y = cumsum(pve))
ggplot(df, aes(x = x, y = y)) +
geom_point(size = 3) +
geom_line() +
labs(x='Principal Component', y = 'Cumulative Proportion of Variance Explained')
}
plot_pve(pr.out)