aseananalysis

# 1. 載入核心套件 (避開 ggstatsplot 以免版本報錯)
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. 讀取資料
df <- read.csv("aseananalysis.csv")

# 3. 確保數值型態正確
df$Align <- as.numeric(df$Align)
df$SCS <- as.factor(df$SCS) # 0 = 無爭議, 1 = 有爭議

# 4. 建立面板數據格式 (指定國家與年份)
p_df <- pdata.frame(df, index = c("Country", "Year"))

四分位趨勢分析 (Boxplot Analysis)

用來觀察東協整體的投票分布,看看 2024 年補值後,整體重心是否上移。

# 繪製各年份的四分位圖
ggplot(df, aes(x = factor(Year), y = Align)) +
  geom_boxplot(aes(fill = factor(Year)), alpha = 0.5, outlier.color = "red") +
  geom_jitter(width = 0.2, alpha = 0.4, color = "black") + # 顯示每個國家的點
  scale_fill_brewer(palette = "Set3") + 
  theme_minimal() +
  labs(title = "ASEAN Alignment Score Distribution (2014-2024)",
       subtitle = "Higher Score = More Pro-US | Lower Score = More Pro-China",
       x = "Year", y = "Alignment Score") +
  theme(legend.position = "none")

南海爭議與投票傾向 (Chi-Square Test)

我們要測試「是否有南海爭議」與「投票傾向(親美/親中)」是否有顯著關聯。

# A. 將 Align 轉換為類別:高於中位數為 "Pro-US", 低於則為 "Pro-China"
df_cat <- df %>%
  mutate(Align_Type = ifelse(Align > median(Align, na.rm=T), "Pro-US", "Pro-China"))

# B. 執行卡方檢定 (SCS vs Align_Type)
contingency_table <- table(df_cat$SCS, df_cat$Align_Type)
print("--- 交叉表 (0=無爭議, 1=有爭議) ---")
[1] "--- 交叉表 (0=無爭議, 1=有爭議) ---"
print(contingency_table)
   
    Pro-China Pro-US
  0        20     24
  1        24     20
chisq_res <- chisq.test(contingency_table)
print("--- 卡方檢定結果 ---")
[1] "--- 卡方檢定結果 ---"
print(chisq_res)

    Pearson's Chi-squared test with Yates' continuity correction

data:  contingency_table
X-squared = 0.40909, df = 1, p-value = 0.5224
# C. 視覺化關聯 (用百分比堆疊條形圖代替 ggstatsplot)
ggplot(df_cat, aes(x = SCS, fill = Align_Type)) +
  geom_bar(position = "fill") +
  scale_y_continuous(labels = scales::percent) +
  theme_minimal() +
  labs(title = "Impact of SCS Dispute on Voting Alignment",
       x = "SCS Dispute (0 = No, 1 = Yes)",
       y = "Percentage", fill = "Alignment Group")

因果推論回歸 (Panel Regression)

利用固定效應模型(FixedEffects),分析在排除國家固定背景後,貿易與軍事變數的影響。

# 執行固定效應模型 (Within-model)
# 探討:對中/美貿易、中/美軍援、民主度、南海爭議對投票的影響
fe_model <- plm(Align ~ Trade_Chn_GDP + Trade_US_GDP + 
                Mil_China + Mil_US + 
                Democracy + SCS, 
                data = p_df, 
                model = "within")

# 顯示統計結果
print("--- 固定效應回歸報告 ---")
[1] "--- 固定效應回歸報告 ---"
summary(fe_model)
Oneway (individual) effect Within Model

Call:
plm(formula = Align ~ Trade_Chn_GDP + Trade_US_GDP + Mil_China + 
    Mil_US + Democracy + SCS, data = p_df, model = "within")

Balanced Panel: n = 8, T = 11, N = 88

Residuals:
   Cambodia-2014    Cambodia-2015    Cambodia-2016    Cambodia-2017 
     -9.9077e-16      -9.9655e-16      -9.9344e-16      -9.9531e-16 
   Cambodia-2018    Cambodia-2019    Cambodia-2020    Cambodia-2021 
     -4.4816e-02       4.4816e-02       2.8081e-03      -9.8684e-16 
   Cambodia-2022    Cambodia-2023    Cambodia-2024   Indonesia-2014 
     -3.6635e-03       8.5538e-04      -9.9285e-16      -1.5635e-15 
  Indonesia-2015   Indonesia-2016   Indonesia-2017   Indonesia-2018 
     -1.5366e-15      -1.5302e-15      -1.5646e-15      -1.5561e-15 
  Indonesia-2019   Indonesia-2020   Indonesia-2021   Indonesia-2022 
     -1.6133e-15      -1.5926e-15      -1.5985e-15      -1.5871e-15 
  Indonesia-2023   Indonesia-2024     Lao PDR-2014     Lao PDR-2015 
     -1.5498e-15      -1.5669e-15      -1.2171e-01       1.9531e-01 
    Lao PDR-2016     Lao PDR-2017     Lao PDR-2018     Lao PDR-2019 
      1.0720e-02      -1.9359e-02      -6.4863e-02      -1.0180e-04 
    Lao PDR-2020     Lao PDR-2021     Lao PDR-2022     Lao PDR-2023 
     -1.7539e-15      -1.7103e-15      -3.6635e-03       3.6635e-03 
    Lao PDR-2024    Malaysia-2014    Malaysia-2015    Malaysia-2016 
     -1.7474e-15      -1.6657e-15      -1.6631e-15      -1.6624e-15 
   Malaysia-2017    Malaysia-2018    Malaysia-2019    Malaysia-2020 
     -1.6539e-15      -1.6627e-15      -1.6594e-15      -1.6644e-15 
   Malaysia-2021    Malaysia-2022    Malaysia-2023    Malaysia-2024 
     -1.6686e-15      -1.6612e-15      -1.6495e-15      -1.6832e-15 
Philippines-2014 Philippines-2015 Philippines-2016 Philippines-2017 
     -5.5444e-16      -5.6430e-16      -5.6085e-16      -5.6398e-16 
Philippines-2018 Philippines-2019 Philippines-2020 Philippines-2021 
     -5.6841e-16      -5.7078e-16      -5.6450e-16      -5.6073e-16 
Philippines-2022 Philippines-2023 Philippines-2024   Singapore-2014 
     -5.5662e-16      -5.6633e-16      -5.5470e-16      -3.5201e-16 
  Singapore-2015   Singapore-2016   Singapore-2017   Singapore-2018 
     -4.5593e-16      -4.5780e-16      -4.5692e-16      -4.5903e-16 
  Singapore-2019   Singapore-2020   Singapore-2021   Singapore-2022 
     -5.2144e-16      -4.5696e-16      -4.3664e-16      -5.7021e-16 
  Singapore-2023   Singapore-2024    Thailand-2014    Thailand-2015 
     -4.1929e-16      -4.3765e-16       3.7411e-17       3.6626e-17 
   Thailand-2016    Thailand-2017    Thailand-2018    Thailand-2019 
      3.4786e-17       4.9894e-17       3.2729e-17       3.0032e-17 
   Thailand-2020    Thailand-2021    Thailand-2022    Thailand-2023 
      3.0957e-17       2.9302e-17       3.5041e-17       3.5403e-17 
   Thailand-2024     Vietnam-2014     Vietnam-2015     Vietnam-2016 
      4.9331e-17       1.5268e-15       1.5468e-15       1.5396e-15 
    Vietnam-2017     Vietnam-2018     Vietnam-2019     Vietnam-2020 
      1.5304e-15       1.5384e-15       1.5380e-15       1.5380e-15 
    Vietnam-2021     Vietnam-2022     Vietnam-2023     Vietnam-2024 
      1.5372e-15       1.5428e-15       1.5408e-15       1.5402e-15 

