############# Testing in the CLM: Nested and Non-Nested Formulations (Code #7) ############# SFX_da <- read.csv("http://www.bauer.uh.edu/rsusmel/4397/Stocks_FX_1973.csv",head=TRUE,sep=",") names(SFX_da) x_sp <- SFX_da$SP500 x_ibm <- SFX_da$IBM x_Mkt_RF<- SFX_da$Mkt_RF x_SMB <- SFX_da$SMB x_HML <- SFX_da$HML x_RF <- SFX_da$RF T <- length(x_ibm) lr_ibm <- log(x_ibm[-1]/x_ibm[-T]) lr_sp <- log(x_sp[-1]/x_sp[-T]) x0 <- matrix(1,T-1,1) Mkt_RF <- x_Mkt_RF[-1]/100 SMB <- x_SMB[-1]/100 HML <- x_HML[-1]/100 RF <- x_RF[-1]/100 ibm_x <- lr_ibm - RF #### Testing Linear Hypothesis in the Fama-French 3 factor Linear Model ibm_x <- lr_ibm - RF x <- cbind(x0, Mkt_RF, SMB, HML) k <- ncol(x) # OLS results fit_ibm_ff3 <- lm(ibm_x ~ Mkt_RF + SMB + HML) summary(fit_ibm_ff3) ## Test Linear Restrictions using R package "car" install.packages("car") library(car) linearHypothesis(fit_ibm_ff3, c("SMB = 0.2","HML = 0.6"), test="F") # Exact F test linearHypothesis(fit_ibm_ff3, c("SMB = 0.2","HML = 0.6"), test="Chisq") # Asymptotic F-test linearHypothesis(fit_ibm_ff3, c("SMB = HML"), test="F") # testing beta_SMB = beta_HMB linearHypothesis(fit_ibm_ff3, c("SMB + HML = 1"), test="F") # testing beta_SMB + beta_HMB = 1 #### Goodness of Fit Model y <- ibm_x T <- length(y) x0 <- matrix(1,T,1) x <- cbind(x0, Mkt_RF, SMB, HML) k <- ncol(x) # Unrestricted k e <- fit_ibm_ff3$residuals # we could have used e <- y - x%*%b RSS <- sum(e^2) # If using e <- y - x%*%b you should use RSS <- as.numeric(t(e)%*%e) R2 <- 1 - RSS/sum(y^2) # R-squared R2 summary(fit_ibm_ff3)$r.squared # extracting R-squared from fit_ibm_ff3 F_goodfit <- (R2/(k-1))/((1-R2)/(T-k)) #F-test of goodness of fit. F_goodfit #### Testing with Unrestricted and Restricted Estimations x_r <- cbind(x0,Mkt_RF) # Restricted X vector k_r <- ncol(x_r) fit_ibm_ff3_R <- lm(y ~ Mkt_RF) # CAPM estimation summary(fit_ibm_ff3_R) eR <- fit_ibm_ff3_R$residuals # extracting Restricted residuals RSSR <- as.numeric(t(eR)%*%eR) J <- k - k_r # J = degrees of freedom of numerator F_test <- ((RSSR - RSS)/J)/(RSS/(T-k)) F_test qf(.95, df1=J, df2=(T - k)) # exact distribution (F-dist) if e normal p_val <- 1 - pf(F_test, df1=J, df2=(T - k)) # p-value(F_t) under e normal p_val ### Testing with package lmtest install.packages("lmtest") library(lmtest) fit_wU <- lm (y ~ Mkt_RF + SMB + HML) fit_wR <- lm (y ~ Mkt_RF) waldtest(fit_wU, fit_wR) ##### Testing for Structural Change (Chow Test): Dot.com Bubble (T_sb = 347) ## Pooled RSS (1973 - 2023) e_p <- fit_ibm_ff3$residuals # extract residuals from pooled FF regression RSS_p <- sum(e_p^2) ## Before T_sb RSS (1973 - 2001) T_sb <- 347 y1 <- y[1:T_sb] x1 <- x[1:T_sb,] fit_ibm_ff3_1 <- lm(y1 ~ x1 -1) # Add -1 inside lm() so no constant is added to regressors summary(fit_ibm_ff3_1) e_1 <- fit_ibm_ff3_1$residuals # extract residuals from pooled FF regression RSS_1 <- sum(e_1^2) ## After T_sb RSS (2002 - 2023) T2 <- T_sb + 1 y2 <- y[T2:T] x2 <- x[T2:T,] fit_ibm_ff3_2 <- lm(y2 ~ x2 -1) # Add -1 inside lm() so no constant is added to regressors summary(fit_ibm_ff3_2) e_2 <- fit_ibm_ff3_2$residuals # extract residuals from pooled FF regression RSS_2 <- sum(e_2^2) ## Compute Chow test F_chow_t <- ((RSS_p - (RSS_1 + RSS_2))/k) / ((RSS_1 + RSS_2)/(T-2*k)) # Chow test (F-test) F_chow_t p_val <- 1 - pf(F_chow_t, df1=k, df2=(T - 2*k)) # p-value(F_t) under e normal p_val ## Compute Chow test with R package strucchange library(strucchange) sctest(ibm_x ~ Mkt_RF + SMB + HML, type = "Chow", point = t_s)