Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
YStrano
GitHub Repository: YStrano/DataScience_GA
Path: blob/master/lessons/lesson_15/rolling_PCA.R
1904 views
1
library(readr)
2
library(dplyr)
3
library(xts)
4
5
## Set working directory
6
setwd("~/ga/repos/lesson-15")
7
8
## Read in data
9
df = read_csv('financial_indicators2.csv', na = "#N/A")
10
11
# Exclude Nulls
12
df <- df[complete.cases(df),]
13
14
#Converto to XTS and Scale
15
df_xts <- as.xts(df %>%
16
select(-DATE) %>%
17
scale(center=TRUE, scale=TRUE),
18
order.by = df$DATE)
19
20
## Function to extract the rolling variance explained by the first PC
21
roll_var<-function(x){
22
output<-prcomp(x)$sdev^2
23
return(output[1]/sum(output))
24
}
25
26
## Run the function
27
rolling_var<-rollapply(df_xts, 90, roll_var, by.column=FALSE)
28
29
## Convert to df for plotting
30
plotting_df <- tbl_df(data.frame(date=index(rolling_var), var = coredata(rolling_var)))
31
32
## Plot
33
ggplot(plotting_df, aes(x=date, y=var)) +
34
geom_line() +
35
theme_bw() +
36
ggtitle('90 Day Rolling Variance Explained')
37
38