############# Chow Test and Non-Nested Tests, Trilogy of Tests (Wald, LR & LM tests) & Specification tests (LM Tests & RESET) (Code #8 - Lecture 13) ############# SFX_da <- read.csv("http://www.bauer.uh.edu/rsusmel/4397/Stocks_FX_1973.csv",head=TRUE,sep=",") names(SFX_da) x_date <- SFX_da$Date x_sp <- SFX_da$SP500 # extract S&P500 price data x_ibm <- SFX_da$IBM # extract IBM price data x_ko <- SFX_da$KO # extract Coca-Cola price data 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]) lr_ko <- log(x_ko[-1]/x_ko[-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 # IBM excess returns ko_x <- lr_ko - RF # IBM excess returns #### Testing Linear Hypothesis in the Fama-French 3 factor Linear Model # OLS results y <- ibm_x x <- cbind(Mkt_RF, SMB, HML) T <- length(y) fit_y_ff3 <- lm(y ~ Mkt_RF + SMB + HML) summary(fit_y_ff3) #### Testing with Unrestricted and Restricted Estimations ## with package 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 - 2024) e_p <- fit_y_ff3$residuals # extract residuals from pooled FF regression RSS_p <- sum(e_p^2) ## Before T_sb RSS (1973 - 2001) T_sb <- 347 x_date[T_sb] y1 <- y[1:T_sb] x1 <- x[1:T_sb,] fit_y_ff3_1 <- lm(y1 ~ x1 -1) # Add -1 inside lm() so no constant is added to regressors summary(fit_y_ff3_1) e_1 <- fit_y_ff3_1$residuals # extract residuals from pooled FF regression RSS_1 <- sum(e_1^2) ## After T_sb RSS (2002 - 2024) T2 <- T_sb + 1 y2 <- y[T2:T] x2 <- x[T2:T,] fit_y_ff3_2 <- lm(y2 ~ x2 -1) # Add -1 inside lm() so no constant is added to regressors summary(fit_y_ff3_2) e_2 <- fit_y_ff3_2$residuals # extract residuals from pooled FF regression RSS_2 <- sum(e_2^2) ## Compute Chow test k <- length(fit_y_ff3$coefficients) 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 (need to install package sandwich too) T_sb <- 347 library(strucchange) library(sandwich) sctest(ibm_x ~ Mkt_RF + SMB + HML, type = "Chow", point = T_sb) sctest(ko_x ~ Mkt_RF + SMB + HML, type = "Chow", point = T_sb) ##### Testing Non-Nested Models ### Encompassing Test SF_da <- read.csv("http://www.bauer.uh.edu/rsusmel/4397/SpFor_prices.csv", head=TRUE, sep=",") x_date <- SF_da$Date x_S <- SF_da$GBPSP # Extract USD/GBP exchange rate x_F3m <- SF_da$GBP3M i_us3 <- SF_da$Dep_USD3M # Extract US 3-mo interest rate (i_d) i_uk3 <- SF_da$Dep_UKP3M # Extract UK 3-mo interest rate (i_f_ cpi_uk <- SF_da$UK_CPI # Extract UK CPI cpi_us <- SF_da$US_CPI # Extract US CPI T <- length(x_S) int_dif <- (i_us3[-1] - i_uk3[-1])/100 # (i_d - i_f): interest rate differential lr_usdgbp <- log(x_S[-1]/x_S[-T]) # log changes in USD/GBP exchange rate I_us <- log(cpi_us[-1]/cpi_us[-T]) # US Inflation rate (I_d): log changes in US CPI I_uk <- log(cpi_uk[-1]/cpi_uk[-T]) # UK Inflation rate (I_f): log changes in UK CPI inf_dif <- (I_us - I_uk) # (I_d - I_f): inflation rate differential fit_me <- lm(lr_usdgbp ~ int_dif + inf_dif) # Encompassing Model (ME) summary(fit_me) ## With R package lmtest library(lmtest) fit_m1 <- lm(lr_usdgbp ~ int_dif) # Restricted Model 1 fit_m2 <- lm(lr_usdgbp ~ inf_dif) # Restricted Model 2 encomptest(fit_m1, fit_m2) ### J-Test y <- lr_usdgbp ## Model 1 fit_m1 <- lm( y ~ int_dif) summary(fit_m1) ## Fitted Values of Model 1 into Model 2 y_hat1 <- fitted(fit_m1) fit_J1 <- lm( y ~ inf_dif + y_hat1) summary(fit_J1) ## Model 2 fit_m2 <- lm( y ~ inf_dif) summary(fit_m2) ## Fitted Values of Model 2 into Model 1 y_hat2 <- fitted(fit_m2) fit_J2 <- lm( y ~ int_dif + y_hat2) summary(fit_J2) ## Using R package lmtest library(lmtest) fit_m1 <- lm(lr_usdgbp ~ int_dif) fit_m2 <- lm(lr_usdgbp ~ inf_dif) jtest(fit_m1, fit_m2) ##### Omitted Variables ## Model for US 3-mo interest rates as a function of US Inflation, Exchange Rates, and Canadian 3-mo int rate Fca_da <- read.csv("http://www.bauer.uh.edu/rsusmel/4397/FX_USA_CAN.csv", head=TRUE, sep=",") names(Fca_da) summary(Fca_da) us_CPI <- Fca_da$US_CPI # Extract US CPI us_U <- Fca_da$US_U # Extract US Unemployment rate us_i <- Fca_da$US_I3M # Extract US interest rates (in %) can_i <- Fca_da$Can_I3M # Extract Canadian interest rate can_CPI <- Fca_da$Can_CPI # Extract US CPI S_cad <- Fca_da$CAD_USD # Extract USD/EUR exchange rate x_Mkt_RF<- Fca_da$Mkt_RF x_SMB <- Fca_da$SMB x_HML <- Fca_da$HML T <- length(us_CPI) us_I <- log(us_CPI[-1]/us_CPI[-T]) # US Inflation: (Log) Changes in CPI can_I <- log(can_CPI[-1]/can_CPI[-T]) # US Money Growth: (Log) Changes in M1 e_cad <- log(S_cad[-1]/S_cad[-T]) # (Log) Changes in USD/EUR us_i_1 <- us_i[-1] # Adjust sample size of untransformed data can_i_1 <- can_i[-1] # Adjust sample size of untransformed data us_u_1 <- us_U[-1] # Adjust sample size of untransformed data us_i_0 <- us_i[-T] # lagged interest rates, by removing T observation SMB <- x_SMB[-1]/100 HML <- x_HML[-1]/100 xx_i <- cbind(us_I ,e_cad, can_i_1) # X matrix summary(xx_i) fit_i <- lm(us_i_1 ~ xx_i) summary(fit_i) xx_i <- cbind(us_I ,e_cad, can_i_1, us_i_0) # X matrix with lagged interest rates fit_i <- lm(us_i_1 ~ xx_i) summary(fit_i) ## Irrelevant Variables (Add SMB as an explanatory variable) xx_i <- cbind(us_I ,e_cad, can_i_1, us_i_0, SMB) # X matrix with lagged interest rates fit_i <- lm(us_i_1 ~ xx_i) summary(fit_i) ##### Trilogy of Tests SFX_da <- read.csv("http://www.bauer.uh.edu/rsusmel/4397/Stocks_FX_1973.csv",head=TRUE,sep=",") 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]) 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 # IBM excess returns ### Specification Test: Using Wald & LM Tests to test for Omitted Variables (SMB + HML in CAPM) library(lmtest) fit_ibm_ff3 <- lm(ibm_x ~ Mkt_RF + SMB + HML) fit_ibm_capm <- lm(ibm_x ~ Mkt_RF) waldtest(fit_ibm_ff3, fit_ibm_capm) lrtest(fit_ibm_ff3, fit_ibm_capm) ### Specification Test: Using LM Tests to test for Omitted Variables (SMB + HML in CAPM) T <- length(ibm_x) resid_r <- fit_ibm_capm$residuals # extract residuals from R model fit_lm <- lm (resid_r ~ Mkt_RF + SMB + HML) # auxiliary regression summary(fit_lm) R2_r <- summary(fit_lm)$r.squared # extract R2 from fit_lm R2_r LM_test <- R2_r * T LM_test qchisq(.95, df = 2) # chi-squared (df=2) value at 5% level p_val <- 1 - pchisq(LM_test, df = 2) # p-value of LM_test p_val ### Specification Test: Using LM Tests to test for Non linearities (Omitted Variable are Mkt_RF^2, SMB^2 + HML^2) ## Wald Test for including in FF3 Model only MKt_RF^2 Mkt_RF2 <- Mkt_RF^2 fit_ibm_ff3_2 <- lm (ibm_x ~ Mkt_RF + SMB + HML + Mkt_RF2) summary(fit_ibm_ff3_2) ## LM Test for including in FF3 Model MKt_RF^2, SMB^2 + HML^2 SMB2 <- SMB^2 HML2 <- HML^2 resid_r <- fit_ibm_ff3$residuals fit_lm <- lm (resid_r ~ Mkt_RF + SMB + HML + Mkt_RF2 + SMB2 + HML2) R2_r <- summary(fit_lm)$r.squared LM_test <- R2_r * T LM_test qchisq(.95, df = 3) # 95% quantile for Chi-square distribution p_val <- 1 - pchisq(LM_test, df = 3) # p-value of LM_test p_val