Preliminaries

The below preliminaries follow what we are used to, expect we now load in the plm package which contains the plm() function capable of using panel data estimators.

# Clear environment, plot pane, and console
rm(list = ls())
graphics.off()
cat("\014")
# Set working directory
setwd("C:/Users/wbras/OneDrive/Desktop/UA/Fall_2024/ECON_418-518/ECON_418-518_Lecture_Slides/ECON_418-518_Lecture_Slides_E/ECON_418-518_E_13_14_Panel/ECON_418-518_E_13_14_Panel_Code")

# Install pacman
if (!require(pacman)) install.packages("pacman")

# Load packages
pacman::p_load(data.table, plm, ggplot2, gridExtra)

# Colors used for plot (https://www.rapidtables.com/web/color/RGB_Color.html)
color_1 <- "#00FF00"
color_2 <- "#ADD8E6"

# Set seed for reproducibility
set.seed(418518)

# Disable scientific notation
options(scipen = 999)

Pooled Cross-Section

Let’s load in the cps78_85 pooled cross-sectional data set from the wooldridge package. This data set contains data on wages for individuals in 1978, data on wages for different individuals in 1985, and a rich set of covariates. We’ll load this data set in and do some basic data analysis.

# Load in cps78_85 data set from wooldridge as a data table
dt <- data.table(wooldridge::cps78_85)

# Show the first few rows of the data
head(dt)
# Show the last few rows of the data
tail(dt)
# Get the names of the variables in our dataset
names(dt)
##  [1] "educ"     "south"    "nonwhite" "female"   "married"  "exper"   
##  [7] "expersq"  "union"    "lwage"    "age"      "year"     "y85"     
## [13] "y85fem"   "y85educ"  "y85union"
# Get summary statistics
summary(dt)
##       educ           south           nonwhite          female     
##  Min.   : 1.00   Min.   :0.0000   Min.   :0.0000   Min.   :0.000  
##  1st Qu.:12.00   1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.000  
##  Median :12.00   Median :0.0000   Median :0.0000   Median :0.000  
##  Mean   :12.77   Mean   :0.2943   Mean   :0.1144   Mean   :0.417  
##  3rd Qu.:14.00   3rd Qu.:1.0000   3rd Qu.:0.0000   3rd Qu.:1.000  
##  Max.   :18.00   Max.   :1.0000   Max.   :1.0000   Max.   :1.000  
##     married           exper          expersq           union       
##  Min.   :0.0000   Min.   : 0.00   Min.   :   0.0   Min.   :0.0000  
##  1st Qu.:0.0000   1st Qu.: 8.00   1st Qu.:  64.0   1st Qu.:0.0000  
##  Median :1.0000   Median :15.00   Median : 225.0   Median :0.0000  
##  Mean   :0.6541   Mean   :18.28   Mean   : 499.8   Mean   :0.2435  
##  3rd Qu.:1.0000   3rd Qu.:28.00   3rd Qu.: 784.0   3rd Qu.:0.0000  
##  Max.   :1.0000   Max.   :55.00   Max.   :3025.0   Max.   :1.0000  
##      lwage             age             year            y85        
##  Min.   :-0.470   Min.   :18.00   Min.   :78.00   Min.   :0.0000  
##  1st Qu.: 1.470   1st Qu.:27.00   1st Qu.:78.00   1st Qu.:0.0000  
##  Median : 1.833   Median :34.00   Median :78.00   Median :0.0000  
##  Mean   : 1.867   Mean   :36.54   Mean   :81.45   Mean   :0.4926  
##  3rd Qu.: 2.225   3rd Qu.:46.00   3rd Qu.:85.00   3rd Qu.:1.0000  
##  Max.   : 3.796   Max.   :64.00   Max.   :85.00   Max.   :1.0000  
##      y85fem         y85educ          y85union      
##  Min.   :0.000   Min.   : 0.000   Min.   :0.00000  
##  1st Qu.:0.000   1st Qu.: 0.000   1st Qu.:0.00000  
##  Median :0.000   Median : 0.000   Median :0.00000  
##  Mean   :0.226   Mean   : 6.413   Mean   :0.08856  
##  3rd Qu.:0.000   3rd Qu.:12.000   3rd Qu.:0.00000  
##  Max.   :1.000   Max.   :18.000   Max.   :1.00000
# Get the number of observations
paste0("There are ", nrow(dt), " observations in this data set.")
## [1] "There are 1084 observations in this data set."
# Show many NA values there are
paste0("There are ", sum(is.na(dt)), " NAs in this data set.")
## [1] "There are 0 NAs in this data set."

You’ll notice the y85 indicator variable. This variable is equal to one if the observation was recorded in 1985 and zero otherwise.

Pooled OLS

Let’s estimate the model

\[\begin{align} \ln (wage_{it}) &= \beta_0 + \beta_1 y85 + \beta_2 educ_{i} + \beta_3 y85 \times educ_{i} + \beta_4 exper_{it} \\ &+ \beta_5 exper_{it}^2 + \beta_6 union_{it} + \beta_7 female_{i} + \beta_8 y85 \times female_{i} \end{align}\]

by using R’s lm() function to determine if the change in female wages in 1985 is statistically significant at the \(\alpha = 0.05\) level.

# Estimate model
reg <- lm(lwage ~ y85 + educ + I(y85 * educ) + exper + I(exper^2) + union + female + I(y85 * female), data = dt)

# Summary of regression
summary(reg)
## 
## Call:
## lm(formula = lwage ~ y85 + educ + I(y85 * educ) + exper + I(exper^2) + 
##     union + female + I(y85 * female), data = dt)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.56098 -0.25828  0.00864  0.26571  2.11669 
## 
## Coefficients:
##                    Estimate  Std. Error t value             Pr(>|t|)    
## (Intercept)      0.45893288  0.09344850   4.911 0.000001046281112436 ***
## y85              0.11780622  0.12378173   0.952               0.3415    
## educ             0.07472091  0.00667643  11.192 < 0.0000000000000002 ***
## I(y85 * educ)    0.01846053  0.00935417   1.974               0.0487 *  
## exper            0.02958431  0.00356731   8.293 0.000000000000000327 ***
## I(exper^2)      -0.00039943  0.00007754  -5.151 0.000000307571128768 ***
## union            0.20213187  0.03029449   6.672 0.000000000040256264 ***
## female          -0.31670865  0.03662145  -8.648 < 0.0000000000000002 ***
## I(y85 * female)  0.08505197  0.05130896   1.658               0.0977 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4127 on 1075 degrees of freedom
## Multiple R-squared:  0.4262, Adjusted R-squared:  0.4219 
## F-statistic:  99.8 on 8 and 1075 DF,  p-value: < 0.00000000000000022

We see that the parameter estimate of \(\beta_8\) is statistically insignificant at the \(\alpha = 0.05\) level. Hence, we fail to reject the null that \(\beta_8 = 0\) and conclude that the effect of being female on wages does not differ between 1978 and 1985.

Moreover, we see the effect of education in 1985 is statistically significant at the \(\alpha = 0.05\) level with a p-value of \(0.0487\). Given this parameter estimate \(\widehat{\beta}_3 > 0\), the returns to education seem to have increased over the seven year period.

Panel Data

We will use the wagepan panel data set contained in the wooldridge package. This data set contains wage information for \(545\) different individuals tracked from 1980 to 1987 along with a rich set of covariates. Let’s load this data in and do some basic data analysis.

# Load in wagepan data set from wooldridge as a data table
dt <- data.table(wooldridge::wagepan)

# Sort the data by the person identifier nr
setorder(dt, nr)

# Show the first few rows of the data
head(dt)
# Show the last few rows of the data
tail(dt)
# Get the names of the variables in our dataset
names(dt)
##  [1] "nr"       "year"     "agric"    "black"    "bus"      "construc"
##  [7] "ent"      "exper"    "fin"      "hisp"     "poorhlth" "hours"   
## [13] "manuf"    "married"  "min"      "nrthcen"  "nrtheast" "occ1"    
## [19] "occ2"     "occ3"     "occ4"     "occ5"     "occ6"     "occ7"    
## [25] "occ8"     "occ9"     "per"      "pro"      "pub"      "rur"     
## [31] "south"    "educ"     "tra"      "trad"     "union"    "lwage"   
## [37] "d81"      "d82"      "d83"      "d84"      "d85"      "d86"     
## [43] "d87"      "expersq"
# Get the number of observations
paste0("There are ", nrow(dt), " observations in this data set.")
## [1] "There are 4360 observations in this data set."
# Show many NA values there are
paste0("There are ", sum(is.na(dt)), " NAs in this data set.")
## [1] "There are 0 NAs in this data set."
# Number of unique individuals in the data set
paste0("There are ", uniqueN(dt[, nr]), " unique individuals in this data set.")
## [1] "There are 545 unique individuals in this data set."

Firstly, nr corresponds to a unique person identifier. This just means each person in the sample was given a unique number so they can be tracked over the eight years.

Secondly, notice how there is no variable corresponding to 1980. This means we treat it as the base year and exclude it from the regression.

Thirdly, you can see we have zero NAs in this data set meaning it is a balanced panel. Thus, we don’t have to worry about any complications that can arise with unbalanced panels.

Pooled OLS

We will estimate the regression model of log wage on marital status, union status, a full set of year indicator variables, and the interaction of these year indicator variables with education. Let’s do this now using pooled OLS.

# Estimate model
reg <- lm(lwage ~ married + union + d81 + d82 + d83 + d84 + d85 + d86 + d87
          + I(educ * d81) + I(educ * d82) + I(educ * d83) + I(educ * d84)
          + I(educ * d85) + I(educ * d86) + I(educ * d87), 
          data = dt)

# Summary of regression
summary(reg)
## 
## Call:
## lm(formula = lwage ~ married + union + d81 + d82 + d83 + d84 + 
##     d85 + d86 + d87 + I(educ * d81) + I(educ * d82) + I(educ * 
##     d83) + I(educ * d84) + I(educ * d85) + I(educ * d86) + I(educ * 
##     d87), data = dt)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.2510 -0.2615  0.0337  0.3016  2.4873 
## 
## Coefficients:
##               Estimate Std. Error t value             Pr(>|t|)    
## (Intercept)    1.32394    0.02141  61.843 < 0.0000000000000002 ***
## married        0.13456    0.01548   8.690 < 0.0000000000000002 ***
## union          0.17743    0.01716  10.341 < 0.0000000000000002 ***
## d81           -0.73135    0.14330  -5.104    0.000000347265789 ***
## d82           -0.71153    0.14326  -4.967    0.000000706798337 ***
## d83           -0.70438    0.14328  -4.916    0.000000915306086 ***
## d84           -0.63915    0.14329  -4.461    0.000008380627669 ***
## d85           -0.65248    0.14325  -4.555    0.000005389297523 ***
## d86           -0.63402    0.14328  -4.425    0.000009877000320 ***
## d87           -0.62762    0.14328  -4.380    0.000012125786965 ***
## I(educ * d81)  0.07115    0.01192   5.971    0.000000002540563 ***
## I(educ * d82)  0.07356    0.01191   6.174    0.000000000725482 ***
## I(educ * d83)  0.07613    0.01191   6.390    0.000000000182873 ***
## I(educ * d84)  0.07593    0.01192   6.373    0.000000000205039 ***
## I(educ * d85)  0.08119    0.01192   6.813    0.000000000010854 ***
## I(educ * d86)  0.08454    0.01192   7.095    0.000000000001508 ***
## I(educ * d87)  0.08846    0.01192   7.423    0.000000000000137 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4856 on 4343 degrees of freedom
## Multiple R-squared:  0.1718, Adjusted R-squared:  0.1688 
## F-statistic: 56.31 on 16 and 4343 DF,  p-value: < 0.00000000000000022

