############# Review of Testing AND Ommited Variables, Trilogy of Tests (Wald, LR & LM tests) & Specification tests (LM Tests & RESET) (Code #8) ############# 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 # OLS results fit_ibm_ff3 <- lm(ibm_x ~ Mkt_RF + SMB + HML) summary(fit_ibm_ff3) ## Test Linear Restrictions using R package "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 #### Testing with Unrestricted and Restricted Estimations ## with package lmtest library(lmtest) fit_wU <- lm (ibm_x ~ Mkt_RF + SMB + HML) fit_wR <- lm (ibm_x ~ Mkt_RF) waldtest(fit_wU, fit_wR) ##### Testing for Structural Change (Chow Test): Dot.com Bubble (T_sb = 347) ## Compute Chow test with R package strucchange T_sb <- 347 library(strucchange) sctest(ibm_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 Fger_da <- read.csv("http://www.bauer.uh.edu/rsusmel/4397/FX_USA_GER.csv", head=TRUE, sep=",") us_CPI <- Fger_da$US_CPI # Extract US CPI us_M1 <- Fger_da$US_M1 # Extract US M1 (Money Supply) us_i <- Fger_da$US_I3M # Extract US interest rates (in %) us_GDP <- Fger_da$US_GDP # Extract US GDP us_u <- Fger_da$US_UN # Extract US unemployment rate S_ger <- Fger_da$USD_EUR # Extract USD/EUR exchange rate T <- length(us_CPI) us_I <- log(us_CPI[-1]/us_CPI[-T]) # US Inflation: (Log) Changes in CPI us_mg <- log(us_M1[-1]/us_M1[-T]) # US Money Growth: (Log) Changes in M1 e_ger <- log(S_ger[-1]/S_ger[-T]) # (Log) Changes in USD/EUR us_i_1 <- us_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 xx_i <- cbind(us_I ,e_ger, us_mg, us_u_1) # X matrix fit_i <- lm(us_i_1 ~ xx_i) summary(fit_i) xx_i <- cbind(us_I ,e_ger, us_mg, us_u_1, us_i_0) # 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 ### Specification Test: RESET Test y_hat <- fitted(fit_ibm_ff3) y_hat2 <- y_hat^2 fit_ramsey <- lm(ibm_x ~ Mkt_RF + SMB + HML + y_hat2) summary(fit_ramsey) y_hat4 <- y_hat^4 fit_ramsey2 <- lm(ibm_x ~ Mkt_RF + SMB + HML + y_hat4) summary(fit_ramsey2) fit_ramsey2 <- lm(ibm_x ~ Mkt_RF + SMB + HML + y_hat2 + y_hat4) summary(fit_ramsey2) ## Using R package lmtest library(lmtest) resettest(fit_ibm_ff3, power=2, type="fitted") resettest(fit_ibm_ff3, power=4, type="fitted")