Pages

Friday, June 17, 2011

Regression Analysis in R

We will perform regression analysis on few banking stocks and nifty index to find out their beta and then make decision on validity of these figures. Regression analysis is done using t test to find out validity of coefficient while standard error and degrees of freedom to find out t score.

To get all the data into R we have download csv files (from nifty website) and following command will be further used to find out return series.

>sbitemp <- read.csv("01-04-2010-TO-01-04-2011SBINALLN.csv")
> sbi <- sbitemp[[9]]

> pnbtemp <- read.csv("01-04-2010-TO-01-04-2011PNBALLN.csv")

> pnb <- pnbtemp[[9]]

> hdfctemp <- read.csv("01-04-2010-TO-01-04-2011HDFCBANKALLN.csv")

> hdfc <- hdfctemp[[9]]
> icicitemp <- read.csv("01-04-2010-TO-01-04-2011ICICIBANKALLN.csv")
> icici <- icicitemp[[9]]

> sbiR <- bankl(sbi,sbi)

> niftyR <- bankl(nifty,nifty)

> pnbR <- bankl(pnb,pnb)
> hdfcR <- bankl(hdfc,hdfc)

> iciciR <- bankl(icici,icici)

Where function bankl has following source code
> bankl function (first,second) { a= first
a[1]=0
k=length(second)

i=2
while(i<=k)
{ a[i]= log(second[i]/first[i-1])
i=i+1 }
a }
>

Let's plot returns of NIFTY as compare to returns of SBI.

Next step is to perform pair wise regression to find out beta for each of these securities. glm function in R language help us to do regression analysis and output can be saved.
> glm(sbiR ~ niftyR) Call: glm(formula = sbiR ~ niftyR) Coefficients: (Intercept) niftyR 0.0005735 1.1515344 Degrees of Freedom: 254 Total (i.e. Null); 253 Residual Null Deviance: 0.09468 Residual Deviance: 0.05277 AIC: -1434

Storing the output of regression into a linear object for (SBI & NIFTY)

> sbi.linear <- glm(sbiR ~ niftyR)
> summary(sbi.linear)

Call:
glm(formula = sbiR ~ niftyR)
Deviance Residuals:
Min 1Q Median 3Q Max -0.051342 -0.008509 -0.000283 0.008339 0.068983
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.0005735 0.0009049 0.634 0.527
niftyR 1.1515344 0.0812352 14.175 <2e-16 *** ---

T value of 14.175 clearly shows the Beta for SBI is 1.151 at more than 99.99% confidence interval. Similarly we will run regression on all the stocks.
> sbiB <- sbi.linear[[1]][[2]]
> sbiB
[1] 1.151534
> pnbB <- pnb.linear[[1]][[2]]
> hdfcB <- hdfc.linear[[1]][[2]]
> iciciB <- icici.linear[[1]][[2]]


(Please change all &gt to >(in symbol) and &lt tp <(in symbol) , I am trying to convert this but his revert back in code)

Reference:




No comments:

Post a Comment