##Load/Install Packages

install.packages("gdata")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages( "AER" )
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages( "car")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages( "lmtest" )
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages( "zoo"  )
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("plm")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("stargazer")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("forecast")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("glmnet")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
library(gdata)
## gdata: read.xls support for 'XLS' (Excel 97-2004) files ENABLED.
## 
## gdata: read.xls support for 'XLSX' (Excel 2007+) files ENABLED.
## 
## Attaching package: 'gdata'
## The following object is masked from 'package:stats':
## 
##     nobs
## The following object is masked from 'package:utils':
## 
##     object.size
## The following object is masked from 'package:base':
## 
##     startsWith
library(AER)
## Loading required package: car
## Loading required package: carData
## Loading required package: lmtest
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: sandwich
## Loading required package: survival
library(car)
library(lmtest)
library(zoo)
library (plm)
## 
## Attaching package: 'plm'
## The following object is masked from 'package:gdata':
## 
##     nobs
library(stargazer)
## 
## Please cite as:
##  Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(glmnet)
## Loading required package: Matrix
## Loaded glmnet 4.1-4

#Download Data

load("/cloud/project/Guns.Rdata")
guns = mydata

I. Empirical Exercise: Shall-Carry Laws and Violent Crime Rates. Using the Guns Data and the Guns P rogram provided to you, replicate the two tables on pages 24 and 28 in Lecture 12.

Part A) Write down the regression equation as well as the R commands to obtain each column of the table on p. 24.

Column #1 : vio_it = 542.2377 − 161.1868shall_it + \(\epsilon\)_it.

#Column #1
regpols<-lm(vio~shall,data=mydata)
summary (regpols)
## 
## Call:
## lm(formula = vio ~ shall, data = mydata)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -495.24 -228.84  -63.64  134.06 2379.56 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   542.24      10.98  49.386  < 2e-16 ***
## shall        -161.19      22.27  -7.236 8.32e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 327.2 on 1171 degrees of freedom
## Multiple R-squared:  0.0428, Adjusted R-squared:  0.04199 
## F-statistic: 52.36 on 1 and 1171 DF,  p-value: 8.319e-13

Column #2 : vio_it = 558.1739 + 57.0242shall_it + \(\hat{\alpha}\)_i + \(\epsilon\)_it.

#Column 2
regfes<-lm(vio~shall+factor(stateid),data=mydata)
summary(regfes)
## 
## Call:
## lm(formula = vio ~ shall + factor(stateid), data = mydata)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -637.28  -54.24   -5.75   46.49  872.82 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        558.174     22.642  24.652  < 2e-16 ***
## shall               57.024     10.349   5.510 4.45e-08 ***
## factor(stateid)2    26.225     32.100   0.817 0.414113    
## factor(stateid)4    37.525     32.100   1.169 0.242651    
## factor(stateid)5  -129.691     32.072  -4.044 5.62e-05 ***
## factor(stateid)6   319.309     32.021   9.972  < 2e-16 ***
## factor(stateid)8   -75.387     32.021  -2.354 0.018730 *  
## factor(stateid)9  -137.500     32.021  -4.294 1.91e-05 ***
## factor(stateid)10    6.183     32.021   0.193 0.846932    
## factor(stateid)11 1490.804     32.021  46.557  < 2e-16 ***
## factor(stateid)12  411.309     32.473  12.666  < 2e-16 ***
## factor(stateid)13   12.611     32.336   0.390 0.696605    
## factor(stateid)15 -298.739     32.021  -9.329  < 2e-16 ***
## factor(stateid)16 -317.844     32.276  -9.848  < 2e-16 ***
## factor(stateid)17  269.991     32.021   8.432  < 2e-16 ***
## factor(stateid)18 -215.329     33.652  -6.399 2.30e-10 ***
## factor(stateid)19 -310.574     32.021  -9.699  < 2e-16 ***
## factor(stateid)20 -166.465     32.021  -5.199 2.38e-07 ***
## factor(stateid)21 -230.916     32.050  -7.205 1.06e-12 ***
## factor(stateid)22  200.431     32.223   6.220 7.00e-10 ***
## factor(stateid)23 -448.754     33.030 -13.586  < 2e-16 ***
## factor(stateid)24  295.461     32.021   9.227  < 2e-16 ***
## factor(stateid)25   58.543     32.021   1.828 0.067774 .  
## factor(stateid)26  137.039     32.021   4.280 2.03e-05 ***
## factor(stateid)27 -280.909     32.021  -8.773  < 2e-16 ***
## factor(stateid)28 -224.962     32.276  -6.970 5.40e-12 ***
## factor(stateid)29   24.478     32.021   0.764 0.444766    
## factor(stateid)30 -394.530     32.223 -12.244  < 2e-16 ***
## factor(stateid)31 -257.439     32.021  -8.040 2.27e-15 ***
## factor(stateid)32  186.944     32.072   5.829 7.29e-09 ***
## factor(stateid)33 -484.350     33.652 -14.393  < 2e-16 ***
## factor(stateid)34   -5.352     32.021  -0.167 0.867286    
## factor(stateid)35  185.696     32.021   5.799 8.66e-09 ***
## factor(stateid)36  383.143     32.021  11.965  < 2e-16 ***
## factor(stateid)37  -41.696     32.072  -1.300 0.193844    
## factor(stateid)38 -524.880     32.635 -16.083  < 2e-16 ***
## factor(stateid)39 -112.722     32.021  -3.520 0.000448 ***
## factor(stateid)40  -76.504     32.072  -2.385 0.017225 *  
## factor(stateid)41  -84.149     32.276  -2.607 0.009252 ** 
## factor(stateid)42 -199.511     32.336  -6.170 9.53e-10 ***
## factor(stateid)44 -188.774     32.021  -5.895 4.94e-09 ***
## factor(stateid)45  234.653     32.050   7.322 4.67e-13 ***
## factor(stateid)46 -435.523     32.635 -13.345  < 2e-16 ***
## factor(stateid)47   10.112     32.100   0.315 0.752806    
## factor(stateid)48   47.045     32.050   1.468 0.142420    
## factor(stateid)49 -306.896     32.551  -9.428  < 2e-16 ***
## factor(stateid)50 -481.989     33.652 -14.323  < 2e-16 ***
## factor(stateid)51 -245.139     32.072  -7.643 4.53e-14 ***
## factor(stateid)53 -167.476     33.652  -4.977 7.48e-07 ***
## factor(stateid)54 -394.206     32.336 -12.191  < 2e-16 ***
## factor(stateid)55 -332.891     32.021 -10.396  < 2e-16 ***
## factor(stateid)56 -283.070     32.100  -8.818  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 108.6 on 1121 degrees of freedom
## Multiple R-squared:  0.8991, Adjusted R-squared:  0.8945 
## F-statistic: 195.8 on 51 and 1121 DF,  p-value: < 2.2e-16

