R Markdown
library(plm)
library(wooldridge)
library(rmarkdown)
data("airfare")
paged_table(airfare)
require(dplyr)
## Loading required package: dplyr
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plm':
##
## between, lag, lead
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(plm)
time <- pdata.frame(airfare, index = c("id","year" ) )
summary(time)
## year id dist passen fare
## 1997:1149 1 : 4 Min. : 95.0 Min. : 2.0 Min. : 37.0
## 1998:1149 2 : 4 1st Qu.: 505.0 1st Qu.: 215.0 1st Qu.:123.0
## 1999:1149 3 : 4 Median : 861.0 Median : 357.0 Median :168.0
## 2000:1149 4 : 4 Mean : 989.7 Mean : 636.8 Mean :178.8
## 5 : 4 3rd Qu.:1304.0 3rd Qu.: 717.0 3rd Qu.:225.0
## 6 : 4 Max. :2724.0 Max. :8497.0 Max. :522.0
## (Other):4572
## bmktshr ldist y98 y99 y00
## Min. :0.1605 Min. :4.554 Min. :0.00 Min. :0.00 Min. :0.00
## 1st Qu.:0.4650 1st Qu.:6.225 1st Qu.:0.00 1st Qu.:0.00 1st Qu.:0.00
## Median :0.6039 Median :6.758 Median :0.00 Median :0.00 Median :0.00
## Mean :0.6101 Mean :6.696 Mean :0.25 Mean :0.25 Mean :0.25
## 3rd Qu.:0.7531 3rd Qu.:7.173 3rd Qu.:0.25 3rd Qu.:0.25 3rd Qu.:0.25
## Max. :1.0000 Max. :7.910 Max. :1.00 Max. :1.00 Max. :1.00
##
## lfare ldistsq concen lpassen
## Min. :3.611 Min. :20.74 Min. :0.1605 Min. :0.6931
## 1st Qu.:4.812 1st Qu.:38.75 1st Qu.:0.4650 1st Qu.:5.3706
## Median :5.124 Median :45.67 Median :0.6039 Median :5.8777
## Mean :5.096 Mean :45.28 Mean :0.6101 Mean :6.0170
## 3rd Qu.:5.416 3rd Qu.:51.45 3rd Qu.:0.7531 3rd Qu.:6.5751
## Max. :6.258 Max. :62.57 Max. :1.0000 Max. :9.0475
##
year id dist passen fare
1997:1149 1 : 4 Min. : 95.0 Min. : 2.0 Min. : 37.0
1998:1149 2 : 4 1st Qu.: 505.0 1st Qu.: 215.0 1st Qu.:123.0
1999:1149 3 : 4 Median : 861.0 Median : 357.0 Median :168.0
2000:1149 4 : 4 Mean : 989.7 Mean : 636.8 Mean :178.8
5 : 4 3rd Qu.:1304.0 3rd Qu.: 717.0 3rd Qu.:225.0
6 : 4 Max. :2724.0 Max. :8497.0 Max. :522.0
(Other):4572
bmktshr ldist y98 y99 y00
Min. :0.1605 Min. :4.554 Min. :0.00 Min. :0.00 Min. :0.00
1st Qu.:0.4650 1st Qu.:6.225 1st Qu.:0.00 1st Qu.:0.00 1st
Qu.:0.00
Median :0.6039 Median :6.758 Median :0.00 Median :0.00 Median
:0.00
Mean :0.6101 Mean :6.696 Mean :0.25 Mean :0.25 Mean :0.25
3rd Qu.:0.7531 3rd Qu.:7.173 3rd Qu.:0.25 3rd Qu.:0.25 3rd
Qu.:0.25
Max. :1.0000 Max. :7.910 Max. :1.00 Max. :1.00 Max. :1.00
lfare ldistsq concen lpassen
Min. :3.611 Min. :20.74 Min. :0.1605 Min. :0.6931
1st Qu.:4.812 1st Qu.:38.75 1st Qu.:0.4650 1st Qu.:5.3706
Median :5.124 Median :45.67 Median :0.6039 Median :5.8777
Mean :5.096 Mean :45.28 Mean :0.6101 Mean :6.0170
3rd Qu.:5.416 3rd Qu.:51.45 3rd Qu.:0.7531 3rd Qu.:6.5751
Max. :6.258 Max. :62.57 Max. :1.0000 Max. :9.0475
pdim(time)
## Balanced Panel: n = 1149, T = 4, N = 4596
Balanced Panel: n = 1149, T = 4, N = 4596
modeli1 <- plm(lpassen ~ fare + dist + passen + bmktshr + I(ldist^2) + year, data = time , model = "pooling" )
summary(modeli1)
## Pooling Model
##
## Call:
## plm(formula = lpassen ~ fare + dist + passen + bmktshr + I(ldist^2) +
## year, data = time, model = "pooling")
##
## Balanced Panel: n = 1149, T = 4, N = 4596
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -4.697330 -0.282895 0.078816 0.389496 0.767094
##
## Coefficients:
## Estimate Std. Error t-value Pr(>|t|)
## (Intercept) 5.3699e+00 1.1033e-01 48.6706 < 2.2e-16 ***
## fare -1.1863e-03 1.3071e-04 -9.0754 < 2.2e-16 ***
## dist 9.0119e-05 4.4067e-05 2.0450 0.04091 *
## passen 8.8803e-04 9.4198e-06 94.2732 < 2.2e-16 ***
## bmktshr 2.2272e-01 4.6567e-02 4.7828 1.783e-06 ***
## I(ldist^2) 1.1674e-03 3.0201e-03 0.3865 0.69911
## year1998 3.7651e-03 2.0999e-02 0.1793 0.85771
## year1999 1.8118e-02 2.1012e-02 0.8623 0.38858
## year2000 4.1192e-02 2.1103e-02 1.9519 0.05101 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 3590.7
## Residual Sum of Squares: 1161.4
## R-Squared: 0.67656
## Adj. R-Squared: 0.676
## F-statistic: 1199.36 on 8 and 4587 DF, p-value: < 2.22e-16
Pooling Model
Call:
plm(formula = lpassen ~ fare + dist + passen + bmktshr + I(ldist^2)
+
year, data = time, model = “pooling”)
Balanced Panel: n = 1149, T = 4, N = 4596
Residuals:
Min. 1st Qu. Median 3rd Qu. Max.
-4.697330 -0.282895 0.078816 0.389496 0.767094
Coefficients:
Estimate Std. Error t-value Pr(>|t|)
(Intercept) 5.3699e+00 1.1033e-01 48.6706 < 2.2e-16 ***
fare -1.1863e-03 1.3071e-04 -9.0754 < 2.2e-16 ***
dist 9.0119e-05 4.4067e-05 2.0450 0.04091 *
passen 8.8803e-04 9.4198e-06 94.2732 < 2.2e-16 ***
bmktshr 2.2272e-01 4.6567e-02 4.7828 1.783e-06 ***
I(ldist^2) 1.1674e-03 3.0201e-03 0.3865 0.69911
year1998 3.7651e-03 2.0999e-02 0.1793 0.85771
year1999 1.8118e-02 2.1012e-02 0.8623 0.38858
year2000 4.1192e-02 2.1103e-02 1.9519 0.05101 .
—
Signif. codes: 0 ‘’ 0.001 ’’ 0.01 ’’ 0.05
‘.’ 0.1 ’ ’ 1
Total Sum of Squares: 3590.7
Residual Sum of Squares: 1161.4
R-Squared: 0.67656
Adj. R-Squared: 0.676
F-statistic: 1199.36 on 8 and 4587 DF, p-value: < 2.22e-16
model2 <- plm(lpassen ~ fare + dist + passen + bmktshr + I(ldist^2) + year, data = time, model = "within" )
summary(model2)
## Oneway (individual) effect Within Model
##
## Call:
## plm(formula = lpassen ~ fare + dist + passen + bmktshr + I(ldist^2) +
## year, data = time, model = "within")
##
## Balanced Panel: n = 1149, T = 4, N = 4596
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -2.17315683 -0.03750978 0.00081035 0.03994864 1.98735732
##
## Coefficients:
## Estimate Std. Error t-value Pr(>|t|)
## fare -4.3260e-03 1.3776e-04 -31.4030 < 2.2e-16 ***
## passen 5.4114e-04 2.2967e-05 23.5623 < 2.2e-16 ***
## bmktshr 1.6054e-01 3.9765e-02 4.0374 5.523e-05 ***
## year1998 1.5337e-02 6.0027e-03 2.5550 0.01066 *
## year1999 5.0585e-02 6.1888e-03 8.1737 4.160e-16 ***
## year2000 1.0955e-01 6.6880e-03 16.3808 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 128.1
## Residual Sum of Squares: 70.409
## R-Squared: 0.45035
## Adj. R-Squared: 0.26602
## F-statistic: 469.9 on 6 and 3441 DF, p-value: < 2.22e-16
Oneway (individual) effect Within Model
Call:
plm(formula = lpassen ~ fare + dist + passen + bmktshr + I(ldist^2)
+
year, data = time, model = “within”)
Balanced Panel: n = 1149, T = 4, N = 4596
Residuals:
Min. 1st Qu. Median 3rd Qu. Max.
-2.17315683 -0.03750978 0.00081035 0.03994864 1.98735732
Coefficients:
Estimate Std. Error t-value Pr(>|t|)
fare -4.3260e-03 1.3776e-04 -31.4030 < 2.2e-16 ***
passen 5.4114e-04 2.2967e-05 23.5623 < 2.2e-16 ***
bmktshr 1.6054e-01 3.9765e-02 4.0374 5.523e-05 ***
year1998 1.5337e-02 6.0027e-03 2.5550 0.01066 *
year1999 5.0585e-02 6.1888e-03 8.1737 4.160e-16 ***
year2000 1.0955e-01 6.6880e-03 16.3808 < 2.2e-16 ***
—
Signif. codes: 0 ‘’ 0.001 ’’ 0.01 ’’ 0.05
‘.’ 0.1 ’ ’ 1
Total Sum of Squares: 128.1
Residual Sum of Squares: 70.409
R-Squared: 0.45035
Adj. R-Squared: 0.26602
F-statistic: 469.9 on 6 and 3441 DF, p-value: < 2.22e-16
model3 <- plm(lpassen ~ fare + dist + passen + bmktshr + I(ldist^2) + year, data = time, model = "random" )
summary(model3)
## Oneway (individual) effect Random Effect Model
## (Swamy-Arora's transformation)
##
## Call:
## plm(formula = lpassen ~ fare + dist + passen + bmktshr + I(ldist^2) +
## year, data = time, model = "random")
##
## Balanced Panel: n = 1149, T = 4, N = 4596
##
## Effects:
## var std.dev share
## idiosyncratic 0.02046 0.14304 0.081
## individual 0.23084 0.48046 0.919
## theta: 0.8528
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -2.561815 -0.055653 0.015611 0.070798 1.641581
##
## Coefficients:
## Estimate Std. Error z-value Pr(>|z|)
## (Intercept) 5.8578e+00 1.9585e-01 29.9097 < 2.2e-16 ***
## fare -3.2722e-03 1.2039e-04 -27.1795 < 2.2e-16 ***
## dist 3.0581e-04 8.4715e-05 3.6099 0.0003063 ***
## passen 7.6429e-04 1.4579e-05 52.4224 < 2.2e-16 ***
## bmktshr 1.9990e-01 3.7525e-02 5.3271 9.977e-08 ***
## I(ldist^2) -4.3679e-03 5.9083e-03 -0.7393 0.4597420
## year1998 9.5304e-03 6.1333e-03 1.5539 0.1202158
## year1999 3.3770e-02 6.2124e-03 5.4358 5.454e-08 ***
## year2000 7.9356e-02 6.5168e-03 12.1771 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 203.17
## Residual Sum of Squares: 98.487
## R-Squared: 0.51524
## Adj. R-Squared: 0.51439
## Chisq: 4875.39 on 8 DF, p-value: < 2.22e-16
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
stargazer(modeli1, model2, model3, type = "text" )
##
## ===============================================================================
## Dependent variable:
## ------------------------------------------------------------------
## lpassen
## (1) (2) (3)
## -------------------------------------------------------------------------------
## fare -0.001*** -0.004*** -0.003***
## (0.0001) (0.0001) (0.0001)
##
## dist 0.0001** 0.0003***
## (0.00004) (0.0001)
##
## passen 0.001*** 0.001*** 0.001***
## (0.00001) (0.00002) (0.00001)
##
## bmktshr 0.223*** 0.161*** 0.200***
## (0.047) (0.040) (0.038)
##
## I(ldist2) 0.001 -0.004
## (0.003) (0.006)
##
## year1998 0.004 0.015** 0.010
## (0.021) (0.006) (0.006)
##
## year1999 0.018 0.051*** 0.034***
## (0.021) (0.006) (0.006)
##
## year2000 0.041* 0.110*** 0.079***
## (0.021) (0.007) (0.007)
##
## Constant 5.370*** 5.858***
## (0.110) (0.196)
##
## -------------------------------------------------------------------------------
## Observations 4,596 4,596 4,596
## R2 0.677 0.450 0.515
## Adjusted R2 0.676 0.266 0.514
## F Statistic 1,199.361*** (df = 8; 4587) 469.900*** (df = 6; 3441) 4,875.385***
## ===============================================================================
## Note: *p<0.1; **p<0.05; ***p<0.01
phtest(model2, model3)
##
## Hausman Test
##
## data: lpassen ~ fare + dist + passen + bmktshr + I(ldist^2) + year
## chisq = 253.23, df = 6, p-value < 2.2e-16
## alternative hypothesis: one model is inconsistent
library(wooldridge)
library(rmarkdown)
data("murder")
paged_table(murder)
library(plm)
bir <- pdata.frame(murder, index = c("state", "year" ) )
summary(bir)
## id state year mrdrte exec
## Min. : 1 AK : 3 87:51 Min. : 0.800 Min. : 0.000
## 1st Qu.:13 AL : 3 90:51 1st Qu.: 3.900 1st Qu.: 0.000
## Median :26 AR : 3 93:51 Median : 6.400 Median : 0.000
## Mean :26 AZ : 3 Mean : 8.071 Mean : 1.229
## 3rd Qu.:39 CA : 3 3rd Qu.:10.200 3rd Qu.: 1.000
## Max. :51 CO : 3 Max. :78.500 Max. :34.000
## (Other):135
## unem d90 d93 cmrdrte
## Min. : 2.200 Min. :0.0000 Min. :0.0000 Min. :-2.6000
## 1st Qu.: 4.900 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:-0.4000
## Median : 5.800 Median :0.0000 Median :0.0000 Median : 0.3000
## Mean : 5.973 Mean :0.3333 Mean :0.3333 Mean : 0.8422
## 3rd Qu.: 7.000 3rd Qu.:1.0000 3rd Qu.:1.0000 3rd Qu.: 1.3000
## Max. :12.000 Max. :1.0000 Max. :1.0000 Max. :41.6000
## NA's :51
## cexec cunem cexec_1 cunem_1
## Min. :-11.0000 Min. :-5.80000 Min. :-11.0000 Min. :-5.8000
## 1st Qu.: 0.0000 1st Qu.:-1.07500 1st Qu.: 0.0000 1st Qu.:-1.9500
## Median : 0.0000 Median : 0.30000 Median : 0.0000 Median :-1.0000
## Mean : 0.1863 Mean : 0.00588 Mean : -0.2745 Mean :-0.8863
## 3rd Qu.: 0.0000 3rd Qu.: 1.00000 3rd Qu.: 0.0000 3rd Qu.: 0.0000
## Max. : 23.0000 Max. : 3.60000 Max. : 5.0000 Max. : 3.1000
## NA's :51 NA's :51 NA's :102 NA's :102
pvar(bir)
## no time variation: id state cmrdrte cexec cunem cexec_1 cunem_1
## no individual variation: year d90 d93
## all NA in time dimension for at least one individual: cmrdrte cexec cunem cexec_1 cunem_1
## all NA in ind. dimension for at least one time period: cmrdrte cexec cunem cexec_1 cunem_1
ar1 <- plm(mrdrte ~ exec + unem + d90 + d93 + I(cexec^2) + year, data = bir , model = "pooling")
summary(ar1)
## Pooling Model
##
## Call:
## plm(formula = mrdrte ~ exec + unem + d90 + d93 + I(cexec^2) +
## year, data = bir, model = "pooling")
##
## Balanced Panel: n = 51, T = 2, N = 102
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -12.9092 -3.3651 -1.5955 1.3242 66.5059
##
## Coefficients: (2 dropped because of singularities)
## Estimate Std. Error t-value Pr(>|t|)
## (Intercept) -7.399751 5.118582 -1.4457 0.151495
## exec 0.323921 0.647303 0.5004 0.617915
## unem 2.519343 0.785822 3.2060 0.001823 **
## d90 2.066181 2.154439 0.9590 0.339926
## I(cexec^2) -0.015443 0.046507 -0.3321 0.740556
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 11401
## Residual Sum of Squares: 10231
## R-Squared: 0.10263
## Adj. R-Squared: 0.065627
## F-statistic: 2.77347 on 4 and 97 DF, p-value: 0.031305
ar2 <- plm(mrdrte ~ exec + unem + d90 + d93 + I(cexec^2) + year, data = bir , model = "within")
summary(ar2)
## Oneway (individual) effect Within Model
##
## Call:
## plm(formula = mrdrte ~ exec + unem + d90 + d93 + I(cexec^2) +
## year, data = bir, model = "within")
##
## Balanced Panel: n = 51, T = 2, N = 102
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -1.32025 -0.37707 0.00000 0.37707 1.32025
##
## Coefficients: (2 dropped because of singularities)
## Estimate Std. Error t-value Pr(>|t|)
## exec -0.0043263 0.1252189 -0.0345 0.97258
## unem -0.0717520 0.1592698 -0.4505 0.65442
## d90 -0.4029345 0.2103551 -1.9155 0.06152 .
## I(cexec^2) -0.0063814 0.0075287 -0.8476 0.40095
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 33.47
## Residual Sum of Squares: 27.516
## R-Squared: 0.1779
## Adj. R-Squared: -0.76664
## F-statistic: 2.54269 on 4 and 47 DF, p-value: 0.051892
ar3 <- plm(mrdrte ~ exec + unem + d90 + d93 + I(cexec^2) + year, data = bir , model = "random")
summary(ar3)
## Oneway (individual) effect Random Effect Model
## (Swamy-Arora's transformation)
##
## Call:
## plm(formula = mrdrte ~ exec + unem + d90 + d93 + I(cexec^2) +
## year, data = bir, model = "random")
##
## Balanced Panel: n = 51, T = 2, N = 102
##
## Effects:
## var std.dev share
## idiosyncratic 0.5854 0.7651 0.005
## individual 106.1862 10.3047 0.995
## theta: 0.9476
##
## Residuals:
## Min. 1st Qu. Median 3rd Qu. Max.
## -1.36973 -0.46929 -0.12669 0.32637 3.83832
##
## Coefficients: (2 dropped because of singularities)
## Estimate Std. Error z-value Pr(>|z|)
## (Intercept) 8.9214366 1.7891607 4.9864 6.152e-07 ***
## exec 0.0059700 0.1265524 0.0472 0.96237
## unem -0.0188165 0.1609622 -0.1169 0.90694
## d90 -0.3520064 0.2135355 -1.6485 0.09926 .
## I(cexec^2) -0.0068039 0.0076242 -0.8924 0.37217
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Total Sum of Squares: 64.721
## Residual Sum of Squares: 59.026
## R-Squared: 0.087985
## Adj. R-Squared: 0.050376
## Chisq: 9.35789 on 4 DF, p-value: 0.052751
stargazer(ar1, ar2, ar3, type = "text" )
##
## ==============================================================
## Dependent variable:
## -------------------------------------------------
## mrdrte
## (1) (2) (3)
## --------------------------------------------------------------
## exec 0.324 -0.004 0.006
## (0.647) (0.125) (0.127)
##
## unem 2.519*** -0.072 -0.019
## (0.786) (0.159) (0.161)
##
## d90 2.066 -0.403* -0.352*
## (2.154) (0.210) (0.214)
##
## I(cexec2) -0.015 -0.006 -0.007
## (0.047) (0.008) (0.008)
##
## Constant -7.400 8.921***
## (5.119) (1.789)
##
## --------------------------------------------------------------
## Observations 102 102 102
## R2 0.103 0.178 0.088
## Adjusted R2 0.066 -0.767 0.050
## F Statistic 2.773** (df = 4; 97) 2.543* (df = 4; 47) 9.358*
## ==============================================================
## Note: *p<0.1; **p<0.05; ***p<0.01
==============================================================
Dependent variable:
————————————————-
mrdrte
(1) (2) (3)
————————————————————–
exec 0.324 -0.004 0.006
(0.647) (0.125) (0.127)
unem 2.519*** -0.072 -0.019
(0.786) (0.159) (0.161)
d90 2.066 -0.403* -0.352*
(2.154) (0.210) (0.214)
I(cexec2) -0.015 -0.006 -0.007
(0.047) (0.008) (0.008)
Constant -7.400 8.921***
(5.119) (1.789)
————————————————————–
Observations 102 102 102
R2 0.103 0.178 0.088
Adjusted R2 0.066 -0.767 0.050
F Statistic 2.773** (df = 4; 97) 2.543* (df = 4; 47) 9.358*
==============================================================
Note: p<0.1; p<0.05; p<0.01