setwd("D:/TKT/RImportData")
library(plm)
## Loading required package: Formula
library(foreign)
library(tidyverse)
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## between(): dplyr, plm
## filter():  dplyr, stats
## lag():     dplyr, plm, stats
## lead():    dplyr, plm
library(car)
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following object is masked from 'package:purrr':
## 
##     some
trang = read.dta("Panel1.dta")
View(trang)
trang %>% scatterplot(C ~ t|i, reg.line = FALSE, data = .) #Hoi quy C theo t, phan lo???i theo i

cor(trang) #He so tuong quan cho cac bien so de xem xet hien tuong da cong tuyen
##              i         t          C          Q         PF         LF
## i   1.00000000 0.0000000 -0.7086242 -0.8679359 0.01329393 -0.3399570
## t   0.00000000 1.0000000  0.5000271  0.2711141 0.93118760  0.6001491
## C  -0.70862418 0.5000271  1.0000000  0.9263269 0.47904374  0.4143377
## Q  -0.86793588 0.2711141  0.9263269  1.0000000 0.22761248  0.4248100
## PF  0.01329393 0.9311876  0.4790437  0.2276125 1.00000000  0.4867001
## LF -0.33995702 0.6001491  0.4143377  0.4248100 0.48670008  1.0000000
#Mo hinh Pooled OLS
#Gia su he so goc va he so chan la khong doi theo thoi gian va khong gian
#Gia su cac bien doc lap ngoai sinh chat 
#(khong phu thuoc vao gia tri qua khu, hien tai va tuong lai cua sai so ngau nhien)
#Khong tinh den cac khac biet dac trung theo khong gian va thoi gian 
#ma gop cac khac biet dec trung vao sai so ngau nhien
#vi vay sai so ngau nhien co the tuong quan voi bien doc lap

trang %>% plm(data = ., C ~ Q + PF + LF, model = "pooling") %>% summary()
## Pooling Model
## 
## Call:
## plm(formula = C ~ Q + PF + LF, data = ., model = "pooling")
## 
## Balanced Panel: n=6, T=15, N=90
## 
## Residuals :
##    Min. 1st Qu.  Median 3rd Qu.    Max. 
## -520654 -250270   37333  208690  849700 
## 
## Coefficients :
##                Estimate  Std. Error t-value  Pr(>|t|)    
## (Intercept)  1.1586e+06  3.6059e+05  3.2129   0.00185 ** 
## Q            2.0261e+06  6.1807e+04 32.7813 < 2.2e-16 ***
## PF           1.2253e+00  1.0372e-01 11.8138 < 2.2e-16 ***
## LF          -3.0658e+06  6.9633e+05 -4.4027 3.058e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    1.2647e+14
## Residual Sum of Squares: 6.8177e+12
## R-Squared:      0.94609
## Adj. R-Squared: 0.94421
## F-statistic: 503.118 on 3 and 86 DF, p-value: < 2.22e-16
#Mo hinh tac dong bien gia va kiem dinh gop
#Least - Squares Dummy Variable Model)
#He so chan khong thay doi theo thoi gian nhung thay doi theo khong gian
#He so goc khong thay doi theo thoi gian va khong gian
#Mo hinh tac dong mot chieu, xet den tac dong dac trung cho tung hang

lsdv = trang %>% lm(formula = C ~ Q + PF + LF + factor(i), data = .)
summary(lsdv) 
## 
## Call:
## lm(formula = C ~ Q + PF + LF + factor(i), data = .)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -551783 -159259    1796  137226  499296 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1.312e+05  3.508e+05  -0.374    0.709    
## Q            3.319e+06  1.714e+05  19.369  < 2e-16 ***
## PF           7.731e-01  9.732e-02   7.944 9.70e-12 ***
## LF          -3.797e+06  6.138e+05  -6.187 2.37e-08 ***
## factor(i)2   6.017e+05  1.009e+05   5.964 6.17e-08 ***
## factor(i)3   1.337e+06  1.862e+05   7.183 2.99e-10 ***
## factor(i)4   1.778e+06  2.132e+05   8.339 1.61e-12 ***
## factor(i)5   1.828e+06  2.312e+05   7.907 1.15e-11 ***
## factor(i)6   1.706e+06  2.283e+05   7.475 8.07e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 210400 on 81 degrees of freedom
## Multiple R-squared:  0.9716, Adjusted R-squared:  0.9688 
## F-statistic: 346.9 on 8 and 81 DF,  p-value: < 2.2e-16
#it nhat mot gia tri p - value nho hon 0.05 cho thay tinh hop li cua mo hinh LSDV
#Pr(>F) co the bac bo gia thuyet H0