Column #3: vio_it = 447.5616 − 2.2071shall_it + \(\hat{\alpha}\)_i + \(\hat{\lambda}\)_t + \(\epsilon\)_it.

#Column 3
regfesy<-lm(vio~shall + factor(stateid)+factor(year),data=mydata)
summary(regfesy)
## 
## Call:
## lm(formula = vio ~ shall + factor(stateid) + factor(year), data = mydata)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -545.86  -43.12   -1.31   41.29  761.59 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        447.562     22.478  19.911  < 2e-16 ***
## shall               -2.207     10.388  -0.212 0.831782    
## factor(stateid)2    39.102     26.589   1.471 0.141690    
## factor(stateid)4    50.402     26.589   1.896 0.058279 .  
## factor(stateid)5  -119.390     26.555  -4.496 7.66e-06 ***
## factor(stateid)6   319.309     26.493  12.053  < 2e-16 ***
## factor(stateid)8   -75.387     26.493  -2.846 0.004516 ** 
## factor(stateid)9  -137.500     26.493  -5.190 2.50e-07 ***
## factor(stateid)10    6.183     26.493   0.233 0.815520    
## factor(stateid)11 1490.804     26.493  56.271  < 2e-16 ***
## factor(stateid)12  442.212     27.042  16.353  < 2e-16 ***
## factor(stateid)13   38.364     26.875   1.427 0.153726    
## factor(stateid)15 -298.739     26.493 -11.276  < 2e-16 ***
## factor(stateid)16 -294.667     26.803 -10.994  < 2e-16 ***
## factor(stateid)17  269.991     26.493  10.191  < 2e-16 ***
## factor(stateid)18 -156.097     28.457  -5.485 5.12e-08 ***
## factor(stateid)19 -310.574     26.493 -11.723  < 2e-16 ***
## factor(stateid)20 -166.465     26.493  -6.283 4.77e-10 ***
## factor(stateid)21 -223.190     26.528  -8.413  < 2e-16 ***
## factor(stateid)22  221.033     26.738   8.267 3.94e-16 ***
## factor(stateid)23 -402.399     27.712 -14.521  < 2e-16 ***
## factor(stateid)24  295.461     26.493  11.152  < 2e-16 ***
## factor(stateid)25   58.543     26.493   2.210 0.027327 *  
## factor(stateid)26  137.039     26.493   5.173 2.74e-07 ***
## factor(stateid)27 -280.909     26.493 -10.603  < 2e-16 ***
## factor(stateid)28 -201.784     26.803  -7.528 1.07e-13 ***
## factor(stateid)29   24.478     26.493   0.924 0.355716    
## factor(stateid)30 -373.928     26.738 -13.985  < 2e-16 ***
## factor(stateid)31 -257.439     26.493  -9.717  < 2e-16 ***
## factor(stateid)32  197.245     26.555   7.428 2.21e-13 ***
## factor(stateid)33 -425.119     28.457 -14.939  < 2e-16 ***
## factor(stateid)34   -5.352     26.493  -0.202 0.839937    
## factor(stateid)35  185.696     26.493   7.009 4.17e-12 ***
## factor(stateid)36  383.143     26.493  14.462  < 2e-16 ***
## factor(stateid)37  -31.394     26.555  -1.182 0.237359    
## factor(stateid)38 -488.826     27.237 -17.947  < 2e-16 ***
## factor(stateid)39 -112.722     26.493  -4.255 2.27e-05 ***
## factor(stateid)40  -66.203     26.555  -2.493 0.012810 *  
## factor(stateid)41  -60.971     26.803  -2.275 0.023111 *  
## factor(stateid)42 -173.758     26.875  -6.465 1.52e-10 ***
## factor(stateid)44 -188.774     26.493  -7.125 1.87e-12 ***
## factor(stateid)45  242.379     26.528   9.137  < 2e-16 ***
## factor(stateid)46 -399.470     27.237 -14.666  < 2e-16 ***
## factor(stateid)47   22.988     26.589   0.865 0.387457    
## factor(stateid)48   54.770     26.528   2.065 0.039190 *  
## factor(stateid)49 -273.418     27.136 -10.076  < 2e-16 ***
## factor(stateid)50 -422.758     28.457 -14.856  < 2e-16 ***
## factor(stateid)51 -234.838     26.555  -8.844  < 2e-16 ***
## factor(stateid)53 -108.245     28.457  -3.804 0.000150 ***
## factor(stateid)54 -368.453     26.875 -13.710  < 2e-16 ***
## factor(stateid)55 -332.891     26.493 -12.565  < 2e-16 ***
## factor(stateid)56 -270.194     26.589 -10.162  < 2e-16 ***
## factor(year)78      19.196     17.791   1.079 0.280847    
## factor(year)79      62.216     17.791   3.497 0.000489 ***
## factor(year)80      92.394     17.791   5.193 2.46e-07 ***
## factor(year)81      96.457     17.791   5.422 7.26e-08 ***
## factor(year)82      77.582     17.793   4.360 1.42e-05 ***
## factor(year)83      46.798     17.793   2.630 0.008653 ** 
## factor(year)84      45.879     17.793   2.579 0.010052 *  
## factor(year)85      57.702     17.793   3.243 0.001218 ** 
## factor(year)86      86.316     17.802   4.849 1.42e-06 ***
## factor(year)87      75.363     17.810   4.231 2.51e-05 ***
## factor(year)88     102.277     17.821   5.739 1.23e-08 ***
## factor(year)89     123.007     17.821   6.903 8.62e-12 ***
## factor(year)90     179.334     17.866  10.038  < 2e-16 ***
## factor(year)91     202.854     17.932  11.312  < 2e-16 ***
## factor(year)92     214.570     17.987  11.929  < 2e-16 ***
## factor(year)93     221.847     17.987  12.333  < 2e-16 ***
## factor(year)94     201.980     17.987  11.229  < 2e-16 ***
## factor(year)95     188.945     18.125  10.424  < 2e-16 ***
## factor(year)96     152.480     18.347   8.311 2.78e-16 ***
## factor(year)97     131.382     18.506   7.099 2.24e-12 ***
## factor(year)98      98.756     18.506   5.337 1.15e-07 ***
## factor(year)99      66.747     18.506   3.607 0.000324 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 89.84 on 1099 degrees of freedom
## Multiple R-squared:  0.9323, Adjusted R-squared:  0.9278 
## F-statistic: 207.2 on 73 and 1099 DF,  p-value: < 2.2e-16

