r - 试图建立一个模型,将一个国家的 GDP 增长与石油和天然气的价格和产量联系起来
问题描述
我正在尝试建立一个模型,将 GDP 增长与石油和天然气价格联系起来。更具体地说,我选择了加拿大的 GDP 和 WCS、WTI 和布伦特原油。然而,足够重要的是,WCS 是一种已经与 WTI 高度相关的商品,所以也许我们可以利用它的波动性。抱歉,我对 R、编程和一般统计数据还很陌生,我一直在尝试同时学习所有内容,并且享受到目前为止的奋斗!
日期集:
> growth_rate <- function(x)(x/lag(x)-1)*100
> df$Ggdp <- growth_rate(df$GDP)
> df
GDP wti brent wcs prod Ggdp
May 2008 1625281 127.35 127.85 102.95 3178 NA
Jun 2008 1630757 139.96 138.40 122.00 3199 0.336926353
Jul 2008 1640312 124.17 124.10 104.33 3423 0.585924206
Aug 2008 1637072 115.55 113.49 94.81 3434 -0.197523398
Sep 2008 1635731 100.70 93.52 80.99 3304 -0.081914540
Oct 2008 1635860 68.10 60.00 46.31 3346 0.007886382
Nov 2008 1617639 55.21 47.72 37.93 3455 -1.113848373
Dec 2008 1596396 44.60 35.82 35.20 3393 -1.313210179
Jan 2009 1582918 41.73 44.17 31.93 3407 -0.844276733
Feb 2009 1576917 44.15 44.41 38.76 3487 -0.379109973
Mar 2009 1566404 49.64 46.13 43.31 3361 -0.666680618
Apr 2009 1561546 50.35 50.30 41.87 3115 -0.310137104
May 2009 1558576 66.31 64.98 57.86 3066 -0.190196126
Jun 2009 1558940 69.82 68.11 59.09 3223 0.023354652
Jul 2009 1563453 69.26 70.08 58.20 3422 0.289491578
Aug 2009 1565190 69.97 69.02 59.46 3333 0.111100238
Sep 2009 1575366 70.46 65.82 59.56 3276 0.650144711
Oct 2009 1577786 77.04 74.91 63.35 3343 0.153615096
Nov 2009 1587571 77.19 77.77 66.28 3480 0.620172824
Dec 2009 1591336 79.39 77.91 71.86 3322 0.237154748
Jan 2010 1601650 72.85 71.20 64.59 3238 0.648134649
Feb 2010 1606768 79.72 76.36 69.41 3318 0.319545469
Mar 2010 1614763 83.45 80.37 70.46 3349 0.497582725
Apr 2010 1613317 86.07 86.19 71.50 3388 -0.089548745
May 2010 1622249 74.00 73.00 61.57 3437 0.553641969
Jun 2010 1625907 75.59 74.94 62.93 3448 0.225489429
Jul 2010 1626913 78.85 77.50 64.30 3457 0.061873158
Aug 2010 1632145 71.93 75.51 51.17 3517 0.321590644
Sep 2010 1632415 79.95 80.77 63.62 3327 0.016542648
Oct 2010 1639515 81.45 82.47 65.08 3417 0.434938419
Nov 2010 1644860 84.12 86.02 69.66 3667 0.326011046
Dec 2010 1656532 91.38 93.23 71.38 3727 0.709604465
Jan 2011 1661194 90.99 98.97 71.69 3588 0.281431328
Feb 2011 1660783 97.10 112.27 72.97 3478 -0.024741240
Mar 2011 1666300 106.19 116.94 91.22 3579 0.332192707
Apr 2011 1668525 113.39 126.59 98.18 3549 0.133529376
May 2011 1664526 102.70 117.18 84.95 3217 -0.239672765
Jun 2011 1670263 95.30 111.71 74.92 3325 0.344662685
Jul 2011 1682272 95.68 115.93 77.70 3598 0.718988566
Aug 2011 1691568 88.81 116.48 77.06 3748 0.552586027
Sep 2011 1694178 78.93 105.42 68.70 3658 0.154294714
Oct 2011 1694510 93.19 108.43 81.69 3737 0.019596524
Nov 2011 1695778 100.36 111.22 88.76 3738 0.074829892
Dec 2011 1703622 98.83 108.09 82.68 3930 0.462560547
Jan 2012 1702562 98.46 110.26 71.48 3885 -0.062220375
Feb 2012 1698087 107.08 122.23 73.82 4056 -0.262839180
Mar 2012 1702254 103.03 123.41 79.02 3794 0.245393787
Apr 2012 1708046 104.89 118.66 90.02 3922 0.340254745
May 2012 1710720 86.52 103.86 67.03 3692 0.156553161
Jun 2012 1711557 85.04 94.17 57.96 3601 0.048926768
Jul 2012 1714908 88.08 105.93 65.31 3781 0.195786643
Aug 2012 1712687 96.47 113.93 80.47 3761 -0.129511321
Sep 2012 1712525 92.18 111.36 80.69 3678 -0.009458821
Oct 2012 1711819 86.23 109.89 56.74 3900 -0.041225676
Nov 2012 1720035 88.54 110.84 59.41 4008 0.479957285
Dec 2012 1719947 91.83 110.80 58.82 4194 -0.005116175
Jan 2013 1728397 97.65 115.55 65.99 4116 0.491294208
Feb 2013 1736155 92.03 112.20 66.55 4027 0.448855211
Mar 2013 1743151 97.24 108.46 82.88 4188 0.402959413
Apr 2013 1745096 93.22 101.53 76.21 3986 0.111579548
May 2013 1749713 91.93 100.43 72.96 3715 0.264569972
Jun 2013 1742837 96.36 102.49 82.31 3875 -0.392978734
Jul 2013 1752313 105.10 107.89 84.53 4035 0.543711202
Aug 2013 1760601 107.98 115.97 83.65 4210 0.472974862
Sep 2013 1766363 102.36 107.85 70.43 4071 0.327274607
Oct 2013 1776243 96.29 107.53 58.88 4064 0.559341426
Nov 2013 1778904 92.55 111.07 63.22 4247 0.149810583
Dec 2013 1770301 98.17 109.95 75.55 4333 -0.483612382
Jan 2014 1775276 97.55 108.16 78.99 4379 0.281025656
Feb 2014 1785782 102.88 108.98 77.34 4410 0.591795304
Mar 2014 1787878 101.57 105.95 81.57 4468 0.117371549
Apr 2014 1794046 100.07 108.63 80.99 4341 0.344989983
May 2014 1800584 103.40 109.21 83.71 4182 0.364427668
Jun 2014 1805647 106.07 111.03 84.37 4304 0.281186548
Jul 2014 1806714 98.23 104.94 76.47 4356 0.059092392
Aug 2014 1803892 97.86 101.12 79.21 4295 -0.156195170
Sep 2014 1815108 91.17 94.67 76.91 4333 0.621766713
Oct 2014 1822608 80.53 84.17 65.29 4515 0.413198553
Nov 2014 1821315 65.94 71.89 48.40 4522 -0.070942298
Dec 2014 1824784 53.45 55.27 37.27 4628 0.190466778
Jan 2015 1818224 47.79 47.52 35.24 4702 -0.359494603
Feb 2015 1817046 49.84 61.89 36.01 4743 -0.064788497
Mar 2015 1815416 47.72 53.69 35.05 4632 -0.089706039
Apr 2015 1813895 59.62 63.90 49.28 4300 -0.083782450
May 2015 1810301 60.25 63.16 52.45 3999 -0.198137158
Jun 2015 1817154 59.48 60.31 47.47 4204 0.378555831
Jul 2015 1824302 47.11 53.29 31.12 4618 0.393362368
Aug 2015 1828312 49.20 47.97 34.55 4759 0.219810097
Sep 2015 1820148 45.06 47.29 31.04 4299 -0.446532102
Oct 2015 1821259 46.60 48.00 31.39 4419 0.061038992
Nov 2015 1825481 40.43 43.73 27.65 4686 0.231817660
Dec 2015 1828773 37.13 36.61 23.79 4773 0.180336032
Jan 2016 1838454 33.66 33.14 18.37 4817 0.529371333
Feb 2016 1835456 32.74 35.92 21.15 4737 -0.163071798
Mar 2016 1830147 36.94 36.75 25.19 4657 -0.289246923
Apr 2016 1831421 45.98 45.64 32.57 4319 0.069611895
May 2016 1816590 49.10 49.26 37.35 3681 -0.809808340
Jun 2016 1825265 48.27 48.05 34.53 3982 0.477543089
Jul 2016 1837864 41.54 40.76 26.85 4607 0.690255935
Aug 2016 1840817 44.68 47.94 30.45 4745 0.160675654
Sep 2016 1849536 47.72 48.24 34.24 4749 0.473648385
Oct 2016 1849617 46.83 46.20 32.51 4813 0.004379477
Nov 2016 1856460 49.41 47.95 33.84 5135 0.369968485
Dec 2016 1863740 53.75 54.96 37.62 4918 0.392144188
Jan 2017 1875014 52.75 55.25 39.56 5120 0.604912702
Feb 2017 1878894 54.00 53.36 40.11 5140 0.206931788
Mar 2017 1886240 50.54 52.20 39.20 4910 0.390974690
Apr 2017 1893750 49.31 49.46 40.08 4500 0.398146577
May 2017 1900669 48.29 49.40 38.62 4633 0.365359736
Jun 2017 1905701 46.02 47.08 36.24 4686 0.264748886
Jul 2017 1910171 50.21 51.99 40.02 4963 0.234559356
Aug 2017 1907877 47.26 52.69 35.98 5117 -0.120093960
Sep 2017 1913738 51.67 57.02 40.42 4933 0.307200097
Oct 2017 1915700 54.36 61.35 41.23 4945 0.102521871
Nov 2017 1924355 57.40 63.53 40.30 5273 0.451793078
Dec 2017 1929676 60.46 66.73 34.42 5350 0.276508233
Jan 2018 1926871 64.82 67.78 35.73 5234 -0.145361190
Feb 2018 1934832 61.43 66.08 36.39 5395 0.413156875
Mar 2018 1940870 64.87 69.02 42.94 5434 0.312068438
Apr 2018 1939261 68.56 75.92 51.97 5068 -0.082900967
May 2018 1948011 66.98 76.45 42.04 5219 0.451202804
Jun 2018 1949676 74.13 77.44 53.15 5147 0.085471797
Jul 2018 1956404 69.88 74.16 39.76 5361 0.345082978
Aug 2018 1959899 69.84 76.94 42.55 5647 0.178644084
Sep 2018 1959168 73.16 82.72 38.35 5224 -0.037297840
Oct 2018 1964180 65.31 74.84 22.31 5540 0.255822880
Nov 2018 1957539 50.78 57.71 21.93 5637 -0.338105469
Dec 2018 1958332 45.15 50.57 29.66 5667 0.040510049
Jan 2019 1959350 53.84 62.46 44.29 5392 0.051983014
Feb 2019 1953947 57.21 65.03 45.22 5413 -0.275754715
Mar 2019 1969024 60.19 67.93 50.86 5498 0.771617654
Apr 2019 1976666 63.83 72.19 50.61 5542 0.388111064
May 2019 1981503 53.49 66.78 36.77 5367 0.244704973
Jun 2019 1986929 58.20 67.52 44.73 5504 0.273832540
Jul 2019 1984618 58.53 64.07 45.20 5500 -0.116310145
Aug 2019 1986990 55.07 61.04 43.51 5527 0.119519222
Sep 2019 1986480 54.09 60.99 41.45 5384 -0.025666964
Oct 2019 1985712 54.02 59.30 35.02 5455 -0.038661351
Nov 2019 1986851 58.12 64.50 36.10 5648 0.057359778
Dec 2019 1992292 61.14 67.77 38.57 5761 0.273850430
Jan 2020 1994590 51.58 57.77 30.73 5688 0.115344538
我们正在尝试建立的模型
**First the correlation table**
GDP wti brent wcs prod Ggdp
GDP 1.0000000 -0.4562972 -0.3658848 -0.5542391 0.9658432 NA
wti -0.4562972 1.0000000 0.9694464 0.9549166 -0.5033770 NA
brent -0.3658848 0.9694464 1.0000000 0.9049227 -0.4165634 NA
wcs -0.5542391 0.9549166 0.9049227 1.0000000 -0.6064784 NA
prod 0.9658432 -0.5033770 -0.4165634 -0.6064784 1.0000000 NA
Ggdp NA NA NA NA NA 1
然后是模型:
> m1=lm(ldf$GDP ~ ldf$brent +ldf$wcs, data=ldf)
> summary(m1)
Call:
lm(formula = ldf$GDP ~ ldf$brent + ldf$wcs, data = ldf)
Residuals:
Min 1Q Median 3Q Max
-0.098497 -0.041036 0.001208 0.041365 0.107232
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 14.66902 0.06892 212.851 < 2e-16 ***
ldf$brent 0.14656 0.03590 4.082 9.17e-05 ***
ldf$wcs -0.22944 0.03084 -7.440 4.07e-11 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.0512 on 97 degrees of freedom
Multiple R-squared: 0.4918, Adjusted R-squared: 0.4813
F-statistic: 46.93 on 2 and 97 DF, p-value: 5.531e-15
>
> m2=lm(ldf$GDP ~ ldf$wcs+lag(ldf$wcs,4)+ldf$wti,data=ldf)
> summary(m2)
Call:
lm(formula = ldf$GDP ~ ldf$wcs + lag(ldf$wcs, 4) + ldf$wti, data = ldf)
Residuals:
Min 1Q Median 3Q Max
-0.115679 -0.037848 0.007694 0.039826 0.104736
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 14.75017 0.08422 175.128 < 2e-16 ***
ldf$wcs -0.16780 0.03992 -4.204 6.08e-05 ***
lag(ldf$wcs, 4) -0.07080 0.01901 -3.725 0.000337 ***
ldf$wti 0.13933 0.05063 2.752 0.007131 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.04976 on 92 degrees of freedom
(4 observations deleted due to missingness)
Multiple R-squared: 0.5049, Adjusted R-squared: 0.4887
F-statistic: 31.27 on 3 and 92 DF, p-value: 5.019e-14
>
> m3=lm(ldf$prod ~ ldf$wcs +lag(ldf$wcs,4), data=ldf)
> summary(m3)
Call:
lm(formula = ldf$prod ~ ldf$wcs + lag(ldf$wcs, 4), data = ldf)
Residuals:
Min 1Q Median 3Q Max
-0.26768 -0.09780 0.01005 0.11000 0.20707
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 9.69410 0.14113 68.689 < 2e-16 ***
ldf$wcs -0.20093 0.04733 -4.246 5.16e-05 ***
lag(ldf$wcs, 4) -0.13319 0.04638 -2.872 0.00505 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.1236 on 93 degrees of freedom
(4 observations deleted due to missingness)
Multiple R-squared: 0.4903, Adjusted R-squared: 0.4793
F-statistic: 44.72 on 2 and 93 DF, p-value: 2.465e-14
>
> df$growgdp=growth_rate(df$GDP)
回归似乎有点好,但问题是解释变量和解释变量是相关的。变量本身是共线的(特别是 WCS 和 WTI)。
另外,几天前我进行了波动率分析。这里的想法是建立一个模型来跟踪 WCS 波动性的影响,并显示其对其价格和 WTI 相对价格的影响。我正在尝试更进一步。更具体地说,我想看看价格波动与 GDP 增长之间是否存在任何联系。
你能帮忙吗 ?
波动率分析
rbrent=na.omit(diff(log(x[,1])))
rwcss=na.omit(diff(log(x[,2])))
rspread=na.omit(diff(log(x[,3])))
vrbrent=rollapply(rbrent, width = 22, FUN = sd, fill=NA)*sqrt(252)
autoplot(vrbrent)
vrwcss=rollapply(rwcss,width = 22,FUN=sd,fill=NA)*sqrt(252)
autoplot(vrwcss)
vrspread=rollapply(rspread,width=22,FUN=sd,fill=NA)*sqrt(252)
autoplot(vrspread)
total=na.omit(cbind(vrbrent,vrwcss,vrspread))
autoplot(total)
max(total[,1:3])
info=rbind(apply(total,2,max),apply(total,2,min))
rownames(info)=c("max","min")
info
# Regression analysis
pairs(coredata(total))
correl1= lm(vrwcss ~vrbrent)
vcov(correl1)
correl1
plot(x=coredata(vrbrent), y=coredata(vrwcss), type="p")
abline(correl1$coefficients[1] ,correl1$coefficients[2])
解决方案
推荐阅读
- python - 计算一行中的单词组(计算某些标题超过一个单词的列)
- botframework - 机器人有没有办法知道用户连接的是哪个频道?
- sql-server - 当所有输入都正确时,Powershell Restore-SQLDatabase 不起作用
- r - 如何评估对 match.call() 的调用?
- javascript - 发出 JavaScript (JS) 使用循环从 firebase 检索数据
- sql - 基于与 CTE 连接的更新列值
- python - 替换文本中的空格(多行)
- asp.net - 允许 nuget 下载 Azure AppService 中缺少的包
- c# - 了解 Parallel.For 循环和局部变量
- ansible - 嵌套的 jinja2 dns 查询