Coefficients: (6 dropped because of singularities)
                 Estimate Std. Error  t-value  Pr(>|t|)    
Trade_Chn_GDP  -9.0284949  3.5674710  -2.5308 0.0646108 .  
Trade_US_GDP   49.3859910 12.4808218   3.9570 0.0167203 *  
Mil_China       0.0410458  0.0061361   6.6893 0.0025975 ** 
Mil_US0.1       0.3633651  0.3361052   1.0811 0.3404761    
Mil_US0.1245    0.1343460  0.2654524   0.5061 0.6394041    
Mil_US0.1452    0.5223941  0.2572227   2.0309 0.1120976    
Mil_US0.15      0.7702547  0.1590245   4.8436 0.0083780 ** 
Mil_US0.2      -0.1590214  0.2829054  -0.5621 0.6040524    
Mil_US0.2145    0.4225914  0.2335391   1.8095 0.1446296    
Mil_US0.28     -0.0184684  0.2605845  -0.0709 0.9469007    
Mil_US0.3      -0.1955169  0.3007200  -0.6502 0.5510402    
Mil_US0.3214   -0.3582284  0.2421978  -1.4791 0.2132079    
Mil_US0.35      0.1456503  0.2575910   0.5654 0.6019884    
Mil_US0.42     -0.1028767  0.4173940  -0.2465 0.8174475    
Mil_US10.2     -0.6257998  0.1869803  -3.3469 0.0286519 *  
Mil_US102.12   -0.7389655  0.1831352  -4.0351 0.0156676 *  
Mil_US102.5    -0.2628866  0.1792026  -1.4670 0.2162775    
Mil_US105.45   -0.8838152  0.2313703  -3.8199 0.0187796 *  
Mil_US108.12   -1.2636213  0.2459818  -5.1371 0.0068052 ** 
Mil_US11.45    -0.9917057  0.2672980  -3.7101 0.0206520 *  
Mil_US110.2145 -0.8092059  0.3389560  -2.3873 0.0753847 .  
Mil_US110.45   -1.1521668  0.2836680  -4.0617 0.0153276 *  
Mil_US112.12   -0.6560746  0.2841673  -2.3088 0.0821439 .  
Mil_US112.1452 -0.8874536  0.5436996  -1.6322 0.1779633    
Mil_US115.4125 -1.3081285  0.4156094  -3.1475 0.0345973 *  
Mil_US12.5112  -1.0846950  0.3331622  -3.2558 0.0312056 *  
Mil_US120.15    1.9494307  0.3803047   5.1260 0.0068577 ** 
Mil_US120.5214 -0.5222347  0.4894904  -1.0669 0.3461329    
Mil_US125.45    1.4025591  0.3681493   3.8098 0.0189441 *  
Mil_US128.05   -0.6585287  0.5237435  -1.2573 0.2770325    
Mil_US132.12    4.8290249  0.7167187   6.7377 0.0025286 ** 
Mil_US138.45    3.8219012  0.6999908   5.4599 0.0054705 ** 
Mil_US142.12    4.4277434  0.7859252   5.6338 0.0048845 ** 
Mil_US144.55    0.6882816  0.2759889   2.4939 0.0672053 .  
Mil_US145.2412  1.4426232  0.3341803   4.3169 0.0124779 *  
Mil_US148.4215  0.2779942  0.1831681   1.5177 0.2036960    
Mil_US15.2452  -1.7939747  0.6493188  -2.7629 0.0507009 .  
Mil_US152.1452  1.1249428  0.2892687   3.8889 0.0177068 *  
Mil_US155.2    -2.3162524  0.2373424  -9.7591 0.0006176 ***
Mil_US160.2145  0.1680890  0.1779444   0.9446 0.3983422    
Mil_US162.45   -2.5342309  0.2529421 -10.0190 0.0005579 ***
Mil_US168.45   -1.3159718  0.3073924  -4.2811 0.0128366 *  
Mil_US170.8125 -2.2738274  0.3499096  -6.4983 0.0028928 ** 
Mil_US172.3    -1.2298963  0.2660813  -4.6223 0.0098644 ** 
Mil_US175.8    -1.4327347  0.3208578  -4.4653 0.0111146 *  
Mil_US18.0125  -0.6187889  0.6073652  -1.0188 0.3659017    
Mil_US180.12   -1.8262461  0.3186763  -5.7307 0.0045912 ** 
Mil_US195.521   0.8055906  0.2408536   3.3447 0.0287091 *  
Mil_US20.1452   0.2744390  0.4969161   0.5523 0.6101598    
Mil_US210.3341 -0.0416366  0.1783617  -0.2334 0.8268804    
Mil_US22.45     0.1936566  0.5567873   0.3478 0.7455140    
Mil_US240.2144 -1.0823252  0.2324999  -4.6552 0.0096243 ** 
Mil_US40.85     1.3761327  0.4269895   3.2229 0.0321926 *  
Mil_US42.15     1.3486089  0.4289399   3.1441 0.0347121 *  
Mil_US43.12    -0.4553799  0.2482606  -1.8343 0.1405276    
Mil_US44.55    -0.2291647  0.2632385  -0.8706 0.4331228    
Mil_US44.85    -0.5982856  0.2181709  -2.7423 0.0517839 .  
Mil_US45.12     1.6619369  0.3721788   4.4654 0.0111137 *  
Mil_US45.3125  -0.4334144  0.2419015  -1.7917 0.1476585    
Mil_US48.7125  -0.4809525  0.1930415  -2.4914 0.0673800 .  
Mil_US5.2       0.2849629  0.4923516   0.5788 0.5937653    
Mil_US50.1452  -0.9064731  0.1871009  -4.8448 0.0083707 ** 
Mil_US52.4145  -0.2445888  0.1877570  -1.3027 0.2626176    
Mil_US6.8      -0.1922175  0.3734064  -0.5148 0.6338535    
Mil_US78.45    -1.6819336  0.2227979  -7.5491 0.0016496 ** 
Mil_US8.42      0.6691058  0.2516700   2.6587 0.0564690 .  
Mil_US80.12    -1.5343597  0.2306790  -6.6515 0.0026528 ** 
Mil_US82.15    -0.2019613  0.2183042  -0.9251 0.4072557    
Mil_US84.45    -0.7639822  0.2132468  -3.5826 0.0231135 *  
Mil_US85.45    -0.7869678  0.2179931  -3.6101 0.0225554 *  
Mil_US85.6452  -0.4424251  0.2178843  -2.0306 0.1121424    
Mil_US86.12    -0.8500996  0.1938572  -4.3852 0.0118270 *  
Mil_US88.4215  -0.9133360  0.2054882  -4.4447 0.0112927 *  
Mil_US92.1124  -1.3818015  0.1894864  -7.2924 0.0018798 ** 
Mil_USn.a.      0.4868301  0.3000572   1.6225 0.1800255    
Democracy       2.0123906  0.3626135   5.5497 0.0051578 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Total Sum of Squares:    32.301
Residual Sum of Squares: 0.061721
R-Squared:      0.99809
Adj. R-Squared: 0.95844
F-statistic: 27.4918 on 76 and 4 DF, p-value: 0.0025812
# 1. 安裝並載入格式化套件
if(!require(stargazer)) install.packages("stargazer")
Loading required package: 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 
library(stargazer)