Column #4: vioit = −747.8744 + 2.2258shall_it + \(\hat{\alpha}\)_i +\(\hat{\lambda}\)_t + \(\hat{\gamma}\)_it + \(\epsilon\)_it.

#Column 4
regfesyt<-lm(vio~shall+factor(stateid)+factor(year)+factor(stateid):year, data=mydata)
summary (regfesyt)
## 
## Call:
## lm(formula = vio ~ shall + factor(stateid) + factor(year) + factor(stateid):year, 
##     data = mydata)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -668.38  -33.05    0.79   28.15  629.07 
## 
## Coefficients: (1 not defined because of singularities)
##                          Estimate Std. Error t value Pr(>|t|)    
## (Intercept)            -747.87436  280.54200  -2.666 0.007798 ** 
## shall                     2.22577   12.15726   0.183 0.854769    
## factor(stateid)2         80.96916  298.65178   0.271 0.786355    
## factor(stateid)4        640.47785  298.65178   2.145 0.032217 *  
## factor(stateid)5       -178.17148  297.69555  -0.599 0.549634    
## factor(stateid)6        942.11739  295.25289   3.191 0.001460 ** 
## factor(stateid)8       1367.33477  295.25289   4.631 4.09e-06 ***
## factor(stateid)9        651.72612  295.25289   2.207 0.027506 *  
## factor(stateid)10      -303.86086  295.25289  -1.029 0.303644    
## factor(stateid)11      -365.85642  295.25289  -1.239 0.215575    
## factor(stateid)12       378.21271  301.98929   1.252 0.210702    
## factor(stateid)13       328.41711  301.98929   1.088 0.277060    
## factor(stateid)15       691.80875  295.25289   2.343 0.019310 *  
## factor(stateid)16       642.13981  301.66021   2.129 0.033513 *  
## factor(stateid)17       399.21739  295.25289   1.352 0.176628    
## factor(stateid)18       -23.35615  295.50308  -0.079 0.937017    
## factor(stateid)19        -0.23476  295.25289  -0.001 0.999366    
## factor(stateid)20       373.72612  295.25289   1.266 0.205872    
## factor(stateid)21       280.14216  296.78615   0.944 0.345428    
## factor(stateid)22      -221.64406  301.12950  -0.736 0.461869    
## factor(stateid)23      1001.31115  297.69555   3.364 0.000797 ***
## factor(stateid)24       791.84348  295.25289   2.682 0.007435 ** 
## factor(stateid)25       312.02174  295.25289   1.057 0.290849    
## factor(stateid)26      1015.97833  295.25289   3.441 0.000602 ***
## factor(stateid)27       100.09134  295.25289   0.339 0.734676    
## factor(stateid)28        91.08333  301.66021   0.302 0.762758    
## factor(stateid)29       350.15650  295.25289   1.186 0.235909    
## factor(stateid)30       998.15163  301.12950   3.315 0.000949 ***
## factor(stateid)31      -358.76953  295.25289  -1.215 0.224591    
## factor(stateid)32      1376.55896  297.69555   4.624 4.23e-06 ***
## factor(stateid)33       703.03947  295.50308   2.379 0.017532 *  
## factor(stateid)34       941.25656  295.25289   3.188 0.001475 ** 
## factor(stateid)35      -272.19999  295.25289  -0.922 0.356781    
## factor(stateid)36      2102.08266  295.25289   7.120 2.01e-12 ***
## factor(stateid)37       -43.95841  297.69555  -0.148 0.882638    
## factor(stateid)38       422.37334  301.12950   1.403 0.161023    
## factor(stateid)39       921.12179  295.25289   3.120 0.001859 ** 
## factor(stateid)40      -120.59321  297.69555  -0.405 0.685495    
## factor(stateid)41      1174.35722  301.66021   3.893 0.000105 ***
## factor(stateid)42       269.31275  301.98929   0.892 0.372707    
## factor(stateid)44       917.35657  295.25289   3.107 0.001941 ** 
## factor(stateid)45      -459.94916  296.78615  -1.550 0.121499    
## factor(stateid)46       385.63425  301.12950   1.281 0.200608    
## factor(stateid)47      -789.50041  298.65178  -2.644 0.008327 ** 
## factor(stateid)48       221.38129  296.78615   0.746 0.455878    
## factor(stateid)49       564.35287  301.66021   1.871 0.061648 .  
## factor(stateid)50       820.00036  295.50308   2.775 0.005619 ** 
## factor(stateid)51       530.23287  297.69555   1.781 0.075181 .  
## factor(stateid)53       675.99163  295.50308   2.288 0.022359 *  
## factor(stateid)54       258.29534  301.98929   0.855 0.392573    
## factor(stateid)55       166.67394  295.25289   0.565 0.572526    
## factor(stateid)56      1026.94308  298.65178   3.439 0.000608 ***
## factor(year)78            9.92921   15.08834   0.658 0.510636    
## factor(year)79           43.68195   15.62995   2.795 0.005289 ** 
## factor(year)80           64.59351   16.49314   3.916 9.57e-05 ***
## factor(year)81           59.38939   17.63076   3.369 0.000783 ***
## factor(year)82           31.16124   19.01026   1.639 0.101476    
## factor(year)83           -8.88995   20.55501  -0.432 0.665470    
## factor(year)84          -19.07643   22.24226  -0.858 0.391273    
## factor(year)85          -16.51977   24.04203  -0.687 0.492159    
## factor(year)86            2.65353   25.97974   0.102 0.918666    
## factor(year)87          -17.65306   27.96811  -0.631 0.528057    
## factor(year)88           -0.09298   30.01533  -0.003 0.997529    
## factor(year)89           11.36956   32.07880   0.354 0.723091    
## factor(year)90           58.16981   34.27817   1.697 0.089995 .  
## factor(year)91           72.16221   36.52046   1.976 0.048423 *  
## factor(year)92           74.43747   38.75609   1.921 0.055045 .  
## factor(year)93           72.44707   40.93231   1.770 0.077030 .  
## factor(year)94           43.31353   43.12731   1.004 0.315457    
## factor(year)95           20.66425   45.50501   0.454 0.649844    
## factor(year)96          -25.50318   47.95749  -0.532 0.594986    
## factor(year)97          -56.12843   50.33579  -1.115 0.265072    
## factor(year)98          -98.02079   52.57333  -1.864 0.062536 .  
## factor(year)99         -139.29746   54.82070  -2.541 0.011198 *  
## factor(stateid)1:year    14.75115    3.38905   4.353 1.48e-05 ***
## factor(stateid)2:year    14.26443    3.34566   4.264 2.19e-05 ***
## factor(stateid)4:year     8.03478    3.34566   2.402 0.016499 *  
## factor(stateid)5:year    15.41036    3.34671   4.605 4.64e-06 ***
## factor(stateid)6:year     7.67377    3.38905   2.264 0.023760 *  
## factor(stateid)8:year    -1.64342    3.38905  -0.485 0.627834    
## factor(stateid)9:year     5.78267    3.38905   1.706 0.088252 .  
## factor(stateid)10:year   18.27437    3.38905   5.392 8.60e-08 ***
## factor(stateid)11:year   35.84956    3.38905  10.578  < 2e-16 ***
## factor(stateid)12:year   15.45213    3.35515   4.605 4.62e-06 ***
## factor(stateid)13:year   11.43319    3.35427   3.409 0.000678 ***
## factor(stateid)15:year    3.49492    3.38905   1.031 0.302666    
## factor(stateid)16:year    4.08591    3.35264   1.219 0.223227    
## factor(stateid)17:year   13.28267    3.38905   3.919 9.46e-05 ***
## factor(stateid)18:year   13.19235    3.38905   3.893 0.000105 ***
## factor(stateid)19:year   11.22456    3.38905   3.312 0.000958 ***
## factor(stateid)20:year    8.61261    3.38905   2.541 0.011187 *  
## factor(stateid)21:year    9.02489    3.35050   2.694 0.007181 ** 
## factor(stateid)22:year   19.76404    3.35050   5.899 4.93e-09 ***
## factor(stateid)23:year   -1.23953    3.34566  -0.370 0.711093    
## factor(stateid)24:year    9.11043    3.38905   2.688 0.007298 ** 
## factor(stateid)25:year   11.87071    3.38905   3.503 0.000480 ***
## factor(stateid)26:year    4.76320    3.38905   1.405 0.160178    
## factor(stateid)27:year   10.42160    3.38905   3.075 0.002159 ** 
## factor(stateid)28:year   11.40339    3.35264   3.401 0.000696 ***
## factor(stateid)29:year   11.05026    3.38905   3.261 0.001148 ** 
## factor(stateid)30:year   -0.85819    3.35050  -0.256 0.797895    
## factor(stateid)31:year   15.90263    3.38905   4.692 3.06e-06 ***
## factor(stateid)32:year    1.34109    3.34671   0.401 0.688709    
## factor(stateid)33:year    1.88079    3.38905   0.555 0.579040    
## factor(stateid)34:year    3.99423    3.38905   1.179 0.238837    
## factor(stateid)35:year   19.95451    3.38905   5.888 5.26e-09 ***
## factor(stateid)36:year   -4.78225    3.38905  -1.411 0.158514    
## factor(stateid)37:year   14.88516    3.34671   4.448 9.60e-06 ***
## factor(stateid)38:year    4.36594    3.35264   1.302 0.193119    
## factor(stateid)39:year    3.00292    3.38905   0.886 0.375784    
## factor(stateid)40:year   15.36045    3.34671   4.590 4.98e-06 ***
## factor(stateid)41:year    0.69361    3.35264   0.207 0.836139    
## factor(stateid)42:year    9.69435    3.35427   2.890 0.003930 ** 
## factor(stateid)44:year    2.18148    3.38905   0.644 0.519920    
## factor(stateid)45:year   22.72558    3.35050   6.783 1.97e-11 ***
## factor(stateid)46:year    5.79885    3.35264   1.730 0.083989 .  
## factor(stateid)47:year   23.97302    3.34566   7.165 1.46e-12 ***
## factor(stateid)48:year   12.85127    3.35050   3.836 0.000133 ***
## factor(stateid)49:year    5.20255    3.35427   1.551 0.121198    
## factor(stateid)50:year    0.57852    3.38905   0.171 0.864491    
## factor(stateid)51:year    6.04840    3.34671   1.807 0.071007 .  
## factor(stateid)53:year    5.78899    3.38905   1.708 0.087905 .  
## factor(stateid)54:year    7.60710    3.35427   2.268 0.023539 *  
## factor(stateid)55:year    9.07427    3.38905   2.678 0.007533 ** 
## factor(stateid)56:year         NA         NA      NA       NA    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 75.26 on 1049 degrees of freedom
## Multiple R-squared:  0.9546, Adjusted R-squared:  0.9493 
## F-statistic: 179.5 on 123 and 1049 DF,  p-value: < 2.2e-16

