# Loading the necessary packages
library(tseries)
## Warning: package 'tseries' was built under R version 4.4.3
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(readxl)
# Read in the data sets
dow_jones = read.csv("/Users/doris/OneDrive/Documents/DA6813/dow_jones_index.data")
SP500Data <- read.csv("/Users/doris/OneDrive/Documents/DA6813/Download Data - INDEX_US_S&P US_SPX.csv", header=TRUE, sep=",")
# Remove any missing values if needed
dow_jones = na.omit(dow_jones)
anyNA(dow_jones)
## [1] FALSE
SP500Data = na.omit(SP500Data)
anyNA(SP500Data)
## [1] FALSE
# See what the data classes are
str(dow_jones)
## 'data.frame': 720 obs. of 16 variables:
## $ quarter : int 1 1 1 1 1 1 1 1 1 1 ...
## $ stock : chr "AA" "AA" "AA" "AA" ...
## $ date : chr "1/14/2011" "1/21/2011" "1/28/2011" "2/4/2011" ...
## $ open : chr "$16.71" "$16.19" "$15.87" "$16.18" ...
## $ high : chr "$16.71" "$16.38" "$16.63" "$17.39" ...
## $ low : chr "$15.64" "$15.60" "$15.82" "$16.18" ...
## $ close : chr "$15.97" "$15.79" "$16.13" "$17.14" ...
## $ volume : int 242963398 138428495 151379173 154387761 114691279 80023895 132981863 109493077 114332562 130374108 ...
## $ percent_change_price : num -4.428 -2.471 1.638 5.933 0.231 ...
## $ percent_change_volume_over_last_wk: num 1.38 -43.02 9.36 1.99 -25.71 ...
## $ previous_weeks_volume : int 239655616 242963398 138428495 151379173 154387761 114691279 80023895 132981863 109493077 114332562 ...
## $ next_weeks_open : chr "$16.19" "$15.87" "$16.18" "$17.33" ...
## $ next_weeks_close : chr "$15.79" "$16.13" "$17.14" "$17.37" ...
## $ percent_change_next_weeks_price : num -2.471 1.638 5.933 0.231 -0.633 ...
## $ days_to_next_dividend : int 19 12 5 97 90 83 76 69 62 55 ...
## $ percent_return_next_dividend : num 0.188 0.19 0.186 0.175 0.173 ...
## - attr(*, "na.action")= 'omit' Named int [1:30] 1 13 25 37 49 61 73 85 97 109 ...
## ..- attr(*, "names")= chr [1:30] "1" "13" "25" "37" ...
# Convert misclassified columns to the correct format
dow_jones$open=as.numeric(gsub("\\$","",dow_jones$open))
dow_jones$high=as.numeric(gsub("\\$","",dow_jones$high))
dow_jones$low=as.numeric(gsub("\\$","",dow_jones$low))
dow_jones$close=as.numeric(gsub("\\$","",dow_jones$close))
dow_jones$next_weeks_open=as.numeric(gsub("\\$","",dow_jones$next_weeks_open))
dow_jones$next_weeks_close=as.numeric(gsub("\\$","",dow_jones$next_weeks_close))
dow_jones$date <- mdy(dow_jones$date)
dow_jones$stock = as.factor(dow_jones$stock)
dow_jones$quarter = as.factor(dow_jones$quarter)
# Verify the change
str(dow_jones)
## 'data.frame': 720 obs. of 16 variables:
## $ quarter : Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ stock : Factor w/ 30 levels "AA","AXP","BA",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ date : Date, format: "2011-01-14" "2011-01-21" ...
## $ open : num 16.7 16.2 15.9 16.2 17.3 ...
## $ high : num 16.7 16.4 16.6 17.4 17.5 ...
## $ low : num 15.6 15.6 15.8 16.2 17 ...
## $ close : num 16 15.8 16.1 17.1 17.4 ...
## $ volume : int 242963398 138428495 151379173 154387761 114691279 80023895 132981863 109493077 114332562 130374108 ...
## $ percent_change_price : num -4.428 -2.471 1.638 5.933 0.231 ...
## $ percent_change_volume_over_last_wk: num 1.38 -43.02 9.36 1.99 -25.71 ...
## $ previous_weeks_volume : int 239655616 242963398 138428495 151379173 154387761 114691279 80023895 132981863 109493077 114332562 ...
## $ next_weeks_open : num 16.2 15.9 16.2 17.3 17.4 ...
## $ next_weeks_close : num 15.8 16.1 17.1 17.4 17.3 ...
## $ percent_change_next_weeks_price : num -2.471 1.638 5.933 0.231 -0.633 ...
## $ days_to_next_dividend : int 19 12 5 97 90 83 76 69 62 55 ...
## $ percent_return_next_dividend : num 0.188 0.19 0.186 0.175 0.173 ...
## - attr(*, "na.action")= 'omit' Named int [1:30] 1 13 25 37 49 61 73 85 97 109 ...
## ..- attr(*, "names")= chr [1:30] "1" "13" "25" "37" ...
str(SP500Data)
## 'data.frame': 24 obs. of 5 variables:
## $ Date : chr "06/24/2011" "06/17/2011" "06/10/2011" "06/03/2011" ...
## $ Open : chr "1,271.50" "1,271.31" "1,300.26" "1,331.10" ...
## $ High : chr "1,298.61" "1,292.50" "1,300.26" "1,345.20" ...
## $ Low : chr "1,262.87" "1,258.07" "1,268.28" "1,297.90" ...
## $ Close: chr "1,268.45" "1,271.50" "1,270.98" "1,300.16" ...
#Rename for consistency
SP500Data$Date <- as.Date(SP500Data$Date, format="%m/%d/%Y")
SP500Data$Open <- as.numeric(gsub(",", "", SP500Data$Open))
SP500Data$High <- as.numeric(gsub(",", "", SP500Data$High))
SP500Data$Low <- as.numeric(gsub(",", "", SP500Data$Low))
SP500Data$Close <- as.numeric(gsub(",", "", SP500Data$Close))
str(SP500Data)
## 'data.frame': 24 obs. of 5 variables:
## $ Date : Date, format: "2011-06-24" "2011-06-17" ...
## $ Open : num 1272 1271 1300 1331 1333 ...
## $ High : num 1299 1292 1300 1345 1335 ...
## $ Low : num 1263 1258 1268 1298 1312 ...
## $ Close: num 1268 1272 1271 1300 1331 ...
colnames(SP500Data)[colnames(SP500Data) == "Date"] <- "date"
summary(SP500Data)
## date Open High Low
## Min. :2011-01-14 Min. :1271 Min. :1292 Min. :1249
## 1st Qu.:2011-02-23 1st Qu.:1291 1st Qu.:1302 1st Qu.:1276
## Median :2011-04-04 Median :1319 Median :1333 Median :1296
## Mean :2011-04-04 Mean :1313 Mean :1327 Mean :1296
## 3rd Qu.:2011-05-14 3rd Qu.:1333 3rd Qu.:1341 3rd Qu.:1313
## Max. :2011-06-24 Max. :1365 Max. :1371 Max. :1332
## Close
## Min. :1268
## 1st Qu.:1291
## Median :1320
## Mean :1313
## 3rd Qu.:1333
## Max. :1364
summary(dow_jones)
## quarter stock date open high
## 1:330 AA : 24 Min. :2011-01-14 Min. : 10.59 Min. : 10.94
## 2:390 AXP : 24 1st Qu.:2011-02-23 1st Qu.: 29.96 1st Qu.: 30.66
## BA : 24 Median :2011-04-04 Median : 46.02 Median : 46.91
## BAC : 24 Mean :2011-04-04 Mean : 53.75 Mean : 54.76
## CAT : 24 3rd Qu.:2011-05-14 3rd Qu.: 72.81 3rd Qu.: 74.34
## CSCO : 24 Max. :2011-06-24 Max. :172.11 Max. :173.54
## (Other):576
## low close volume percent_change_price
## Min. : 10.40 Min. : 10.52 Min. :9.719e+06 Min. :-15.42290
## 1st Qu.: 28.71 1st Qu.: 30.41 1st Qu.:3.069e+07 1st Qu.: -1.29736
## Median : 44.95 Median : 46.12 Median :5.275e+07 Median : 0.00000
## Mean : 52.73 Mean : 53.83 Mean :1.156e+08 Mean : 0.03014
## 3rd Qu.: 71.11 3rd Qu.: 72.77 3rd Qu.:1.312e+08 3rd Qu.: 1.63087
## Max. :167.82 Max. :170.58 Max. :1.054e+09 Max. : 9.88223
##
## percent_change_volume_over_last_wk previous_weeks_volume next_weeks_open
## Min. :-61.4332 Min. :9.719e+06 Min. : 10.52
## 1st Qu.:-19.8043 1st Qu.:3.068e+07 1st Qu.: 30.39
## Median : 0.5126 Median :5.295e+07 Median : 46.04
## Mean : 5.5936 Mean :1.174e+08 Mean : 53.81
## 3rd Qu.: 21.8006 3rd Qu.:1.333e+08 3rd Qu.: 72.81
## Max. :327.4089 Max. :1.453e+09 Max. :172.11
##
## next_weeks_close percent_change_next_weeks_price days_to_next_dividend
## Min. : 10.52 Min. :-15.4229 Min. : 0.00
## 1st Qu.: 30.51 1st Qu.: -1.2524 1st Qu.: 24.00
## Median : 45.93 Median : 0.0360 Median : 47.00
## Mean : 53.97 Mean : 0.1933 Mean : 52.26
## 3rd Qu.: 73.00 3rd Qu.: 1.7927 3rd Qu.: 69.25
## Max. :174.54 Max. : 9.8822 Max. :329.00
##
## percent_return_next_dividend
## Min. :0.06557
## 1st Qu.:0.53409
## Median :0.68014
## Mean :0.69156
## 3rd Qu.:0.85402
## Max. :1.56421
##
Dow = split(dow_jones, dow_jones$stock)
# Compute the returns and remove any missing values.
ReturnAA = na.omit(Delt(Dow$AA[,7]))
ReturnAXP = na.omit(Delt(Dow$AXP[,7]))
ReturnBA = na.omit(Delt(Dow$BA[,7]))
ReturnBAC = na.omit(Delt(Dow$BAC[,7]))
ReturnCAT = na.omit(Delt(Dow$CAT[,7]))
ReturnCSCO = na.omit(Delt(Dow$CSCO[,7]))
ReturnCVX = na.omit(Delt(Dow$CVX[,7]))
ReturnDD = na.omit(Delt(Dow$DD[,7]))
ReturnDIS = na.omit(Delt(Dow$DIS[,7]))
ReturnGE = na.omit(Delt(Dow$GE[,7]))
ReturnHD = na.omit(Delt(Dow$HD[,7]))
ReturnHPQ =na.omit(Delt(Dow$HPQ[,7]))
ReturnIBM = na.omit(Delt(Dow$IBM[,7]))
ReturnINTC = na.omit(Delt(Dow$INTC[,7]))
ReturnJNJ = na.omit(Delt(Dow$JNJ[,7]))
ReturnJPM = na.omit(Delt(Dow$JPM[,7]))
ReturnKO = na.omit(Delt(Dow$KO[,7]))
ReturnKRFT = na.omit(Delt(Dow$KRFT[,7]))
ReturnMCD = na.omit(Delt(Dow$MCD[,7]))
ReturnMMM = na.omit(Delt(Dow$MMM[,7]))
ReturnMRK = na.omit(Delt(Dow$MRK[,7]))
ReturnMSFT = na.omit(Delt(Dow$MSFT[,7]))
ReturnPFE = na.omit(Delt(Dow$PFE[,7]))
ReturnPG = na.omit(Delt(Dow$PG[,7]))
ReturnT = na.omit(Delt(Dow$T[,7]))
ReturnTRV = na.omit(Delt(Dow$TRV[,7]))
ReturnUTX = na.omit(Delt(Dow$UTX[,7]))
ReturnVZ = na.omit(Delt(Dow$VZ[,7]))
ReturnWMT = na.omit(Delt(Dow$WMT[,7]))
ReturnXOM = na.omit(Delt(Dow$XOM[,7]))
ReturnSP500 = na.omit(Delt(SP500Data[,5]))
# Merge the returns of all stocks into single data
MyData = cbind(ReturnSP500,
ReturnAA,
ReturnAXP,
ReturnBA,
ReturnBAC,
ReturnCAT,
ReturnCSCO,
ReturnCVX,
ReturnDD,
ReturnDIS,
ReturnGE,
ReturnHD,
ReturnHPQ,
ReturnIBM,
ReturnINTC,
ReturnJNJ,
ReturnJPM,
ReturnKO,
ReturnKRFT,
ReturnMCD,
ReturnMMM,
ReturnMRK,
ReturnMSFT,
ReturnPFE,
ReturnPG,
ReturnT,
ReturnTRV,
ReturnUTX,
ReturnVZ,
ReturnWMT,
ReturnXOM)
# Weekly Return of Investment of Interest
colnames(MyData) = c("SP500", "AA", "AXP", "BA", "BAC", "CAT", "CSCO", "CVX", "DD", "DIS", "GE", "HD", "HPQ", "IBM", "INTC", "JNJ", "JPM", "KO", "KRFT", "MCD", "MMM", "MRK", "MSFT", "PFE", "PG", "T", "TRV", "UTX",
"VZ", "WMT", "XOM")
head(MyData)
## SP500 AA AXP BA BAC
## [1,] 0.0024045094 -0.011271133 -0.0054054054 0.02297702 -0.065573770
## [2,] -0.0004089658 0.021532616 -0.0465217391 -0.03417969 -0.045614035
## [3,] 0.0229586618 0.062616243 -0.0009119927 0.03105590 0.050735294
## [4,] 0.0237970711 0.013418903 0.0668644455 0.01064724 0.033589923
## [5,] 0.0016302306 -0.005181347 -0.0260962567 0.01247574 -0.001354096
## [6,] 0.0033751603 -0.034722222 -0.0439270810 -0.01013143 -0.037288136
## CAT CSCO CVX DD DIS GE
## [1,] -0.01340283 -0.02310231 0.010233761 -0.02911647 0.011453296 0.048884166
## [2,] 0.03159030 0.01013514 -0.004371934 0.04012410 -0.022395571 0.023302938
## [3,] 0.04086538 0.05351171 0.040055692 0.04454166 0.047876448 0.017821782
## [4,] 0.03966262 -0.15192744 -0.006796416 0.03902532 0.066322771 0.037451362
## [5,] 0.02240680 0.00802139 0.023535511 0.02565042 0.003455425 0.005157056
## [6,] -0.03646325 -0.01114058 0.034238250 -0.03411933 -0.014003673 -0.028917910
## HD HPQ IBM INTC JNJ
## [1,] 0.017275007 0.0211891892 0.0366666667 -0.012333966 0.001758593
## [2,] 0.005204054 -0.0364175312 0.0238585209 0.030739673 -0.042291733
## [3,] 0.002724796 0.0421885300 0.0300860499 0.010251631 0.013831028
## [4,] 0.018478261 0.0255112798 -0.0009146341 0.003690037 -0.002301118
## [5,] 0.026680896 0.0006167763 0.0060421117 0.017463235 0.006754530
## [6,] -0.036382536 -0.1230737621 -0.0155302111 -0.012646793 -0.024054983
## JPM KO KRFT MCD MMM
## [1,] 0.008461367 -0.005702519 0.000319081 0.012827437 0.013507378
## [2,] -0.016559947 -0.008921459 -0.026156300 -0.023063592 -0.020719005
## [3,] 0.001122586 0.005626105 0.021618081 0.010507642 0.009720952
## [4,] 0.044404575 0.016144501 -0.016992626 0.028224173 0.039755352
## [5,] 0.030706463 0.015416077 0.008153947 -0.000131337 0.012636166
## [6,] -0.027500000 -0.003718048 0.025881592 -0.022198870 -0.029152324
## MRK MSFT PFE PG T
## [1,] -0.009640666 -0.0098939929 0.001090513 0.005798871 -0.003517411
## [2,] -0.024483776 -0.0096359743 -0.011437908 -0.025944470 -0.029650547
## [3,] -0.005443000 0.0007207207 0.063360882 -0.009190031 0.017460895
## [4,] 0.005472788 -0.0187252431 -0.024352332 0.017607294 0.017876296
## [5,] -0.006652555 -0.0069724771 0.019118428 -0.006642979 0.003512469
## [6,] -0.020091324 -0.0188470067 -0.017196456 -0.022706065 -0.015400770
## TRV UTX VZ WMT XOM
## [1,] 0.006772835 0.014162873 -0.014382403 0.016785258 0.0146454265
## [2,] 0.014727273 0.015336658 0.019456366 0.017405347 0.0001266143
## [3,] 0.028668697 0.013385730 0.019085041 -0.011816578 0.0543106722
## [4,] 0.027521338 0.032476975 0.002203250 -0.006068178 -0.0055235351
## [5,] 0.032717410 -0.002230047 0.006320418 -0.005566529 0.0202849553
## [6,] -0.021667761 -0.019291848 -0.017749863 -0.065547129 0.0099408284
boxplot(MyData,
main = "Expected Return",
xlab = "Stock Picks ",
ylab = "Return",
col = "lightblue",
las = 2) # Rotate labels for readability