# 2. 建立模型 (這裡以固定效應 FE 為例)
# 確保變數名稱與你的 csv 一致
model_fe <- plm(Align ~ Trade_Chn_GDP + Trade_US_GDP + 
                Mil_China + Mil_US + 
                Democracy + SCS, 
                data = p_df, 
                model = "within")

# 3. 輸出成像截圖中那樣的文字表格
stargazer(model_fe, 
          type = "text", 
          title = "Regression Results: ASEAN Alignment Drivers",
          dep.var.labels = "Alignment Score (US vs China)",
          covariate.labels = c("Trade Dependency (China)", "Trade Dependency (US)", 
                              "Military Aid (China)", "Military Aid (US)", 
                              "Democracy Index", "SCS Dispute"),
          out = "regression_results.txt") # 這會同時存成一個文字檔

Regression Results: ASEAN Alignment Drivers
======================================================
                              Dependent variable:     
                         -----------------------------
                         Alignment Score (US vs China)
------------------------------------------------------
Trade Dependency (China)            -9.028*           
                                    (3.567)           
                                                      
Trade Dependency (US)              49.386**           
                                   (12.481)           
                                                      
Military Aid (China)               0.041***           
                                    (0.006)           
                                                      
Military Aid (US)                    0.363            
                                    (0.336)           
                                                      
Democracy Index                      0.134            
                                    (0.265)           
                                                      
SCS Dispute                          0.522            
                                    (0.257)           
                                                      
Mil_US0.15                         0.770***           
                                    (0.159)           
                                                      
Mil_US0.2                           -0.159            
                                    (0.283)           
                                                      
Mil_US0.2145                         0.423            
                                    (0.234)           
                                                      
Mil_US0.28                          -0.018            
                                    (0.261)           
                                                      
Mil_US0.3                           -0.196            
                                    (0.301)           
                                                      
Mil_US0.3214                        -0.358            
                                    (0.242)           
                                                      
Mil_US0.35                           0.146            
                                    (0.258)           
                                                      
Mil_US0.42                          -0.103            
                                    (0.417)           
                                                      
Mil_US10.2                         -0.626**           
                                    (0.187)           
                                                      
Mil_US102.12                       -0.739**           
                                    (0.183)           
                                                      
Mil_US102.5                         -0.263            
                                    (0.179)           
                                                      
Mil_US105.45                       -0.884**           
                                    (0.231)           
                                                      
Mil_US108.12                       -1.264***          
                                    (0.246)           
                                                      
Mil_US11.45                        -0.992**           
                                    (0.267)           
                                                      
Mil_US110.2145                      -0.809*           
                                    (0.339)           
                                                      
Mil_US110.45                       -1.152**           
                                    (0.284)           
                                                      
Mil_US112.12                        -0.656*           
                                    (0.284)           
                                                      
Mil_US112.1452                      -0.887            
                                    (0.544)           
                                                      
Mil_US115.4125                     -1.308**           
                                    (0.416)           
                                                      
Mil_US12.5112                      -1.085**           
                                    (0.333)           
                                                      
Mil_US120.15                       1.949***           
                                    (0.380)           
                                                      
Mil_US120.5214                      -0.522            
                                    (0.489)           
                                                      
Mil_US125.45                        1.403**           
                                    (0.368)           
                                                      
Mil_US128.05                        -0.659            
                                    (0.524)           
                                                      
Mil_US132.12                       4.829***           
                                    (0.717)           
                                                      
Mil_US138.45                       3.822***           
                                    (0.700)           
                                                      
Mil_US142.12                       4.428***           
                                    (0.786)           
                                                      
Mil_US144.55                        0.688*            
                                    (0.276)           
                                                      
Mil_US145.2412                      1.443**           
                                    (0.334)           
                                                      
Mil_US148.4215                       0.278            
                                    (0.183)           
                                                      
Mil_US15.2452                       -1.794*           
                                    (0.649)           
                                                      
Mil_US152.1452                      1.125**           
                                    (0.289)           
                                                      
Mil_US155.2                        -2.316***          
                                    (0.237)           
                                                      
Mil_US160.2145                       0.168            
                                    (0.178)           
                                                      
Mil_US162.45                       -2.534***          
                                    (0.253)           
                                                      
Mil_US168.45                       -1.316**           
                                    (0.307)           
                                                      
Mil_US170.8125                     -2.274***          
                                    (0.350)           
                                                      
Mil_US172.3                        -1.230***          
                                    (0.266)           
                                                      
Mil_US175.8                        -1.433**           
                                    (0.321)           
                                                      
Mil_US18.0125                       -0.619            
                                    (0.607)           
                                                      
Mil_US180.12                       -1.826***          
                                    (0.319)           
                                                      
Mil_US195.521                       0.806**           
                                    (0.241)           
                                                      
Mil_US20.1452                        0.274            
                                    (0.497)           
                                                      
Mil_US210.3341                      -0.042            
                                    (0.178)           
                                                      
Mil_US22.45                          0.194            
                                    (0.557)           
                                                      
Mil_US240.2144                     -1.082***          
                                    (0.232)           
                                                      
Mil_US40.85                         1.376**           
                                    (0.427)           
                                                      
Mil_US42.15                         1.349**           
                                    (0.429)           
                                                      
Mil_US43.12                         -0.455            
                                    (0.248)           
                                                      
Mil_US44.55                         -0.229            
                                    (0.263)           
                                                      
Mil_US44.85                         -0.598*           
                                    (0.218)           
                                                      
Mil_US45.12                         1.662**           
                                    (0.372)           
                                                      
Mil_US45.3125                       -0.433            
                                    (0.242)           
                                                      
Mil_US48.7125                       -0.481*           
                                    (0.193)           
                                                      
Mil_US5.2                            0.285            
                                    (0.492)           
                                                      
Mil_US50.1452                      -0.906***          
                                    (0.187)           
                                                      
Mil_US52.4145                       -0.245            
                                    (0.188)           
                                                      
Mil_US6.8                           -0.192            
                                    (0.373)           
                                                      
Mil_US78.45                        -1.682***          
                                    (0.223)           
                                                      
Mil_US8.42                          0.669*            
                                    (0.252)           
                                                      
Mil_US80.12                        -1.534***          
                                    (0.231)           
                                                      
