hedging

# 1. 載入套件
library(readxl)
library(tidyverse)
Warning: package 'ggplot2' was built under R version 4.5.2
Warning: package 'tidyr' was built under R version 4.5.2
Warning: package 'purrr' was built under R version 4.5.2
Warning: package 'dplyr' was built under R version 4.5.2
Warning: package 'lubridate' was built under R version 4.5.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.0     ✔ readr     2.1.5
✔ forcats   1.0.1     ✔ stringr   1.5.2
✔ ggplot2   4.0.2     ✔ tibble    3.3.0
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(plm)

Attaching package: 'plm'

The following objects are masked from 'package:dplyr':

    between, lag, lead
# 2. 讀取資料
hedging_data <- read_excel("~/Desktop/data/hedging.new.xlsx")

# 3. 指定成 df
df <- hedging_data

# 4. 修正欄名(全形底線改正常)
names(df) <- c(
  "Country",
  "Year",
  "Political_Stability",
  "Government_Effectiveness",
  "Democracy",
  "hedging",
  "SCS",
  "post2022",
  "trade_China",
  "sec_us"
)

# 5. 檢查欄名
names(df)
 [1] "Country"                  "Year"                    
 [3] "Political_Stability"      "Government_Effectiveness"
 [5] "Democracy"                "hedging"                 
 [7] "SCS"                      "post2022"                
 [9] "trade_China"              "sec_us"                  
df <- df %>%
  mutate(
    Country = as.factor(Country),
    Year = as.numeric(Year),
    hedging = as.numeric(hedging),
    Political_Stability = as.numeric(Political_Stability),
    Government_Effectiveness = as.numeric(Government_Effectiveness),
    Democracy = as.numeric(Democracy)
  )
p_df <- pdata.frame(df, index = c("Country", "Year"))

fe_model <- plm(
  hedging ~ Political_Stability + Government_Effectiveness + Democracy,
  data = p_df,
  model = "within",
  effect = "individual"
)

summary(fe_model)
Oneway (individual) effect Within Model

Call:
plm(formula = hedging ~ Political_Stability + Government_Effectiveness + 
    Democracy, data = p_df, effect = "individual", model = "within")

Balanced Panel: n = 8, T = 10, N = 80

Residuals:
       Min.     1st Qu.      Median     3rd Qu.        Max. 
-0.07454432 -0.00931777  0.00058948  0.00925053  0.04817956 

Coefficients:
                            Estimate  Std. Error t-value Pr(>|t|)  
Political_Stability       0.00225102  0.00095298  2.3621   0.0210 *
Government_Effectiveness -0.00139580  0.00114708 -1.2168   0.2278  
Democracy                -0.00900933  0.00612222 -1.4716   0.1457  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Total Sum of Squares:    0.035588
Residual Sum of Squares: 0.03121
R-Squared:      0.12302
Adj. R-Squared: -0.0040817
F-statistic: 3.22629 on 3 and 69 DF, p-value: 0.027712
ggplot(df, aes(x = Year, y = hedging, color = Country, group = Country)) +
  geom_line(linewidth = 1.2) +
  geom_point(size = 2) +
  theme_minimal()

cor_data <- df %>%
  select(
    hedging,
    Political_Stability,
    Government_Effectiveness,
    Democracy,
    SCS,
    post2022
  ) %>%
  mutate(across(everything(), as.numeric)) %>%
  na.omit()

cor_matrix <- cor(cor_data)

heatmap(cor_matrix)

library(corrplot)
corrplot 0.95 loaded
corrplot(
  cor_matrix,
  method = "color",
  type = "upper",
  addCoef.col = "black",
  tl.col = "black",
  tl.srt = 45,
  number.cex = 0.8
)

ggplot(df, aes(x = Political_Stability, y = hedging, color = Country)) +
  geom_point(size = 3, alpha = 0.8) +
  geom_smooth(method = "lm", se = TRUE, color = "black") +
  labs(
    title = "Political Stability and Hedging",
    x = "Political Stability",
    y = "Hedging",
    color = "Country"
  ) +
  theme_minimal()
`geom_smooth()` using formula = 'y ~ x'

# ===============================
# ASEAN Hedging Panel Regression
# 你的研究正式版 R 程式
# ===============================

# 1. 載入套件
library(readxl)
library(tidyverse)
library(plm)
library(lmtest)
Loading required package: zoo

Attaching package: 'zoo'
The following objects are masked from 'package:base':

    as.Date, as.Date.numeric
library(sandwich)

# 2. 讀取資料
df <- read_excel("~/Desktop/data/hedging.new.xlsx")

# 3. 修正欄位名稱(避免全形底線)
names(df) <- c("Country",
               "Year",
               "Political_Stability",
               "Government_Effectiveness",
               "Democracy",
               "hedging",
               "SCS",
               "post2022",
               "trade_China",
               "sec_us")