We see an additional year of education in 1981 leads to an estimated increase in wage of approximately \(7.12\%\) relative to 1980 while an additional year of education in 1987 leads to an estimated increase in wage of approximately \(8.85\%\) relative to 1980.

Now, we will use the plm() function in the plm package to run this same regression. Whenever estimating models with a panel data set, you will likely want to use this function as we can easily estimate the common panel data methods very easily with it. Running a regression using plm() is almost identical to using lm() except we add two things:

  1. model = "pooling" tells the plm() function to use the pooled OLS estimator
  2. index = c("nr", "year") tells the plm() function that the observations are indexed by the nr and year variables

Let’s run our pooled OLS model using plm(). You will see we get identical results to that produced above by the lm() function.

# Estimate model
reg <- plm(lwage ~ married + union + d81 + d82 + d83 + d84 + d85 + d86 + d87
          + I(educ * d81) + I(educ * d82) + I(educ * d83) + I(educ * d84)
          + I(educ * d85) + I(educ * d86) + I(educ * d87), 
          model = "pooling",
          index = c("nr", "year"),
          data = dt)

# Summary of regression
summary(reg)
## Pooling Model
## 
## Call:
## plm(formula = lwage ~ married + union + d81 + d82 + d83 + d84 + 
##     d85 + d86 + d87 + I(educ * d81) + I(educ * d82) + I(educ * 
##     d83) + I(educ * d84) + I(educ * d85) + I(educ * d86) + I(educ * 
##     d87), data = dt, model = "pooling", index = c("nr", "year"))
## 
## Balanced Panel: n = 545, T = 8, N = 4360
## 
## Residuals:
##      Min.   1st Qu.    Median   3rd Qu.      Max. 
## -5.250995 -0.261461  0.033653  0.301559  2.487278 
## 
## Coefficients:
##                Estimate Std. Error t-value              Pr(>|t|)    
## (Intercept)    1.323937   0.021408 61.8430 < 0.00000000000000022 ***
## married        0.134563   0.015484  8.6902 < 0.00000000000000022 ***
## union          0.177432   0.017157 10.3414 < 0.00000000000000022 ***
## d81           -0.731346   0.143297 -5.1037    0.0000003472657885 ***
## d82           -0.711531   0.143257 -4.9668    0.0000007067983366 ***
## d83           -0.704384   0.143280 -4.9161    0.0000009153060857 ***
## d84           -0.639154   0.143290 -4.4606    0.0000083806276688 ***
## d85           -0.652475   0.143252 -4.5547    0.0000053892975234 ***
## d86           -0.634015   0.143278 -4.4251    0.0000098770003196 ***
## d87           -0.627619   0.143279 -4.3804    0.0000121257869647 ***
## I(educ * d81)  0.071151   0.011915  5.9714    0.0000000025405630 ***
## I(educ * d82)  0.073556   0.011913  6.1743    0.0000000007254821 ***
## I(educ * d83)  0.076132   0.011913  6.3904    0.0000000001828732 ***
## I(educ * d84)  0.075933   0.011915  6.3727    0.0000000002050392 ***
## I(educ * d85)  0.081193   0.011917  6.8132    0.0000000000108538 ***
## I(educ * d86)  0.084544   0.011917  7.0946    0.0000000000015077 ***
## I(educ * d87)  0.088459   0.011917  7.4229    0.0000000000001371 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    1236.5
## Residual Sum of Squares: 1024.1
## R-Squared:      0.17182
## Adj. R-Squared: 0.16877
## F-statistic: 56.3131 on 16 and 4343 DF, p-value: < 0.000000000000000222

First Differences

Now, we will estimate the same model as before, except using the first differences estimation strategy. You’ll see that we now have the model = "fd" command within the plm() function telling it to use the first differences estimator.

# Estimate model
reg <- plm(lwage ~ married + union + d81 + d82 + d83 + d84 + d85 + d86 + d87
          + I(educ * d81) + I(educ * d82) + I(educ * d83) + I(educ * d84)
          + I(educ * d85) + I(educ * d86) + I(educ * d87), 
          model = "fd",
          index = c("nr", "year"),
          data = dt)

# Summary of regression
summary(reg)
## Oneway (individual) effect First-Difference Model
## 
## Call:
## plm(formula = lwage ~ married + union + d81 + d82 + d83 + d84 + 
##     d85 + d86 + d87 + I(educ * d81) + I(educ * d82) + I(educ * 
##     d83) + I(educ * d84) + I(educ * d85) + I(educ * d86) + I(educ * 
##     d87), data = dt, model = "fd", index = c("nr", "year"))
## 
## Balanced Panel: n = 545, T = 8, N = 4360
## Observations used in estimation: 3815
## 
## Residuals:
##     Min.  1st Qu.   Median  3rd Qu.     Max. 
## -4.57851 -0.14609 -0.01293  0.13535  4.84367 
## 
## Coefficients: (1 dropped because of singularities)
##                Estimate Std. Error t-value Pr(>|t|)  
## (Intercept)    0.012968   0.048926  0.2651  0.79098  
## married        0.039850   0.022975  1.7345  0.08291 .
## union          0.041176   0.019734  2.0865  0.03700 *
## d81           -0.039864   0.119901 -0.3325  0.73955  
## d82           -0.035560   0.154726 -0.2298  0.81824  
## d83           -0.029814   0.169490 -0.1759  0.86038  
## d84            0.035198   0.169486  0.2077  0.83549  
## d85           -0.020070   0.154735 -0.1297  0.89680  
## d86           -0.019377   0.119890 -0.1616  0.87161  
## I(educ * d81)  0.012090   0.010884  1.1108  0.26672  
## I(educ * d82)  0.015358   0.015393  0.9977  0.31848  
## I(educ * d83)  0.017546   0.018851  0.9308  0.35201  
## I(educ * d84)  0.016756   0.021768  0.7698  0.44148  
## I(educ * d85)  0.024485   0.024342  1.0058  0.31456  
## I(educ * d86)  0.028376   0.026664  1.0642  0.28730  
## I(educ * d87)  0.030990   0.028798  1.0761  0.28194  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    751.19
## Residual Sum of Squares: 747.34
## R-Squared:      0.0051256
## Adj. R-Squared: 0.0011974
## F-statistic: 1.30482 on 15 and 3799 DF, p-value: 0.18966

We see our parameter estimates have changed drastically! For instance, now the return to education in 1987 is \(3.1\%\) higher than in 1980 while when using pooled OLS the return to education in 1987 is \(8.85\%\) higher than in 1980. This difference in slope estimates is likely attributable to the fixed effect \(c_i\)! Pooled OLS does not account for unobserved heterogeneity across individuals that is fixed over time, such as innate ability, while first differences does. In practice, if you have a panel data set, you will want to take advantage of the suite of estimators available to you like first differences and fixed effects rather than pooled OLS for reasons exactly like this.

Now, let’s estimate the first differences model manually by taking the first differences ourselves and then using the lm() function. After all, that is exactly what plm() is doing.

You’ll notice that we don’t difference the variable educ. This is because this covariate is fixed across time so differencing it effectively removes the variable from the regression (education would be zero for all individuals in all time periods). Thus, we only take the first difference of a variable that varies over time when running the first differences estimator.

Moreover, you’ll see some new R code we haven’t used before

  1. by = nr groups the data by the nr column so all data manipulations are done only within each person category nr
  2. shift(lwage, type = "lag") grabs the previous value of log wage (the lag of log wage)
  3. lwage - shift(lwage, type = "lag") is the first difference of log wage
# Compute first differences for all relevant columns
dt[, `:=`(
  diff_lwage = lwage - shift(lwage, type = "lag"),
  diff_married = married - shift(married, type = "lag"),
  diff_union = union - shift(union, type = "lag"),
  diff_d81 = d81 - shift(d81, type = "lag"),
  diff_d82 = d82 - shift(d82, type = "lag"),
  diff_d83 = d83 - shift(d83, type = "lag"),
  diff_d84 = d84 - shift(d84, type = "lag"),
  diff_d85 = d85 - shift(d85, type = "lag"),
  diff_d86 = d86 - shift(d86, type = "lag"),
  diff_d87 = d87 - shift(d87, type = "lag")
), by = nr]

# Run first differences regression
reg <- lm(diff_lwage ~ diff_married + diff_union + diff_d81 + 
          diff_d82 + diff_d83 + diff_d84 + diff_d85 + 
          diff_d86 + diff_d87 + 
          I(educ * diff_d81) + 
          I(educ * diff_d82) + 
          I(educ * diff_d83) + 
          I(educ * diff_d84) + 
          I(educ * diff_d85) + 
          I(educ * diff_d86) + 
          I(educ * diff_d87),
          data = dt)

# Summary of regression
reg
## 
## Call:
## lm(formula = diff_lwage ~ diff_married + diff_union + diff_d81 + 
##     diff_d82 + diff_d83 + diff_d84 + diff_d85 + diff_d86 + diff_d87 + 
##     I(educ * diff_d81) + I(educ * diff_d82) + I(educ * diff_d83) + 
##     I(educ * diff_d84) + I(educ * diff_d85) + I(educ * diff_d86) + 
##     I(educ * diff_d87), data = dt)
## 
## Coefficients:
##        (Intercept)        diff_married          diff_union            diff_d81  
##            0.01297             0.03985             0.04118            -0.03986  
##           diff_d82            diff_d83            diff_d84            diff_d85  
##           -0.03556            -0.02981             0.03520            -0.02007  
##           diff_d86            diff_d87  I(educ * diff_d81)  I(educ * diff_d82)  
##           -0.01938                  NA             0.01209             0.01536  
## I(educ * diff_d83)  I(educ * diff_d84)  I(educ * diff_d85)  I(educ * diff_d86)  
##            0.01755             0.01676             0.02448             0.02838  
## I(educ * diff_d87)  
##            0.03099

You can see we get identical results to that produced when using plm().

Fixed Effects

Now, we will estimate the same model as before, except using the fixed effects estimation strategy. You’ll see that we now have the model = "within" command in the plm() function telling it to use the within estimator. The within estimator is just another name for the fixed effect estimator.