Mil_US82.15                         -0.202            
                                    (0.218)           
                                                      
Mil_US84.45                        -0.764**           
                                    (0.213)           
                                                      
Mil_US85.45                        -0.787**           
                                    (0.218)           
                                                      
Mil_US85.6452                       -0.442            
                                    (0.218)           
                                                      
Mil_US86.12                        -0.850**           
                                    (0.194)           
                                                      
Mil_US88.4215                      -0.913**           
                                    (0.205)           
                                                      
Mil_US92.1124                      -1.382***          
                                    (0.189)           
                                                      
Mil_USn.a.                           0.487            
                                    (0.300)           
                                                      
Democracy                          2.012***           
                                    (0.363)           
                                                      
------------------------------------------------------
Observations                          88              
R2                                   0.998            
Adjusted R2                          0.958            
F Statistic                 27.492*** (df = 76; 4)    
======================================================
Note:                      *p<0.1; **p<0.05; ***p<0.01
# 1. 載入套件
library(plm)
library(stargazer)

# 2. 建立三個不同層次的模型
# 模型 1: 經濟與軍事基礎模型
m1 <- plm(Align ~ Trade_Chn_GDP + Trade_US_GDP + Mil_China + Mil_US, 
          data = p_df, model = "within")

# 模型 2: 加入民主程度
m2 <- plm(Align ~ Trade_Chn_GDP + Trade_US_GDP + Mil_China + Mil_US + Democracy, 
          data = p_df, model = "within")

# 模型 3: 全模型 (包含南海爭議 SCS)
m3 <- plm(Align ~ Trade_Chn_GDP + Trade_US_GDP + Mil_China + Mil_US + Democracy + SCS, 
          data = p_df, model = "within")

# 3. 輸出成像截圖那樣的專業表格
stargazer(m1, m2, m3, 
          type = "text", 
          title = "Table 1: Determinants of ASEAN UN Voting Alignment (2014-2024)",
          dep.var.labels = "Alignment Score (Pro-US)",
          covariate.labels = c("Trade w/ China (GDP%)", "Trade w/ US (GDP%)", 
                              "Military Aid (China)", "Military Aid (US)", 
                              "Democracy Index", "SCS Dispute (Yes=1)"),
          keep.stat = c("n", "rsq", "adj.rsq"), # 保留樣本數與 R2
          notes = "Standard errors in parentheses: *p<0.1; **p<0.05; ***p<0.01",
          column.labels = c("Economic/Mil", "Institutional", "Full Model"))

Table 1: Determinants of ASEAN UN Voting Alignment (2014-2024)
===================================================================================
                                           Dependent variable:                     
                      -------------------------------------------------------------
                                        Alignment Score (Pro-US)                   
                          Economic/Mil         Institutional         Full Model    
                               (1)                  (2)                 (3)        
-----------------------------------------------------------------------------------
Trade w/ China (GDP%)       -17.502*              -9.028*             -9.028*      
                             (8.506)              (3.567)             (3.567)      
                                                                                   
Trade w/ US (GDP%)           62.539               49.386**            49.386**     
                            (32.327)              (12.481)            (12.481)     
                                                                                   
Military Aid (China)          0.027               0.041***            0.041***     
                             (0.015)              (0.006)             (0.006)      
                                                                                   
Military Aid (US)            -0.836                0.363               0.363       
                             (0.679)              (0.336)             (0.336)      
                                                                                   
Democracy Index              -0.603                0.134               0.134       
                             (0.606)              (0.265)             (0.265)      
                                                                                   
SCS Dispute (Yes=1)          -0.150                0.522               0.522       
                             (0.598)              (0.257)             (0.257)      
                                                                                   
Mil_US0.15                    0.763               0.770***            0.770***     
                             (0.420)              (0.159)             (0.159)      
                                                                                   
Mil_US0.2                    -0.593                -0.159              -0.159      
                             (0.717)              (0.283)             (0.283)      
                                                                                   
Mil_US0.2145                 -0.418                0.423               0.423       
                             (0.469)              (0.234)             (0.234)      
                                                                                   
Mil_US0.28                   1.043*                -0.018              -0.018      
                             (0.467)              (0.261)             (0.261)      
                                                                                   
Mil_US0.3                    -0.782                -0.196              -0.196      
                             (0.743)              (0.301)             (0.301)      
                                                                                   
Mil_US0.3214                 -1.028                -0.358              -0.358      
                             (0.554)              (0.242)             (0.242)      
                                                                                   
Mil_US0.35                    0.966                0.146               0.146       
                             (0.557)              (0.258)             (0.258)      
                                                                                   
Mil_US0.42                   1.664*                -0.103              -0.103      
                             (0.712)              (0.417)             (0.417)      
                                                                                   
Mil_US10.2                   -0.560               -0.626**            -0.626**     
                             (0.492)              (0.187)             (0.187)      
                                                                                   
Mil_US102.12                 -0.502               -0.739**            -0.739**     
                             (0.470)              (0.183)             (0.183)      
                                                                                   
Mil_US102.5                  -0.232                -0.263              -0.263      
                             (0.473)              (0.179)             (0.179)      
                                                                                   
Mil_US105.45                 -0.156               -0.884**            -0.884**     
                             (0.503)              (0.231)             (0.231)      
                                                                                   
Mil_US108.12                 -0.684              -1.264***           -1.264***     
                             (0.587)              (0.246)             (0.246)      
                                                                                   
Mil_US11.45                  -0.973               -0.992**            -0.992**     
                             (0.705)              (0.267)             (0.267)      
                                                                                   
Mil_US110.2145               -0.860               -0.809*             -0.809*      
                             (0.894)              (0.339)             (0.339)      
                                                                                   
Mil_US110.45                 -0.448               -1.152**            -1.152**     
                             (0.669)              (0.284)             (0.284)      
                                                                                   
Mil_US112.12                 -0.688               -0.656*             -0.656*      
                             (0.750)              (0.284)             (0.284)      
                                                                                   
Mil_US112.1452               -1.016                -0.887              -0.887      
                             (1.433)              (0.544)             (0.544)      
                                                                                   
Mil_US115.4125               -1.048               -1.308**            -1.308**     
                             (1.089)              (0.416)             (0.416)      
                                                                                   
Mil_US12.5112                -1.394               -1.085**            -1.085**     
                             (0.867)              (0.333)             (0.333)      
                                                                                   
Mil_US120.15                  1.280               1.949***            1.949***     
                             (0.952)              (0.380)             (0.380)      
                                                                                   
Mil_US120.5214               -0.739                -0.522              -0.522      
                             (1.287)              (0.489)             (0.489)      
                                                                                   
Mil_US125.45                  0.772               1.403**             1.403**      
                             (0.924)              (0.368)             (0.368)      
                                                                                   
Mil_US128.05                 -0.856                -0.659              -0.659      
                             (1.379)              (0.524)             (0.524)      
                                                                                   
Mil_US132.12                  1.259               4.829***            4.829***     
                             (0.834)              (0.717)             (0.717)      
                                                                                   
Mil_US138.45                  0.271               3.822***            3.822***     
                             (0.749)              (0.700)             (0.700)      
                                                                                   