# 4. 資料型態轉換
df$Year <- as.numeric(df$Year)
df$hedging <- as.numeric(df$hedging)
df$Political_Stability <- as.numeric(df$Political_Stability)
df$Government_Effectiveness <- as.numeric(df$Government_Effectiveness)
df$Democracy <- as.numeric(df$Democracy)
df$SCS <- as.factor(df$SCS)
df$post2022 <- as.factor(df$post2022)

# 5. 建立 panel data
p_df <- pdata.frame(df, index = c("Country", "Year"))

# ===============================
# Model 1:固定效果模型(主模型)
# ===============================

model1 <- plm(
  hedging ~ Political_Stability +
            Government_Effectiveness +
            Democracy +
            post2022,
  data = p_df,
  model = "within"
)

summary(model1)
Oneway (individual) effect Within Model

Call:
plm(formula = hedging ~ Political_Stability + Government_Effectiveness + 
    Democracy + post2022, data = p_df, model = "within")

Balanced Panel: n = 8, T = 10, N = 80

Residuals:
      Min.    1st Qu.     Median    3rd Qu.       Max. 
-0.0699649 -0.0063760  0.0018528  0.0091569  0.0434486 

Coefficients:
                            Estimate  Std. Error t-value Pr(>|t|)   
Political_Stability       0.00097050  0.00097604  0.9943 0.323593   
Government_Effectiveness -0.00070885  0.00109552 -0.6470 0.519781   
Democracy                -0.00694831  0.00577214 -1.2038 0.232853   
post20221                 0.02030061  0.00624377  3.2513 0.001789 **
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Total Sum of Squares:    0.035588
Residual Sum of Squares: 0.027011
R-Squared:      0.24101
Adj. R-Squared: 0.11823
F-statistic: 5.39816 on 4 and 68 DF, p-value: 0.00077904
# robust SE
coeftest(model1, vcov = vcovHC(model1, type = "HC1"))

t test of coefficients:

                            Estimate  Std. Error t value Pr(>|t|)   
Political_Stability       0.00097050  0.00089709  1.0818 0.283153   
Government_Effectiveness -0.00070885  0.00061144 -1.1593 0.250382   
Democracy                -0.00694831  0.01038215 -0.6693 0.505598   
post20221                 0.02030061  0.00708538  2.8651 0.005541 **
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# ===============================
# Model 2:加入南海變數(Random Effects)
# 固定效果會吸收不變變數SCS
# ===============================

model2 <- plm(
  hedging ~ Political_Stability +
            Government_Effectiveness +
            Democracy +
            SCS +
            post2022,
  data = p_df,
  model = "random"
)

