rm(list = ls())
setwd("/Users/vancam/Documents/WAIKATO-Thesis/VESuse/CVan/TFP_OwnGO")
load("tfpgo_unblRD.rda")
load("/Users/vancam/Documents/WAIKATO-Thesis/VESuse/CVan/Final_forpaper/export_ublfinal.rda")
load("/Users/vancam/Documents/WAIKATO-Thesis/VESuse/CVan/2015/export15clear_4digit.rda")

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
export <- filter(export_ublfinal, exportvol >0)
library(quantreg)
## Loading required package: SparseM
## 
## Attaching package: 'SparseM'
## The following object is masked from 'package:base':
## 
##     backsolve
attach(export)
## The following object is masked _by_ .GlobalEnv:
## 
##     export
Y <- cbind(exportvol)
X <- cbind(scale, labor, capital2, ownership)
ols <- lm(Y~X, data = export)
quan25 <- rq(Y~X, data = export, tau = 0.25)
quan50 <- rq(Y~X, data = export, tau = 0.5)
quan75 <- rq(Y~X, data = export, tau = 0.75)
## Warning in rq.fit.br(x, y, tau = tau, ...): Solution may be nonunique
summary(quan25)
## Warning in summary.rq(quan25): 594 non-positive fis
## 
## Call: rq(formula = Y ~ X, tau = 0.25, data = export)
## 
## tau: [1] 0.25
## 
## Coefficients:
##             Value      Std. Error t value    Pr(>|t|)  
## (Intercept)   -2.46519    0.96666   -2.55020    0.01077
## Xscale        10.35629    7.87170    1.31564    0.18831
## Xlabor         1.37156    0.13044   10.51463    0.00000
## Xcapital2      0.00433    0.00380    1.14060    0.25405
## Xownership  -118.91260   23.78784   -4.99888    0.00000
summary(quan50)
## Warning in summary.rq(quan50): 370 non-positive fis
## 
## Call: rq(formula = Y ~ X, tau = 0.5, data = export)
## 
## tau: [1] 0.5
## 
## Coefficients:
##             Value      Std. Error t value    Pr(>|t|)  
## (Intercept)  -29.57723    4.96586   -5.95611    0.00000
## Xscale       199.72462   48.52622    4.11581    0.00004
## Xlabor         4.16621    0.36677   11.35925    0.00000
## Xcapital2      0.01518    0.00553    2.74651    0.00603
## Xownership  -425.19968   99.99429   -4.25224    0.00002
summary(quan75)
## Warning in summary.rq(quan75): 1 non-positive fis
## 
## Call: rq(formula = Y ~ X, tau = 0.75, data = export)
## 
## tau: [1] 0.75
## 
## Coefficients:
##             Value      Std. Error t value    Pr(>|t|)  
## (Intercept)  -62.30066   17.85600   -3.48906    0.00049
## Xscale       374.64990   61.85280    6.05712    0.00000
## Xlabor        10.73823    0.63446   16.92500    0.00000
## Xcapital2      0.07139    0.01191    5.99305    0.00000
## Xownership  -490.20750   82.18345   -5.96480    0.00000
#Ploting the data
kevin <- rq(Y~X, tau = seq(0.05, 0.95, by = 0.05), data = export)
## Warning in rq.fit.br(x, y, tau = tau, ...): Solution may be nonunique

## Warning in rq.fit.br(x, y, tau = tau, ...): Solution may be nonunique

## Warning in rq.fit.br(x, y, tau = tau, ...): Solution may be nonunique

## Warning in rq.fit.br(x, y, tau = tau, ...): Solution may be nonunique

## Warning in rq.fit.br(x, y, tau = tau, ...): Solution may be nonunique

## Warning in rq.fit.br(x, y, tau = tau, ...): Solution may be nonunique

## Warning in rq.fit.br(x, y, tau = tau, ...): Solution may be nonunique

## Warning in rq.fit.br(x, y, tau = tau, ...): Solution may be nonunique
kevin.plot <- summary(kevin)
## Warning in summary.rq(xi, U = U, ...): 125 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 1438 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 864 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 594 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 1141 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 556 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 433 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 341 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 370 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 483 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 294 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 88 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 34 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 1 non-positive fis
plot(kevin.plot)