Mil_US142.12                  0.330               4.428***            4.428***     
                             (0.710)              (0.786)             (0.786)      
                                                                                   
Mil_US144.55                 -0.002                0.688*              0.688*      
                             (0.650)              (0.276)             (0.276)      
                                                                                   
Mil_US145.2412                0.234               1.443**             1.443**      
                             (0.669)              (0.334)             (0.334)      
                                                                                   
Mil_US148.4215                0.210                0.278               0.278       
                             (0.482)              (0.183)             (0.183)      
                                                                                   
Mil_US15.2452                -1.929               -1.794*             -1.794*      
                             (1.712)              (0.649)             (0.649)      
                                                                                   
Mil_US152.1452               -0.041               1.125**             1.125**      
                             (0.525)              (0.289)             (0.289)      
                                                                                   
Mil_US155.2                 -1.866**             -2.316***           -2.316***     
                             (0.588)              (0.237)             (0.237)      
                                                                                   
Mil_US160.2145                0.110                0.168               0.168       
                             (0.469)              (0.178)             (0.178)      
                                                                                   
Mil_US162.45                -1.807**             -2.534***           -2.534***     
                             (0.571)              (0.253)             (0.253)      
                                                                                   
Mil_US168.45                 -0.466               -1.316**            -1.316**     
                             (0.703)              (0.307)             (0.307)      
                                                                                   
Mil_US170.8125               -0.855              -2.274***           -2.274***     
                             (0.631)              (0.350)             (0.350)      
                                                                                   
Mil_US172.3                  -0.535              -1.230***           -1.230***     
                             (0.619)              (0.266)             (0.266)      
                                                                                   
Mil_US175.8                  -0.601               -1.433**            -1.433**     
                             (0.748)              (0.321)             (0.321)      
                                                                                   
Mil_US18.0125                -1.358                -0.619              -0.619      
                             (1.563)              (0.607)             (0.607)      
                                                                                   
Mil_US180.12                 -0.709              -1.826***           -1.826***     
                             (0.652)              (0.319)             (0.319)      
                                                                                   
Mil_US195.521                -0.090               0.806**             0.806**      
                             (0.472)              (0.241)             (0.241)      
                                                                                   
Mil_US20.1452                -0.738                0.274               0.274       
                             (1.219)              (0.497)             (0.497)      
                                                                                   
Mil_US210.3341               -0.028                -0.042              -0.042      
                             (0.471)              (0.178)             (0.178)      
                                                                                   
Mil_US22.45                  -0.791                0.194               0.194       
                             (1.392)              (0.557)             (0.557)      
                                                                                   
Mil_US240.2144               -0.418              -1.082***           -1.082***     
                             (0.526)              (0.232)             (0.232)      
                                                                                   
Mil_US40.85                  -0.092               1.376**             1.376**      
                             (0.884)              (0.427)             (0.427)      
                                                                                   
Mil_US42.15                  -0.040               1.349**             1.349**      
                             (0.919)              (0.429)             (0.429)      
                                                                                   
Mil_US43.12                  -0.416                -0.455              -0.455      
                             (0.655)              (0.248)             (0.248)      
                                                                                   
Mil_US44.55                  -0.238                -0.229              -0.229      
                             (0.694)              (0.263)             (0.263)      
                                                                                   
Mil_US44.85                  -0.586               -0.598*             -0.598*      
                             (0.576)              (0.218)             (0.218)      
                                                                                   
Mil_US45.12                   0.427               1.662**             1.662**      
                             (0.787)              (0.372)             (0.372)      
                                                                                   
Mil_US45.3125                -0.403                -0.433              -0.433      
                             (0.638)              (0.242)             (0.242)      
                                                                                   
Mil_US48.7125                -0.128               -0.481*             -0.481*      
                             (0.481)              (0.193)             (0.193)      
                                                                                   
Mil_US5.2                     0.962                0.285               0.285       
                             (1.258)              (0.492)             (0.492)      
                                                                                   
Mil_US50.1452                -0.601              -0.906***           -0.906***     
                             (0.472)              (0.187)             (0.187)      
                                                                                   
Mil_US52.4145                 0.045                -0.245              -0.245      
                             (0.476)              (0.188)             (0.188)      
                                                                                   
Mil_US6.8                     0.686                -0.192              -0.192      
                             (0.892)              (0.373)             (0.373)      
                                                                                   
Mil_US78.45                  -1.306*             -1.682***           -1.682***     
                             (0.560)              (0.223)             (0.223)      
                                                                                   
Mil_US8.42                   1.270*                0.669*              0.669*      
                             (0.599)              (0.252)             (0.252)      
                                                                                   
Mil_US80.12                  -0.927              -1.534***           -1.534***     
                             (0.536)              (0.231)             (0.231)      
                                                                                   
Mil_US82.15                  -0.792                -0.202              -0.202      
                             (0.503)              (0.218)             (0.218)      
                                                                                   
Mil_US84.45                 -1.323**              -0.764**            -0.764**     
                             (0.496)              (0.213)             (0.213)      
                                                                                   
Mil_US85.45                  -0.256               -0.787**            -0.787**     
                             (0.517)              (0.218)             (0.218)      
                                                                                   
Mil_US85.6452                -1.077*               -0.442              -0.442      
                             (0.489)              (0.218)             (0.218)      
                                                                                   
Mil_US86.12                  -1.184*              -0.850**            -0.850**     
                             (0.486)              (0.194)             (0.194)      
                                                                                   
Mil_US88.4215                -0.640               -0.913**            -0.913**     
                             (0.526)              (0.205)             (0.205)      
                                                                                   
Mil_US92.1124                -1.071*             -1.382***           -1.382***     
                             (0.478)              (0.189)             (0.189)      
                                                                                   
Mil_USn.a.                   -0.348                0.487               0.487       
                             (0.685)              (0.300)             (0.300)      
                                                                                   
Democracy                                         2.012***            2.012***     
                                                  (0.363)             (0.363)      
                                                                                   
-----------------------------------------------------------------------------------
Observations                   88                    88                  88        
R2                            0.983                0.998               0.998       
Adjusted R2                   0.711                0.958               0.958       
===================================================================================
Note:                                                   *p<0.1; **p<0.05; ***p<0.01
                        Standard errors in parentheses: *p<0.1; **p<0.05; ***p<0.01
library(ggplot2)

# 繪製四分位圖
ggplot(df, aes(x = factor(Year), y = Align, fill = factor(Year))) +
  geom_boxplot(alpha = 0.7) +
  geom_jitter(width = 0.2, alpha = 0.5) +
  theme_minimal() +
  labs(title = "ASEAN Alignment Score Distribution (2014-2024)",
       x = "Year", y = "Align") +
  theme(legend.position = "none")

# 1. 載入套件
library(tidyverse)
library(plm)
library(stargazer)

# 2. 讀取並「深度清洗」數據
df <- read.csv("aseananalysis.csv", stringsAsFactors = FALSE)

