Created
June 17, 2018 00:45
-
-
Save accessnash/2b8c36241d01e1ca2b851dd03aea47b9 to your computer and use it in GitHub Desktop.
Principal component analysis and factor analysis for stock return data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| findata <- read.table("C:/Users/DASA0/Desktop/MS Econ/Stat 524/wichern data/T8-4.dat", sep="\t", header=F) | |
| colnames(findata) <- c("JP Morgan", "City bank", "Wells Fargo", "Royal Dutch", "Exxon") | |
| Xbar <- colMeans(findata) | |
| S <- cov(findata) | |
| weight <- diag(1/sqrt(diag(cov(findata)))) | |
| #standardization | |
| stdfindata <- (as.matrix(findata)-rep(1, dim(findata)[1])%*%t(apply(findata, 2, mean)))%*%weight | |
| stdfindata <- as.data.frame(stdfindata) | |
| colnames(stdfindata) <- c("JP Morgan", "City bank", "Wells Fargo", "Royal Dutch", "Exxon") | |
| R <- cov(stdfindata) | |
| eig <- eigen(R) | |
| fit <- princomp(stdfindata) | |
| (loading.pc <- fit$loadings[,c(1, 2)]%*%diag(fit$sdev[c(1, 2)])) | |
| loading.pc.varimax <- varimax(loading.pc) #varimax function maximizes variances of squares of scaled loadings | |
| plot(loading.pc.varimax$loadings, type="n") | |
| text(loading.pc.varimax$loadings, names(findata)) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://nashstatistica.wordpress.com/2018/06/17/principal-component-analysis-of-stock-price-data/