#On tfp
tfp15 <- filter(tfpgo_unblRD, year == 2015)
attach(tfp15)
## The following object is masked _by_ .GlobalEnv:
## 
##     export
## The following objects are masked from export:
## 
##     Blinkage, Flinkage, HHI, Hlinkage, capital1, capital2,
##     establishedyear, expint, export, exportvol, fcode, ftype, go1,
##     id, importvol, industrialzone, industrycode, investment,
##     labor, laborincome, laborincome1, ownership, province, region,
##     sales, scale, sec2_code, sec4_code, year
Y <- cbind(tfp)
X <- cbind(scale, labor, industrialzone, ownership)

ols <- lm(Y~X, data = tfp15)
quan25tfp <- rq(Y~X, data = tfp15, tau = 0.25)
quan50tfp <- rq(Y~X, data = tfp15, tau = 0.5)
quan75tfp <- rq(Y~X, data = tfp15, tau = 0.75)
summary(quan25tfp)
## Warning in summary.rq(quan25tfp): 36 non-positive fis
## 
## Call: rq(formula = Y ~ X, tau = 0.25, data = tfp15)
## 
## tau: [1] 0.25
## 
## Coefficients:
##                 Value    Std. Error t value  Pr(>|t|)
## (Intercept)      2.27716  0.06029   37.77221  0.00000
## Xscale           0.07084  0.02252    3.14638  0.00165
## Xlabor           0.00024  0.00010    2.28696  0.02220
## Xindustrialzone  0.67624  0.03430   19.71480  0.00000
## Xownership       0.65015  0.06007   10.82282  0.00000
summary(quan50tfp)
## Warning in summary.rq(quan50tfp): 83 non-positive fis
## 
## Call: rq(formula = Y ~ X, tau = 0.5, data = tfp15)
## 
## tau: [1] 0.5
## 
## Coefficients:
##                 Value    Std. Error t value  Pr(>|t|)
## (Intercept)      3.25875  0.04084   79.78585  0.00000
## Xscale           0.16106  0.01903    8.46383  0.00000
## Xlabor          -0.00030  0.00009   -3.30280  0.00096
## Xindustrialzone  0.42261  0.02372   17.81649  0.00000
## Xownership       0.62250  0.04102   15.17568  0.00000
summary(quan75tfp)
## Warning in summary.rq(quan75tfp): 37 non-positive fis
## 
## Call: rq(formula = Y ~ X, tau = 0.75, data = tfp15)
## 
## tau: [1] 0.75
## 
## Coefficients:
##                 Value     Std. Error t value   Pr(>|t|) 
## (Intercept)       4.15979   0.03443  120.82512   0.00000
## Xscale            0.28002   0.01811   15.46429   0.00000
## Xlabor           -0.00103   0.00006  -17.63529   0.00000
## Xindustrialzone   0.21362   0.02577    8.28816   0.00000
## Xownership        0.53283   0.03415   15.60111   0.00000
summary(ols)
## 
## Call:
## lm(formula = Y ~ X, data = tfp15)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -25.5368  -0.8342   0.1193   0.9944   7.1214 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     3.260e+00  4.076e-02   79.97   <2e-16 ***
## Xscale          2.527e-02  1.157e-03   21.84   <2e-16 ***
## Xlabor          3.429e-04  2.838e-05   12.08   <2e-16 ***
## Xindustrialzone 6.200e-01  2.877e-02   21.55   <2e-16 ***
## Xownership      5.106e-01  4.114e-02   12.41   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.538 on 52082 degrees of freedom
## Multiple R-squared:  0.02954,    Adjusted R-squared:  0.02947 
## F-statistic: 396.4 on 4 and 52082 DF,  p-value: < 2.2e-16
#Ploting the data
kvtfp <- rq(Y~X, tau = seq(0.05, 0.95, by = 0.05), data = tfp15)
kvtfp.plot <- summary(kvtfp)
## Warning in summary.rq(xi, U = U, ...): 1 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 23 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 36 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 16 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 31 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 59 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 69 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 83 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 105 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 119 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 89 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 61 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 37 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 24 non-positive fis
plot(kvtfp.plot)