# --- 關鍵步驟:把所有 "n.a." 或文字轉為數字,並處理缺失值 ---
# 我們將核心變數強制轉換為數值,非數字的會變成 NA
df <- df %>%
  mutate(across(c(Align, Mil_US, Mil_China, Trade_Chn_GDP, Trade_US_GDP, Democracy), 
                ~ as.numeric(gsub("[^0-9.-]", "", .))))
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `across(...)`.
Caused by warning:
! NAs introduced by coercion
# 剔除包含 NA 的列,確保回歸模型不會因為 NA 而出錯
df_clean <- na.omit(df)

# 3. 建立面板數據格式
p_df <- pdata.frame(df_clean, index = c("Country", "Year"))

# 4. 重新跑模型 (確保所有變數都是數字)
res1 <- plm(Align ~ Mil_US + Mil_China, data = p_df, model = "within")
res2 <- plm(Align ~ Mil_US + Mil_China + Trade_US_GDP + Trade_Chn_GDP, data = p_df, model = "within")
res3 <- plm(Align ~ Mil_US + Mil_China + Trade_US_GDP + Trade_Chn_GDP + Democracy + SCS, data = p_df, model = "within")

# 5. 輸出表格 (先不加標籤,測試是否成功)
# 如果這次成功了,再慢慢把 labels 加回去
stargazer(res1, res2, res3, 
          type = "text", 
          title = "Regression Analysis of ASEAN Alignment",
          column.labels = c("Model 1", "Model 2", "Model 3"))

Regression Analysis of ASEAN Alignment
==================================================================================
                                      Dependent variable:                         
              --------------------------------------------------------------------
                                             Align                                
                     Model 1                Model 2                Model 3        
                       (1)                    (2)                    (3)          
----------------------------------------------------------------------------------
Mil_US               0.021***               0.017***               0.017***       
                     (0.004)                (0.004)                (0.004)        
                                                                                  
Mil_China            0.023***               0.018***               0.019***       
                     (0.005)                (0.005)                (0.005)        
                                                                                  
Trade_US_GDP                               32.511***              32.301***       
                                            (5.263)                (5.315)        
                                                                                  
Trade_Chn_GDP                              -16.359***             -16.028***      
                                            (3.281)                (3.383)        
                                                                                  
Democracy                                                           0.055         
                                                                   (0.123)        
                                                                                  
----------------------------------------------------------------------------------
Observations            82                     82                     82          
R2                    0.417                  0.624                  0.625         
Adjusted R2           0.344                  0.564                  0.559         
F Statistic   25.734*** (df = 2; 72) 28.995*** (df = 4; 70) 22.969*** (df = 5; 69)
==================================================================================
Note:                                                  *p<0.1; **p<0.05; ***p<0.01
res_ols <- plm(Align ~ Mil_US + Mil_China + Trade_US_GDP + Trade_Chn_GDP + Democracy + SCS, 
               data = p_df, model = "pooling")
summary(res_ols)
Pooling Model

Call:
plm(formula = Align ~ Mil_US + Mil_China + Trade_US_GDP + Trade_Chn_GDP + 
    Democracy + SCS, data = p_df, model = "pooling")

Unbalanced Panel: n = 8, T = 5-11, N = 82

Residuals:
    Min.  1st Qu.   Median  3rd Qu.     Max. 
-1.14119 -0.39514 -0.16007  0.47550  1.19221 

Coefficients:
                Estimate Std. Error t-value  Pr(>|t|)    
(Intercept)   -4.4416073  0.8493106 -5.2297 1.485e-06 ***
Mil_US         0.0066043  0.0016120  4.0969 0.0001048 ***
Mil_China      0.0054695  0.0024596  2.2238 0.0291745 *  
Trade_US_GDP   4.5735251  2.6539712  1.7233 0.0889592 .  
Trade_Chn_GDP  1.4378689  2.5605944  0.5615 0.5761056    
Democracy      0.0290944  0.0805729  0.3611 0.7190441    
SCS            0.0814135  0.1724115  0.4722 0.6381520    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Total Sum of Squares:    35.338
Residual Sum of Squares: 25.087
R-Squared:      0.2901
Adj. R-Squared: 0.2333
F-statistic: 5.10799 on 6 and 75 DF, p-value: 0.00019103

檢驗貢獻性

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
# 注意:plm 模型不能直接跑 vif,要轉成普通 lm 跑一下看相關性
vif_check <- lm(Align ~ Mil_US + Mil_China + Trade_US_GDP + Trade_Chn_GDP + Democracy + SCS, data = df_clean)
vif(vif_check)
       Mil_US     Mil_China  Trade_US_GDP Trade_Chn_GDP     Democracy 
     2.849601      2.794066      3.749078     10.458091      4.830572 
          SCS 
     1.812050 
# 使用 lag 函數
res_lag <- plm(Align ~ lag(Mil_US) + lag(Mil_China) + lag(Trade_Chn_GDP) + SCS, 
               data = p_df, model = "within")
summary(res_lag)
Oneway (individual) effect Within Model

Call:
plm(formula = Align ~ lag(Mil_US) + lag(Mil_China) + lag(Trade_Chn_GDP) + 
    SCS, data = p_df, model = "within")

Unbalanced Panel: n = 8, T = 4-10, N = 74

Residuals:
     Min.   1st Qu.    Median   3rd Qu.      Max. 
-0.852143 -0.395867 -0.038797  0.346545  1.076137 

Coefficients:
                    Estimate Std. Error t-value  Pr(>|t|)    
lag(Mil_US)        0.0279098  0.0059420  4.6970 1.475e-05 ***
lag(Mil_China)     0.0232148  0.0074638  3.1103  0.002806 ** 
lag(Trade_Chn_GDP) 2.2080885  2.8343964  0.7790  0.438877    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Total Sum of Squares:    28.444
Residual Sum of Squares: 15.567
R-Squared:      0.45271
Adj. R-Squared: 0.36584
F-statistic: 17.3708 on 3 and 63 DF, p-value: 2.4911e-08
library(plm)
library(stargazer)

# 1. 數據清洗與標準化 (Standardization)
# 將數值變數標準化,這能讓係數更易於比較,並優化模型擬合度
df_final <- df_clean %>%
  mutate(across(c(Mil_US, Mil_China, Trade_US_GDP, Trade_Chn_GDP, Democracy), 
                ~ as.numeric(scale(.))))

p_df_final <- pdata.frame(df_final, index = c("Country", "Year"))

# 2. 建立三個模型
# Model 1: 安全與軍事 (Realism perspective)
m1 <- plm(Align ~ Mil_US + Mil_China, data = p_df_final, model = "within")

# Model 2: 經濟與制度 (Liberalism perspective)
m2 <- plm(Align ~ Trade_US_GDP + Trade_Chn_GDP + Democracy, data = p_df_final, model = "within")

# Model 3: 綜合地緣模型 (包含 SCS)
m3 <- plm(Align ~ Mil_US + Mil_China + Trade_US_GDP + Trade_Chn_GDP + Democracy + SCS, 
          data = p_df_final, model = "within")

