Last updated: 2020-12-29

Checks: 6 1

Knit directory: R-Guide/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


The R Markdown file has unstaged changes. To know which version of the R Markdown file created these results, you’ll want to first commit it to the Git repo. If you’re still working on the analysis, you can ignore this warning. When you’re finished, you can run wflow_publish to commit the R Markdown file and build the HTML.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20201221) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 62aa447. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .RData
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/

Unstaged changes:
    Modified:   analysis/q_8731_hw3.Rmd

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/q_8731_hw3.Rmd) and HTML (docs/q_8731_hw3.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd 62aa447 KaranSShakya 2020-12-28 8731 qt3
html 62aa447 KaranSShakya 2020-12-28 8731 qt3
Rmd 97b8329 KaranSShakya 2020-12-28 8731 qt 2
html 97b8329 KaranSShakya 2020-12-28 8731 qt 2
Rmd bd2489c KaranSShakya 2020-12-27 8731 q1 half solved
html bd2489c KaranSShakya 2020-12-27 8731 q1 half solved
Rmd b07f4c3 KaranSShakya 2020-12-27 econometrics problems page + data
html b07f4c3 KaranSShakya 2020-12-27 econometrics problems page + data

1 Lagged Regression

Dataset is:

# A tibble: 2 x 5
    Obs  Year Quarter Income Consumption
  <dbl> <dbl>   <dbl>  <dbl>       <dbl>
1     1  1947       1 59505        57168
2     2  1947       2 59717.       55464

\(c_t\) is log of consumption, \(y_t\) is log of income. Their first differences are \(\Delta c_t = c_t - c_{t-1}\) and \(\Delta y_t = y_t - y_{t-1}\).

The model for the period 1953:1 to 1996:4, \[ \Delta c_t = \beta_1 + \beta_2 \Delta y_t + \beta_3 \Delta y_{t-1} + \beta_4 \Delta y_{t-2} + \beta_5 \Delta y_{t-3} + \beta_6 \Delta y_{t-4} \] ## Estimation Let \(\gamma = \sum_{l=2}^{6} \beta_l\). Calculate \(\hat{\gamma}\) and its SE.

consumption1 <- consumption %>% 
  mutate(ct = log(Consumption), yt=log(Income),
         del_ct = ct-lag(ct),
         del_yt = yt-lag(yt),
         yt1 = lag(yt, 1),
         yt2 = lag(yt, 2),
         yt3 = lag(yt, 3),
         yt4 = lag(yt, 4),
         del_yt1 = yt1-lag(yt1),
         del_yt2 = yt2-lag(yt2),
         del_yt3 = yt3-lag(yt3),
         del_yt4 = yt4-lag(yt4),
         ct1 = lag(ct, 1)) #adding new variables 

cons1 <- consumption1 %>% 
  filter(Year >= 1953 & Year <= 1996)

cons1.model <- lm(del_ct ~ del_yt+del_yt1+del_yt2+del_yt3+del_yt4, data=cons1)
summ(cons1.model, digits = 4)
MODEL INFO:
Observations: 176
Dependent Variable: del_ct
Type: OLS linear regression 

MODEL FIT:
F(5,170) = 10.6489, p = 0.0000
R² = 0.2385
Adj. R² = 0.2161 

Standard errors: OLS
-------------------------------------------------------
                       Est.     S.E.    t val.        p
----------------- --------- -------- --------- --------
(Intercept)          0.0051   0.0012    4.3589   0.0000
del_yt               0.3058   0.0545    5.6117   0.0000
del_yt1              0.1242   0.0544    2.2813   0.0238
del_yt2              0.0425   0.0551    0.7724   0.4410
del_yt3              0.1239   0.0545    2.2730   0.0243
del_yt4             -0.1233   0.0539   -2.2883   0.0234
-------------------------------------------------------
gamma_h <- cons1.model$coefficients[2]+cons1.model$coefficients[3]+
  cons1.model$coefficients[4]+cons1.model$coefficients[5]+
  cons1.model$coefficients[6]
kable(gamma_h, row.names = F, align = "l")
x
0.4731631

2 Hypothesis Tests

The dataset is as follows:

# A tibble: 5 x 4
    Obs   Dep    X_2    X_3
  <dbl> <dbl>  <dbl>  <dbl>
1     1  2.75 0.0121 0.0365
2     2  8.23 1.69   3.74  
3     3  6.70 0.404  2.31  
4     4  7.28 0.606  1.62  
5     5  7.64 2.11   6.95  

Classical linear regression model, \[ \begin{align} y = \beta_1 + \beta_2 x_2 + \beta_3 x_3 + u, && u \sim N (0, \sigma^2 I) \end{align} \]

model.class <- lm(Dep~X_2+X_3, data = classical)
summ(model.class, digits = 4)
MODEL INFO:
Observations: 50
Dependent Variable: Dep
Type: OLS linear regression 

MODEL FIT:
F(2,47) = 68.1609, p = 0.0000
R² = 0.7436
Adj. R² = 0.7327 

Standard errors: OLS
------------------------------------------------------
                      Est.     S.E.    t val.        p
----------------- -------- -------- --------- --------
(Intercept)         5.0949   0.1976   25.7815   0.0000
X_2                 1.1349   0.1408    8.0583   0.0000
X_3                 0.2221   0.0945    2.3487   0.0231
------------------------------------------------------

test \(\beta_3 = 0\)

linearHypothesis(model.class, "X_3 = 0")
Linear hypothesis test

Hypothesis:
X_3 = 0

Model 1: restricted model
Model 2: Dep ~ X_2 + X_3

  Res.Df    RSS Df Sum of Sq      F  Pr(>F)  
1     48 44.114                              
2     47 39.481  1    4.6339 5.5165 0.02309 *
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Or we could have simply looked at the t-value provided at the summ table. The p-values are the same for both.

Warning in remove(classical, model.class, X_3): object 'X_3' not found

3 Dummy Variables

Dummy variable dataset, where Group 1, Group 2, and Group 3 are dummy variables.

# A tibble: 3 x 5
    Obs Group1 Group2 Group3 Earnings
  <dbl>  <dbl>  <dbl>  <dbl>    <dbl>
1     1      0      0      1     570.
2     2      0      0      1     896.
3     3      0      0      1    1111 

Regress y on all 3 dummy variables. Important to remove the intercept term. And construct a 0.95 CI of Group3:

model.dum <- lm(Earnings~-1+Group1+Group2+Group3, data=earnings)
summ(model.dum, confint = T, ci.width = 0.95, digits = 2)
MODEL INFO:
Observations: 4266
Dependent Variable: Earnings
Type: OLS linear regression 

MODEL FIT:
F(3,4263) = 3842.49, p = 0.00
R² = 0.73
Adj. R² = 0.73 

Standard errors: OLS
-------------------------------------------------------------
                   Est.       2.5%      97.5%   t val.      p
------------ ---------- ---------- ---------- -------- ------
Group1         22880.48   21964.03   23796.92    48.95   0.00
Group2         25080.17   24335.14   25825.21    66.00   0.00
Group3         27973.63   27180.06   28767.21    69.11   0.00
-------------------------------------------------------------

Regress y on Group3 only:

model.dum3 <- lm(Earnings~-1+Group3, data=earnings)
summ(model.dum3, digits = 2, confint = T, ci.width = 0.95, robust = "HC1")
MODEL INFO:
Observations: 4266
Dependent Variable: Earnings
Type: OLS linear regression 

MODEL FIT:
F(1,4265) = 1849.35, p = 0.00
R² = 0.30
Adj. R² = 0.30 

Standard errors: Robust, type = HC1
-------------------------------------------------------------
                   Est.       2.5%      97.5%   t val.      p
------------ ---------- ---------- ---------- -------- ------
Group3         27973.63   27132.54   28814.73    65.20   0.00
-------------------------------------------------------------

4 Log - Elasticities

We start off with the regression:

model.gas <- lm(Gas_pop~Year+GasP+Income+Pnc+Puc+Ppt+Pd+Pn+Ps, 
                data=gasoline)
summ(model.gas, digits = 10)
MODEL INFO:
Observations: 52
Dependent Variable: Gas_pop
Type: OLS linear regression 

MODEL FIT:
F(9,42) = 9827.1336396413, p = 0.0000000000
R² = 0.9995253497
Adj. R² = 0.9994236390 

Standard errors: OLS
-------------------------------------------------------------------------------
                             Est.           S.E.          t val.              p
----------------- --------------- -------------- --------------- --------------
(Intercept)          0.0038179534   0.0011927155    3.2010594676   0.0026091196
Year                -0.0000020401   0.0000006219   -3.2803740791   0.0020897345
GasP                 0.0000050346   0.0000001744   28.8615597254   0.0000000000
Income               0.0000000147   0.0000000023    6.4728930994   0.0000000829
Pnc                  0.0000017334   0.0000005632    3.0777460893   0.0036654373
Puc                  0.0000006405   0.0000002136    2.9988805264   0.0045399261
Ppt                  0.0000000137   0.0000002121    0.0644989404   0.9488789686
Pd                  -0.0000030835   0.0000005210   -5.9183360943   0.0000005206
Pn                   0.0000009728   0.0000005524    1.7609219249   0.0855314029
Ps                  -0.0000005597   0.0000003506   -1.5961901256   0.1179443038
-------------------------------------------------------------------------------

The log transformation for elasticity:

model.logg <- lm(log(Gas_pop)~Year+log(GasP)+
                     log(Income)+log(Pnc)+log(Puc)+log(Ppt)+log(Pd)+
                     log(Pn)+log(Ps), data=gasoline)
summ(model.logg, digits = 5)
MODEL INFO:
Observations: 52
Dependent Variable: log(Gas_pop)
Type: OLS linear regression 

MODEL FIT:
F(9,42) = 4950.26087, p = 0.00000
R² = 0.99906
Adj. R² = 0.99886 

Standard errors: OLS
-------------------------------------------------------------
                         Est.       S.E.     t val.         p
----------------- ----------- ---------- ---------- ---------
(Intercept)         -95.22401   13.11888   -7.25855   0.00000
Year                  0.03797    0.00751    5.05370   0.00001
log(GasP)             1.06052    0.05401   19.63552   0.00000
log(Income)           0.99299    0.25038    3.96600   0.00028
log(Pnc)             -0.15472    0.26696   -0.57954   0.56532
log(Puc)             -0.48909    0.08520   -5.74053   0.00000
log(Ppt)              0.01927    0.13645    0.14122   0.88837
log(Pd)               1.73206    0.25989    6.66468   0.00000
log(Pn)              -0.72954    0.26507   -2.75227   0.00870
log(Ps)              -0.86798    0.35291   -2.45949   0.01811
-------------------------------------------------------------

5 Cobb-Douglas Function

The dataset is:

# A tibble: 3 x 14
     Id  Year  Cost     Q    Pl     Sl    Pk    Sk    Pf    Sf   C_Pf  ln_q
  <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl> <dbl>
1     1  1970 0.213     8 6869. 0.329   64.9 0.420  18   0.251 0.0118  2.16
2     4  1970 3.04    869 8373. 0.103   68.2 0.291  21.1 0.606 0.144  22.9 
3     5  1970 9.41   1412 7961. 0.0891  40.7 0.157  41.5 0.754 0.226  26.3 
# … with 2 more variables: Pk_Pf <dbl>, Pl_Pf <dbl>

The model is:

model.c <- lm(log(C_Pf) ~ log(Q)+ln_q+log(Pk_Pf)+log(Pl_Pf), data=cost)
summ(model.c, digits = 6)
MODEL INFO:
Observations: 158
Dependent Variable: log(C_Pf)
Type: OLS linear regression 

MODEL FIT:
F(4,153) = 4879.590243, p = 0.000000
R² = 0.992222
Adj. R² = 0.992019 

Standard errors: OLS
----------------------------------------------------------------
                         Est.       S.E.       t val.          p
----------------- ----------- ---------- ------------ ----------
(Intercept)         -6.818163   0.252439   -27.009131   0.000000
log(Q)               0.402745   0.031483    12.792425   0.000000
ln_q                 0.060895   0.004325    14.078838   0.000000
log(Pk_Pf)           0.162034   0.040406     4.010187   0.000095
log(Pl_Pf)           0.152445   0.046597     3.271532   0.001322
----------------------------------------------------------------

Estimate the covariance matrix:

vcov(model.c)
              (Intercept)        log(Q)          ln_q    log(Pk_Pf)
(Intercept)  0.0637255474 -0.0023838181  3.104204e-04  3.994585e-03
log(Q)      -0.0023838181  0.0009911868 -1.335824e-04  1.002255e-04
ln_q         0.0003104204 -0.0001335824  1.870819e-05 -1.493338e-05
log(Pk_Pf)   0.0039945854  0.0001002255 -1.493338e-05  1.632609e-03
log(Pl_Pf)  -0.0104712922 -0.0001995679  2.453652e-05 -1.019813e-03
               log(Pl_Pf)
(Intercept) -1.047129e-02
log(Q)      -1.995679e-04
ln_q         2.453652e-05
log(Pk_Pf)  -1.019813e-03
log(Pl_Pf)   2.171313e-03


sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS  10.16

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] AER_1.2-9       survival_3.1-12 sandwich_3.0-0  lmtest_0.9-38  
 [5] zoo_1.8-8       car_3.0-10      carData_3.0-4   gridExtra_2.3  
 [9] GGally_2.0.0    jtools_2.1.1    stargazer_5.2.2 broom_0.5.6    