# Estimate model
reg <- plm(lwage ~ married + union + d81 + d82 + d83 + d84 + d85 + d86 + d87
          + I(educ * d81) + I(educ * d82) + I(educ * d83) + I(educ * d84)
          + I(educ * d85) + I(educ * d86) + I(educ * d87), 
          model = "within",
          index = c("nr", "year"),
          data = dt)

# Summary of regression
summary(reg)
## Oneway (individual) effect Within Model
## 
## Call:
## plm(formula = lwage ~ married + union + d81 + d82 + d83 + d84 + 
##     d85 + d86 + d87 + I(educ * d81) + I(educ * d82) + I(educ * 
##     d83) + I(educ * d84) + I(educ * d85) + I(educ * d86) + I(educ * 
##     d87), data = dt, model = "within", index = c("nr", "year"))
## 
## Balanced Panel: n = 545, T = 8, N = 4360
## 
## Residuals:
##      Min.   1st Qu.    Median   3rd Qu.      Max. 
## -4.152111 -0.125630  0.010897  0.160800  1.483401 
## 
## Coefficients:
##                 Estimate Std. Error t-value   Pr(>|t|)    
## married        0.0548205  0.0184126  2.9773   0.002926 ** 
## union          0.0829785  0.0194461  4.2671 0.00002029 ***
## d81           -0.0224158  0.1458885 -0.1537   0.877893    
## d82           -0.0057611  0.1458558 -0.0395   0.968495    
## d83            0.0104297  0.1458579  0.0715   0.942999    
## d84            0.0843743  0.1458518  0.5785   0.562965    
## d85            0.0497253  0.1458602  0.3409   0.733190    
## d86            0.0656064  0.1458917  0.4497   0.652958    
## d87            0.0904448  0.1458505  0.6201   0.535216    
## I(educ * d81)  0.0115854  0.0122625  0.9448   0.344827    
## I(educ * d82)  0.0147905  0.0122635  1.2061   0.227872    
## I(educ * d83)  0.0171182  0.0122633  1.3959   0.162830    
## I(educ * d84)  0.0165839  0.0122657  1.3521   0.176437    
## I(educ * d85)  0.0237085  0.0122738  1.9316   0.053479 .  
## I(educ * d86)  0.0274123  0.0122740  2.2334   0.025583 *  
## I(educ * d87)  0.0304332  0.0122723  2.4798   0.013188 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    572.05
## Residual Sum of Squares: 474.35
## R-Squared:      0.1708
## Adj. R-Squared: 0.048567
## F-statistic: 48.9069 on 16 and 3799 DF, p-value: < 0.000000000000000222

You can see our results are very close to that produced by the first differences estimator. While at the top you see “Oneway (individual) effect Within Model”, this is actually a two-way effect model. The plm() function takes care of the individual fixed effect a_i for us via mean differencing and we are manually taking care of the time effect by including the full suite of time indicators.

We will use the lm() function to account for unobserved individual heterogeneity that is fixed across time (individual fixed effects), such as innate ability. To do so, we will simply add in a full suite of individual indicator variables. This can be done by adding factor(nr) into the lm() equation. You will see we get identical results to that produced above because mean differencing and individual indicator variables do the same thing with regard to fixed effects estimation.

# Estimate model
reg <- lm(lwage ~ married + union + d81 + d82 + d83 + d84 + d85 + d86 + d87
          + I(educ * d81) + I(educ * d82) + I(educ * d83) + I(educ * d84)
          + I(educ * d85) + I(educ * d86) + I(educ * d87) + factor(nr), 
          data = dt)

