4. 除了貿易開放程度可能與一國人均GDP有關,請找出其他可能會影響人均GDP的變數。

(1) 額外在WDI中再找2個或以上的影響變數,利用相關係數、圖形、ANOVA,或複迴歸等任意方法分析描述他們各自與人均GDP為正向還是負向關係 (此部分需整理到 ``期末0611第四題作答檔案.RMD” R Markdown檔案中並生成html檔上傳至tronclass) 。

install.packages("WDI")
## 程式套件 'WDI' 開啟成功,MD5 和檢查也透過
## 
## 下載的二進位程式套件在
##  C:\Users\ariel\AppData\Local\Temp\RtmpCINQNj\downloaded_packages 裡
install.packages("ggplot2")
## 程式套件 'ggplot2' 開啟成功,MD5 和檢查也透過
## 
## 下載的二進位程式套件在
##  C:\Users\ariel\AppData\Local\Temp\RtmpCINQNj\downloaded_packages 裡
install.packages("dplyr")
## 程式套件 'dplyr' 開啟成功,MD5 和檢查也透過
## 
## 下載的二進位程式套件在
##  C:\Users\ariel\AppData\Local\Temp\RtmpCINQNj\downloaded_packages 裡
library(WDI)
library(ggplot2)
library(dplyr)

countries <- c("USA", "CHN")
indicators <- c("NY.GDP.PCAP.CD",  # 人均GDP
                "SL.UEM.TOTL.ZS",  # 失业率
                "SP.DYN.CBRT.IN",  # 出生率
                "FP.CPI.TOTL")     # 通货膨胀率


data <- WDI(country = countries, indicator = indicators, start = 2013, end = 2022)


names(data) <- c("iso2c", "country", "year", "gdp_per_capita", "unemployment_rate", "birth_rate", "inflation_rate")


head(data)
##   iso2c country year gdp_per_capita unemployment_rate birth_rate inflation_rate
## 1 China      CN  CHN           2013          7020.386       4.60          13.03
## 2 China      CN  CHN           2014          7636.074       4.63          13.83
## 3 China      CN  CHN           2015          8016.446       4.65          11.99
## 4 China      CN  CHN           2016          8094.390       4.56          13.57
## 5 China      CN  CHN           2017          8817.046       4.47          12.64
## 6 China      CN  CHN           2018          9905.406       4.31          10.86
##         NA
## 1 111.1580
## 2 113.2941
## 3 114.9221
## 4 117.2206
## 5 119.0881
## 6 121.5589
data <- na.omit(data)


summary(data)
##     iso2c             country              year           gdp_per_capita
##  Length:20          Length:20          Length:20          Min.   :2013  
##  Class :character   Class :character   Class :character   1st Qu.:2015  
##  Mode  :character   Mode  :character   Mode  :character   Median :2018  
##                                                           Mean   :2018  
##                                                           3rd Qu.:2020  
##                                                           Max.   :2022  
##  unemployment_rate   birth_rate    inflation_rate        NA       
##  Min.   : 7020     Min.   :3.650   Min.   : 6.77   Min.   :106.8  
##  1st Qu.: 9633     1st Qu.:4.442   1st Qu.:10.89   1st Qu.:112.1  
##  Median :33006     Median :4.615   Median :11.70   Median :117.2  
##  Mean   :35818     Mean   :4.949   Mean   :11.32   Mean   :118.4  
##  3rd Qu.:60637     3rd Qu.:5.070   3rd Qu.:12.43   3rd Qu.:124.5  
##  Max.   :76330     Max.   :8.050   Max.   :13.83   Max.   :134.2
cor_matrix <- cor(data[, c("gdp_per_capita", "unemployment_rate", "birth_rate", "inflation_rate")])
print(cor_matrix)
##                   gdp_per_capita unemployment_rate  birth_rate inflation_rate
## gdp_per_capita         1.0000000         0.1551991 -0.20087213    -0.77952588
## unemployment_rate      0.1551991         1.0000000  0.21250353     0.13677080
## birth_rate            -0.2008721         0.2125035  1.00000000     0.03630817
## inflation_rate        -0.7795259         0.1367708  0.03630817     1.00000000
ggplot(data, aes(x = unemployment_rate, y = gdp_per_capita, color = country)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  labs(title = "Unemployment Rate vs GDP per Capita", x = "Unemployment Rate (%)", y = "GDP per Capita (current US$)")

ggplot(data, aes(x = birth_rate, y = gdp_per_capita, color = country)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  labs(title = "Birth Rate vs GDP per Capita", x = "Birth Rate (per 1,000 people)", y = "GDP per Capita (current US$)")

ggplot(data, aes(x = inflation_rate, y = gdp_per_capita, color = country)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  labs(title = "Inflation Rate vs GDP per Capita", x = "Inflation Rate (%)", y = "GDP per Capita (current US$)")

lm_unemployment <- lm(gdp_per_capita ~ unemployment_rate, data = data)
summary(lm_unemployment)
## 
## Call:
## lm(formula = gdp_per_capita ~ unemployment_rate, data = data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -4.791 -2.240  0.000  2.260  4.885 
## 
## Coefficients:
##                    Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)       2.017e+03  1.118e+00 1804.306   <2e-16 ***
## unemployment_rate 1.667e-05  2.501e-05    0.667    0.514    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.991 on 18 degrees of freedom
## Multiple R-squared:  0.02409,    Adjusted R-squared:  -0.03013 
## F-statistic: 0.4443 on 1 and 18 DF,  p-value: 0.5135
lm_birth_rate <- lm(gdp_per_capita ~ birth_rate, data = data)
summary(lm_birth_rate)
## 
## Call:
## lm(formula = gdp_per_capita ~ birth_rate, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.6860 -2.4075 -0.4072  2.7172  4.5165 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 2020.1380     3.1040  650.81   <2e-16 ***
## birth_rate    -0.5330     0.6127   -0.87    0.396    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.966 on 18 degrees of freedom
## Multiple R-squared:  0.04035,    Adjusted R-squared:  -0.01296 
## F-statistic: 0.7568 on 1 and 18 DF,  p-value: 0.3958
lm_inflation <- lm(gdp_per_capita ~ inflation_rate, data = data)
summary(lm_inflation)
## 
## Call:
## lm(formula = gdp_per_capita ~ inflation_rate, data = data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.162 -1.169 -0.230  1.172  4.108 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    2031.481      2.682  757.56  < 2e-16 ***
## inflation_rate   -1.235      0.234   -5.28 5.08e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.896 on 18 degrees of freedom
## Multiple R-squared:  0.6077, Adjusted R-squared:  0.5859 
## F-statistic: 27.88 on 1 and 18 DF,  p-value: 5.085e-05
lm_multiple <- lm(gdp_per_capita ~ unemployment_rate + birth_rate + inflation_rate, data = data)
summary(lm_multiple)
## 
## Call:
## lm(formula = gdp_per_capita ~ unemployment_rate + birth_rate + 
##     inflation_rate, data = data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -2.164 -1.094 -0.386  1.174  2.982 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        2.034e+03  2.858e+00 711.750  < 2e-16 ***
## unemployment_rate  3.408e-05  1.435e-05   2.374   0.0304 *  
## birth_rate        -6.335e-01  3.515e-01  -1.802   0.0904 .  
## inflation_rate    -1.290e+00  2.071e-01  -6.231  1.2e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.663 on 16 degrees of freedom
## Multiple R-squared:  0.7319, Adjusted R-squared:  0.6817 
## F-statistic: 14.56 on 3 and 16 DF,  p-value: 7.768e-05