Column #5: vio_it = −253.3828 − 1.5958shall_it − 105.2594density_it + 8.4545avginc_it + 0.2345incarc_it + 31.6939pm1029_it + \(\hat{\alpha}\)_i +\(\hat{\lambda}\)_t + \(\hat{\gamma}\)_it + \(\epsilon\)_it.

#Column 5
regfesymulti<-lm(vio~shall+density+avginc+incarc_rate+pm1029+factor(stateid) +factor(year),data=mydata)
summary(regfesymulti)
## 
## Call:
## lm(formula = vio ~ shall + density + avginc + incarc_rate + pm1029 + 
##     factor(stateid) + factor(year), data = mydata)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -656.73  -41.63   -0.67   40.47  655.11 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       -253.38285  128.91439  -1.966 0.049607 *  
## shall               -1.59575   10.35422  -0.154 0.877547    
## density           -105.25936   38.73320  -2.718 0.006681 ** 
## avginc               8.45455    3.76599   2.245 0.024969 *  
## incarc_rate          0.23448    0.05089   4.608 4.55e-06 ***
## pm1029              31.69391    6.74425   4.699 2.94e-06 ***
## factor(stateid)2   -82.99671   36.57359  -2.269 0.023444 *  
## factor(stateid)4    26.10318   25.83076   1.011 0.312457    
## factor(stateid)5   -86.31919   25.88512  -3.335 0.000883 ***
## factor(stateid)6   282.61679   30.41738   9.291  < 2e-16 ***
## factor(stateid)8   -92.42287   29.34543  -3.149 0.001680 ** 
## factor(stateid)9   -80.57591   44.68742  -1.803 0.071647 .  
## factor(stateid)10    5.01747   31.44162   0.160 0.873241    
## factor(stateid)11 2305.54247  405.85057   5.681 1.72e-08 ***
## factor(stateid)12  493.27290   32.57434  15.143  < 2e-16 ***
## factor(stateid)13    8.50786   26.32850   0.323 0.746649    
## factor(stateid)15 -313.51700   30.50496 -10.278  < 2e-16 ***
## factor(stateid)16 -287.09979   26.94842 -10.654  < 2e-16 ***
## factor(stateid)17  278.31713   29.92820   9.299  < 2e-16 ***
## factor(stateid)18 -137.45330   28.67874  -4.793 1.87e-06 ***
## factor(stateid)19 -275.50082   27.92462  -9.866  < 2e-16 ***
## factor(stateid)20 -165.56016   27.37818  -6.047 2.02e-09 ***
## factor(stateid)21 -204.73057   25.64506  -7.983 3.58e-15 ***
## factor(stateid)22  179.10087   26.09434   6.864 1.12e-11 ***
## factor(stateid)23 -345.28740   30.00731 -11.507  < 2e-16 ***
## factor(stateid)24  309.04783   35.70689   8.655  < 2e-16 ***
## factor(stateid)25  141.82329   39.51054   3.590 0.000346 ***
## factor(stateid)26  124.26996   27.42273   4.532 6.50e-06 ***
## factor(stateid)27 -255.71185   30.92724  -8.268 3.91e-16 ***
## factor(stateid)28 -209.10929   26.29041  -7.954 4.48e-15 ***
## factor(stateid)29   41.61305   26.73147   1.557 0.119829    
## factor(stateid)30 -338.57481   27.48231 -12.320  < 2e-16 ***
## factor(stateid)31 -231.47270   28.41451  -8.146 1.02e-15 ***
## factor(stateid)32  146.59859   29.62867   4.948 8.68e-07 ***
## factor(stateid)33 -388.20674   33.41878 -11.616  < 2e-16 ***
## factor(stateid)34  103.89685   50.87420   2.042 0.041368 *  
## factor(stateid)35  194.51017   26.85298   7.244 8.23e-13 ***
## factor(stateid)36  411.40783   34.61098  11.887  < 2e-16 ***
## factor(stateid)37  -42.10407   25.68967  -1.639 0.101511    
## factor(stateid)38 -472.24790   30.02116 -15.731  < 2e-16 ***
## factor(stateid)39  -89.36273   27.79460  -3.215 0.001342 ** 
## factor(stateid)40  -83.41108   25.39150  -3.285 0.001052 ** 
## factor(stateid)41  -28.14007   29.04991  -0.969 0.332919    
## factor(stateid)42 -109.88422   30.65655  -3.584 0.000353 ***
## factor(stateid)44  -68.31700   40.02088  -1.707 0.088099 .  
## factor(stateid)45  205.37778   26.01662   7.894 7.06e-15 ***
## factor(stateid)46 -382.28467   27.61317 -13.844  < 2e-16 ***
## factor(stateid)47   52.30056   26.05104   2.008 0.044929 *  
## factor(stateid)48    0.46509   26.64474   0.017 0.986077    
## factor(stateid)49 -308.02221   31.08534  -9.909  < 2e-16 ***
## factor(stateid)50 -388.38960   30.04099 -12.929  < 2e-16 ***
## factor(stateid)51 -258.40510   28.38027  -9.105  < 2e-16 ***
## factor(stateid)53 -100.83843   31.82162  -3.169 0.001573 ** 
## factor(stateid)54 -292.10268   28.32075 -10.314  < 2e-16 ***
## factor(stateid)55 -314.13547   27.75559 -11.318  < 2e-16 ***
## factor(stateid)56 -294.18885   28.21541 -10.427  < 2e-16 ***
## factor(year)78      18.84328   16.99116   1.109 0.267671    
## factor(year)79      64.77400   17.07492   3.794 0.000157 ***
## factor(year)80     100.88516   17.12598   5.891 5.11e-09 ***
## factor(year)81     108.48391   17.35159   6.252 5.79e-10 ***
## factor(year)82      94.92329   17.85657   5.316 1.29e-07 ***
## factor(year)83      67.56475   18.70311   3.612 0.000317 ***
## factor(year)84      69.42013   20.10747   3.452 0.000577 ***
## factor(year)85      86.62470   21.59842   4.011 6.46e-05 ***
## factor(year)86     120.39456   23.42973   5.139 3.28e-07 ***
## factor(year)87     114.71451   25.24275   4.544 6.12e-06 ***
## factor(year)88     145.49820   27.19883   5.349 1.07e-07 ***
## factor(year)89     168.94232   29.03464   5.819 7.78e-09 ***
## factor(year)90     227.68784   30.64504   7.430 2.18e-13 ***
## factor(year)91     256.69345   31.96020   8.032 2.47e-15 ***
## factor(year)92     268.09274   33.62394   7.973 3.86e-15 ***
## factor(year)93     277.31653   34.71166   7.989 3.42e-15 ***
## factor(year)94     256.19677   36.09610   7.098 2.28e-12 ***
## factor(year)95     239.32627   37.43897   6.392 2.41e-10 ***
## factor(year)96     199.23630   38.72331   5.145 3.17e-07 ***
## factor(year)97     173.20331   39.95580   4.335 1.59e-05 ***
## factor(year)98     132.11441   41.38705   3.192 0.001452 ** 
## factor(year)99      94.35142   42.45962   2.222 0.026477 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 85.09 on 1095 degrees of freedom
## Multiple R-squared:  0.9395, Adjusted R-squared:  0.9352 
## F-statistic: 220.7 on 77 and 1095 DF,  p-value: < 2.2e-16