#Mo hinh khong tinh den tac dong dac trung cua thoi gian
#Mo hinh khong the hien tac dong cua cac bien khong thay doi theo thoi gian


#Mo hinh tac dong co dinh khong co bien gia FEM (Fix Effects Model)
library(plm)
FEM = trang %>% plm(C ~ Q + PF + LF, data = ., index = c("i", "t"), model = "within")
summary(FEM)
## Oneway (individual) effect Within Model
## 
## Call:
## plm(formula = C ~ Q + PF + LF, data = ., model = "within", index = c("i", 
##     "t"))
## 
## Balanced Panel: n=6, T=15, N=90
## 
## Residuals :
##      Min.   1st Qu.    Median   3rd Qu.      Max. 
## -551783.1 -159258.6    1796.2  137225.9  499296.0 
## 
## Coefficients :
##       Estimate  Std. Error t-value  Pr(>|t|)    
## Q   3.3190e+06  1.7135e+05 19.3694 < 2.2e-16 ***
## PF  7.7307e-01  9.7319e-02  7.9437 9.698e-12 ***
## LF -3.7974e+06  6.1377e+05 -6.1869 2.375e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    5.0776e+13
## Residual Sum of Squares: 3.5865e+12
## R-Squared:      0.92937
## Adj. R-Squared: 0.92239
## F-statistic: 355.254 on 3 and 81 DF, p-value: < 2.22e-16
#He so chan rieng cua tung hang trong mo hinh FEM
fixef(FEM)
##         1         2         3         4         5         6 
## -131236.0  470497.3 1205944.6 1646356.2 1697016.5 1575238.4
#Mo hinh tac dong ngau nhien (Random Effects Model) 
#hay mo hinh sai so bo phan ECM (Error Components Model)
#Bieu dien he so chan thanh hai bo phan trung binh va sai so ngau nhien
#bieu dien lai khien sai so ngau nhien co hai bo phan: 
#sai so ngau nhien dac trung cho tung ca the va sai so ngau nhien ket hop khong gian va thoi gian
#Neu bo phan sai so ngau nhien dac trung cho tung ca the co phuong sai bang 0, tuc la hang so 
#thi co the thuc hien hoi quy gop FEM cho cac ca the
#Tuong quan cua sai so ngau nhien doi voi mot ca the la khong doi trong mot khoang thoi gian nhat dinh, 
#tuonq quan nay la nhu nhau doi voi moi ca the

REM = trang %>% plm(C ~ Q + PF + LF, data =., index = c("i", "t"), model = "random")
summary(REM)
## Oneway (individual) effect Random Effect Model 
##    (Swamy-Arora's transformation)
## 
## Call:
## plm(formula = C ~ Q + PF + LF, data = ., model = "random", index = c("i", 
##     "t"))
## 
## Balanced Panel: n=6, T=15, N=90
## 
## Effects:
##                     var   std.dev share
## idiosyncratic 4.428e+10 2.104e+05 0.906
## individual    4.615e+09 6.793e+04 0.094
## theta:  0.3754  
## 
## Residuals :
##    Min. 1st Qu.  Median 3rd Qu.    Max. 
## -530516 -241650   50248  203965  783397 
## 
## Coefficients :
##                Estimate  Std. Error t-value  Pr(>|t|)    
## (Intercept)  1.0952e+06  3.7697e+05  2.9052  0.004665 ** 
## Q            2.1446e+06  8.8171e+04 24.3228 < 2.2e-16 ***
## PF           1.1757e+00  1.0356e-01 11.3531 < 2.2e-16 ***
## LF          -3.0261e+06  7.2713e+05 -4.1616 7.466e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    8.0306e+13
## Residual Sum of Squares: 6.2698e+12
## R-Squared:      0.92193
## Adj. R-Squared: 0.9192
## F-statistic: 338.508 on 3 and 86 DF, p-value: < 2.22e-16
#So sanh 2 mo hinh FEM va REM
library(stargazer)
## 
## Please cite as:
##  Hlavac, Marek (2015). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2. http://CRAN.R-project.org/package=stargazer
stargazer(FEM, REM, title = "So sanh 2 mo hinh FEM và REM", type = "text")
## 
## So sanh 2 mo hinh FEM và REM
## ============================================================
##                            Dependent variable:              
##              -----------------------------------------------
##                                     C                       
##                        (1)                     (2)          
## ------------------------------------------------------------
## Q               3,319,023.000***        2,144,561.000***    
##                   (171,354.100)           (88,170.630)      
##                                                             
## PF                  0.773***                1.176***        
##                      (0.097)                 (0.104)        
##                                                             
## LF              -3,797,368.000***       -3,026,060.000***   
##                   (613,773.100)           (727,130.000)     
##                                                             
## Constant                                1,095,172.000***    
##                                           (376,967.000)     
##                                                             
## ------------------------------------------------------------
## Observations           90                      90           
## R2                    0.929                   0.922         
## Adjusted R2           0.922                   0.919         
## F Statistic  355.254*** (df = 3; 81) 338.508*** (df = 3; 86)
## ============================================================
## Note:                            *p<0.1; **p<0.05; ***p<0.01
#Mo hinh tac dong ngau nhien hai chieu
#Tac dong ngau nhien theo ca chieu khong gian va thoi gian
#Theo phuong phap tiep can cua Amemiya