# 3. 輸出正式表格
stargazer(m1, m2, m3, 
          type = "text", 
          title = "Determinants of ASEAN UN Voting Alignment",
          dep.var.labels = "Alignment Score",
          covariate.labels = c("Military Aid (US)", "Military Aid (China)", 
                              "Trade (US % GDP)", "Trade (China % GDP)", 
                              "Democracy Index", "SCS Dispute (Yes=1)"),
          column.labels = c("Security", "Econ & Inst", "Full Model"),
          digits = 3,
          star.cutoffs = c(0.05, 0.01, 0.001), # 使用更嚴謹的顯著性標準
          notes = "Normalized coefficients. Standard errors in parentheses.")

Determinants of ASEAN UN Voting Alignment
=========================================================================================
                                             Dependent variable:                         
                     --------------------------------------------------------------------
                                               Alignment Score                           
                            Security                 Econ                   Inst         
                              (1)                    (2)                    (3)          
-----------------------------------------------------------------------------------------
Military Aid (US)           1.424***                                      1.158***       
                            (0.285)                                       (0.250)        
                                                                                         
Military Aid (China)        1.005***                                      0.815***       
                            (0.237)                                       (0.223)        
                                                                                         
Trade (US % GDP)                                   1.925***               1.514***       
                                                   (0.289)                (0.249)        
                                                                                         
Trade (China % GDP)                               -1.170***              -1.301***       
                                                   (0.331)                (0.275)        
                                                                                         
Democracy Index                                     -0.046                 0.096         
                                                   (0.252)                (0.216)        
                                                                                         
-----------------------------------------------------------------------------------------
Observations                   82                     82                     82          
R2                           0.417                  0.436                  0.625         
Adjusted R2                  0.344                  0.356                  0.559         
F Statistic          25.734*** (df = 2; 72) 18.284*** (df = 3; 71) 22.969*** (df = 5; 69)
=========================================================================================
Note:                                                       *p<0.05; **p<0.01; ***p<0.001
                                 Normalized coefficients. Standard errors in parentheses.
# 看看 res3 實際產出的變數名稱和數量
names(coef(res3))
[1] "Mil_US"        "Mil_China"     "Trade_US_GDP"  "Trade_Chn_GDP"
[5] "Democracy"    
# 1. 強制轉換資料型態,確保沒有文字干擾
df_clean <- df %>%
  mutate(across(c(Align, Mil_US, Mil_China, Trade_US_GDP, Trade_Chn_GDP, Democracy), 
                ~ as.numeric(as.character(.)))) %>%
  na.omit()

p_df <- pdata.frame(df_clean, index = c("Country", "Year"))

# 2. 重新跑模型
res1 <- plm(Align ~ Mil_US + Mil_China, data = p_df, model = "within")
res2 <- plm(Align ~ Mil_US + Mil_China + Trade_US_GDP + Trade_Chn_GDP + Democracy, data = p_df, model = "within")
res3 <- plm(Align ~ Mil_US + Mil_China + Trade_US_GDP + Trade_Chn_GDP + Democracy + SCS, data = p_df, model = "within")
# 載入必要的套件
library(ggplot2)
library(plm)
library(sjPlot)

Attaching package: 'sjPlot'
The following object is masked from 'package:ggplot2':

    set_theme
library(readr) 

# 1. 正確讀取資料 (請確認引號內的檔名跟你的資料夾裡的一模一樣)
asean_clean <- read_csv("aseananalysis.csv") 
Rows: 88 Columns: 9
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): Country, Mil_US
dbl (7): Year, Align, Mil_China, Trade_Chn_GDP, Trade_US_GDP, Democracy, SCS

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# 2. 數據清洗與轉換 (這步很重要,否則畫圖會出錯)
asean_clean$Align <- as.numeric(as.character(asean_clean$Align))
asean_clean$Trade_Chn_GDP <- as.numeric(as.character(asean_clean$Trade_Chn_GDP))
asean_clean$Trade_US_GDP <- as.numeric(as.character(asean_clean$Trade_US_GDP))
asean_clean$Mil_China <- as.numeric(as.character(asean_clean$Mil_China))
asean_clean$Mil_US <- as.numeric(as.character(asean_clean$Mil_US))
Warning: NAs introduced by coercion
asean_clean$Democracy <- as.numeric(as.character(asean_clean$Democracy))

# 3. 定義面板與模型
p_df_full <- pdata.frame(asean_clean, index = c("Country", "Year"))

model_full <- plm(Align ~ Trade_Chn_GDP + Trade_US_GDP + 
                  Mil_China + Mil_US + 
                  Democracy + SCS, 
                  data = p_df_full, 
                  model = "within")

# 4. 執行森林圖
plot_model(model_full, 
           show.values = TRUE, 
           value.offset = .3,
           title = "Determinants of ASEAN Strategic Alignment",
           axis.labels = c("Democracy Index", "Mil (US)", "Mil (China)", 
                           "Trade (US)", "Trade (China)")) +
  theme_minimal()
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
ℹ The deprecated feature was likely used in the sjPlot package.
  Please report the issue at <https://github.com/strengejacke/sjPlot/issues>.

# 看看對美貿易與對齊度的關係
ggplot(asean_clean, aes(x = Trade_US_GDP, y = Align)) +
  geom_point(aes(color = Country), size = 3) +
  geom_smooth(method = "lm", color = "black", linetype = "dashed") + # 加入回歸線
  theme_minimal() +
  labs(title = "Relationship between US Trade Dependence and Alignment",
       x = "Trade dependence (US) % of GDP",
       y = "Strategic Alignment Score")
`geom_smooth()` using formula = 'y ~ x'

# 1. 確保軍事數據是數值型態
asean_clean$Mil_US <- as.numeric(as.character(asean_clean$Mil_US))

# 2. 繪製散佈圖與回歸線
library(ggplot2)

ggplot(asean_clean, aes(x = Mil_US, y = Align)) +
  geom_point(aes(color = Country), size = 3, alpha = 0.7) + # alpha 增加透明度,避免點重疊
  geom_smooth(method = "lm", color = "darkblue", linetype = "solid") + # 改為深藍實線增加辨識度
  theme_minimal() +
  labs(title = "Relationship between US Military Cooperation and Alignment",
       subtitle = "ASEAN Strategic Alignment (2014-2023)",
       x = "US Military Expenditure/Cooperation (Value)",
       y = "Strategic Alignment Score") +
  theme(legend.position = "right")
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 6 rows containing non-finite outside the scale range
(`stat_smooth()`).
Warning: Removed 6 rows containing missing values or values outside the scale range
(`geom_point()`).

# 1. 確保數據格式正確
asean_clean$Trade_Chn_GDP <- as.numeric(as.character(asean_clean$Trade_Chn_GDP))

# 2. 繪製散佈圖(對中貿易 vs 對齊度)
library(ggplot2)

ggplot(asean_clean, aes(x = Trade_Chn_GDP, y = Align)) +
  geom_point(aes(color = Country), size = 3, alpha = 0.7) + 
  # 使用紅色系代表中國因素,並加入回歸線
  geom_smooth(method = "lm", color = "darkred", linetype = "dashed") + 
  theme_minimal() +
  labs(title = "Relationship between China Trade Dependence and Alignment",
       subtitle = "Negative correlation: Higher trade dependence leads to more pro-China alignment",
       x = "Trade dependence (China) % of GDP",
       y = "Strategic Alignment Score") +
  theme(legend.position = "right")