#Stargazer Table

stargazer(regpols, regfes, regfesy,regfesyt, regfesymulti, type = "text", omit = c("year", "stateid"), no.space = TRUE, align = TRUE)
## 
## =========================================================================================================================================================
##                                                                              Dependent variable:                                                         
##                     -------------------------------------------------------------------------------------------------------------------------------------
##                                                                                      vio                                                                 
##                               (1)                       (2)                        (3)                         (4)                        (5)            
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
## shall                     -161.187***                57.024***                    -2.207                      2.226                      -1.596          
##                             (22.275)                  (10.349)                   (10.388)                   (12.157)                    (10.354)         
## density                                                                                                                               -105.259***        
##                                                                                                                                         (38.733)         
## avginc                                                                                                                                  8.455**          
##                                                                                                                                         (3.766)          
## incarc_rate                                                                                                                             0.234***         
##                                                                                                                                         (0.051)          
## pm1029                                                                                                                                 31.694***         
##                                                                                                                                         (6.744)          
## Constant                   542.238***                558.174***                 447.562***                 -747.874***                 -253.383**        
##                             (10.980)                  (22.642)                   (22.478)                   (280.542)                  (128.914)         
## ---------------------------------------------------------------------------------------------------------------------------------------------------------
## Observations                 1,173                     1,173                      1,173                       1,173                      1,173           
## R2                           0.043                     0.899                      0.932                       0.955                      0.939           
## Adjusted R2                  0.042                     0.894                      0.928                       0.949                      0.935           
## Residual Std. Error   327.184 (df = 1171)       108.589 (df = 1121)         89.842 (df = 1099)         75.259 (df = 1049)          85.090 (df = 1095)    
## F Statistic         52.364*** (df = 1; 1171) 195.790*** (df = 51; 1121) 207.202*** (df = 73; 1099) 179.457*** (df = 123; 1049) 220.687*** (df = 77; 1095)
## =========================================================================================================================================================
## Note:                                                                                                                         *p<0.1; **p<0.05; ***p<0.01