summary(model2)
Oneway (individual) effect Random Effect Model 
   (Swamy-Arora's transformation)

Call:
plm(formula = hedging ~ Political_Stability + Government_Effectiveness + 
    Democracy + SCS + post2022, data = p_df, model = "random")

Balanced Panel: n = 8, T = 10, N = 80

Effects:
                    var   std.dev share
idiosyncratic 0.0003972 0.0199304  0.04
individual    0.0094434 0.0971773  0.96
theta: 0.9353

Residuals:
       Min.     1st Qu.      Median     3rd Qu.        Max. 
-0.06726651 -0.00924393 -0.00066896  0.00992457  0.04707561 

Coefficients:
                            Estimate  Std. Error z-value  Pr(>|z|)    
(Intercept)               0.11708782  0.09202750  1.2723 0.2032618    
Political_Stability       0.00094776  0.00091910  1.0312 0.3024574    
Government_Effectiveness -0.00040324  0.00097798 -0.4123 0.6801060    
Democracy                -0.00650175  0.00551423 -1.1791 0.2383645    
SCS1                      0.01799247  0.06887228  0.2612 0.7939044    
post20221                 0.02074811  0.00610092  3.4008 0.0006719 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Total Sum of Squares:    0.036952
Residual Sum of Squares: 0.02851
R-Squared:      0.22845
Adj. R-Squared: 0.17632
Chisq: 21.9105 on 5 DF, p-value: 0.00054452
coeftest(model2, vcov = vcovHC(model2, type = "HC1"))

t test of coefficients:

                            Estimate  Std. Error t value Pr(>|t|)   
(Intercept)               0.11708782  0.07235560  1.6182 0.109868   
Political_Stability       0.00094776  0.00083338  1.1372 0.259104   
Government_Effectiveness -0.00040324  0.00053086 -0.7596 0.449911   
Democracy                -0.00650175  0.01002102 -0.6488 0.518469   
SCS1                      0.01799247  0.05130673  0.3507 0.726821   
post20221                 0.02074811  0.00729695  2.8434 0.005767 **
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# ===============================
# Model 3:治理能力 × post2022
# 國際壓力下治理國家是否更能避險
# ===============================

model3 <- plm(
  hedging ~ Government_Effectiveness * post2022 +
            Political_Stability +
            Democracy,
  data = p_df,
  model = "within"
)

summary(model3)
Oneway (individual) effect Within Model

Call:
plm(formula = hedging ~ Government_Effectiveness * post2022 + 
    Political_Stability + Democracy, data = p_df, model = "within")

Balanced Panel: n = 8, T = 10, N = 80

Residuals:
      Min.    1st Qu.     Median    3rd Qu.       Max. 
-0.0700787 -0.0062442  0.0018180  0.0091225  0.0435024 

Coefficients:
                                      Estimate  Std. Error t-value Pr(>|t|)
Government_Effectiveness           -6.9831e-04  1.1240e-03 -0.6213   0.5365
post20221                           2.1366e-02  2.2443e-02  0.9520   0.3445
Political_Stability                 9.5543e-04  1.0294e-03  0.9281   0.3567
Democracy                          -6.8860e-03  5.9502e-03 -1.1573   0.2513
Government_Effectiveness:post20221 -1.7848e-05  3.6104e-04 -0.0494   0.9607

Total Sum of Squares:    0.035588
Residual Sum of Squares: 0.02701
R-Squared:      0.24104
Adj. R-Squared: 0.1051
F-statistic: 4.25567 on 5 and 67 DF, p-value: 0.0020241
coeftest(model3, vcov = vcovHC(model3, type = "HC1"))

t test of coefficients:

                                      Estimate  Std. Error t value Pr(>|t|)
Government_Effectiveness           -6.9831e-04  7.3178e-04 -0.9543   0.3434
post20221                           2.1366e-02  2.1175e-02  1.0090   0.3166
Political_Stability                 9.5543e-04  9.3099e-04  1.0263   0.3085
Democracy                          -6.8860e-03  9.9446e-03 -0.6924   0.4911
Government_Effectiveness:post20221 -1.7848e-05  3.0354e-04 -0.0588   0.9533
# ===============================
# Hausman Test
# Fixed vs Random
# ===============================

phtest(model1, model2)

    Hausman Test

data:  hedging ~ Political_Stability + Government_Effectiveness + Democracy +  ...
chisq = 0.47959, df = 4, p-value = 0.9755
alternative hypothesis: one model is inconsistent
# ===============================
# VIF(共線性檢查)
# ===============================

library(car)
Loading required package: carData

Attaching package: 'car'
The following object is masked from 'package:dplyr':

    recode
The following object is masked from 'package:purrr':

    some
ols_model <- lm(
  hedging ~ Political_Stability +
            Government_Effectiveness +
            Democracy +
            post2022,
  data = df
)

vif(ols_model)
     Political_Stability Government_Effectiveness                Democracy 
                4.193471                 5.805257                 4.637602 
                post2022 
                1.038000 
# ===============================
# 結果輸出(可做表格)
# ===============================

library(stargazer)

Please cite as: 
 Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
 R package version 5.2.3. https://CRAN.R-project.org/package=stargazer 
stargazer(model1, model2, model3,
          type = "text",
          title = "Determinants of ASEAN Hedging",
          digits = 3)

Determinants of ASEAN Hedging
========================================================================================
                                                    Dependent variable:                 
                                   -----------------------------------------------------
                                                          hedging                       
                                            (1)             (2)             (3)         
----------------------------------------------------------------------------------------
Political_Stability                        0.001           0.001           0.001        
                                          (0.001)         (0.001)         (0.001)       
                                                                                        
Government_Effectiveness                  -0.001          -0.0004         -0.001        
                                          (0.001)         (0.001)         (0.001)       
                                                                                        
Democracy                                 -0.007          -0.007          -0.007        
                                          (0.006)         (0.006)         (0.006)       
                                                                                        
SCS1                                                       0.018                        
                                                          (0.069)                       
                                                                                        
Government_Effectiveness:post20221                                       -0.00002       
                                                                         (0.0004)       
                                                                                        
post20221                                0.020***        0.021***          0.021        
                                          (0.006)         (0.006)         (0.022)       
                                                                                        
Constant                                                   0.117                        
                                                          (0.092)                       
                                                                                        
----------------------------------------------------------------------------------------
Observations                                80              80              80          
R2                                         0.241           0.228           0.241        
Adjusted R2                                0.118           0.176           0.105        
F Statistic                        5.398*** (df = 4; 68) 21.910*** 4.256*** (df = 5; 67)
========================================================================================
Note:                                                        *p<0.1; **p<0.05; ***p<0.01