# Summary of regression
summary(reg)
## 
## Call:
## lm(formula = lwage ~ married + union + d81 + d82 + d83 + d84 + 
##     d85 + d86 + d87 + I(educ * d81) + I(educ * d82) + I(educ * 
##     d83) + I(educ * d84) + I(educ * d85) + I(educ * d86) + I(educ * 
##     d87) + factor(nr), data = dt)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.1521 -0.1256  0.0109  0.1608  1.4834 
## 
## Coefficients:
##                     Estimate   Std. Error t value             Pr(>|t|)    
## (Intercept)      0.963373230  0.127165245   7.576  0.00000000000004458 ***
## married          0.054820477  0.018412571   2.977             0.002926 ** 
## union            0.082978472  0.019446086   4.267  0.00002028558142820 ***
## d81             -0.022415835  0.145888470  -0.154             0.877893    
## d82             -0.005761061  0.145855789  -0.039             0.968495    
## d83              0.010429718  0.145857920   0.072             0.942999    
## d84              0.084374320  0.145851822   0.578             0.562965    
## d85              0.049725289  0.145860184   0.341             0.733190    
## d86              0.065606423  0.145891739   0.450             0.652958    
## d87              0.090444839  0.145850528   0.620             0.535216    
## I(educ * d81)    0.011585424  0.012262457   0.945             0.344827    
## I(educ * d82)    0.014790475  0.012263529   1.206             0.227872    
## I(educ * d83)    0.017118202  0.012263303   1.396             0.162830    
## I(educ * d84)    0.016583929  0.012265686   1.352             0.176437    
## I(educ * d85)    0.023708543  0.012273823   1.932             0.053479 .  
## I(educ * d86)    0.027412292  0.012273983   2.233             0.025583 *  
## I(educ * d87)    0.030433178  0.012272340   2.480             0.013188 *  
## factor(nr)17     0.410210611  0.176880164   2.319             0.020440 *  
## factor(nr)18     0.769694699  0.178460577   4.313  0.00001651707756617 ***
## factor(nr)45     0.536194690  0.177464197   3.021             0.002533 ** 
## factor(nr)110    0.807474880  0.177693544   4.544  0.00000568592650625 ***
## factor(nr)120    0.258640329  0.179647221   1.440             0.150031    
## factor(nr)126    0.745943004  0.177029716   4.214  0.00002571031680378 ***
## factor(nr)150   -0.156887995  0.177422635  -0.884             0.376611    
## factor(nr)162    0.269898692  0.178348356   1.513             0.130281    
## factor(nr)166    0.153447372  0.180005130   0.852             0.394012    
## factor(nr)189    0.139467469  0.176695287   0.789             0.429980    
## factor(nr)193    0.926034315  0.176832856   5.237  0.00000017226408870 ***
## factor(nr)209    0.646499320  0.178360709   3.625             0.000293 ***
## factor(nr)212    0.831926297  0.178450785   4.662  0.00000324006369755 ***
## factor(nr)218    1.156359289  0.177241273   6.524  0.00000000007737489 ***
## factor(nr)243    0.429816623  0.177853983   2.417             0.015709 *  
## factor(nr)259    0.389758652  0.178575518   2.183             0.029126 *  
## factor(nr)260    0.785126601  0.178795538   4.391  0.00001157980245837 ***
## factor(nr)309    0.968154972  0.177853983   5.444  0.00000005553545725 ***
## factor(nr)351    0.273336140  0.178026658   1.535             0.124777    
## factor(nr)353    0.504806442  0.177422635   2.845             0.004462 ** 
## factor(nr)383    0.547434358  0.177422635   3.085             0.002047 ** 
## factor(nr)408    0.794346907  0.179300639   4.430  0.00000967760097277 ***
## factor(nr)424    1.102625472  0.178460577   6.179  0.00000000071516955 ***
## factor(nr)464    0.040455410  0.177448017   0.228             0.819671    
## factor(nr)483    0.478754519  0.177422635   2.698             0.006999 ** 
## factor(nr)556    1.037460214  0.176695287   5.871  0.00000000468968965 ***
## factor(nr)560    0.189020103  0.177422635   1.065             0.286778    
## factor(nr)569   -0.447279704  0.177436463  -2.521             0.011750 *  
## factor(nr)583    0.697284826  0.177597870   3.926  0.00008782854524825 ***
## factor(nr)647    0.335289267  0.178800897   1.875             0.060840 .  
## factor(nr)658    0.686211424  0.177494870   3.866             0.000112 ***
## factor(nr)684    1.109941778  0.178026658   6.235  0.00000000050217060 ***
## factor(nr)711   -0.095526673  0.178360709  -0.536             0.592279    
## factor(nr)729    0.795197591  0.177682086   4.475  0.00000785055780008 ***
## factor(nr)731    0.418585275  0.177710952   2.355             0.018552 *  
## factor(nr)732    0.452990052  0.177436463   2.553             0.010720 *  
## factor(nr)793    0.446950017  0.176695287   2.529             0.011463 *  
## factor(nr)797    0.790873892  0.180499317   4.382  0.00001209984421503 ***
## factor(nr)800    0.376859625  0.179041325   2.105             0.035368 *  
## factor(nr)813   -0.690305958  0.176880164  -3.903  0.00009678389005893 ***
## factor(nr)823   -0.436181014  0.179647221  -2.428             0.015229 *  
## factor(nr)827    0.488822204  0.177514088   2.754             0.005920 ** 
## factor(nr)847    0.487267857  0.176841258   2.755             0.005890 ** 
## factor(nr)851    0.712496962  0.177898767   4.005  0.00006318515370327 ***
## factor(nr)863    0.531087880  0.177597634   2.990             0.002804 ** 
## factor(nr)873   -0.110015442  0.180074843  -0.611             0.541274    
## factor(nr)891    0.711501659  0.178242133   3.992  0.00006682054259214 ***
## factor(nr)908   -0.388654777  0.179948633  -2.160             0.030850 *  
## factor(nr)910    0.645301239  0.176880164   3.648             0.000268 ***
## factor(nr)916    0.530484603  0.176880164   2.999             0.002725 ** 
## factor(nr)919    0.148992624  0.179008589   0.832             0.405280    
## factor(nr)922    0.994274427  0.177460355   5.603  0.00000002258862449 ***
## factor(nr)924   -0.018477522  0.178388488  -0.104             0.917508    
## factor(nr)925    0.736999799  0.177008292   4.164  0.00003201567387340 ***
## factor(nr)944    0.072147438  0.176880164   0.408             0.683378    
## factor(nr)945    0.105932495  0.176695287   0.600             0.548861    
## factor(nr)947    1.050488629  0.177485967   5.919  0.00000000353184442 ***
## factor(nr)955    0.851652842  0.178256342   4.778  0.00000184025849265 ***
## factor(nr)965    0.196276483  0.177710952   1.104             0.269459    
## factor(nr)996    0.000884869  0.179411454   0.005             0.996065    
## factor(nr)1007   0.843384248  0.177514088   4.751  0.00000209811634140 ***
## factor(nr)1054   0.084786435  0.180084066   0.471             0.637799    
## factor(nr)1064   0.698866484  0.178460577   3.916  0.00009157947626265 ***
## factor(nr)1081   0.198435845  0.179647221   1.105             0.269409    
## factor(nr)1085   0.147117336  0.177597634   0.828             0.407511    
## factor(nr)1091  -0.056457046  0.182197294  -0.310             0.756679    
## factor(nr)1094   0.513349531  0.178460577   2.877             0.004043 ** 
## factor(nr)1096  -0.061060621  0.178431812  -0.342             0.732214    
## factor(nr)1098   0.836196183  0.176711181   4.732  0.00000230418694979 ***
## factor(nr)1102   0.851737299  0.176880164   4.815  0.00000152680016790 ***
## factor(nr)1107   0.520814141  0.178228889   2.922             0.003497 ** 
## factor(nr)1142   0.660264953  0.176832856   3.734             0.000191 ***
## factor(nr)1156   0.335039597  0.177436463   1.888             0.059072 .  
## factor(nr)1180   0.597199535  0.177134961   3.371             0.000755 ***
## factor(nr)1190   0.101488445  0.177460355   0.572             0.567428    
## factor(nr)1204   0.722073546  0.183894901   3.927  0.00008769954278210 ***
## factor(nr)1249   0.247748457  0.178649798   1.387             0.165589    
## factor(nr)1272   0.529794403  0.177597870   2.983             0.002871 ** 
## factor(nr)1311   0.412088538  0.176882995   2.330             0.019873 *  
## factor(nr)1316   0.834676647  0.183939227   4.538  0.00000586063201342 ***
## factor(nr)1318   0.150690623  0.183274388   0.822             0.411007    
## factor(nr)1345   0.390956489  0.178228889   2.194             0.028327 *  
## factor(nr)1397   0.415804636  0.178256342   2.333             0.019720 *  
## factor(nr)1434  -0.071120050  0.180613736  -0.394             0.693774    
## factor(nr)1492   0.235212596  0.177139358   1.328             0.184311    
## factor(nr)1496  -0.183465861  0.177442108  -1.034             0.301226    
## factor(nr)1506   0.003849965  0.177436463   0.022             0.982690    
## factor(nr)1515   0.298685175  0.179667026   1.662             0.096508 .  
## factor(nr)1520   0.131536740  0.178497144   0.737             0.461221    
## factor(nr)1528   0.020365183  0.179636351   0.113             0.909744    
## factor(nr)1554   0.288800053  0.178208840   1.621             0.105193    
## factor(nr)1575   0.474131177  0.177646879   2.669             0.007641 ** 
## factor(nr)1576   0.500364788  0.178796081   2.799             0.005160 ** 
## factor(nr)1628   0.361666706  0.177436463   2.038             0.041590 *  
## factor(nr)1641   0.815895622  0.178228889   4.578  0.00000484935129699 ***
## factor(nr)1644   0.297762124  0.177445628   1.678             0.093420 .  
## factor(nr)1653   0.558207296  0.177126280   3.151             0.001637 ** 
## factor(nr)1654   0.526858404  0.178533069   2.951             0.003186 ** 
## factor(nr)1721   0.942968841  0.178369131   5.287  0.00000013161493545 ***
## factor(nr)1742   0.841893286  0.177516126   4.743  0.00000218708675145 ***
## factor(nr)1744   0.556138164  0.179187538   3.104             0.001925 ** 
## factor(nr)1763  -0.141632108  0.178369131  -0.794             0.427222    
## factor(nr)1777   0.094452015  0.176930154   0.534             0.593485    
## factor(nr)1843   1.232640263  0.177813744   6.932  0.00000000000484751 ***
## factor(nr)1891  -0.106061624  0.177422635  -0.598             0.550015    
## factor(nr)1895   0.560665762  0.178016260   3.150             0.001648 ** 
## factor(nr)1899   0.087044289  0.178026658   0.489             0.624913    
## factor(nr)1925   0.485869311  0.178649798   2.720             0.006564 ** 
## factor(nr)1930   0.251823939  0.178388488   1.412             0.158132    
## factor(nr)1961   0.586386082  0.178460577   3.286             0.001026 ** 
## factor(nr)1963   0.465174325  0.178971845   2.599             0.009382 ** 
## factor(nr)1979   0.668729340  0.178636134   3.744             0.000184 ***
## factor(nr)1988  -0.078571662  0.177422635  -0.443             0.657899    
## factor(nr)2000   0.569592281  0.179017864   3.182             0.001476 ** 
## factor(nr)2014   0.293583876  0.177422635   1.655             0.098065 .  
## factor(nr)2025   0.390881028  0.177625354   2.201             0.027825 *  
## factor(nr)2038   0.199322569  0.179647221   1.110             0.267275    
## factor(nr)2075   0.684598434  0.178497144   3.835             0.000127 ***
## factor(nr)2101   1.076958420  0.177567065   6.065  0.00000000144776929 ***
## factor(nr)2106   0.435953666  0.178636134   2.440             0.014714 *  
## factor(nr)2107   0.474573589  0.179954170   2.637             0.008394 ** 
## factor(nr)2108   0.791030432  0.178588541   4.429  0.00000971810620816 ***
## factor(nr)2147  -0.858824243  0.178360709  -4.815  0.00000152860671960 ***
## factor(nr)2157  -0.169024879  0.179158457  -0.943             0.345517    
## factor(nr)2163   0.930435730  0.179218662   5.192  0.00000021939084427 ***
## factor(nr)2173   0.127164458  0.177853983   0.715             0.474657    
## factor(nr)2180   0.426216143  0.179950150   2.369             0.017909 *  
## factor(nr)2183   0.405440101  0.177278814   2.287             0.022249 *  
## factor(nr)2216   0.570030294  0.177835691   3.205             0.001360 ** 
## factor(nr)2220   0.934521696  0.176955613   5.281  0.00000013560345240 ***
## factor(nr)2227   0.168561303  0.178221944   0.946             0.344314    
## factor(nr)2264  -0.112599622  0.177651252  -0.634             0.526234    
## factor(nr)2306  -0.084796253  0.182130971  -0.466             0.641544    
## factor(nr)2312   0.778480308  0.177139358   4.395  0.00001139386568146 ***
## factor(nr)2314   0.728935388  0.177980101   4.096  0.00004298483181683 ***
## factor(nr)2329   0.537709198  0.177659242   3.027             0.002490 ** 
## factor(nr)2335   0.271149550  0.177460355   1.528             0.126610    
## factor(nr)2341   0.470515748  0.178968879   2.629             0.008597 ** 
## factor(nr)2351   0.047465048  0.178360709   0.266             0.790163    
## factor(nr)2386  -0.136000687  0.179688152  -0.757             0.449174    
## factor(nr)2401   0.402166777  0.178409053   2.254             0.024241 *  
## factor(nr)2413   0.839238924  0.178800897   4.694  0.00000277761290536 ***
## factor(nr)2421  -0.047386120  0.177853983  -0.266             0.789920    
## factor(nr)2445   0.304807014  0.178208840   1.710             0.087275 .  
## factor(nr)2451   0.567633731  0.178460577   3.181             0.001481 ** 
## factor(nr)2494  -0.009727299  0.177871723  -0.055             0.956391    
## factor(nr)2508   0.744476820  0.177646879   4.191  0.00002843372770997 ***
## factor(nr)2535   0.325446862  0.178497144   1.823             0.068342 .  
## factor(nr)2540  -0.043239350  0.179816758  -0.240             0.809984    
## factor(nr)2685   0.997444661  0.176880164   5.639  0.00000001833907810 ***
## factor(nr)2711   0.030943823  0.177731134   0.174             0.861792    
## factor(nr)2718   0.993089862  0.179258111   5.540  0.00000003229887372 ***
## factor(nr)2721  -0.125860562  0.182384642  -0.690             0.490184    
## factor(nr)2733  -0.304031244  0.178021502  -1.708             0.087749 .  
## factor(nr)2741   0.176675737  0.178439657   0.990             0.322181    
## factor(nr)2745   0.474383971  0.177581116   2.671             0.007587 ** 
## factor(nr)2751   0.295704634  0.178605478   1.656             0.097879 .  
## factor(nr)2774   0.781128354  0.177659242   4.397  0.00001128763517194 ***
## factor(nr)2801   0.063212990  0.180101590   0.351             0.725619    
## factor(nr)2813   0.180218125  0.177875302   1.013             0.311043    
## factor(nr)2833   0.597243307  0.178460577   3.347             0.000826 ***
## factor(nr)2839   0.549728484  0.176880164   3.108             0.001898 ** 
## factor(nr)2842   0.773454190  0.176880164   4.373  0.00001259741323273 ***
## factor(nr)2866   0.674164080  0.182130971   3.702             0.000217 ***
## factor(nr)2868   0.876612615  0.177259050   4.945  0.00000079284887243 ***
## factor(nr)2874  -0.090856420  0.181288289  -0.501             0.616280    
## factor(nr)2916   0.328704659  0.177436463   1.853             0.064029 .  
## factor(nr)2951   0.038434950  0.181288289   0.212             0.832111    
## factor(nr)2980   0.249465656  0.177813744   1.403             0.160710    
## factor(nr)2994   0.421442089  0.178026658   2.367             0.017968 *  
## factor(nr)2997   0.519235064  0.177597634   2.924             0.003480 ** 
## factor(nr)3017   0.928007878  0.177542500   5.227  0.00000018159171981 ***
## factor(nr)3037   0.878873100  0.178995105   4.910  0.00000094887911645 ***
## factor(nr)3059   0.452878859  0.178500898   2.537             0.011216 *  
## factor(nr)3062   1.096562411  0.176882995   6.199  0.00000000062749051 ***
## factor(nr)3100   0.476321941  0.178575518   2.667             0.007678 ** 
## factor(nr)3102  -0.223509576  0.177436463  -1.260             0.207870    
## factor(nr)3127  -0.388318132  0.179678784  -2.161             0.030744 *  
## factor(nr)3136   0.616307136  0.178256342   3.457             0.000551 ***
## factor(nr)3137   0.839411883  0.176711181   4.750  0.00000210734108405 ***
## factor(nr)3138   0.512494480  0.177700127   2.884             0.003948 ** 
## factor(nr)3140   0.741115840  0.177448017   4.177  0.00003026449660731 ***
## factor(nr)3193   0.684600241  0.178533069   3.835             0.000128 ***
## factor(nr)3196   0.599758187  0.178037946   3.369             0.000763 ***
## factor(nr)3200   0.407638277  0.183591278   2.220             0.026453 *  
## factor(nr)3202   0.505521071  0.178800897   2.827             0.004719 ** 
## factor(nr)3207   0.312282858  0.178460577   1.750             0.080221 .  
## factor(nr)3208   0.768020510  0.178460016   4.304  0.00001722795243527 ***
## factor(nr)3210   0.725584028  0.180074843   4.029  0.00005702687168325 ***
## factor(nr)3215   0.170723848  0.177852507   0.960             0.337158    
## factor(nr)3219   0.237191593  0.178423023   1.329             0.183803    
## factor(nr)3226   0.544040666  0.176949946   3.075             0.002123 ** 
## factor(nr)3235   0.120080091  0.178208840   0.674             0.500469    
## factor(nr)3239  -0.322126752  0.178348356  -1.806             0.070972 .  
## factor(nr)3271   0.516642175  0.185613327   2.783             0.005405 ** 
## factor(nr)3275   0.577753019  0.177436463   3.256             0.001139 ** 
## factor(nr)3282   0.616545385  0.179658371   3.432             0.000606 ***
## factor(nr)3289   0.430899000  0.177218679   2.431             0.015084 *  
## factor(nr)3290   0.488360560  0.177581116   2.750             0.005986 ** 
## factor(nr)3307   1.204771716  0.177279512   6.796  0.00000000001244723 ***
## factor(nr)3333  -0.043338346  0.177460355  -0.244             0.807078    
## factor(nr)3353   0.039639363  0.179647221   0.221             0.825376    
## factor(nr)3380   0.500518476  0.177853983   2.814             0.004915 ** 
## factor(nr)3381   0.602467644  0.179950150   3.348             0.000822 ***
## factor(nr)3389   0.589941797  0.177710952   3.320             0.000910 ***
## factor(nr)3394   0.582545142  0.177109867   3.289             0.001014 ** 
## factor(nr)3401   0.393537060  0.178796081   2.201             0.027793 *  
## factor(nr)3414   0.662656583  0.177554921   3.732             0.000193 ***
## factor(nr)3420   0.266869300  0.176880164   1.509             0.131444    
## factor(nr)3440  -0.216122406  0.179042402  -1.207             0.227468    
## factor(nr)3461   0.712579391  0.178360709   3.995  0.00006587589157821 ***
## factor(nr)3468   0.195651969  0.176695287   1.107             0.268241    
## factor(nr)3482   0.959158042  0.176900087   5.422  0.00000006259298897 ***
## factor(nr)3495   0.299860711  0.178102736   1.684             0.092334 .  
## factor(nr)3503   0.809494177  0.178102736   4.545  0.00000566188324659 ***
## factor(nr)3525   1.039995708  0.176678566   5.886  0.00000000428936368 ***
## factor(nr)3526   0.434534383  0.178347567   2.436             0.014878 *  
## factor(nr)3538   0.719488750  0.178588541   4.029  0.00005717101337812 ***
## factor(nr)3563   0.569039808  0.178900791   3.181             0.001481 ** 
## factor(nr)3575   0.839261120  0.178228889   4.709  0.00000257958615242 ***
## factor(nr)3580   0.344088827  0.177460355   1.939             0.052580 .  
## factor(nr)3581   0.615749606  0.176938576   3.480             0.000507 ***
## factor(nr)3589   0.260108826  0.179218662   1.451             0.146765    
## factor(nr)3591   0.651773461  0.178104802   3.659             0.000256 ***
## factor(nr)3598   0.781896299  0.177581116   4.403  0.00001096833995382 ***
## factor(nr)3602   0.840722204  0.178242133   4.717  0.00000248265794897 ***
## factor(nr)3607  -0.414408805  0.179647221  -2.307             0.021120 *  
## factor(nr)3621   0.374686025  0.177852507   2.107             0.035207 *  
## factor(nr)3628   0.862442754  0.177659242   4.854  0.00000125560269454 ***
## factor(nr)3653   0.700712202  0.178026658   3.936  0.00008433555072906 ***
## factor(nr)3706   0.480855995  0.176900087   2.718             0.006593 ** 
## factor(nr)3707   0.514790738  0.177646879   2.898             0.003779 ** 
## factor(nr)3708   0.650471769  0.177852507   3.657             0.000258 ***
## factor(nr)3743   0.655934043  0.178649798   3.672             0.000244 ***
## factor(nr)3777   0.705238444  0.179218662   3.935  0.00008465911617169 ***
## factor(nr)3831   0.163409829  0.178533069   0.915             0.360097    
## factor(nr)3844   0.448952563  0.178360709   2.517             0.011873 *  
## factor(nr)3847   1.017108423  0.178800897   5.688  0.00000001378273916 ***
## factor(nr)3848   0.632970121  0.177514088   3.566             0.000367 ***
## factor(nr)3882   0.029110667  0.176864877   0.165             0.869273    
## factor(nr)3937   0.454460481  0.178788699   2.542             0.011065 *  
## factor(nr)4000   0.485780802  0.186089801   2.610             0.009077 ** 
## factor(nr)4004   0.823683594  0.177460355   4.642  0.00000357554728991 ***
## factor(nr)4025   0.169727281  0.181376786   0.936             0.349450    
## factor(nr)4032   0.208614381  0.178375241   1.170             0.242265    
## factor(nr)4046   0.281578719  0.182384945   1.544             0.122703    
## factor(nr)4088   1.276573063  0.176949946   7.214  0.00000000000065116 ***
## factor(nr)4091   1.182057378  0.179218662   6.596  0.00000000004819173 ***
## factor(nr)4122   0.711484329  0.178337844   3.990  0.00006745185847564 ***
## factor(nr)4127   0.112778115  0.179647221   0.628             0.530189    
## factor(nr)4128   0.143828014  0.178037946   0.808             0.419227    
## factor(nr)4159   0.600546064  0.178900791   3.357             0.000796 ***
## factor(nr)4204   0.188765469  0.178395116   1.058             0.290063    
## factor(nr)4229   0.297941428  0.177436463   1.679             0.093206 .  
## factor(nr)4258   0.698830377  0.178256342   3.920  0.00008997297614307 ***
## factor(nr)4261   0.775129312  0.188448794   4.113  0.00003984605272770 ***
## factor(nr)4264  -0.050085018  0.180468426  -0.278             0.781390    
## factor(nr)4278   0.507224289  0.178460577   2.842             0.004504 ** 
## factor(nr)4297   0.516243956  0.177835691   2.903             0.003718 ** 
## factor(nr)4302  -0.128878743  0.178388488  -0.722             0.470056    
## factor(nr)4321   0.517653797  0.179360601   2.886             0.003922 ** 
## factor(nr)4328   0.088597860  0.177436463   0.499             0.617582    
## factor(nr)4332  -0.387473822  0.177436463  -2.184             0.029043 *  
## factor(nr)4335   0.273034938  0.179411454   1.522             0.128133    
## factor(nr)4357   0.441546639  0.178149062   2.479             0.013236 *  
## factor(nr)4365  -0.043239797  0.178971845  -0.242             0.809102    
## factor(nr)4380   0.704849455  0.177513136   3.971  0.00007299159309138 ***
## factor(nr)4394   0.724581625  0.178369131   4.062  0.00004958147142455 ***
## factor(nr)4510   0.607664029  0.176883897   3.435             0.000598 ***
## factor(nr)4559   0.133493563  0.178228889   0.749             0.453903    
## factor(nr)4563   0.068681147  0.178536108   0.385             0.700488    
## factor(nr)4569   0.820947726  0.178395255   4.602  0.00000432435488758 ***
## factor(nr)4603   0.733025095  0.179187538   4.091  0.00004387544264577 ***
## factor(nr)4607   0.129711177  0.176932856   0.733             0.463537    
## factor(nr)4633   0.423274657  0.177581116   2.384             0.017195 *  
## factor(nr)4676   0.144291947  0.179647221   0.803             0.421912    
## factor(nr)4701   0.932963659  0.177597634   5.253  0.00000015764757641 ***
## factor(nr)4716   0.033034142  0.179008589   0.185             0.853600    
## factor(nr)4720   0.892440581  0.178831311   4.990  0.00000062954134101 ***
## factor(nr)4759   0.435421172  0.178783874   2.435             0.014919 *  
## factor(nr)4791   0.433371243  0.179218662   2.418             0.015648 *  
## factor(nr)4811   0.079381399  0.177442108   0.447             0.654637    
## factor(nr)4828   0.106789181  0.178431812   0.598             0.549550    
## factor(nr)4857  -0.215175608  0.176832856  -1.217             0.223744    
## factor(nr)4858   0.953795283  0.178007484   5.358  0.00000008905858269 ***
## factor(nr)4859   0.015255820  0.177436463   0.086             0.931488    
## factor(nr)4866   0.758241950  0.177837204   4.264  0.00002059706959212 ***
## factor(nr)4881   0.298099048  0.177659242   1.678             0.093444 .  
## factor(nr)4884   0.363703254  0.178068704   2.042             0.041172 *  
## factor(nr)4888   0.071592045  0.178347567   0.401             0.688134    
## factor(nr)4901   0.075192824  0.179950313   0.418             0.676078    
## factor(nr)4917  -0.091408919  0.177436463  -0.515             0.606468    
## factor(nr)4926   0.280940575  0.178347567   1.575             0.115284    
## factor(nr)4982   0.000743432  0.181481526   0.004             0.996732    
## factor(nr)5017   1.102672243  0.177029716   6.229  0.00000000052142503 ***
## factor(nr)5033   1.040642780  0.177710952   5.856  0.00000000514902700 ***
## factor(nr)5048   0.251617193  0.178533069   1.409             0.158811    
## factor(nr)5122   0.570296934  0.178460577   3.196             0.001407 ** 
## factor(nr)5141   0.687275437  0.178460577   3.851             0.000120 ***
## factor(nr)5147   0.884193230  0.178783874   4.946  0.00000079194940353 ***
## factor(nr)5158   0.636530951  0.178533069   3.565             0.000368 ***
## factor(nr)5221   0.417229301  0.177189518   2.355             0.018588 *  
## factor(nr)5223   0.407292288  0.177436463   2.295             0.021763 *  
## factor(nr)5227   0.415303996  0.177498464   2.340             0.019348 *  
## factor(nr)5248   0.126200915  0.176693557   0.714             0.475125    
## factor(nr)5252   0.101736208  0.180084066   0.565             0.572150    
## factor(nr)5263   0.372937202  0.177434574   2.102             0.035634 *  
## factor(nr)5274   0.731995739  0.178208840   4.108  0.00004083618648557 ***
## factor(nr)5335   0.232483111  0.178208840   1.305             0.192124    
## factor(nr)5345   0.215767656  0.177460355   1.216             0.224112    
## factor(nr)5359  -0.233031375  0.177436463  -1.313             0.189153    
## factor(nr)5368   0.089433563  0.178026658   0.502             0.615443    
## factor(nr)5377   0.366349336  0.177436463   2.065             0.039021 *  
## factor(nr)5390   0.227980785  0.177659242   1.283             0.199484    
## factor(nr)5419   0.123993005  0.178450785   0.695             0.487204    
## factor(nr)5435   0.029803351  0.178796081   0.167             0.867624    
## factor(nr)5437   0.240714250  0.178460577   1.349             0.177470    
## factor(nr)5497   0.170527665  0.178460577   0.956             0.339361    
## factor(nr)5525   0.147556999  0.183602511   0.804             0.421634    
## factor(nr)5529   0.449658636  0.180722163   2.488             0.012885 *  
## factor(nr)5531   0.688781542  0.177710952   3.876             0.000108 ***
## factor(nr)5579   0.244051266  0.179950150   1.356             0.175111    
## factor(nr)5588   0.436614060  0.182130971   2.397             0.016567 *  
## factor(nr)5599   0.510492876  0.177434574   2.877             0.004036 ** 
## factor(nr)5650   0.326982548  0.178587263   1.831             0.067188 .  
## factor(nr)5660   0.702877571  0.177853983   3.952  0.00007891224897346 ***
## factor(nr)5665   0.039775874  0.176745439   0.225             0.821956    
## factor(nr)5666   0.489146105  0.177239510   2.760             0.005811 ** 
## factor(nr)5698   0.525570260  0.178605478   2.943             0.003274 ** 
## factor(nr)5699   0.637619595  0.177646879   3.589             0.000336 ***
## factor(nr)5731   0.249007133  0.179405563   1.388             0.165232    
## factor(nr)5750   0.435154752  0.179531365   2.424             0.015404 *  
## factor(nr)5755   0.421507083  0.180719086   2.332             0.019732 *  
## factor(nr)5772   0.020206652  0.182384945   0.111             0.911788    
## factor(nr)5816   0.127582346  0.177853983   0.717             0.473206    
## factor(nr)5823   0.045651281  0.178348356   0.256             0.797990    
## factor(nr)5851   0.078714580  0.176880164   0.445             0.656333    
## factor(nr)5857   0.422121205  0.177436463   2.379             0.017409 *  
## factor(nr)5859   0.349698212  0.177813744   1.967             0.049296 *  
## factor(nr)6016   0.777735736  0.177436463   4.383  0.00001201231037422 ***
## factor(nr)6020  -0.397704823  0.179647221  -2.214             0.026901 *  
## factor(nr)6025  -0.472727244  0.179973259  -2.627             0.008658 ** 
## factor(nr)6056  -0.120535675  0.177436463  -0.679             0.496978    
## factor(nr)6094   0.309237195  0.177272617   1.744             0.081167 .  
## factor(nr)6186   0.464518202  0.178026658   2.609             0.009109 ** 
## factor(nr)6395   0.594085208  0.182146465   3.262             0.001118 ** 
## factor(nr)6430   0.184656804  0.178968879   1.032             0.302240    
## factor(nr)6446   0.547665652  0.179559970   3.050             0.002304 ** 
## factor(nr)6463   0.040249864  0.191704274   0.210             0.833712    
## factor(nr)6558   0.731514363  0.177378969   4.124  0.00003802820581976 ***
## factor(nr)6559   0.504217703  0.177593300   2.839             0.004547 ** 
## factor(nr)6561   0.880206506  0.177109867   4.970  0.00000069966222936 ***
## factor(nr)6574  -0.057204302  0.178272947  -0.321             0.748319    
## factor(nr)6648   0.756341269  0.177700127   4.256  0.00002128770778443 ***
## factor(nr)6813   0.193343066  0.177852507   1.087             0.277063    
## factor(nr)6824   0.258167898  0.178995105   1.442             0.149295    
## factor(nr)6888   0.095269200  0.179816758   0.530             0.596273    
## factor(nr)6942   0.504963022  0.179667026   2.811             0.004971 ** 
## factor(nr)6954   0.523508313  0.177875302   2.943             0.003269 ** 
## factor(nr)6955   0.615765202  0.178395116   3.452             0.000563 ***
## factor(nr)6964   0.307334018  0.183393654   1.676             0.093857 .  
## factor(nr)6987   1.380470824  0.177402445   7.782  0.00000000000000917 ***
## factor(nr)7025   0.746593390  0.178636134   4.179  0.00002988488544468 ***
## factor(nr)7043   0.475084072  0.178228889   2.666             0.007718 ** 
## factor(nr)7060   0.441402234  0.177852507   2.482             0.013113 *  
## factor(nr)7087  -0.206191356  0.178423023  -1.156             0.247904    
## factor(nr)7238   0.234980302  0.188918815   1.244             0.213644    
## factor(nr)7279   0.115672748  0.178256342   0.649             0.516434    
## factor(nr)7342   0.197634366  0.177436463   1.114             0.265422    
## factor(nr)7343   0.325744178  0.177350886   1.837             0.066329 .  
## factor(nr)7411  -0.057780603  0.177768640  -0.325             0.745174    
## factor(nr)7424   0.700757574  0.178805351   3.919  0.00009044229889721 ***
## factor(nr)7429   0.257683883  0.183304339   1.406             0.159874    
## factor(nr)7454   0.660807620  0.176882995   3.736             0.000190 ***
## factor(nr)7472   0.350518303  0.180532586   1.942             0.052262 .  
## factor(nr)7474   0.280419918  0.178286390   1.573             0.115834    
## factor(nr)7509   0.461926660  0.179966310   2.567             0.010304 *  
## factor(nr)7539   0.017793721  0.177956233   0.100             0.920358    
## factor(nr)7769   0.595234640  0.177659242   3.350             0.000815 ***
## factor(nr)7783   0.370733913  0.177436463   2.089             0.036739 *  
## factor(nr)7784   1.982005350  0.178360709  11.112 < 0.0000000000000002 ***
## factor(nr)7801  -0.030428822  0.176882995  -0.172             0.863425    
## factor(nr)7824   0.526393330  0.177581116   2.964             0.003053 ** 
## factor(nr)7874   1.037983115  0.176882995   5.868  0.00000000478238540 ***
## factor(nr)7887   0.483338407  0.177693544   2.720             0.006557 ** 
## factor(nr)7923   0.999667416  0.178102736   5.613  0.00000002132213640 ***
## factor(nr)7926   0.463191922  0.179008589   2.588             0.009703 ** 
## factor(nr)8021   0.754950787  0.176882995   4.268  0.00002019747932190 ***
## factor(nr)8087   0.381845098  0.177436463   2.152             0.031459 *  
## factor(nr)8089   0.138530699  0.177853983   0.779             0.436086    
## factor(nr)8090   1.171344607  0.176832856   6.624  0.00000000003986478 ***
## factor(nr)8096   0.455465869  0.188231047   2.420             0.015579 *  
## factor(nr)8106   0.347631919  0.183304339   1.896             0.057973 .  
## factor(nr)8107   0.396266431  0.197954177   2.002             0.045376 *  
## factor(nr)8142   0.183443672  0.178360709   1.028             0.303781    
## factor(nr)8168   0.544437228  0.179371568   3.035             0.002420 ** 
## factor(nr)8173   0.302429045  0.184390277   1.640             0.101055    
## factor(nr)8203   1.012056388  0.177659242   5.697  0.00000001314784094 ***
## factor(nr)8211   0.035529618  0.183471285   0.194             0.846459    
## factor(nr)8224   0.572050810  0.177442108   3.224             0.001275 ** 
## factor(nr)8272   0.905291021  0.181104859   4.999  0.00000060318874758 ***
## factor(nr)8300   0.462040115  0.177580893   2.602             0.009308 ** 
## factor(nr)8304   0.449850603  0.177693544   2.532             0.011394 *  
## factor(nr)8364   0.279574263  0.179158457   1.560             0.118728    
## factor(nr)8370   0.760757140  0.177956233   4.275  0.00001958599436521 ***
## factor(nr)8381   0.236524184  0.179636351   1.317             0.188024    
## factor(nr)8388   0.260935063  0.184390277   1.415             0.157114    
## factor(nr)8406   0.360675689  0.184375462   1.956             0.050515 .  
## factor(nr)8415   0.068241684  0.180241500   0.379             0.704997    
## factor(nr)8496  -0.129308808  0.177460355  -0.729             0.466253    
## factor(nr)8501  -0.269868469  0.179801016  -1.501             0.133457    
## factor(nr)8518   1.000949183  0.178409053   5.610  0.00000002162401599 ***
## factor(nr)8520  -0.427456023  0.177218679  -2.412             0.015911 *  
## factor(nr)8524  -0.074874494  0.179788584  -0.416             0.677098    
## factor(nr)8548   0.106607959  0.183313133   0.582             0.560896    
## factor(nr)8556   0.621686384  0.178796081   3.477             0.000513 ***
## factor(nr)8564   0.152717583  0.179177003   0.852             0.394086    
## factor(nr)8581  -0.330400835  0.177597634  -1.860             0.062908 .  
## factor(nr)8586   0.180134069  0.177436463   1.015             0.310074    
## factor(nr)8587  -0.430866373  0.178369131  -2.416             0.015757 *  
## factor(nr)8597   0.476345660  0.180479255   2.639             0.008341 ** 
## factor(nr)8656   0.131727836  0.177835691   0.741             0.458904    
## factor(nr)8722   0.486004498  0.177436463   2.739             0.006191 ** 
## factor(nr)8743   0.428256405  0.178460577   2.400             0.016455 *  
## factor(nr)8749   0.333638320  0.184390277   1.809             0.070466 .  
## factor(nr)8758   0.448530046  0.178460577   2.513             0.012001 *  
## factor(nr)8796   0.743173466  0.177691337   4.182  0.00002949806383597 ***
## factor(nr)8838   0.325776082  0.177514088   1.835             0.066552 .  
## factor(nr)8842   0.141540140  0.178796081   0.792             0.428627    
## factor(nr)8846   0.084779335  0.179218662   0.473             0.636205    
## factor(nr)8860  -0.049372636  0.177182791  -0.279             0.780526    
## factor(nr)8862   0.365005017  0.176880164   2.064             0.039126 *  
## factor(nr)8880   1.062216722  0.178104802   5.964  0.00000000268605821 ***
## factor(nr)8886   0.395114016  0.177514088   2.226             0.026085 *  
## factor(nr)8903  -0.243117993  0.178971845  -1.358             0.174413    
## factor(nr)8908  -0.047413068  0.178347567  -0.266             0.790372    
## factor(nr)8911  -0.186421659  0.177580893  -1.050             0.293884    
## factor(nr)8917   0.659429714  0.178533069   3.694             0.000224 ***
## factor(nr)8991   0.850095545  0.178448104   4.764  0.00000197055584538 ***
## factor(nr)8997   0.843972733  0.177496020   4.755  0.00000205927440878 ***
## factor(nr)9014   0.387101723  0.177498464   2.181             0.029254 *  
## factor(nr)9015   0.313460166  0.181760995   1.725             0.084686 .  
## factor(nr)9027   0.922053730  0.177700127   5.189  0.00000022269652681 ***
## factor(nr)9066  -0.100784617  0.178423023  -0.565             0.572200    
## factor(nr)9082   0.189849732  0.183602511   1.034             0.301190    
## factor(nr)9131   0.205027464  0.192104298   1.067             0.285917    
## factor(nr)9132   0.375499042  0.189371429   1.983             0.047454 *  
## factor(nr)9154   1.041548499  0.177456726   5.869  0.00000000475050255 ***
## factor(nr)9158  -0.073288443  0.178460577  -0.411             0.681338    
## factor(nr)9184   0.095859856  0.179177003   0.535             0.592681    
## factor(nr)9230   0.210726294  0.178369131   1.181             0.237516    
## factor(nr)9265   0.490509303  0.178348356   2.750             0.005982 ** 
## factor(nr)9367   0.208981797  0.178360709   1.172             0.241399    
## factor(nr)9390   0.592673252  0.177494870   3.339             0.000849 ***
## factor(nr)9391   0.309387050  0.178360709   1.735             0.082890 .  
## factor(nr)9418   1.059138778  0.177241273   5.976  0.00000000250199023 ***
## factor(nr)9424   0.724577041  0.178055470   4.069  0.00004809473486651 ***
## factor(nr)9447   0.388264595  0.176711181   2.197             0.028068 *  
## factor(nr)9449   0.245449028  0.177000041   1.387             0.165609    
## factor(nr)9453   0.669767202  0.177562702   3.772             0.000164 ***
## factor(nr)9468   0.454965122  0.177554921   2.562             0.010434 *  
## factor(nr)9502   0.651240458  0.178575518   3.647             0.000269 ***
## factor(nr)9505  -0.116664700  0.183274388  -0.637             0.524451    
## factor(nr)9603  -0.091792073  0.180277812  -0.509             0.610663    
## factor(nr)9643   0.416477438  0.177436463   2.347             0.018967 *  
## factor(nr)9667   1.080859471  0.177120480   6.102  0.00000000114959079 ***
## factor(nr)9683  -0.112357389  0.178021502  -0.631             0.527984    
## factor(nr)9694   0.824774522  0.177241273   4.653  0.00000337640947307 ***
## factor(nr)9710   0.128605951  0.177436463   0.725             0.468619    
## factor(nr)9718   0.954094173  0.176949946   5.392  0.00000007396610408 ***
## factor(nr)9725   0.340431542  0.176880164   1.925             0.054348 .  
## factor(nr)9744   0.617265507  0.176930154   3.489             0.000491 ***
## factor(nr)9752   1.313583497  0.177593300   7.397  0.00000000000017105 ***
## factor(nr)9776   0.882982625  0.179738812   4.913  0.00000093670564134 ***
## factor(nr)9786   0.606468547  0.177580893   3.415             0.000644 ***
## factor(nr)9791   0.305086547  0.177241273   1.721             0.085277 .  
## factor(nr)9794   0.385726662  0.178605478   2.160             0.030862 *  
## factor(nr)9810  -0.147651458  0.178779285  -0.826             0.408920    
## factor(nr)9846   0.738010585  0.178439657   4.136  0.00003611962488188 ***
## factor(nr)9859  -0.006000718  0.178037946  -0.034             0.973114    
## factor(nr)9868   0.028160387  0.178431812   0.158             0.874606    
## factor(nr)9876   0.675990658  0.177875302   3.800             0.000147 ***
## factor(nr)9883   0.692144071  0.177840768   3.892             0.000101 ***
## factor(nr)9889   0.928467132  0.177659242   5.226  0.00000018242172538 ***
## factor(nr)9901   0.537226170  0.177442108   3.028             0.002481 ** 
## factor(nr)9936   0.810239672  0.178094578   4.549  0.00000554556548868 ***
## factor(nr)9964   0.314493952  0.181288289   1.735             0.082862 .  
## factor(nr)10043 -0.328162412  0.177835691  -1.845             0.065070 .  
## factor(nr)10067  0.505345129  0.178971845   2.824             0.004773 ** 
## factor(nr)10091  0.454207807  0.178439657   2.545             0.010953 *  
## factor(nr)10120 -0.153633886  0.176949946  -0.868             0.385321    
## factor(nr)10121  0.326806045  0.178360709   1.832             0.066989 .  
## factor(nr)10167  0.413154971  0.178256342   2.318             0.020515 *  
## factor(nr)10209  0.066661830  0.179739816   0.371             0.710748    
## factor(nr)10230  0.520490359  0.180099349   2.890             0.003874 ** 
## factor(nr)10265  0.933562369  0.177411612   5.262  0.00000015026841140 ***
## factor(nr)10274  0.228822142  0.177652640   1.288             0.197814    
## factor(nr)10311  0.298238182  0.178633884   1.670             0.095091 .  
## factor(nr)10392  0.027950727  0.183274388   0.153             0.878795    
## factor(nr)10425  0.900655879  0.178238825   5.053  0.00000045520118566 ***
## factor(nr)10441  0.121631070  0.176832856   0.688             0.491601    
## factor(nr)10457  0.406032579  0.177448017   2.288             0.022182 *  
## factor(nr)10469  0.368034825  0.177442108   2.074             0.038136 *  
## factor(nr)10524 -0.044706777  0.178360709  -0.251             0.802095    
## factor(nr)10552 -0.211263692  0.181302100  -1.165             0.243988    
## factor(nr)10553  0.529427024  0.177875302   2.976             0.002935 ** 
## factor(nr)10570 -0.812325619  0.178796081  -4.543  0.00000570990771057 ***
## factor(nr)10593  0.000001437  0.182513878   0.000             0.999994    
## factor(nr)10666 -0.391747726  0.179158457  -2.187             0.028833 *  
## factor(nr)11275  0.067881990  0.177436463   0.383             0.702060    
## factor(nr)11328  0.261427243  0.177436463   1.473             0.140738    
## factor(nr)11750  0.403509515  0.179647221   2.246             0.024753 *  
## factor(nr)11821  0.738697614  0.176882995   4.176  0.00003030823401417 ***
## factor(nr)11857  0.849579994  0.178395255   4.762  0.00000198496805491 ***
## factor(nr)11887  0.314349715  0.177597634   1.770             0.076806 .  
## factor(nr)11890  0.386587367  0.189383209   2.041             0.041290 *  
## factor(nr)11892  0.998380757  0.178360709   5.598  0.00000002327875755 ***
## factor(nr)11924  0.335991230  0.177514088   1.893             0.058466 .  
## factor(nr)11925  0.420363050  0.180485450   2.329             0.019908 *  
## factor(nr)11957 -0.165463954  0.176934509  -0.935             0.349760    
## factor(nr)11973  0.426935660  0.177422635   2.406             0.016162 *  
## factor(nr)11990  0.952887512  0.177239510   5.376  0.00000008062107310 ***
## factor(nr)12012  0.236268485  0.177448017   1.331             0.183111    
## factor(nr)12013  0.587309394  0.179636351   3.269             0.001087 ** 
## factor(nr)12045  0.157674232  0.178122066   0.885             0.376103    
## factor(nr)12055  0.589737096  0.177850621   3.316             0.000922 ***
## factor(nr)12084  0.215176508  0.177460355   1.213             0.225384    
## factor(nr)12088  0.100616236  0.177436463   0.567             0.570710    
## factor(nr)12122 -0.021109333  0.184390277  -0.114             0.908862    
## factor(nr)12179  0.255752070  0.180074843   1.420             0.155616    
## factor(nr)12182  0.527788704  0.177422020   2.975             0.002951 ** 
## factor(nr)12220  0.647552536  0.178605478   3.626             0.000292 ***
## factor(nr)12221 -0.436258650  0.178971845  -2.438             0.014831 *  
## factor(nr)12245 -0.109520161  0.176695287  -0.620             0.535410    
## factor(nr)12276  0.503619936  0.177436463   2.838             0.004559 ** 
## factor(nr)12385  0.584367279  0.177460355   3.293             0.001001 ** 
## factor(nr)12410 -0.048134071  0.178783874  -0.269             0.787767    
## factor(nr)12420  0.752509621  0.178451848   4.217  0.00002534800079511 ***
## factor(nr)12433  0.322315515  0.176928849   1.822             0.068575 .  
## factor(nr)12451  0.374754115  0.177074150   2.116             0.034378 *  
## factor(nr)12477  0.844394666  0.178800897   4.723  0.00000241328423235 ***
## factor(nr)12500  0.064923448  0.178026658   0.365             0.715368    
## factor(nr)12534  0.937852346  0.178971845   5.240  0.00000016910123015 ***
## factor(nr)12548  0.160041858  0.181812237   0.880             0.378775    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3534 on 3799 degrees of freedom
## Multiple R-squared:  0.6164, Adjusted R-squared:  0.5598 
## F-statistic:  10.9 on 560 and 3799 DF,  p-value: < 0.00000000000000022

We see the slope estimates are identical to that produced above with the only difference being we now have individual indicator variables. We usually don’t care about them and this approach is more computationally taxing so we rarely opt for this way of the fixed effects model relative to the mean differencing approach.

Now, let’s estimate the fixed effects model manually by taking the mean differences ourselves and then using the lm() function. After all, that is exactly what plm() is doing.

Like the first differences approach, you will see we don’t take the mean difference of the education.

# Compute mean differences for all relevant columns
dt[, `:=`(
  mean_diff_lwage = lwage - mean(lwage),
  mean_diff_married = married - mean(married),
  mean_diff_union = union - mean(union),
  mean_diff_d81 = d81 - mean(d81),
  mean_diff_d82 = d82 - mean(d82),
  mean_diff_d83 = d83 - mean(d83),
  mean_diff_d84 = d84 - mean(d84),
  mean_diff_d85 = d85 - mean(d85),
  mean_diff_d86 = d86 - mean(d86),
  mean_diff_d87 = d87 - mean(d87)
), by = nr]

# Run fixed effects regression
reg <- lm(mean_diff_lwage ~ mean_diff_married + mean_diff_union + mean_diff_d81 + 
          mean_diff_d82 + mean_diff_d83 + mean_diff_d84 + mean_diff_d85 + 
          mean_diff_d86 + mean_diff_d87 + 
          I(educ * mean_diff_d81) + 
          I(educ * mean_diff_d82) + 
          I(educ * mean_diff_d83) + 
          I(educ * mean_diff_d84) + 
          I(educ * mean_diff_d85) + 
          I(educ * mean_diff_d86) + 
          I(educ * mean_diff_d87),
          data = dt)