REM2chieu = trang %>% plm(C ~ Q + PF + LF, data = ., index = c("i", "t"),
                          model = "random",
                          random.method = "amemiya",
                          effect = "twoways")
summary(REM2chieu)
## Twoways effects Random Effect Model 
##    (Amemiya's transformation)
## 
## Call:
## plm(formula = C ~ Q + PF + LF, data = ., effect = "twoways", 
##     model = "random", random.method = "amemiya", index = c("i", 
##         "t"))
## 
## Balanced Panel: n=6, T=15, N=90
## 
## Effects:
##                     var   std.dev share
## idiosyncratic 4.732e+10 2.175e+05 0.061
## individual    6.536e+11 8.084e+05 0.837
## time          7.977e+10 2.824e+05 0.102
## theta  : 0.9307 (id) 0.7 (time) 0.6984 (total)
## 
## Residuals :
##    Min. 1st Qu.  Median 3rd Qu.    Max. 
## -554575 -153985  -11270  144817  453530 
## 
## Coefficients :
##                Estimate  Std. Error t-value  Pr(>|t|)    
## (Intercept)  7.9910e+05  5.7947e+05  1.3790  0.171468    
## Q            3.2787e+06  1.7583e+05 18.6462 < 2.2e-16 ***
## PF           7.5411e-01  2.3730e-01  3.1779  0.002062 ** 
## LF          -3.2458e+06  9.0000e+05 -3.6064  0.000520 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    2.1117e+13
## Residual Sum of Squares: 3.5633e+12
## R-Squared:      0.83126
## Adj. R-Squared: 0.82537
## F-statistic: 141.216 on 3 and 86 DF, p-value: < 2.22e-16
#Kiem dinh lua chon mo hinh Pooled OLS, FEM, REM
#Kiem dinh F, Wald test: neu cac he so chan cua tung ca the coi nhu bang 0 thi su dung Pooled OLS
#Kiem dinh Hausman de lua chon giua hai mo hinh REM va FEM
#Neu bac bo H0 thi FEM phu hop hon, H0: Ket qua giua hai phuong phap la khong khac biet

phtest(FEM, REM)
## 
##  Hausman Test
## 
## data:  C ~ Q + PF + LF
## chisq = 63.785, df = 3, p-value = 9.126e-14
## alternative hypothesis: one model is inconsistent
#Kiem dinh Breusch - Pagan de lua chon REM va Pooled OLS
ols = trang %>% plm(data = ., C ~ Q + PF + LF, model = "pooling")
plmtest(ols, type = "bp")
## 
##  Lagrange Multiplier Test - (Breusch-Pagan) for balanced panels
## 
## data:  C ~ Q + PF + LF
## chisq = 0.61309, df = 1, p-value = 0.4336
## alternative hypothesis: significant effects
#Neu p - value nho hon 0.05 thi phuong phap REM phu hop hon

#Kiem dinh tuong quan phan du
pcdtest(FEM, test = c("lm"))
## 
##  Breusch-Pagan LM test for cross-sectional dependence in panels
## 
## data:  C ~ Q + PF + LF
## chisq = 70.675, df = 15, p-value = 3.386e-09
## alternative hypothesis: cross-sectional dependence
#H0 la phan du giua cac ca the khong tuong quan