AFIN 8005: Research in Banking and
Finance
Lecture 5: Applications in R
Applications in R
• We’ll Cover
• If-statement and loop
• Simple plots in R
• Linear regression in R
• Example: Fama-French 3 factors
• Panel regression in R
• Example: firm’s capital structure
If-statement
if Example:
x=2
if (x>0){
print("positive number")
} else {
print("non-positive number")
}
If-statement
ifelse Example:
x=2
ifelse(x>0, “positive”,”non-positive”)
Multiple conditional statement:
if (x>0){
print("positive number")
} else if (x<0){
print("negative number")
} else{
print(“zero")}
ifelse(x>0, "positive", ifelse(x<0,"negative","zero"))
Loop
for Example sum from 1 to 100:
result=0
for (i in 1:100){
result=result+i
}
result=0
i=1
while (i<=100){
result=result+i
i=i+1
}
Loop by 2
for Example sum from 0 to 100, by 2:
result=0
for (i in seq(0,100,2)){
result=result+i
}
result=0
i=1
while (i<=100){
result=result+i
i=i+1
}
Basic line plots
x=1:10
y1=x^2
y2=2*x^2
plot(x,y1,type="l")
plot(x,y1,type="S")
plot(x,y1,type="p")
plot(x,y1,type="b",col="red")
lines(x,y2,type="b",col="blue")
Histogram plots
hist(islands)
hist(sqrt(islands))
library(data.table)
dow=fread("Dow.csv")
hist(dow$RET)
Linear regression
Function to run linear regression in R:
lm()
Example:
Y=a+b*x+e
test=lm(y~x,data=dataframe)
Example
Market model:
= + , +
R code:
returndata=fread("h9_data1.csv")
mkmodel=lm(GE~DJI,data=returndata)
mkmodel
summary(mkmodel)
coef(mkmodel)
resid(mkmodel)
fitted(mkmodel)
Example
Draw a plot with the regression line
plot(returndata$DJI,returndata$GE)
abline(mkmodel,col="blue")
Diagnostic Plots for Linear
Regression Analysis
• Linearity
• Residuals follow a normal distribution
• For different X observations, equal variance(homoscedasticity) of residuals or
heteroscedasticity
• The effect of outliers
• https://data.library.virginia.edu/diagnostic-plots/
• https://data.library.virginia.edu/understanding-q-q-plots/
par(mfrow=c(2,2))
plot(mkmodel)
Diagnostic Plots for Linear
Regression Analysis
Fama French Three Factors
• Fama and French (1992,1993) extended the basic CAPM to include size and book-to-
market effects as explanatory factors in explaining the cross-section of stock returns.
• SMB(Small minus Big) gives the size premium which is the additional return received by
investors from investing in companies having a low market capitalization.
• HML(High minus Low) gives the value premium which is the return provided to investors
for investing in companies having high book-to-market values.
• Three factors Fama-French Model:
• − = + − + + () +
Panel regressions
• Panel data has two dimensions: time and individual
• Two basic panel regression models: time-fixed effect and individual-fixed
effect models.
• A time-fixed effect is capturing the common factor for all individuals at the
same time.
• An individual-fixed effect is capturing the unchanged factor for an individual
through time.
Example
• Determinants of a firms leverage
• Data description:
• gvkey: firm identifier; each firm has a unique identifier;
• fyear: fiscal year of the data
• conm: firm name
• sic: SIC industry code (https://en.wikipedia.org/wiki/Standard_Industrial_Classification)
• at: total asset (millions)
• bkvlps: book value per share (dollars)
• ceq: common equity (millions)
• che: cash and short-term investments (millions)
• csho: common shares outstanding (millions)
• dlc: debt in current liabilities (millions)
• dltt: long term debt (millions)
• ebitda: earnings before interest, depreciation and amortisation (millions)
• invt: inventory (millions)
• ppent: property, plant and equilibrium (millions)
• sale: net sales (millions)
• prcc_f: end of fiscal year share price (dollars)
Example
• Book leverage: leverage=(DLTT+DLC)/(AT);
• Tangibility: tangibility=(Inventory+ Net Property, Plant and Equipment)/Total Asset=(INVT+PPENT)/AT;
• Market to book ratio: M/B= stock price of fiscal year/book value per share=PRCC_F/BKVLPS
• Size: size=log(SALE)
• Profitability: profitability=EBITDA/Total Assets=EBITDA/AT
• Model:
• Leverage~tangibility+Mbratio+Size+Profitability+year fixed effect+firm fixed
effect
学霸联盟