Skip to content

Instantly share code, notes, and snippets.

@Shreyes2010
Created December 26, 2011 18:01
Show Gist options
  • Save Shreyes2010/1521777 to your computer and use it in GitHub Desktop.
Save Shreyes2010/1521777 to your computer and use it in GitHub Desktop.
###############################
## Access the relevant files ##
###############################
returns <- read.csv("Returns_CNX_500.csv")
returns1 <- returns
nifty <- read.csv("Nifty_returns.csv")
mibor <- read.csv("MIBOR.csv", na.strings="#N/A")
exchange <- read.csv("Exchange_rates.csv", na.strings="#N/A")
###################################################################
## Dealing with missing values ##
###################################################################
for(i in 2:ncol(returns))
{
returns1[, i] <- approx(returns$Year, returns1[ ,i], returns$Year)$y # Replacing the missing values by linear approximates
}
## Dealing with blanks in the MIBOR rates ##
mibor[, 2] <- approx(as.Date(mibor$Dates, '%d-%b-%y'), mibor[ ,2], as.Date(mibor$Dates, '%d-%b-%y'))$y
for(k in 2:nrow(mibor))
{
mibor$Change1[k] <- ((mibor$MIBOR[k] - mibor$MIBOR[k-1])/mibor$MIBOR[k-1])*100
}
## Dealing with blanks in the exchange rates ##
exchange[, 2] <- approx(as.Date(exchange$Year,'%d-%b-%y'), exchange[ ,2], as.Date(exchange$Year, '%d-%b-%y'))$y
exchange$Change <- as.numeric(exchange$Change)
for(j in 2:nrow(exchange))
{
exchange$Change[j] <- ((exchange$Exchange.rates[j] - exchange$Exchange.rates[j-1])/exchange$Exchange.rates[j-1])*100
}
#######################################
### Summary Statistics calculations ###
#######################################
library(moments)
library(tseries)
## Unit root testing ##
adf.test(exchange$Exchange.rates)
adf.test(exchange$Change)
adf.test(mibor$MIBOR)
adf.test(mibor$Change1)
adf.test(nifty$S...P.Cnx.Nifty)
# Calculating moments
skewness(mibor$MIBOR)
skewness(mibor$Change1)
skewness(exchange$Exchange.rates)
skewness(exchange$Change)
skewness(nifty$S...P.Cnx.Nifty)
kurtosis(mibor$MIBOR)
kurtosis(mibor$Change1)
kurtosis(exchange$Exchange.rates)
kurtosis(exchange$Change)
kurtosis(nifty$S...P.Cnx.Nifty)
var(exchange$Exchange.rates)
var(exchange$Change)
var(mibor$MIBOR)
var(mibor$Change1)
var(nifty$S...P.Cnx.Nifty)
#####################################################
## Calculating the principal component of returns ##
#####################################################
# Store the relevent data as a matrix and then pass it through princomp()
ret <- as.matrix(returns1[ ,-1], nrow = dim(returns1)[1], ncol = dim(returns1)[2]-1)
princ.return <- princomp(ret)
plot(princ.return, main = "Principal component variance") # Plot of variance of the components
barplot(height=princ.return$sdev[1:10]/princ.return$sdev[1], main = "Relative variance of the principal components" )
## Loadings of first 10 components ##
# Extracting the components using the loadings of the PCA
load.cp <- loadings(princ.return)[,1:10]
pr.cp <- ret %*% load.cp
pr <- as.data.frame(pr.cp)
##########################################
## Regressions single factor model CAPM ##
##########################################
reg.capm <- lapply( 1:10, function (i) { lm(pr[, i] ~ nifty$S...P.Cnx.Nifty) })
# Storing the coefficients of the 10 regressions in "cfs"
cfs <- sapply(reg.capm, function(x) as.vector(x$coefficients))
# Similarly storing the R^2 of the 10 regressions in
r.sq.values <- lapply(reg.capm, function(x) summary(x)$r.squared)
colnames(cfs) <- lapply(1:10, function(x) paste("Comp", as.character(x), sep=''))
rownames(cfs) <- c("Intercept", "Nifty")
# Storing the output in a LaTeX friendly format
write.table(format(cfs, digits=3), row.names=T, col.names=T, sep=" & ", eol=" \\\\ \n", file="my.temp.file.txt")
write.table(format(r.sq.values, digits = 3), file="my.temp.file.txt", append=T, col.names=F, row.names=T, sep = " & ", eol="\\\\ \n")
###################################
## Regressions multi factor APT ##
###################################
reg.apt <- lapply( 1:10, function (i) { lm(pr[, i] ~ nifty$S...P.Cnx.Nifty + mibor$Change1 + exchange$Change) })
# Storing the coefficients and the R^2 values
cfs1 <- sapply(reg.apt, function(x) as.vector(x$coefficients))
r.sq.values1 <- lapply(reg.apt, function(x) summary(x)$r.squared)
colnames(cfs1) <- lapply(1:10, function(x) paste("Comp", as.character(x), sep=''))
rownames(cfs1) <- c("Intercept", "Nifty", "MIBOR" , "INR/USD Exchange rate")
# Storing the output in a LaTeX friendly format
write.table(format(cfs1, digits=4), row.names=T, col.names=T, sep=" & ", eol=" \\\\ \n", file="my.temp.file.APT.txt")
write.table(format(r.sq.values1, digits = 4), file="my.temp.file.APT.txt", append=T, col.names=F, row.names=T, sep = " & ", eol="\\\\ \n")
###################################################
#### Comparision of the regression F-Statistic ####
###################################################
# ANOVA table for illustration from where we can extract the F-values for the second
# regression directly
all.fstats <- mapply(anova, reg.capm, reg.apt )
lapply(all.fstats, function(x) x$F )
# Extracting the F-stats from the ANOVA table of all the 10 regressions
all.Fs <- mapply(function(x, y) anova(x,y)$F[2], reg.capm, reg.apt)
all.Ps <- mapply(function(x, y) anova(x,y)[[ 2, "Pr(>F)"]], reg.capm, reg.apt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment