Model use data: EmpLuk and psi Method: Tool Variables, GMM and FGLS. Code copy from Econometric use R, author Nguyen Chi Dung. This paper has many errors.

library(tidyverse)
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag():    dplyr, stats
library(plm)
## Loading required package: Formula
## 
## Attaching package: 'plm'
## The following objects are masked from 'package:dplyr':
## 
##     between, lag, lead
data("EmplUK")
head(EmplUK)
##   firm year sector   emp    wage capital   output
## 1    1 1977      7 5.041 13.1516  0.5894  95.7072
## 2    1 1978      7 5.600 12.3018  0.6318  97.3569
## 3    1 1979      7 5.015 12.8395  0.6771  99.6083
## 4    1 1980      7 4.715 13.8039  0.6171 100.5501
## 5    1 1981      7 4.093 14.2897  0.5076  99.5581
## 6    1 1982      7 3.166 14.8681  0.4229  98.6151
trang = table(EmplUK$firm)
View(trang)

Can see unbalance data, every firms were studied with other numbers of year, maximum is 9, so set ylim = 10 in barplot of firms.

Visualization:

barplot(trang, ylim = c(0, 10)) 

mydata = data.frame(trang)
table(mydata$Freq) 
## 
##   7   8   9 
## 103  23  14
fyear = table(EmplUK$year)
library(fBasics)
## Loading required package: timeDate
## Loading required package: timeSeries
## 
## Rmetrics Package fBasics
## Analysing Markets and calculating Basic Statistics
## Copyright (C) 2005-2014 Rmetrics Association Zurich
## Educational Software for Financial Engineering and Computational Science
## Rmetrics is free software and comes with ABSOLUTELY NO WARRANTY.
## https://www.rmetrics.org --- Mail to: info@rmetrics.org
barplot(fyear, col = qualiPalette(12, "Set3"))

  1. Tool_var: lag(wage, 1) is tool variable of wage:
ivPanel = EmplUK %>%  plm(emp ~ wage + capital|lag(wage, 1) + capital,
              data = .,
              inst.method = "bvk")
summary(ivPanel)
## Oneway (individual) effect Within Model
## Instrumental variable estimation
##    (Balestra-Varadharajan-Krishnakumar's transformation)
## 
## Call:
## plm(formula = emp ~ wage + capital | lag(wage, 1) + capital, 
##     data = ., inst.method = "bvk")
## 
## Unbalanced Panel: n=140, T=6-8, N=891
## 
## Residuals :
##        Min.     1st Qu.      Median     3rd Qu.        Max. 
## -15.3000607  -0.3058983   0.0015485   0.3278123  19.6057783 
## 
## Coefficients :
##          Estimate Std. Error t-value  Pr(>|t|)    
## wage    -0.228807   0.078561 -2.9125  0.003692 ** 
## capital  1.026579   0.065893 15.5794 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    3832
## Residual Sum of Squares: 2729.2
## R-Squared:      0.28796
## Adj. R-Squared: 0.15392
## F-statistic: 151.322 on 2 and 749 DF, p-value: < 2.22e-16

ms is tool variable of wks:

setwd("D:/TKT/RImportData")
library(foreign)
trang = read.dta("psi.dta")
table(trang$t)
## 
##   1   2   3   4   5   6   7 
## 595 595 595 595 595 595 595
#Thiet lap du lieu mang
trang = plm.data(trang, index = c("id", "t"))
ivPanel = trang %>% plm(lwage ~ exp + exp2 + wks|exp + exp2 + ms,
              data = .,
              model = "within",
              inst.method = "bvk") 
summary(ivPanel)
## Oneway (individual) effect Within Model
## Instrumental variable estimation
##    (Balestra-Varadharajan-Krishnakumar's transformation)
## 
## Call:
## plm(formula = lwage ~ exp + exp2 + wks | exp + exp2 + ms, data = ., 
##     model = "within", inst.method = "bvk")
## 
## Balanced Panel: n=595, T=7, N=4165
## 
## Residuals :
##      Min.   1st Qu.    Median   3rd Qu.      Max. 
## -4.114922 -0.119156  0.039161  0.198531  2.160009 
## 
## Coefficients :
##        Estimate Std. Error t-value Pr(>|t|)  
## exp   0.1408101  0.0547014  2.5742  0.01009 *
## exp2 -0.0011207  0.0014052 -0.7975  0.42520  
## wks  -0.1149742  0.2316926 -0.4962  0.61976  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    240.65
## Residual Sum of Squares: 946.63
## R-Squared:      0.094827
## Adj. R-Squared: -0.056669
## F-statistic: -886.733 on 3 and 3567 DF, p-value: 1
  1. Hausman - Taylor Estimation Method
formul = lwage ~ occ + south + smsa + ind + 
  exp + I(exp^2) + wks + ms + union + blk + fem +
  ed|exp + I(exp^2) + wks + ms + union + ed