#(b) Write down the regression equation as well as the R commands to obtain each column of the table on p. 28.

mydatap<-pdata.frame(mydata,c("stateid","year"))

Column #1 : vio_it = 542.2377 − 161.1868shall_it + \(\epsilon\)_it.

pregpols<-plm(vio~shall,model="pooling",data=mydatap)
rvpols<-vcovHC(pregpols,method="arellano",cluster="group")
coeftest(pregpols,vcov=rvpols)
## 
## t test of coefficients:
## 
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  542.238     50.155 10.8113   <2e-16 ***
## shall       -161.187     67.006 -2.4056   0.0163 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Column #2 : vio_it = 52.024shall_it + \(\hat{\alpha}\)_i +\(\hat{\lambda}\)_t +u_it.

pregfes <- plm(vio~shall,model="within", data=mydatap)
rvfes<-vcovHC(pregfes,method="arellano", cluster="group")
coeftest(pregfes,vcov=rvfes)
## 
## t test of coefficients:
## 
##       Estimate Std. Error t value  Pr(>|t|)    
## shall   57.024     17.083  3.3381 0.0008712 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Column #3 : vio_it = -2.207shall_it + \(\hat{\alpha}\)_i +\(\hat{\lambda}\)_t +u_it.

pregfesy<-plm(vio~shall,effect="twoway",model="within",data=mydatap)
rvfesy<-vcovHC(pregfesy,,method="arellano",cluster="group")
coeftest(pregfesy,vcov=rvfesy)
## 
## t test of coefficients:
## 
##       Estimate Std. Error t value Pr(>|t|)
## shall  -2.2071    21.8838 -0.1009   0.9197

Column #4 : vio_it = -1.596shall_it - 105.259density_it + 8.555avginc_it +0.234incarc_rate+ 31.694_pm109 + \(\hat{\alpha}\)_i +\(\hat{\lambda}\)_t +u_it.

pregfesymulti<-plm(vio~shall+density+avginc+incarc_rate+pm1029, effect="twoway",model="within", data=mydatap)
rvfesymulti<-vcovHC(pregfesymulti,method="arellano",cluster="group")
coeftest(pregfesymulti,vcov=rvfesymulti)
## 
## t test of coefficients:
## 
##               Estimate Std. Error t value Pr(>|t|)  
## shall         -1.59575   20.65190 -0.0773  0.93842  
## density     -105.25936   68.86385 -1.5285  0.12667  
## avginc         8.45455    8.92215  0.9476  0.34355  
## incarc_rate    0.23448    0.11610  2.0196  0.04367 *
## pm1029        31.69391   15.65655  2.0243  0.04318 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

#Stargazer for Pooled Data