# Summary of regression
reg
## 
## Call:
## lm(formula = mean_diff_lwage ~ mean_diff_married + mean_diff_union + 
##     mean_diff_d81 + mean_diff_d82 + mean_diff_d83 + mean_diff_d84 + 
##     mean_diff_d85 + mean_diff_d86 + mean_diff_d87 + I(educ * 
##     mean_diff_d81) + I(educ * mean_diff_d82) + I(educ * mean_diff_d83) + 
##     I(educ * mean_diff_d84) + I(educ * mean_diff_d85) + I(educ * 
##     mean_diff_d86) + I(educ * mean_diff_d87), data = dt)
## 
## Coefficients:
##             (Intercept)        mean_diff_married          mean_diff_union  
## -0.00000000000000006392   0.05482047658611996499   0.08297847246951141342  
##           mean_diff_d81            mean_diff_d82            mean_diff_d83  
## -0.02241583519593714352  -0.00576106146545012163   0.01042971848065066091  
##           mean_diff_d84            mean_diff_d85            mean_diff_d86  
##  0.08437432048976944987   0.04972528912171240989   0.06560642336601145430  
##           mean_diff_d87  I(educ * mean_diff_d81)  I(educ * mean_diff_d82)  
##  0.09044483880832528133   0.01158542421396283745   0.01479047532172163976  
## I(educ * mean_diff_d83)  I(educ * mean_diff_d84)  I(educ * mean_diff_d85)  
##  0.01711820223417566747   0.01658392868128379452   0.02370854304929120859  
## I(educ * mean_diff_d86)  I(educ * mean_diff_d87)  
##  0.02741229248268452906   0.03043317761922559664