hausmantaylor = trang %>% plm(formul, data = ., model = "ht") 
summary(hausmantaylor)
## Oneway (individual) effect Hausman-Taylor Model
## Call:
## pht(formula = formul, data = .)
## 
## T.V. exo  : exp, I(exp^2), wks, ms, union
## T.V. endo : occ, south, smsa, ind
## T.I. exo  : ed
## T.I. endo : blk, fem
## 
## Balanced Panel: n=595, T=7, N=4165
## 
## Effects:
##                    var  std.dev share
## idiosyncratic  0.02304  0.15180     0
## individual    58.38642  7.64110     1
## theta:  0.9925  
## 
## Residuals :
##       Min.    1st Qu.     Median    3rd Qu.       Max. 
## -1.8254038 -0.0643299 -0.0021546  0.0669370  1.9446714 
## 
## Coefficients :
##                Estimate  Std. Error t-value  Pr(>|t|)    
## (Intercept)  8.0007e+00  3.4251e+00  2.3359   0.01950 *  
## occ         -2.1452e-02  1.3643e-02 -1.5724   0.11587    
## south       -2.1349e-03  3.3949e-02 -0.0629   0.94986    
## smsa        -4.2543e-02  1.9230e-02 -2.2123   0.02694 *  
## ind          1.9173e-02  1.5289e-02  1.2541   0.20982    
## exp          1.1315e-01  2.4454e-03 46.2703 < 2.2e-16 ***
## I(exp^2)    -4.1901e-04  5.4028e-05 -7.7553 8.812e-15 ***
## wks          8.3480e-04  5.9354e-04  1.4065   0.15958    
## ms          -2.9846e-02  1.8790e-02 -1.5884   0.11219    
## union        3.3223e-02  1.4769e-02  2.2495   0.02448 *  
## blk         -2.7984e+01  1.6539e+01 -1.6920   0.09064 .  
## fem          6.7401e+00  4.2331e+00  1.5922   0.11133    
## ed          -1.6246e-01  2.1432e-01 -0.7580   0.44843    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    886.9
## Residual Sum of Squares: 93.974
## F-statistic: 2919.47 on 12 and 4152 DF, p-value: < 2.22e-16
  1. GMM method
gmmPanel = EmplUK %>%  pgmm(log(emp) ~ lag(log(emp), 1) + lag(log(emp), 2)
                           + log(wage)+ lag(log(wage), 1) 
                           + log(capital) 
                           + log(output)
                           + lag(log(output), 1)|lag(log(emp), c(2:99)),
                data = .,
                effect = "twoways",
                model = "twosteps") 
summary(gmmPanel)
## Twoways effects Two steps model
## 
## Call:
## pgmm(formula = log(emp) ~ lag(log(emp), 1) + lag(log(emp), 2) + 
##     log(wage) + lag(log(wage), 1) + log(capital) + log(output) + 
##     lag(log(output), 1) | lag(log(emp), c(2:99)), data = ., effect = "twoways", 
##     model = "twosteps")
## 
## Unbalanced Panel: n=140, T=7-9, N=1031
## 
## Number of Observations Used:  611 
## 
## Residuals
##       Min.    1st Qu.     Median       Mean    3rd Qu.       Max. 
## -0.6190677 -0.0255683  0.0000000 -0.0001339  0.0332013  0.6410272 
## 
## Coefficients
##                      Estimate Std. Error z-value  Pr(>|z|)    
## lag(log(emp), 1)     0.474151   0.185398  2.5575 0.0105437 *  
## lag(log(emp), 2)    -0.052967   0.051749 -1.0235 0.3060506    
## log(wage)           -0.513205   0.145565 -3.5256 0.0004225 ***
## lag(log(wage), 1)    0.224640   0.141950  1.5825 0.1135279    
## log(capital)         0.292723   0.062627  4.6741 2.953e-06 ***
## log(output)          0.609775   0.156263  3.9022 9.530e-05 ***
## lag(log(output), 1) -0.446373   0.217302 -2.0542 0.0399605 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Sargan Test: chisq(25) = 30.11247 (p.value=0.22011)
## Autocorrelation test (1): normal = -1.53845 (p.value=0.12394)
## Autocorrelation test (2): normal = -0.2796829 (p.value=0.77972)
## Wald test for coefficients: chisq(7) = 142.0353 (p.value=< 2.22e-16)
## Wald test for time dummies: chisq(6) = 16.97046 (p.value=0.0093924)
  1. GFLS Estimation Method
fgls = EmplUK %>% pggls(log(emp) ~ log(wage) + log(capital), data = .,
                        model = "pooling")
summary(fgls)
##  NA
## 
## Call:
## pggls(formula = log(emp) ~ log(wage) + log(capital), data = ., 
##     model = "pooling")
## 
## Unbalanced Panel: n=140, T=7-9, N=1031
## 
## Residuals
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## -1.80696 -0.36552  0.06181  0.03230  0.44279  1.58719 
## 
## Coefficients
##               Estimate Std. Error z-value  Pr(>|z|)    
## (Intercept)   2.023480   0.158468 12.7690 < 2.2e-16 ***
## log(wage)    -0.232329   0.048001 -4.8401 1.298e-06 ***
## log(capital)  0.610484   0.017434 35.0174 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Total Sum of Squares: 1853.6
## Residual Sum of Squares: 402.55
## Multiple R-squared: 0.78283

Thank you!!