# Compute mean and stdev for the returns.
DataMean=apply(MyData, 2, mean)
DataSD=apply(MyData, 2, sd)
# Take a look at the means and standard deviations.
cbind(DataMean,DataSD)
## DataMean DataSD
## SP500 0.0009469730 0.01480999
## AA -0.0014758553 0.03486277
## AXP 0.0022992844 0.02809141
## BA 0.0011022005 0.02795967
## BAC -0.0156049349 0.02911342
## CAT 0.0032480226 0.03411175
## CSCO -0.0143473271 0.03971904
## CVX 0.0026118394 0.02482674
## DD 0.0022017080 0.02723925
## DIS -0.0015588482 0.02808084
## GE -0.0016902251 0.02574530
## HD -0.0007599981 0.02198521
## HPQ -0.0113893855 0.03927282
## IBM 0.0043318296 0.01836236
## INTC 0.0007425153 0.03258276
## JNJ 0.0019245403 0.02114399
## JPM -0.0053481151 0.02181402
## KO 0.0013550453 0.01664583
## KRFT 0.0044681344 0.01816930
## MCD 0.0045364377 0.01954457
## MMM 0.0015614511 0.02073424
## MRK 0.0006072965 0.02069553
## MSFT -0.0064307236 0.01898709
## PFE 0.0042807309 0.02655822
## PG -0.0018427834 0.01779427
## T 0.0031507287 0.01934575
## TRV 0.0017787893 0.01920565
## UTX 0.0030140170 0.02130799
## VZ 0.0008114707 0.01799993
## WMT -0.0017666125 0.01910305
## XOM -0.0002927384 0.02512918
# Using LM here as interpretation
# is key.
# Linear Regression models
# Model AA
lm.AA <- lm(AA ~ SP500, data = as.data.frame(MyData))
summary(lm.AA)
##
## Call:
## lm(formula = AA ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.072080 -0.029183 -0.000855 0.022727 0.066652
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.001766 0.007393 -0.239 0.814
## SP500 0.306355 0.509317 0.602 0.554
##
## Residual standard error: 0.03538 on 21 degrees of freedom
## Multiple R-squared: 0.01694, Adjusted R-squared: -0.02988
## F-statistic: 0.3618 on 1 and 21 DF, p-value: 0.5539
BetaAA <- summary(lm.AA)$coefficients[2, 1]
# Model AXP
lm.AXP <- lm(AXP ~ SP500, data = as.data.frame(MyData))
summary(lm.AXP)
##
## Call:
## lm(formula = AXP ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.048172 -0.013327 0.001369 0.013786 0.046256
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.001540 0.005446 0.283 0.7800
## SP500 0.801279 0.375167 2.136 0.0446 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.02606 on 21 degrees of freedom
## Multiple R-squared: 0.1785, Adjusted R-squared: 0.1393
## F-statistic: 4.562 on 1 and 21 DF, p-value: 0.04463
BetaAXP <- summary(lm.AXP)$coefficients[2, 1]
#Model BA
lm.BA <- lm(BA ~ SP500, data = as.data.frame(MyData))
summary(lm.BA)
##
## Call:
## lm(formula = BA ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.042628 -0.016080 -0.007602 0.014311 0.065337
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0007631 0.0058713 0.130 0.898
## SP500 0.3581406 0.4044911 0.885 0.386
##
## Residual standard error: 0.0281 on 21 degrees of freedom
## Multiple R-squared: 0.03599, Adjusted R-squared: -0.009918
## F-statistic: 0.784 on 1 and 21 DF, p-value: 0.386
BetaBA <- summary(lm.BA)$coefficients[2, 1]
#Model BAC
lm.BAC <- lm(BAC ~ SP500, data = as.data.frame(MyData))
summary(lm.BAC)
##
## Call:
## lm(formula = BAC ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.051800 -0.013741 0.002985 0.013326 0.039262
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.016795 0.004788 -3.507 0.00210 **
## SP500 1.256567 0.329891 3.809 0.00102 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.02292 on 21 degrees of freedom
## Multiple R-squared: 0.4086, Adjusted R-squared: 0.3804
## F-statistic: 14.51 on 1 and 21 DF, p-value: 0.001025
BetaBAC <- summary(lm.BAC)$coefficients[2, 1]
#Model for CAT
lm.CAT <- lm(CAT ~ SP500, data = as.data.frame(MyData))
summary(lm.CAT)
##
## Call:
## lm(formula = CAT ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.051372 -0.032386 0.003282 0.029367 0.054203
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.002917 0.007211 0.404 0.690
## SP500 0.349837 0.496789 0.704 0.489
##
## Residual standard error: 0.03451 on 21 degrees of freedom
## Multiple R-squared: 0.02307, Adjusted R-squared: -0.02345
## F-statistic: 0.4959 on 1 and 21 DF, p-value: 0.489
BetaCAT <- summary(lm.CAT)$coefficients[2, 1]
#Model for CSCO
lm.CSCO <- lm(CSCO ~ SP500, data = as.data.frame(MyData))
summary(lm.CSCO)
##
## Call:
## lm(formula = CSCO ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.142264 -0.011737 0.002709 0.018442 0.063347
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.01454 0.00847 -1.717 0.101
## SP500 0.20498 0.58353 0.351 0.729
##
## Residual standard error: 0.04053 on 21 degrees of freedom
## Multiple R-squared: 0.005841, Adjusted R-squared: -0.0415
## F-statistic: 0.1234 on 1 and 21 DF, p-value: 0.7289
BetaCSCO <- summary(lm.CSCO)$coefficients[2, 1]
#Model for CVX
lm.CVX <- lm(CVX ~ SP500, data = as.data.frame(MyData))
summary(lm.CVX)
##
## Call:
## lm(formula = CVX ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.060691 -0.011020 0.001877 0.013173 0.040863
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.002759 0.005287 0.522 0.607
## SP500 -0.155348 0.364236 -0.427 0.674
##
## Residual standard error: 0.0253 on 21 degrees of freedom
## Multiple R-squared: 0.008588, Adjusted R-squared: -0.03862
## F-statistic: 0.1819 on 1 and 21 DF, p-value: 0.6741
BetaCVX <- summary(lm.CVX)$coefficients[2, 1]
#Model for DD
lm.DD <- lm(DD ~ SP500, data = as.data.frame(MyData))
summary(lm.DD)
##
## Call:
## lm(formula = DD ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.045028 -0.019426 -0.001111 0.023795 0.043311
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.001823 0.005687 0.321 0.752
## SP500 0.399690 0.391765 1.020 0.319
##
## Residual standard error: 0.02721 on 21 degrees of freedom
## Multiple R-squared: 0.04722, Adjusted R-squared: 0.001854
## F-statistic: 1.041 on 1 and 21 DF, p-value: 0.3192
BetaDD <- summary(lm.DD)$coefficients[2, 1]
#Model for DIS
lm.DIS <- lm(DIS ~ SP500, data = as.data.frame(MyData))
summary(lm.DIS)
##
## Call:
## lm(formula = DIS ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.038637 -0.017770 -0.005468 0.011312 0.054707
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.002290 0.005486 -0.417 0.6806
## SP500 0.771826 0.377926 2.042 0.0539 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.02625 on 21 degrees of freedom
## Multiple R-squared: 0.1657, Adjusted R-squared: 0.126
## F-statistic: 4.171 on 1 and 21 DF, p-value: 0.05388
BetaDIS <- summary(lm.DIS)$coefficients[2, 1]
#Model for GE
lm.GE <- lm(GE ~ SP500, data = as.data.frame(MyData))
summary(lm.GE)
##
## Call:
## lm(formula = GE ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.038389 -0.020116 0.000577 0.015061 0.049532
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.002368 0.005018 -0.472 0.642
## SP500 0.715414 0.345731 2.069 0.051 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.02402 on 21 degrees of freedom
## Multiple R-squared: 0.1694, Adjusted R-squared: 0.1298
## F-statistic: 4.282 on 1 and 21 DF, p-value: 0.05105
BetaGE <- summary(lm.GE)$coefficients[2, 1]
#Model for HD
lm.HD <- lm(HD ~ SP500, data = as.data.frame(MyData))
summary(lm.HD)
##
## Call:
## lm(formula = HD ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.036701 -0.014744 0.002064 0.011387 0.046503
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.001181 0.004487 -0.263 0.795
## SP500 0.444162 0.309101 1.437 0.165
##
## Residual standard error: 0.02147 on 21 degrees of freedom
## Multiple R-squared: 0.08952, Adjusted R-squared: 0.04617
## F-statistic: 2.065 on 1 and 21 DF, p-value: 0.1655
BetaHD <- summary(lm.HD)$coefficients[2, 1]
#Model for HPQ
lm.HPQ <- lm(HPQ ~ SP500, data = as.data.frame(MyData))
summary(lm.HPQ)
##
## Call:
## lm(formula = HPQ ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.111113 -0.010222 0.005085 0.024105 0.058758
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.011167 0.008366 -1.335 0.196
## SP500 -0.235339 0.576382 -0.408 0.687
##
## Residual standard error: 0.04004 on 21 degrees of freedom
## Multiple R-squared: 0.007876, Adjusted R-squared: -0.03937
## F-statistic: 0.1667 on 1 and 21 DF, p-value: 0.6872
BetaHPQ <- summary(lm.HPQ)$coefficients[2, 1]
#Model for IBM
lm.IBM <- lm(IBM ~ SP500, data = as.data.frame(MyData))
summary(lm.IBM)
##
## Call:
## lm(formula = IBM ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.038869 -0.009825 -0.002418 0.010032 0.040041
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.004063 0.003823 1.063 0.300
## SP500 0.283707 0.263382 1.077 0.294
##
## Residual standard error: 0.0183 on 21 degrees of freedom
## Multiple R-squared: 0.05236, Adjusted R-squared: 0.007233
## F-statistic: 1.16 on 1 and 21 DF, p-value: 0.2936
BetaIBM <- summary(lm.IBM)$coefficients[2, 1]
#Model for INTC
lm.INTC <- lm(INTC ~ SP500, data = as.data.frame(MyData))
summary(lm.INTC)
##
## Call:
## lm(formula = INTC ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.04340 -0.01412 -0.01017 0.01056 0.08905
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.000631 0.006959 0.091 0.929
## SP500 0.117811 0.479402 0.246 0.808
##
## Residual standard error: 0.0333 on 21 degrees of freedom
## Multiple R-squared: 0.002868, Adjusted R-squared: -0.04461
## F-statistic: 0.06039 on 1 and 21 DF, p-value: 0.8083
BetaINTC <- summary(lm.INTC)$coefficients[2, 1]
#Model for JNJ
lm.JNJ <- lm(JNJ ~ SP500, data = as.data.frame(MyData))
summary(lm.JNJ)
##
## Call:
## lm(formula = JNJ ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.04468 -0.01417 0.00033 0.01109 0.04674
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.002247 0.004392 0.512 0.614
## SP500 -0.340509 0.302555 -1.125 0.273
##
## Residual standard error: 0.02102 on 21 degrees of freedom
## Multiple R-squared: 0.05688, Adjusted R-squared: 0.01197
## F-statistic: 1.267 on 1 and 21 DF, p-value: 0.2731
BetaJNJ <- summary(lm.JNJ)$coefficients[2, 1]
#Model for JPM
lm.JPM <- lm(JPM ~ SP500, data = as.data.frame(MyData))
summary(lm.JPM)
##
## Call:
## lm(formula = JPM ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.035508 -0.014586 0.000598 0.015202 0.036498
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.005897 0.004289 -1.375 0.184
## SP500 0.580051 0.295446 1.963 0.063 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.02052 on 21 degrees of freedom
## Multiple R-squared: 0.1551, Adjusted R-squared: 0.1149
## F-statistic: 3.855 on 1 and 21 DF, p-value: 0.06299
BetaJPM <- summary(lm.JPM)$coefficients[2, 1]
#Model for KO
lm.KO <- lm(KO ~ SP500, data = as.data.frame(MyData))
summary(lm.KO)
##
## Call:
## lm(formula = KO ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.030370 -0.010602 -0.001005 0.011521 0.041325
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.001189 0.003517 0.338 0.739
## SP500 0.175481 0.242260 0.724 0.477
##
## Residual standard error: 0.01683 on 21 degrees of freedom
## Multiple R-squared: 0.02438, Adjusted R-squared: -0.02208
## F-statistic: 0.5247 on 1 and 21 DF, p-value: 0.4768
BetaKO <- summary(lm.KO)$coefficients[2, 1]
#Model for KRFT
lm.KRFT <- lm(KRFT ~ SP500, data = as.data.frame(MyData))
summary(lm.KO)
##
## Call:
## lm(formula = KO ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.030370 -0.010602 -0.001005 0.011521 0.041325
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.001189 0.003517 0.338 0.739
## SP500 0.175481 0.242260 0.724 0.477
##
## Residual standard error: 0.01683 on 21 degrees of freedom
## Multiple R-squared: 0.02438, Adjusted R-squared: -0.02208
## F-statistic: 0.5247 on 1 and 21 DF, p-value: 0.4768
BetaKRFT <- summary(lm.KRFT)$coefficients[2, 1]
#Model for MCD
lm.MCD <- lm(MCD ~ SP500, data = as.data.frame(MyData))
summary(lm.MCD)
##
## Call:
## lm(formula = MCD ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.042231 -0.006555 0.002294 0.009673 0.034190
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.004018 0.003804 1.056 0.3028
## SP500 0.547377 0.262039 2.089 0.0491 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.0182 on 21 degrees of freedom
## Multiple R-squared: 0.172, Adjusted R-squared: 0.1326
## F-statistic: 4.364 on 1 and 21 DF, p-value: 0.04907
BetaMCD <- summary(lm.MCD)$coefficients[2, 1]
#Model for MMM
lm.MMM <- lm(MMM ~ SP500, data = as.data.frame(MyData))
summary(lm.MMM)
##
## Call:
## lm(formula = MMM ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.031615 -0.017256 0.002137 0.011217 0.039293
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.001302 0.004349 0.299 0.768
## SP500 0.273591 0.299618 0.913 0.372
##
## Residual standard error: 0.02081 on 21 degrees of freedom
## Multiple R-squared: 0.03819, Adjusted R-squared: -0.007612
## F-statistic: 0.8338 on 1 and 21 DF, p-value: 0.3715
BetaMMM <- summary(lm.MMM)$coefficients[2, 1]
#Model for MRK
lm.MRK <- lm(MRK ~ SP500, data = as.data.frame(MyData))
summary(lm.MRK)
##
## Call:
## lm(formula = MRK ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.026928 -0.015688 -0.004038 0.014662 0.048367
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0002451 0.0042573 0.058 0.955
## SP500 0.3824517 0.2932955 1.304 0.206
##
## Residual standard error: 0.02037 on 21 degrees of freedom
## Multiple R-squared: 0.0749, Adjusted R-squared: 0.03085
## F-statistic: 1.7 on 1 and 21 DF, p-value: 0.2064
BetaMRK <- summary(lm.MRK)$coefficients[2, 1]
#Model for MSFT
lm.MSFT <- lm(MSFT ~ SP500, data = as.data.frame(MyData))
summary(lm.MSFT)
##
## Call:
## lm(formula = MSFT ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.026672 -0.015547 -0.000618 0.011350 0.041069
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.006536 0.004046 -1.616 0.121
## SP500 0.110945 0.278716 0.398 0.695
##
## Residual standard error: 0.01936 on 21 degrees of freedom
## Multiple R-squared: 0.007489, Adjusted R-squared: -0.03977
## F-statistic: 0.1585 on 1 and 21 DF, p-value: 0.6946
BetaMSFT <- summary(lm.MSFT)$coefficients[2, 1]
#Model for PFE
lm.PFE <- lm(PFE ~ SP500, data = as.data.frame(MyData))
summary(lm.PFE)
##
## Call:
## lm(formula = PFE ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.039599 -0.022258 -0.001526 0.013618 0.048516
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.003826 0.005473 0.699 0.492
## SP500 0.479915 0.377049 1.273 0.217
##
## Residual standard error: 0.02619 on 21 degrees of freedom
## Multiple R-squared: 0.07162, Adjusted R-squared: 0.02741
## F-statistic: 1.62 on 1 and 21 DF, p-value: 0.217
BetaPFE <- summary(lm.PFE)$coefficients[2, 1]
#Model for PG
lm.PG <- lm(PG ~ SP500, data = as.data.frame(MyData))
summary(lm.PG)
##
## Call:
## lm(formula = PG ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.032400 -0.011244 -0.002129 0.008728 0.042603
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.002092 0.003713 -0.563 0.579
## SP500 0.263308 0.255816 1.029 0.315
##
## Residual standard error: 0.01777 on 21 degrees of freedom
## Multiple R-squared: 0.04803, Adjusted R-squared: 0.002694
## F-statistic: 1.059 on 1 and 21 DF, p-value: 0.3151
BetaPG <- summary(lm.PG)$coefficients[2, 1]
#Model for T
lm.T <- lm(T ~ SP500, data = as.data.frame(MyData))
summary(lm.T)
##
## Call:
## lm(formula = T ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.032182 -0.011611 0.000050 0.004279 0.055697
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.002718 0.003877 0.701 0.491
## SP500 0.456481 0.267079 1.709 0.102
##
## Residual standard error: 0.01855 on 21 degrees of freedom
## Multiple R-squared: 0.1221, Adjusted R-squared: 0.08032
## F-statistic: 2.921 on 1 and 21 DF, p-value: 0.1022
BetaT <- summary(lm.T)$coefficients[2, 1]
#Model for TRV
lm.TRV <- lm(TRV ~ SP500, data = as.data.frame(MyData))
summary(lm.TRV)
##
## Call:
## lm(formula = TRV ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.025969 -0.011764 -0.004341 0.014924 0.034865
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.001389 0.003895 0.357 0.725
## SP500 0.411462 0.268364 1.533 0.140
##
## Residual standard error: 0.01864 on 21 degrees of freedom
## Multiple R-squared: 0.1007, Adjusted R-squared: 0.05785
## F-statistic: 2.351 on 1 and 21 DF, p-value: 0.1401
BetaTRV <- summary(lm.TRV)$coefficients[2, 1]
#Model for UTX
lm.UTX <- lm(UTX ~ SP500, data = as.data.frame(MyData))
summary(lm.UTX)
##
## Call:
## lm(formula = UTX ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.036825 -0.014469 -0.005822 0.015152 0.044403
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.002886 0.004537 0.636 0.532
## SP500 0.134830 0.312581 0.431 0.671
##
## Residual standard error: 0.02171 on 21 degrees of freedom
## Multiple R-squared: 0.008782, Adjusted R-squared: -0.03842
## F-statistic: 0.1861 on 1 and 21 DF, p-value: 0.6706
BetaUTX <- summary(lm.UTX)$coefficients[2, 1]
#Model for VZ
lm.VZ <- lm(VZ ~ SP500, data = as.data.frame(MyData))
summary(lm.VZ)
##
## Call:
## lm(formula = VZ ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.023881 -0.014426 -0.000661 0.009177 0.044751
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0004706 0.0036771 0.128 0.899
## SP500 0.3599403 0.2533225 1.421 0.170
##
## Residual standard error: 0.0176 on 21 degrees of freedom
## Multiple R-squared: 0.08771, Adjusted R-squared: 0.04426
## F-statistic: 2.019 on 1 and 21 DF, p-value: 0.17
BetaVZ <- summary(lm.VZ)$coefficients[2, 1]
#Model for WMT
lm.WMT <- lm(WMT ~ SP500, data = as.data.frame(MyData))
summary(lm.WMT)
##
## Call:
## lm(formula = WMT ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.064114 -0.007832 0.000849 0.012119 0.025335
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.001897 0.004063 -0.467 0.645
## SP500 0.137241 0.279876 0.490 0.629
##
## Residual standard error: 0.01944 on 21 degrees of freedom
## Multiple R-squared: 0.01132, Adjusted R-squared: -0.03576
## F-statistic: 0.2405 on 1 and 21 DF, p-value: 0.629
BetaWMT <- summary(lm.WMT)$coefficients[2, 1]
#Model for XOM
lm.XOM <- lm(XOM ~ SP500, data = as.data.frame(MyData))
summary(lm.XOM)
##
## Call:
## lm(formula = XOM ~ SP500, data = as.data.frame(MyData))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.060482 -0.015971 0.000493 0.015014 0.053415
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.0003439 0.0053718 -0.064 0.950
## SP500 0.0539848 0.3700786 0.146 0.885
##
## Residual standard error: 0.02571 on 21 degrees of freedom
## Multiple R-squared: 0.001012, Adjusted R-squared: -0.04656
## F-statistic: 0.02128 on 1 and 21 DF, p-value: 0.8854
BetaXOM <- summary(lm.XOM)$coefficients[2, 1]
# Display betas
paste("Beta of AA: ", BetaAA)
## [1] "Beta of AA: 0.306354776120703"
paste("Beta of AXP: ", BetaAXP)
## [1] "Beta of AXP: 0.801278586406885"
paste("Beta of BA: ", BetaBA)
## [1] "Beta of BA: 0.35814062155553"
paste("Beta of BAC: ", BetaBAC)
## [1] "Beta of BAC: 1.2565665507821"
paste("Beta of CAT: ", BetaCAT)
## [1] "Beta of CAT: 0.349837279026352"
paste("Beta of CSCO: ", BetaCSCO)
## [1] "Beta of CSCO: 0.204975853538089"
paste("Beta of CVX: ", BetaCVX)
## [1] "Beta of CVX: -0.155348031565196"
paste("Beta of DD: ", BetaDD)
## [1] "Beta of DD: 0.39969027038004"
paste("Beta of DIS: ", BetaDIS)
## [1] "Beta of DIS: 0.771826338820731"
paste("Beta of GE: ", BetaGE)
## [1] "Beta of GE: 0.715414496369996"
paste("Beta of HD: ", BetaHD)
## [1] "Beta of HD: 0.444162451791471"
paste("Beta of HPQ: ", BetaHPQ)
## [1] "Beta of HPQ: -0.235339447119812"
paste("Beta of IBM: ", BetaIBM)
## [1] "Beta of IBM: 0.283706884688943"
paste("Beta of INTC: ", BetaINTC)
## [1] "Beta of INTC: 0.1178111734686"
paste("Beta of JNJ: ", BetaJNJ)
## [1] "Beta of JNJ: -0.340508808878504"
paste("Beta of JPM: ", BetaJPM)
## [1] "Beta of JPM: 0.580051053760392"
paste("Beta of KO: ", BetaKO)
## [1] "Beta of KO: 0.175481246903899"
paste("Beta of KRFT: ", BetaKRFT)
## [1] "Beta of KRFT: 0.214370501404088"
paste("Beta of MCD: ", BetaMCD)
## [1] "Beta of MCD: 0.547377294139315"
paste("Beta of MMM: ", BetaMMM)
## [1] "Beta of MMM: 0.273591200231556"
paste("Beta of MRK: ", BetaMRK)
## [1] "Beta of MRK: 0.382451665861558"
paste("Beta of MSFT: ", BetaMSFT)
## [1] "Beta of MSFT: 0.110945264398497"
paste("Beta of PFE: ", BetaPFE)
## [1] "Beta of PFE: 0.479914759329808"
paste("Beta of PG: ", BetaPG)
## [1] "Beta of PG: 0.263308018325903"
paste("Beta of T: ", BetaT)
## [1] "Beta of T: 0.456480571124317"
paste("Beta of TRV: ", BetaTRV)
## [1] "Beta of TRV: 0.411461545045901"
paste("Beta of UTX: ", BetaUTX)
## [1] "Beta of UTX: 0.13482974300242"
paste("Beta of VZ: ", BetaVZ)
## [1] "Beta of VZ: 0.359940283752318"
paste("Beta of WMT: ", BetaWMT)
## [1] "Beta of WMT: 0.137241467534037"
paste("Beta of XOM: ", BetaXOM)
## [1] "Beta of XOM: 0.0539848182889091"
# Treasury data
treasury <- read_excel("/Users/doris/OneDrive/Documents/Treasury.xlsx")
str(treasury)
## tibble [25 × 7] (S3: tbl_df/tbl/data.frame)
## $ Date : POSIXct[1:25], format: "2011-06-18" "2011-06-11" ...
## $ Open : num [1:25] 2.9 2.99 3 3.09 3.1 ...
## $ High : num [1:25] 3 3.1 3.06 3.1 3.16 ...
## $ Low : num [1:25] 2.89 2.89 2.92 2.95 3.06 ...
## $ Close : num [1:25] 2.91 2.94 2.97 3 3.06 ...
## $ Adj Close : num [1:25] 2.91 2.94 2.97 3 3.06 ...
## $ Volume : chr [1:25] "-" "-" "-" "-" ...
colSums(is.na(treasury))
## Date Open High Low Close Adj Close Volume
## 0 0 0 0 0 0 0
# Remove leading and trailing spaces from all column names
colnames(treasury) <- trimws(colnames(treasury))
# Check the column names again
colnames(treasury)
## [1] "Date" "Open" "High" "Low" "Close "
## [6] "Adj Close " "Volume"
# Print the exact column names with quotation marks to see any hidden characters
print(colnames(treasury))
## [1] "Date" "Open" "High" "Low" "Close "
## [6] "Adj Close " "Volume"
# Fix column names by completely redefining them
colnames(treasury) <- c("Date", "Open", "High", "Low", "Close", "AdjClose", "Volume")
# Now try to access the Close column
average_risk_free_rate <- mean(treasury$Close, na.rm = TRUE)
cat("The average risk-free rate is:", round(average_risk_free_rate, 4), "\n")
## The average risk-free rate is: 3.3133
# Define risk-free rate (using the average from your Treasury data)
Rf <- 3.3133/100 # Convert percentage to decimal
# Calculate the average market return (S&P500)
market_return <- mean(MyData[, "SP500"], na.rm = TRUE)
# Calculate excess market return
excess_market_return <- market_return - Rf
# Initialize a dataframe to store results
results <- data.frame(Stock = colnames(MyData)[-1], # Exclude S&P500
Beta = numeric(ncol(MyData) - 1),
ExpectedReturn = numeric(ncol(MyData) - 1))
# Calculate beta and expected returns for each stock
for (i in 2:ncol(MyData)) {
# Calculate beta using covariance/variance
stock_returns <- MyData[, i]
market_returns <- MyData[, "SP500"]
# Beta calculation
beta <- cov(stock_returns, market_returns, use = "complete.obs") /
var(market_returns, na.rm = TRUE)
# Expected return using CAPM
expected_return <- Rf + beta * excess_market_return
# Store results
stock_idx <- i - 1
results$Beta[stock_idx] <- beta
results$ExpectedReturn[stock_idx] <- expected_return * 100 # Convert to percentage
}
# Print results
print(results)
## Stock Beta ExpectedReturn
## 1 AA 0.30635478 2.3272657
## 2 AXP 0.80127859 0.7343026
## 3 BA 0.35814062 2.1605876
## 4 BAC 1.25656655 -0.7310885
## 5 CAT 0.34983728 2.1873128
## 6 CSCO 0.20497585 2.6535642
## 7 CVX -0.15534803 3.8133036
## 8 DD 0.39969027 2.0268558
## 9 DIS 0.77182634 0.8290977
## 10 GE 0.71541450 1.0106650
## 11 HD 0.44416245 1.8837175
## 12 HPQ -0.23533945 4.0707642
## 13 IBM 0.28370688 2.4001603
## 14 INTC 0.11781117 2.9341126
## 15 JNJ -0.34050881 4.4092626
## 16 JPM 0.58005105 1.4463461
## 17 KO 0.17548125 2.7484956
## 18 KRFT 0.21437050 2.6233265
## 19 MCD 0.54737729 1.5515100
## 20 MMM 0.27359120 2.4327186
## 21 MRK 0.38245167 2.0823400
## 22 MSFT 0.11094526 2.9562113
## 23 PFE 0.47991476 1.7686451
## 24 PG 0.26330802 2.4658161
## 25 T 0.45648057 1.8440704
## 26 TRV 0.41146155 1.9889688
## 27 UTX 0.13482974 2.8793366
## 28 VZ 0.35994028 2.1547952
## 29 WMT 0.13724147 2.8715742
## 30 XOM 0.05398482 3.1395443