1. Static panel data modeling:
\[ FPI_{it} = \alpha + \beta' X_{it} + \gamma' Z_t + \mu_i + \varepsilon_{it} \]
Trong đó:
\(FPI_{it}\): dòng vốn đầu tư gián tiếp nước ngoài vào quốc gia \(i\) tại thời điểm \(t\)
\(X_{it}\): các biến trong nước của quốc gia \(i\) như GDP, lạm phát, lãi suất, tỷ giá, độ mở thương mại, phát triển tài chính, chất lượng thể chế
\(Z_t\): các biến toàn cầu như VIX, lãi suất Mỹ, điều kiện tài chính quốc tế
\(\mu_i\): yếu tố đặc thù không quan sát được của từng quốc gia
\(\varepsilon_{it}\): sai số ngẫu nhiên
2. Dynamic panel data modeling: \[ FPI_{it} = \rho FPI_{i,t-1} + \beta' X_{it} + \gamma' Z_t + \mu_i + \varepsilon_{it} \]
Các biến cần thiết của các mô hình kinh tế lượng ở trên đều có thể lấy được từ một trong hai package của R là WDI hoặc imfapi. Lưu ý rằng nhiều indicators lấy từ WDI là dữ liệu được lấy từ IMF. Dưới đây là R codes để lấy, xử lí và chuẩn hóa dữ liệu cần thiết cho các mô hình nghiên cứu:
rm(list = ls())
library(WDI)
library(dplyr)
library(tidyr)
library(writexl)
library(stringr)
# =========================================
# 1) KHOẢNG THỜI GIAN
# =========================================
start_year <- 2000
end_year <- 2025
# =========================================
# 2) KHAI BÁO BỘ BIẾN
# =========================================
# Lưu ý:
# - Các biến FPI dưới đây là series/proxy từ World Bank.
# - Không phải là bản IMF BOP "chuẩn" như pipeline dùng imfapi.
indicators <- c(
# (1) Biến phụ thuộc / FPI proxies
fpi_total_net = "BN.KLT.PTXL.CD",
fpi_equity_net = "BX.PEF.TOTL.CD.WD",
fpi_bond_proxy = "DT.NFL.BOND.CD",
# (2) Nhóm vĩ mô trong nước
gdp_growth = "NY.GDP.MKTP.KD.ZG",
inflation_cpi = "FP.CPI.TOTL.ZG",
real_interest_rate = "FR.INR.RINR",
official_exrate = "PA.NUS.FCRF",
trade_open = "NE.TRD.GNFS.ZS",
# (3) Nhóm thị trường tài chính / phát triển tài chính
mkt_cap_gdp = "CM.MKT.LCAP.GD.ZS",
stocks_traded_gdp = "CM.MKT.TRAD.GD.ZS",
equity_index_growth = "CM.MKT.INDX.ZG",
credit_private_gdp = "FS.AST.PRVT.GD.ZS",
# Nhóm môi trường
renewable_energy_share = "EG.FEC.RNEW.ZS",
# Nhóm xã hội
tertiary_enrollment_gross = "SE.TER.ENRR",
# Nhóm quản trị
control_corruption = "CC.EST",
government_effectiveness = "GE.EST",
rule_of_law = "RL.EST",
regulatory_quality = "RQ.EST"
)
# =========================================
# 3) LẤY DỮ LIỆU CHO TẤT CẢ QUỐC GIA
# =========================================
wb_panel <- WDI(
country = "all",
indicator = indicators,
start = start_year,
end = end_year,
extra = TRUE,
cache = NULL
) %>%
rename(year = year) %>%
mutate(year = as.integer(year)) %>%
# bỏ các aggregate nếu chỉ muốn quốc gia thực
filter(region != "Aggregates") %>%
arrange(iso3c, year)
# xem nhanh
glimpse(wb_panel)
# =========================================
# 4) BIẾN QUỐC TẾ (CŨNG CHỈ DÙNG WDI)
# =========================================
# Vì chỉ dùng WDI, ta lấy các proxy quốc tế từ World Bank:
# - World GDP growth
# - US real interest rate
#
# Không lấy được VIX/GPR đúng nghĩa bằng WDI.
global_indicators <- c(
world_gdp_growth = "NY.GDP.MKTP.KD.ZG",
us_real_interest = "FR.INR.RINR"
)
world_global <- WDI(
country = "1W", # World aggregate
indicator = c(world_gdp_growth = "NY.GDP.MKTP.KD.ZG"),
start = start_year,
end = end_year,
extra = FALSE
) %>%
transmute(
year = as.integer(year),
world_gdp_growth
)
us_global <- WDI(
country = "US",
indicator = c(us_real_interest = "FR.INR.RINR"),
start = start_year,
end = end_year,
extra = FALSE
) %>%
transmute(
year = as.integer(year),
us_real_interest
)
global_factors <- full_join(world_global, us_global, by = "year") %>%
arrange(year)
# =========================================
# 5) GHÉP BIẾN QUỐC TẾ VÀO PANEL
# =========================================
wb_panel_full <- wb_panel %>%
left_join(global_factors, by = "year") %>%
arrange(iso3c, year)
# =========================================
# 6) TẠO DẠNG LONG THEO NHÓM BIẾN
# =========================================
var_groups <- tibble::tribble(
~variable, ~group,
"fpi_total_net", "dependent",
"fpi_equity_net", "dependent",
"fpi_bond_proxy", "dependent",
"gdp_growth", "domestic_macro",
"inflation_cpi", "domestic_macro",
"real_interest_rate", "domestic_macro",
"official_exrate", "domestic_macro",
"trade_open", "domestic_macro",
"mkt_cap_gdp", "financial_market",
"stocks_traded_gdp", "financial_market",
"equity_index_growth", "financial_market",
"credit_private_gdp", "financial_market",
"renewable_energy_share", "environment",
"tertiary_enrollment_gross", "social",
"control_corruption", "governance",
"government_effectiveness", "governance",
"rule_of_law", "governance",
"regulatory_quality", "governance",
"world_gdp_growth", "global",
"us_real_interest", "global"
)
# =========================================
# 8) TẠO THÊM BIẾN TỶ LỆ FPI / GDP NẾU MUỐN
# =========================================
# Cần thêm GDP current US$ để chia
gdp_current <- WDI(
country = "all",
indicator = c(gdp_current_usd = "NY.GDP.MKTP.CD"),
start = start_year,
end = end_year,
extra = TRUE
) %>%
filter(region != "Aggregates") %>%
select(iso3c, year, gdp_current_usd)
wb_panel_full <- wb_panel_full %>%
left_join(gdp_current, by = c("iso3c", "year")) %>%
mutate(
fpi_total_gdp = 100 * fpi_total_net / gdp_current_usd,
fpi_equity_gdp = 100 * fpi_equity_net / gdp_current_usd,
fpi_bond_gdp = 100 * fpi_bond_proxy / gdp_current_usd
)
# Lưu dữ liệu:
write_xlsx(wb_panel_full, "wb_panel_full_fromWDI.xlsx")Với dữ liệu đã có chúng ta có thể xem qua một số kết quả từ chạy Pooled Panel Data Regression. Kết quả như dưới đây:
readxl::read_excel("C:\\Users\\DUNG\\Documents\\wb_panel_full_fromWDI.xlsx") -> wb_panel_full
asia_iso3c <- c(
"AFG","ARM","AZE","BHR","BGD","BTN","BRN","KHM","CHN","CYP",
"GEO","IND","IDN","IRN","IRQ","ISR","JPN","JOR","KAZ","KWT",
"KGZ","LAO","LBN","MYS","MDV","MNG","MMR","NPL","PRK","OMN",
"PAK","PSE","PHL","QAT","SAU","SGP","KOR","LKA","SYR","TJK",
"THA","TLS","TUR","TKM","ARE","UZB","VNM","YEM"
)
wb_panel_full %>%
filter(iso3c %in% asia_iso3c) %>%
lm(fpi_total_gdp ~ gdp_growth + inflation_cpi + trade_open + real_interest_rate +
government_effectiveness + control_corruption + regulatory_quality, data = .) -> model1
wb_panel_full %>%
filter(iso3c %in% asia_iso3c) %>%
lm(fpi_equity_gdp ~ gdp_growth + inflation_cpi + trade_open + real_interest_rate +
government_effectiveness + control_corruption + regulatory_quality, data = .) -> model2
wb_panel_full %>%
filter(iso3c %in% asia_iso3c) %>%
lm(fpi_bond_gdp ~ gdp_growth + inflation_cpi + trade_open + real_interest_rate +
government_effectiveness + control_corruption + regulatory_quality, data = .) -> model3
library(stargazer)
stargazer(model1,
model2,
model3,
title = "Table 1: Pooled Panel Data Regression Results",
column.labels = c("", "", ""),
type = "text",
align = TRUE)