You can see we get identical results to that produced when using plm().

Sampling Distribution of FE Estimator

Let’s construct a panel data set where there is unobserved individual specific heterogeneity that is fixed across time and also correlated with our covariate. We know OLS will be biased unless we correct for this issue by using some panel data estimator. The true model will be \(y_{it} = \beta_0 + \beta_1 x_{it} + a_i + u_{it} = 1 + 2x_{i1} + a_i + u_i\). We will estimate this model accounting for the fixed effect, but we will also estimate the model \(y_{it} = \beta_0 + \beta_1 x_{it} + u_{it}\) not accounting for the fixed effect. We will see that the former case produces an unbiased estimator of \(\widehat{\beta}_1\) while the latter case does not. Let’s verify this by generating \(s = 1000\) samples each with \(n\_individuals = 200\) individuals and \(n\_time\_periods = 10\) time periods, estimate both models for each sample, and plot the sampling distribution of \(\widehat{\beta}_1\) for each model. If we are correct, then only the sampling distribution of the estimator \(\widehat{\beta}_1\) in the model including fixed effects will be centered around \(\beta_1 = 2\).

# Number of samples
s <- 1000

# Number of individuals
n_individuals <- 100

# Number of time periods
n_time_periods <- 10

# Total number of observations per sample
n <- n_individuals * n_time_periods

