#S&P500 + 30 stocks. ^GSPC,AMZN,BA,COST,CVX, DIS,ELY,FL,GILD,GS, HD,JCP,LMT,M,MMM, PG,S,SBUX,SHOO,SMRT, TM,YUM,K,C,XOM, IBM,F,CAT,MCD,COKE,AAPL #Get data for three periods: #2009-12-31 to 2012-12-31, historical period 1 #2012-12-31 to 2015-12-31, historical period 2 #2015-12-31 to 2018-10-31, forecast period #Read your csv file: a1 <- read.csv("stockData-8.csv", sep=",", header=TRUE) a2 <- read.csv("stockData-9.csv", sep=",", header=TRUE) a3 <- read.csv("stockData-10.csv", sep=",", header=TRUE) #Convert adjusted close prices into returns: r1 <- (a1[-1,3:ncol(a1)]-a1[-nrow(a1),3:ncol(a1)])/a1[-nrow(a1),3:ncol(a1)] r2 <- (a2[-1,3:ncol(a2)]-a2[-nrow(a2),3:ncol(a2)])/a2[-nrow(a2),3:ncol(a2)] r3 <- (a3[-1,3:ncol(a3)]-a3[-nrow(a3),3:ncol(a3)])/a3[-nrow(a3),3:ncol(a3)] #=============================================== #Compute betas of all stocks in the three periods: #Initialize vectors: beta1 <- rep(0,30) beta2 <- rep(0,30) beta3 <- rep(0,30) for(i in 1:30){ q1 <- lm(data=r1, formula=r1[,i+1] ~ r1[,1]) beta1[i] <- q1$coefficients[2] q2 <- lm(data=r2, formula=r2[,i+1] ~ r2[,1]) beta2[i] <- q2$coefficients[2] q3 <- lm(data=r3, formula=r3[,i+1] ~ r3[,1]) beta3[i] <- q3$coefficients[2] } #Compute PRESS without adjusting the betas: PRESS1 <- sum((beta3-beta2)^2)/30 #Compute PRESS after adjusting the betas: qq <- lm(beta2 ~ beta1) beta22 <- qq$coef[1] + beta2*qq$coef[2] PRESS2 <- sum((beta3-beta22)^2)/30