stargazer(rvpols,rvfes, rvfesy, rvfesymulti,title="pooled regression results",no.space = TRUE, align = TRUE)
## 
## % Table created by stargazer v.5.2.3 by Marek Hlavac, Social Policy Institute. E-mail: marek.hlavac at gmail.com
## % Date and time: Fri, Jun 03, 2022 - 04:56:52
## % Requires LaTeX packages: dcolumn 
## \begin{table}[!htbp] \centering 
##   \caption{pooled regression results} 
##   \label{} 
## \begin{tabular}{@{\extracolsep{5pt}} D{.}{.}{-3} D{.}{.}{-3} D{.}{.}{-3} } 
## \\[-1.8ex]\hline 
## \hline \\[-1.8ex] 
## \multicolumn{1}{c}{} & \multicolumn{1}{c}{(Intercept)} & \multicolumn{1}{c}{shall} \\ 
## \hline \\[-1.8ex] 
## \multicolumn{1}{c}{(Intercept)} & 2,515.505 & -2,044.921 \\ 
## \multicolumn{1}{c}{shall} & -2,044.921 & 4,489.760 \\ 
## \hline \\[-1.8ex] 
## \end{tabular} 
## \end{table} 
## 
## % Table created by stargazer v.5.2.3 by Marek Hlavac, Social Policy Institute. E-mail: marek.hlavac at gmail.com
## % Date and time: Fri, Jun 03, 2022 - 04:56:52
## % Requires LaTeX packages: dcolumn 
## \begin{table}[!htbp] \centering 
##   \caption{pooled regression results} 
##   \label{} 
## \begin{tabular}{@{\extracolsep{5pt}} D{.}{.}{-3} D{.}{.}{-3} } 
## \\[-1.8ex]\hline 
## \hline \\[-1.8ex] 
## \multicolumn{1}{c}{} & \multicolumn{1}{c}{shall} \\ 
## \hline \\[-1.8ex] 
## \multicolumn{1}{c}{shall} & 291.821 \\ 
## \hline \\[-1.8ex] 
## \end{tabular} 
## \end{table} 
## 
## % Table created by stargazer v.5.2.3 by Marek Hlavac, Social Policy Institute. E-mail: marek.hlavac at gmail.com
## % Date and time: Fri, Jun 03, 2022 - 04:56:52
## % Requires LaTeX packages: dcolumn 
## \begin{table}[!htbp] \centering 
##   \caption{pooled regression results} 
##   \label{} 
## \begin{tabular}{@{\extracolsep{5pt}} D{.}{.}{-3} D{.}{.}{-3} } 
## \\[-1.8ex]\hline 
## \hline \\[-1.8ex] 
## \multicolumn{1}{c}{} & \multicolumn{1}{c}{shall} \\ 
## \hline \\[-1.8ex] 
## \multicolumn{1}{c}{shall} & 478.902 \\ 
## \hline \\[-1.8ex] 
## \end{tabular} 
## \end{table} 
## 
## % Table created by stargazer v.5.2.3 by Marek Hlavac, Social Policy Institute. E-mail: marek.hlavac at gmail.com
## % Date and time: Fri, Jun 03, 2022 - 04:56:52
## % Requires LaTeX packages: dcolumn 
## \begin{table}[!htbp] \centering 
##   \caption{pooled regression results} 
##   \label{} 
## \begin{tabular}{@{\extracolsep{5pt}} D{.}{.}{-3} D{.}{.}{-3} D{.}{.}{-3} D{.}{.}{-3} D{.}{.}{-3} D{.}{.}{-3} } 
## \\[-1.8ex]\hline 
## \hline \\[-1.8ex] 
## \multicolumn{1}{c}{} & \multicolumn{1}{c}{shall} & \multicolumn{1}{c}{density} & \multicolumn{1}{c}{avginc} & \multicolumn{1}{c}{incarc\_rate} & \multicolumn{1}{c}{pm1029} \\ 
## \hline \\[-1.8ex] 
## \multicolumn{1}{c}{shall} & 426.501 & -6.583 & 11.013 & 0.070 & -22.468 \\ 
## \multicolumn{1}{c}{density} & -6.583 & 4,742.230 & 252.740 & 7.569 & -71.047 \\ 
## \multicolumn{1}{c}{avginc} & 11.013 & 252.740 & 79.605 & 0.249 & 22.168 \\ 
## \multicolumn{1}{c}{incarc\_rate} & 0.070 & 7.569 & 0.249 & 0.013 & 0.231 \\ 
## \multicolumn{1}{c}{pm1029} & -22.468 & -71.047 & 22.168 & 0.231 & 245.128 \\ 
## \hline \\[-1.8ex] 
## \end{tabular} 
## \end{table}

#c) Why are the standard errors different between the two tables? Which command leads to this difference? Which standard errors should we use in practice and why? The results are more accurate in the second table (pooled regression table) as a result of the difference in these standard errors. The command used for robust standard errors is “vcovHC()”.

#d) Do you think that the data suggests that “shall” carry laws have a (positive or negative) effect on violent crime rates? Explain your answer and perform any statistical tests to support your answer. Since the data we have is panel data, our best regression model is “rvfesymulti” where we have controlled for most variables. inspite of this, the coefficent we get is -1.596, which indicates a negative correlation between shall gun laws and crime rate.

However, in order to test whether our data is statistically significant, we carry out t-test at a 5% significance level which reveals that the results are not statistically significant as -0.0773<1.96, hence we cannot deduce a causal relationship.

##II. Model Selection in the Sales and Advertising Example. Using the “Advertising” data set which we used extensively in our class, write an R program that can perform the best subset selection as well as LASSO:

#Load Data

load("/cloud/project/Advertising.RData")
ads_data<-mydata

#a) Perform best subset selection using (1) Akaike information criterion (AIC), (2) Bayesian Schwarz information criterion (BIC), and (3) leave-one-out cross-validation (CV). Below are the steps you need to perform for best subset selection:

regnull = lm(Sales ~ 1, data=ads_data)
regn = lm(Sales ~ Newspaper, data=ads_data)
regt = lm(Sales ~ TV, data=ads_data)
regr = lm(Sales ~ Radio, data=ads_data)
regrn = lm(Sales ~ Newspaper + Radio, data=ads_data)
regrt = lm(Sales ~ Radio + TV, data=ads_data)
regnt = lm(Sales ~ Newspaper + TV, data=ads_data)
regall = lm(Sales ~ Newspaper + TV + Radio, data=ads_data)
CV(regnull)
##        CV       AIC      AICc       BIC     AdjR2 
##  27.35865 663.80151 663.86242 670.39814   0.00000
CV(regn)
##           CV          AIC         AICc          BIC        AdjR2 
##  26.27078080 655.09593833 655.21838730 664.99089042   0.04733317
CV(regt)
##          CV         AIC        AICc         BIC       AdjR2 
##  10.7410876 476.5159143 476.6383632 486.4108664   0.6099148
CV(regr)
##          CV         AIC        AICc         BIC       AdjR2 
##  18.4856639 585.0983672 585.2208162 594.9933193   0.3286589
CV(regrn)
##          CV         AIC        AICc         BIC       AdjR2 
##  18.7544100 586.8968402 587.1019684 600.0901097   0.3259306
CV(regrt)
##          CV         AIC        AICc         BIC       AdjR2 
##   2.9106758 212.8186854 213.0238136 226.0119549   0.8961505
CV(regnt)
##          CV         AIC        AICc         BIC       AdjR2 
##   9.8975987 460.2027523 460.4078805 473.3960218   0.6422399
CV(regall)
##          CV         AIC        AICc         BIC       AdjR2 
##   2.9468998 214.7868226 215.0961010 231.2784094   0.8956373