# Create a data table to store the estimates of beta_hat_1
dt_estimates <- data.table(beta_hat_1_fixed = rep(NA, s),
                           beta_hat_1_no_fixed = rep(NA, s))

# Intercept term when y is the outcome
beta_0 <- 1

# Coefficient on x_1 when y is the outcome
beta_1 <- 2

# Loop over each sample
for (i in 1:s) {
  
  # Individual-specific fixed effects a_i
  a <- rnorm(n_individuals, mean = 0, sd = 1)
  
  # Error term when y is the outcome
  u <- rnorm(n, mean = 0, sd = 2)
  
  # Create a data table with individual and time identifiers
  dt <- data.table(id = rep(1:n_individuals, each = n_time_periods),
                   t = rep(1:n_time_periods, n_individuals))
  
  # Add individual fixed effect a_i to the data table
  dt[, a := rep(a, each = n_time_periods)]
  
  # x_1 as a function of a_i (fixed effect) and an error term
  dt[, x_1 := a + rnorm(.N, mean = 0, sd = 1)]
  
  # Outcome y as a function of x_1, a_i, and error term u
  dt[, y := beta_0 + beta_1 * x_1 + a + u]
  
  # Run regression of y on x_1 with individual fixed effects
  reg_fixed <- lm(y ~ x_1 + factor(id), data = dt)
  
  # Run regression of y on x_1 without fixed effects (so this is just pooled OLS)
  reg_no_fixed <- lm(y ~ x_1, data = dt)
  
  # Place the current slope estimate for x_1 in dt_estimates
  dt_estimates$beta_hat_1_fixed[i] <- reg_fixed$coefficients["x_1"]
  dt_estimates$beta_hat_1_no_fixed[i] <- reg_no_fixed$coefficients["x_1"]
}
# Plot the density of the slope estimate on x_1 when using fixed effects
plot_beta_hat_1_fixed <- ggplot(dt_estimates, aes(x = beta_hat_1_fixed)) +
  geom_density(color = color_2, fill = color_2, alpha = 0.7) +
  geom_vline(xintercept = beta_1, color = color_1, linetype = "dashed", size = 1) +
  labs(title = expression(paste("With Fixed Effects"))) +
  theme(
    panel.background = element_rect(fill = "black", color = "black"),
    plot.background = element_rect(fill = "black", color = "black"),
    panel.grid.major = element_line(color = "gray"),
    panel.grid.minor = element_line(color = "gray"),
    axis.text = element_text(color = color_1, size = 8),
    axis.title = element_text(color = color_2, size = 10),
    plot.title = element_text(hjust = 0.5, color = color_2, size = 15, face = "bold")
  ) +
  xlab(expression(hat(beta)[1])) +
  xlim(range(dt_estimates$beta_hat_1_fixed, dt_estimates$beta_hat_1_no_fixed)) +
  ylab("Density")

# Plot the density of the slope estimate on x_1 when not using fixed effects
plot_beta_hat_1_no_fixed <- ggplot(dt_estimates, aes(x = beta_hat_1_no_fixed)) +
  geom_density(color = color_2, fill = color_2, alpha = 0.7) +
  geom_vline(xintercept = beta_1, color = color_1, linetype = "dashed", size = 1) +
  labs(title = expression(paste("Without Fixed Effects"))) +
  theme(
    panel.background = element_rect(fill = "black", color = "black"),
    plot.background = element_rect(fill = "black", color = "black"),
    panel.grid.major = element_line(color = "gray"),
    panel.grid.minor = element_line(color = "gray"),
    axis.text = element_text(color = color_1, size = 8),
    axis.title = element_text(color = color_2, size = 10),
    plot.title = element_text(hjust = 0.5, color = color_2, size = 15, face = "bold")
  ) +
  xlab(expression(hat(beta)[1])) +
  xlim(range(dt_estimates$beta_hat_1_fixed, dt_estimates$beta_hat_1_no_fixed)) +
  ylab("Density")

# Arrange plots in a single figure
grid.arrange(plot_beta_hat_1_fixed, plot_beta_hat_1_no_fixed, nrow = 1)

As we can see, only the model that included individual specific fixed effects is unbiased confirming our hypothesis above.

Difference-in-Differences (DiD)

Let’s load in the kielmc data set from the wooldridge package. This data set was used in the Kiel and McClain (1995) paper to examine the effect a new garbage incinerator had on housing values in North Andover, Massachusetts. This data contains the prices of sold houses in 1978 when rumors started swirling that a new incinerator would be built and the prices of sold houses in 1981 when the new incinerator actually underwent construction.

# Load in kielmc data set from wooldridge as a data table
dt <- data.table(wooldridge::kielmc)

# Filter out unwanted data
dt <- dt[, .(lrprice, y81, nearinc)]

# Show the first few rows of the data
head(dt)
# Show the last few rows of the data
tail(dt)
# Get summary statistics
summary(dt)
##     lrprice           y81            nearinc      
##  Min.   :10.17   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:10.99   1st Qu.:0.0000   1st Qu.:0.0000  
##  Median :11.31   Median :0.0000   Median :0.0000  
##  Mean   :11.26   Mean   :0.4424   Mean   :0.2991  
##  3rd Qu.:11.52   3rd Qu.:1.0000   3rd Qu.:1.0000  
##  Max.   :12.61   Max.   :1.0000   Max.   :1.0000
# Get the number of observations
paste0("There are ", nrow(dt), " observations in this data set.")
## [1] "There are 321 observations in this data set."
# Show many NA values there are
paste0("There are ", sum(is.na(dt)), " NAs in this data set.")
## [1] "There are 0 NAs in this data set."

The outcome variable lrprice represents the log of the sale price of the home, y81 corresponds to an indicator variable that equals one if the house was sold in 1981 and zero if the house was sold in 1978, and nearinc corresponds to an indicator variable equaling one if the house sold was near an incinerator and zero otherwise. Essentially, y81 is the “post” variable as 1981 is when construction of the incinerator started while nearinc is the “treatment” variable as a home is “treated” if they are located near an incinerator.

Moreover, we can see that \(44.24\%\) of the sold homes in our sample are from 1981 indicating our sample is fairly balanced in terms of the pre- and post-periods.

Now, let us estimate the DiD model. This comes out to estimating the model of \[\begin{align} \ln (rprice) = \beta_0 + \beta_1 y81 + \beta_2 nearinc + \beta_3 y81 \times nearinc + u. \end{align}\]

# Estimate model
reg <- lm(lrprice ~ y81 + nearinc + I(y81 * nearinc), 
          data = dt)

# Summary of regression
summary(reg)
## 
## Call:
## lm(formula = lrprice ~ y81 + nearinc + I(y81 * nearinc), data = dt)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.11957 -0.20328  0.02226  0.18909  1.66604 
## 
## Coefficients:
##                  Estimate Std. Error t value             Pr(>|t|)    
## (Intercept)      11.28542    0.03051 369.839 < 0.0000000000000002 ***
## y81               0.19309    0.04532   4.261        0.00002691218 ***
## nearinc          -0.33992    0.05456  -6.231        0.00000000148 ***
## I(y81 * nearinc) -0.06265    0.08344  -0.751                0.453    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3384 on 317 degrees of freedom
## Multiple R-squared:  0.246,  Adjusted R-squared:  0.2388 
## F-statistic: 34.47 on 3 and 317 DF,  p-value: < 0.00000000000000022

The DiD estimate is given by \(\widehat{\beta}_3 = -0.063\). This means that the selling price of a treated home (home near an incinerator) in the treatment period (home sold in 1981) is estimated to be \(6.3\%\) lower than had that home not been treated, assuming all else is held constant.

We can actually obtain this estimate manually. The true DiD parameter \(\beta_3\) is given by \[\begin{align} \beta_3 &= (\mathbb{E} \left[ \ln(price) \mid treated, post \right] - \mathbb{E} \left[ \ln(price) \mid treated, pre \right]) \\ &- (\mathbb{E} \left[ \ln(price) \mid control, post \right] - \mathbb{E} \left[ \ln(price) \mid control, pre \right]) \end{align}\]

So, the DiD estimate is the just difference of a difference of averages. Namely, \[\begin{align} \widehat{\beta}_3 &= [\overline{\ln(price_{treated, post})} - \overline{\ln(price_{treated, pre})}] \\ &- [\overline{\ln(price_{control, post})} - \overline{\ln(price_{control, pre})}]. \end{align}\]

Let’s obtain these averages and compute \(\widehat{\beta}_3\) manually.

# Average log price for homes near incinerator in 1981
mean_price_nearinc_1981 <- dt[y81 == 1 & nearinc == 1, mean(lrprice)]

# Average log price for homes near incinerator in 1978
mean_price_nearinc_1978 <- dt[y81 == 0 & nearinc == 1, mean(lrprice)]

# Average log price for homes not near incinerator in 1981
mean_price_notnearinc_1981 <- dt[y81 == 1 & nearinc == 0, mean(lrprice)]

# Average log price for homes not near incinerator in 1978
mean_price_notnearinc_1978 <- dt[y81 == 0 & nearinc == 0, mean(lrprice)]

# DiD estimate 
DiD <- (mean_price_nearinc_1981 - mean_price_nearinc_1978) - (mean_price_notnearinc_1981 - mean_price_notnearinc_1978)

# Show DiD estimate
DiD
## [1] -0.06264893

The value is identical to that given by the lm() function confirming the equations above.