Data sets can be downloaded from Stock and Watson. We are working here with California Test School data used in Chapter 4-9. We are using here ‘tidyverse’ package.
library(tidyverse) ## Upload set of packages including dplyr and ggplot2
library(vembedr)
embed_url("https://www.youtube.com/watch?v=WSgzBZHhbds")
We use restricted and unrestricted model and then use ANOVA to test the hypothesis. Alternatively, you can use Sum of Squares of Residuals from Restricted and Unrestricted model for F-test or \(R^2\) from restricted and unrestricted models for F-test.
library(readxl) ## to read Excel data. Data sets
caschool<- read_excel("C:/Users/hp/Dropbox/Applied Econometrics SBP/Stock and Watson Data sets/caschool (DrZahid-PC's conflicted copy 2017-07-12).xls")
## Lets create a an unrestricted model by have three regressors
reg_ur<-lm(testscr~str+el_pct+expn_stu,data=caschool)
## restricted model
reg_r<-lm(testscr~str,data=caschool)
## we can recall summary
summary(reg_ur)$r.squared
## [1] 0.4365923
summary(reg_r)$r.squared
## [1] 0.0512401
#one can run F-test using these r-squares values or simply do anova
anova(reg_r,reg_ur)
## Analysis of Variance Table
##
## Model 1: testscr ~ str
## Model 2: testscr ~ str + el_pct + expn_stu
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 418 144315
## 2 416 85700 2 58616 142.27 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# With single regressor, F-test is the same as t-square
summary(reg_r)
##
## Call:
## lm(formula = testscr ~ str, data = caschool)
##
## Residuals:
## Min 1Q Median 3Q Max
## -47.727 -14.251 0.483 12.822 48.540
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 698.9330 9.4675 73.825 < 2e-16 ***
## str -2.2798 0.4798 -4.751 2.78e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 18.58 on 418 degrees of freedom
## Multiple R-squared: 0.05124, Adjusted R-squared: 0.04897
## F-statistic: 22.58 on 1 and 418 DF, p-value: 2.783e-06
# one can notice that F-stat is 22.58 which is square of t-statistics -4.75
We can write both models as follows:
library(stargazer)# using stargazer to report results in side by side regression table
##
## Please cite as:
## Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
## R package version 5.2.2. https://CRAN.R-project.org/package=stargazer
stargazer(reg_r, reg_ur, type="text")
##
## ====================================================================
## Dependent variable:
## ------------------------------------------------
## testscr
## (1) (2)
## --------------------------------------------------------------------
## str -2.280*** -0.286
## (0.480) (0.481)
##
## el_pct -0.656***
## (0.039)
##
## expn_stu 0.004***
## (0.001)
##
## Constant 698.933*** 649.578***
## (9.467) (15.206)
##
## --------------------------------------------------------------------
## Observations 420 420
## R2 0.051 0.437
## Adjusted R2 0.049 0.433
## Residual Std. Error 18.581 (df = 418) 14.353 (df = 416)
## F Statistic 22.575*** (df = 1; 418) 107.455*** (df = 3; 416)
## ====================================================================
## Note: *p<0.1; **p<0.05; ***p<0.01
mod1<-lm(testscr~str,data=caschool)
mod2<-lm(testscr~str+el_pct,data=caschool)
mod3<-lm(testscr~str+el_pct+meal_pct,data=caschool)
mod4<-lm(testscr~str+el_pct+calw_pct,data=caschool)
mod5<-lm(testscr~str+el_pct+meal_pct+calw_pct,data=caschool)
stargazer(mod1,mod2,mod3,mod4,mod5, type="text",font.size = "small",
align = TRUE,
omit.stat=c("f", "ser"),
column.sep.width = "-15pt")
##
## ===================================================================
## Dependent variable:
## ------------------------------------------------------
## testscr
## (1) (2) (3) (4) (5)
## -------------------------------------------------------------------
## str -2.280*** -1.101*** -0.998*** -1.308*** -1.014***
## (0.480) (0.380) (0.239) (0.307) (0.240)
##
## el_pct -0.650*** -0.122*** -0.488*** -0.130***
## (0.039) (0.032) (0.033) (0.034)
##
## meal_pct -0.547*** -0.529***
## (0.022) (0.032)
##
## calw_pct -0.790*** -0.048
## (0.053) (0.061)
##
## Constant 698.933*** 686.032*** 700.150*** 697.999*** 700.392***
## (9.467) (7.411) (4.686) (6.024) (4.698)
##
## -------------------------------------------------------------------
## Observations 420 420 420 420 420
## R2 0.051 0.426 0.775 0.629 0.775
## Adjusted R2 0.049 0.424 0.773 0.626 0.773
## ===================================================================
## Note: *p<0.1; **p<0.05; ***p<0.01