[13] readxl_1.3.1    forcats_0.5.0   stringr_1.4.0   dplyr_0.8.5    
[17] purrr_0.3.4     readr_1.3.1     tidyr_1.0.3     tibble_3.0.1   
[21] ggplot2_3.3.0   tidyverse_1.3.0 knitr_1.28      workflowr_1.6.2

loaded via a namespace (and not attached):
 [1] nlme_3.1-147       fs_1.4.1           lubridate_1.7.8    RColorBrewer_1.1-2
 [5] httr_1.4.1         rprojroot_1.3-2    tools_4.0.0        backports_1.1.6   
 [9] utf8_1.1.4         R6_2.4.1           DBI_1.1.0          colorspace_1.4-1  
[13] withr_2.2.0        tidyselect_1.1.0   curl_4.3           compiler_4.0.0    
[17] git2r_0.27.1       cli_2.0.2          rvest_0.3.5        xml2_1.3.2        
[21] scales_1.1.1       digest_0.6.25      foreign_0.8-78     rmarkdown_2.6     
[25] rio_0.5.16         pkgconfig_2.0.3    htmltools_0.5.0    highr_0.8         
[29] dbplyr_1.4.3       rlang_0.4.6        rstudioapi_0.11    generics_0.0.2    
[33] jsonlite_1.6.1     zip_2.0.4          magrittr_1.5       Formula_1.2-3     
[37] Matrix_1.2-18      Rcpp_1.0.4.6       munsell_0.5.0      fansi_0.4.1       
[41] abind_1.4-5        lifecycle_0.2.0    stringi_1.4.6      whisker_0.4       
[45] yaml_2.2.1         plyr_1.8.6         grid_4.0.0         promises_1.1.0    
[49] crayon_1.3.4       lattice_0.20-41    haven_2.2.0        splines_4.0.0     
[53] pander_0.6.3       hms_0.5.3          pillar_1.4.4       reprex_0.3.0      
[57] glue_1.4.1         evaluate_0.14      data.table_1.12.8  modelr_0.1.7      
[61] vctrs_0.3.0        httpuv_1.5.2       cellranger_1.1.0   gtable_0.3.0      
[65] reshape_0.8.8      assertthat_0.2.1   xfun_0.19          openxlsx_4.1.5    
[69] later_1.0.0        ellipsis_0.3.0