`geom_smooth()` using formula = 'y ~ x'

ggplot(asean_clean, aes(x = Trade_US_GDP, y = Trade_Chn_GDP)) +
  geom_path(aes(group = Country, color = Country), arrow = arrow(length = unit(0.2,"cm"))) + # 畫出路徑
  geom_point(aes(size = abs(Align))) + 
  theme_minimal() +
  labs(title = "Trade Entrapment: Dual Dependence Trajectory",
       x = "Dependence on US", y = "Dependence on China")

# 計算每年對齊分數的分散程度
polarization <- aggregate(Align ~ Year, data = asean_clean, FUN = sd)

ggplot(polarization, aes(x = Year, y = Align)) +
  geom_line(size = 1.2, color = "darkred") +
  geom_point(size = 3) +
  theme_minimal() +
  labs(title = "Diplomatic Polarization in ASEAN",
       subtitle = "Increasing SD means countries are being pushed to opposite poles",
       y = "Standard Deviation of Alignment", x = "Year")

# 計算每年東協整體的平均立場與標準差
asean_consensus <- aggregate(Align ~ Year, data = asean_clean, 
                             FUN = function(x) c(mean = mean(x), sd = sd(x)))
asean_consensus <- do.call(data.frame, asean_consensus)

# 畫出標準差隨時間的變化
library(ggplot2)
ggplot(asean_consensus, aes(x = Year, y = Align.sd)) +
  geom_line(size = 1.2, color = "darkgreen") +
  geom_point(size = 3) +
  theme_minimal() +
  labs(title = "Is ASEAN Consensus Collapsing?",
       subtitle = "Higher Standard Deviation means increasing internal polarization",
       x = "Year", y = "Standard Deviation of Alignment Score")

library(ggplot2)

ggplot(asean_clean, aes(x = as.factor(Year), y = Align)) +
  geom_boxplot(fill = "lightgray", outlier.shape = NA) + # 畫出整體的範圍
  geom_jitter(aes(color = Country), width = 0.2, size = 2) + # 點出每個國家的位置
  theme_minimal() +
  labs(title = "ASEAN Internal Alignment Consensus (2014-2023)",
       subtitle = "Wider vertical spread indicates declining consensus",
       x = "Year", y = "Strategic Alignment Score")

# 假設你已經根據 Democracy 指數將國家分類 (例如:High vs Low)
# 如果還沒分類,我們可以用中位數簡單切分:
asean_clean$Regime_Type <- ifelse(asean_clean$Democracy > median(asean_clean$Democracy, na.rm=T), 
                                 "Higher Democracy", "Lower Democracy")

ggplot(asean_clean, aes(x = Regime_Type, y = Align, fill = Regime_Type)) +
  geom_boxplot(alpha = 0.7) +
  geom_jitter(width = 0.1) +
  theme_minimal() +
  labs(title = "Impact of Regime Heterogeneity on Alignment",
       x = "Regime Type", y = "Strategic Alignment Score")

# 馬來西亞的數據
malaysia_data <- subset(asean_clean, Country == "Malaysia")

ggplot(malaysia_data, aes(x = Year)) +
  geom_line(aes(y = Align, color = "Strategic Alignment"), size = 1.2) +
  geom_line(aes(y = Trade_Chn_GDP / 10, color = "China Trade (scaled)"), linetype = "dashed") + # 縮小比例方便對比
  theme_minimal() +
  labs(title = "The Hedging Profile of Malaysia",
       subtitle = "Maintaining stability despite economic fluctuations",
       y = "Value", x = "Year")

# 越南的數據
malaysia_data <- subset(asean_clean, Country == "Vietnam")

ggplot(malaysia_data, aes(x = Year)) +
  geom_line(aes(y = Align, color = "Strategic Alignment"), size = 1.2) +
  geom_line(aes(y = Trade_Chn_GDP / 10, color = "China Trade (scaled)"), linetype = "dashed") + # 縮小比例方便對比
  theme_minimal() +
  labs(title = "The Hedging Profile of vietnam",
       subtitle = "Maintaining stability despite economic fluctuations",
       y = "Value", x = "Year")

# 篩選菲律賓數據
philippines_data <- subset(asean_clean, Country == "Philippines")

ggplot(philippines_data, aes(x = Year)) +
  # 畫出對齊分數(實線)
  geom_line(aes(y = Align, color = "Strategic Alignment"), size = 1.5) +
  geom_point(aes(y = Align, color = "Strategic Alignment"), size = 3) +
  # 畫出對中貿易(標準化後)
  geom_line(aes(y = (Trade_Chn_GDP - mean(Trade_Chn_GDP))/sd(Trade_Chn_GDP), 
                color = "China Trade (Standardized)"), linetype = "dotted", size = 1) +
  # 加入政治事件標記
  geom_vline(xintercept = 2016, linetype = "dashed", color = "darkred", alpha = 0.5) + # 杜特蒂上任
  geom_vline(xintercept = 2022, linetype = "dashed", color = "darkblue", alpha = 0.5) + # 小馬可仕上任
  annotate("text", x = 2018, y = -1, label = "Duterte Era\n(Pro-China Pivot)", size = 3) +
  annotate("text", x = 2023, y = 1, label = "Marcos Jr. Era\n(Back to US)", size = 3) +
  theme_minimal() +
  scale_color_manual(values = c("Strategic Alignment" = "#2980B9", 
                                "China Trade (Standardized)" = "#C0392B")) +
  labs(title = "The Philippine Pendulum: From Pivot to Realignment",
       subtitle = "Strategic alignment shifts driven by leadership changes",
       y = "Alignment Score", x = "Year")

library(ggplot2)

# 1. 篩選具代表性的四個國家
target_countries <- c("Malaysia", "Vietnam", "Philippines", "Cambodia")
comparison_df <- subset(asean_clean, Country %in% target_countries)

# 2. 繪製對比圖
ggplot(comparison_df, aes(x = Year, y = Align, color = Country, group = Country)) +
  # 加入背景中立線
  geom_hline(yintercept = 0, linetype = "dashed", color = "gray60", alpha = 0.5) +
  # 畫出平滑趨勢線與數據點
  geom_line(size = 1.2, alpha = 0.8) +
  geom_point(size = 3) +
  # 使用專業色調
  scale_color_manual(values = c(
    "Malaysia" = "#27ae60",    # 穩定的綠色(對沖/中道)
    "Vietnam" = "#2980b9",     # 深藍色(韌性/抗衡)
    "Philippines" = "#f39c12", # 橘色(劇烈擺盪)
    "Cambodia" = "#c0392b"     # 深紅色(高度依賴/下沉)
  )) +
  # 加上圖表資訊
  labs(
    title = "Strategic Trajectories of Selected ASEAN States (2014-2023)",
    subtitle = "Contrasting Hedging, Balancing, and Bandwagoning Behaviors",
    x = "Year", 
    y = "Alignment Score (Positive: Pro-US | Negative: Pro-China)",
    color = "Country Case"
  ) +
  theme_minimal(base_size = 14) +
  theme(legend.position = "bottom")