Based on our findings we find that the best subset is regrt.

#B) Perform the LASSO method to select the variables to include in the model to replicate the LASSO results in the Week 9 Lecture Notes.

X = model.matrix(Sales ~ ., ads_data)
y = ads_data$Sales
lassoreg = cv.glmnet(X, y, alpha = 1)
summary(lassoreg)
##            Length Class  Mode     
## lambda     58     -none- numeric  
## cvm        58     -none- numeric  
## cvsd       58     -none- numeric  
## cvup       58     -none- numeric  
## cvlo       58     -none- numeric  
## nzero      58     -none- numeric  
## call        4     -none- call     
## name        1     -none- character
## glmnet.fit 12     elnet  list     
## lambda.min  1     -none- numeric  
## lambda.1se  1     -none- numeric  
## index       2     -none- numeric

#C)Compare the results from the best subset selection method using the different criteria and the LASSO method.

predict(lassoreg, type = "coefficients")
## 6 x 1 sparse Matrix of class "dgCMatrix"
##             lambda.1se
## (Intercept) 4.56000275
## (Intercept) .         
## X           .         
## TV          0.03993424
## Radio       0.15433575
## Newspaper   .

##III. Empirical Exercise: S&P500. Using the Stock P rice Data and the Stock P rice P rogram provided to you, perform the following tasks. Notes on the data: SP 500 is the index itself, SP 500L is its lag; SP 500D is its difference, SP 500D1 is the lag of the difference; SP 500R is the return on the S&P500, SP 500RL is its lag.

#Load Data

load("/cloud/project/Stock_Price_Data.RData")
stock_data<-mydata

#a) Perform three OLS regressions (1) SP 500 on its lag SP 500L, (2) SP 500D on its lag SP 500D1, (3) SP 500R on its lag SP 500RL. Report the regression coefficients and the R^2’s. (answered under each plots)

regsp<-lm(SP500~SP500L, stock_data)
summary(regsp)
## 
## Call:
## lm(formula = SP500 ~ SP500L, data = stock_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -107.048   -6.883    0.687    8.207  103.480 
## 
## Coefficients:
##              Estimate Std. Error  t value Pr(>|t|)    
## (Intercept) 1.2848466  1.3807548    0.931    0.352    
## SP500L      0.9992939  0.0009278 1077.078   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 16.21 on 2515 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.9978, Adjusted R-squared:  0.9978 
## F-statistic: 1.16e+06 on 1 and 2515 DF,  p-value: < 2.2e-16
coeftest(regsp, vcov = NeweyWest(regsp,lag=1))
## 
## t test of coefficients:
## 
##              Estimate Std. Error  t value Pr(>|t|)    
## (Intercept) 1.2848466  1.5040266   0.8543    0.393    
## SP500L      0.9992939  0.0010178 981.8438   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
stock_data$SP500t<-ts(stock_data$SP500)
plot(stock_data$SP500t,ylab="S&P500")

Coefficient = 0.999 R^2 (adjusted) =0.9978

regsd<-lm(SP500D~SP500D1,stock_data)
summary(regsd)
## 
## Call:
## lm(formula = SP500D ~ SP500D1, data = stock_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -106.602   -6.998    0.946    7.933  103.012 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.28126    0.32227   0.873    0.383    
## SP500D1     -0.07821    0.01989  -3.932 8.65e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 16.16 on 2514 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  0.006112,   Adjusted R-squared:  0.005717 
## F-statistic: 15.46 on 1 and 2514 DF,  p-value: 8.654e-05
coeftest(regsd,vcov=NeweyWest(regsd,lag=1))
## 
## t test of coefficients:
## 
##              Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  0.281265   0.321849  0.8739 0.382254   
## SP500D1     -0.078206   0.028715 -2.7235 0.006503 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
stock_data$SP500Dt<-ts(stock_data$SP500D)
plot(stock_data$SP500Dt,ylab="Changes in S&P500")

Coefficient = -0.07821 R^2 (adjusted) = 0.005717

regsr<-lm(SP500R~SP500RL,stock_data)
summary(regsr)
## 
## Call:
## lm(formula = SP500R ~ SP500RL, data = stock_data)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.090973 -0.004755  0.000472  0.005594  0.115095 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)
## (Intercept)  1.451e-03  1.121e-03   1.295    0.195
## SP500RL     -8.292e-07  7.531e-07  -1.101    0.271
## 
## Residual standard error: 0.01316 on 2515 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.0004819,  Adjusted R-squared:  8.446e-05 
## F-statistic: 1.213 on 1 and 2515 DF,  p-value: 0.2709
coeftest(regsr,vcov=NeweyWest(regsr,lag=1))
## 
## t test of coefficients:
## 
##                Estimate  Std. Error t value Pr(>|t|)
## (Intercept)  1.4512e-03  1.3649e-03  1.0632   0.2878
## SP500RL     -8.2923e-07  8.4590e-07 -0.9803   0.3270
stock_data$SP500Rt<-ts(stock_data$SP500R)
plot(stock_data$SP500Rt,ylab="Returns on S&P500")

Coefficient= 1.451e-03 R^2(adjusted)= 8.446e-05

#b) Which of the regressions in (a) may be spurious? Why? The most likely regression that is spurious is the first regression because both the coefficient and the R^2 is 1. This may indicate that there is no real relations but rather it is simply the 2 variables following a very close trend.

#c) In finance theory, it is well established that stock returns are slightly negatively correlated with their lags, does any of the regressions in (a) support this theory? Refer to the appropriate regression and explain in words what it means. Third regression is the closest to this theory. The coefficient is negative but very close to 0.