Cecere Danilo
25/03/2015
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
library(zoo)
#download gold price data
getSymbols("GOLDAMGBD228NLBM",src="FRED",return.class="zoo")
## As of 0.4-0, 'getSymbols' uses env=parent.frame() and
## auto.assign=TRUE by default.
##
## This behavior will be phased out in 0.5-0 when the call will
## default to use auto.assign=FALSE. getOption("getSymbols.env") and
## getOptions("getSymbols.auto.assign") are now checked for alternate defaults
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbol for more details
## [1] "GOLDAMGBD228NLBM"
#change the name in order to have a clearer data
gold <- GOLDAMGBD228NLBM
gold.data = data.frame("date"=index(gold),"gold"=as.numeric(coredata(gold)))
#download data about government reserves
getSymbols("FGMOGOQ027S",src="FRED",return.class="zoo")
## [1] "FGMOGOQ027S"
#since that this kind of data is in quarterly, it is useful change that in daily
daily.res <- data.frame("date"=seq(from=as.Date("1945-10-01"),to=as.Date("2015-01-01"),by="days"))
res = data.frame("date"=index(FGMOGOQ027S),"reserves"=as.numeric(coredata(FGMOGOQ027S)))
#download data about dollar currency
getSymbols("DTWEXM",src="FRED",return.class="zoo")
## [1] "DTWEXM"
#change the name
currency.dollar<-DTWEXM
dollar <- data.frame("date"=index(DTWEXM),"dollar"=as.numeric(coredata(DTWEXM)))
#download data about interest rates
getSymbols("DFF",src="FRED",return.class="zoo")
## [1] "DFF"
#change the name
int.rate<-DFF
#merging together
data.m <- data.frame()
gold.t <- ts(as.numeric(coredata(GOLDAMGBD228NLBM)),start=c(1968,64),freq=260.8913)
plot(gold.t, main="Gold Price throughout the years",xlab="Time",ylab="Gold Price")
Some variables, wich affect the Gold Price, must to be considered:
Government Reserves
Dollar Currency
It is useful to create a Multiple regression model in order to understand how much these variables are correlated.
\[ gold_{it} = \beta_0 + \beta_1 X_1{it} + \beta_2 X_2{it} + e_{it}. \] - Where:
+ $seats_{it}$ is the gold price
+ $X_1{it}$ is the value of the variable "government reserve"
+ $X_2{it}$ represents the variable "dollar currency"
#constructing the multiple regression model
new.data = merge(daily.res,res,by=c("date"),all.x=TRUE)
new.data = merge(new.data,gold.data,by=c("date"),all.x=TRUE)
new.data = merge(new.data,dollar,by=c("date"),all.x=TRUE)
new.data[-1] = na.locf(new.data[-1],na.rm=F)
colnames(new.data)
## [1] "date" "reserves" "gold" "dollar"
fit <- step(lm(gold ~ reserves + dollar, data=new.data))
## Start: AIC=174410.8
## gold ~ reserves + dollar
##
## Df Sum of Sq RSS AIC
## <none> 1328742829 174411
## - reserves 1 36216605 1364959434 174821
## - dollar 1 906247216 2234990045 182386
gold.reg <- lm(gold ~ reserves + dollar, data=new.data)
summary(gold.reg)
##
## Call:
## lm(formula = gold ~ reserves + dollar, data = new.data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -391.7 -217.2 -107.2 126.9 982.3
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2091.0165 15.5108 134.81 <2e-16 ***
## reserves -2.8596 0.1399 -20.45 <2e-16 ***
## dollar -16.6125 0.1624 -102.28 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 294.3 on 15337 degrees of freedom
## (9955 observations deleted due to missingness)
## Multiple R-squared: 0.4207, Adjusted R-squared: 0.4206
## F-statistic: 5568 on 2 and 15337 DF, p-value: < 2.2e-16
options(scipen=13)
summary(gold.reg)
##
## Call:
## lm(formula = gold ~ reserves + dollar, data = new.data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -391.7 -217.2 -107.2 126.9 982.3
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2091.0165 15.5108 134.81 <0.0000000000000002 ***
## reserves -2.8596 0.1399 -20.45 <0.0000000000000002 ***
## dollar -16.6125 0.1624 -102.28 <0.0000000000000002 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 294.3 on 15337 degrees of freedom
## (9955 observations deleted due to missingness)
## Multiple R-squared: 0.4207, Adjusted R-squared: 0.4206
## F-statistic: 5568 on 2 and 15337 DF, p-value: < 0.00000000000000022
library(knitr)
kable(summary(gold.reg)$coef, digits=2)
| Estimate | Std. Error | t value | Pr(>|t|) | |
|---|---|---|---|---|
| (Intercept) | 2091.02 | 15.51 | 134.81 | 0 |
| reserves | -2.86 | 0.14 | -20.45 | 0 |
| dollar | -16.61 | 0.16 | -102.28 | 0 |
library(forecast)
## Loading required package: timeDate
## This is forecast 5.8
##
##
## Attaching package: 'forecast'
##
## The following object is masked _by_ '.GlobalEnv':
##
## gold
gold.ts <- ts(gold, frequency=7)
gold.ts.auto <- auto.arima(gold.ts)
gold.ts.auto
## Series: gold.ts
## ARIMA(2,1,1)
##
## Coefficients:
## ar1 ar2 ma1
## -0.8574 -0.0618 0.8094
## s.e. 0.1560 0.0093 0.1560
##
## sigma^2 estimated as 62.45: log likelihood=-41523.14
## AIC=83054.27 AICc=83054.28 BIC=83083.93
gold.arima.f <- forecast(gold.ts.auto,h=100)
gold.arima.f
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 1752.000 1192.436 1182.309 1202.564 1176.948 1207.925
## 1752.143 1192.401 1178.418 1206.384 1171.016 1213.786
## 1752.286 1192.482 1175.615 1209.348 1166.687 1218.277
## 1752.429 1192.415 1172.987 1211.842 1162.703 1222.127
## 1752.571 1192.467 1170.852 1214.082 1159.410 1225.524
## 1752.714 1192.426 1168.774 1216.079 1156.253 1228.600
## 1752.857 1192.458 1166.968 1217.949 1153.474 1231.443
## 1753.000 1192.433 1165.201 1219.666 1150.786 1234.081
## 1753.143 1192.453 1163.604 1221.301 1148.333 1236.573
## 1753.286 1192.438 1162.044 1222.832 1145.954 1238.921
## 1753.429 1192.449 1160.596 1224.303 1143.734 1241.165
## 1753.571 1192.440 1159.183 1225.698 1141.578 1243.303
## 1753.714 1192.447 1157.849 1227.045 1139.534 1245.360
## 1753.857 1192.442 1156.549 1228.335 1137.548 1247.336
## 1754.000 1192.446 1155.306 1229.586 1135.646 1249.247
## 1754.143 1192.443 1154.094 1230.791 1133.794 1251.092
## 1754.286 1192.445 1152.927 1231.964 1132.007 1252.884
## 1754.429 1192.443 1151.787 1233.099 1130.265 1254.622
## 1754.571 1192.445 1150.683 1234.207 1128.576 1256.314
## 1754.714 1192.444 1149.604 1235.283 1126.926 1257.961
## 1754.857 1192.445 1148.554 1236.335 1125.320 1259.569
## 1755.000 1192.444 1147.527 1237.361 1123.749 1261.139
## 1755.143 1192.444 1146.524 1238.365 1122.215 1262.674
## 1755.286 1192.444 1145.541 1239.347 1120.713 1264.175
## 1755.429 1192.444 1144.580 1240.309 1119.242 1265.647
## 1755.571 1192.444 1143.637 1241.252 1117.799 1267.089
## 1755.714 1192.444 1142.712 1242.177 1116.385 1268.504
## 1755.857 1192.444 1141.803 1243.085 1114.996 1269.893
## 1756.000 1192.444 1140.911 1243.977 1113.631 1271.257
## 1756.143 1192.444 1140.034 1244.854 1112.290 1272.598
## 1756.286 1192.444 1139.172 1245.717 1110.971 1273.918
## 1756.429 1192.444 1138.323 1246.566 1109.673 1275.216
## 1756.571 1192.444 1137.487 1247.401 1108.394 1276.494
## 1756.714 1192.444 1136.664 1248.225 1107.135 1277.753
## 1756.857 1192.444 1135.853 1249.036 1105.895 1278.994
## 1757.000 1192.444 1135.053 1249.836 1104.672 1280.217
## 1757.143 1192.444 1134.264 1250.624 1103.465 1281.423
## 1757.286 1192.444 1133.486 1251.403 1102.275 1282.613
## 1757.429 1192.444 1132.718 1252.171 1101.100 1283.788
## 1757.571 1192.444 1131.959 1252.929 1099.941 1284.948
## 1757.714 1192.444 1131.210 1253.678 1098.795 1286.093
## 1757.857 1192.444 1130.471 1254.418 1097.664 1287.225
## 1758.000 1192.444 1129.739 1255.149 1096.546 1288.343
## 1758.143 1192.444 1129.017 1255.872 1095.440 1289.448
## 1758.286 1192.444 1128.302 1256.586 1094.347 1290.541
## 1758.429 1192.444 1127.595 1257.293 1093.267 1291.622
## 1758.571 1192.444 1126.896 1257.992 1092.197 1292.691
## 1758.714 1192.444 1126.205 1258.684 1091.139 1293.749
## 1758.857 1192.444 1125.520 1259.368 1090.092 1294.796
## 1759.000 1192.444 1124.842 1260.046 1089.056 1295.832
## 1759.143 1192.444 1124.171 1260.717 1088.030 1296.858
## 1759.286 1192.444 1123.507 1261.381 1087.014 1297.874
## 1759.429 1192.444 1122.849 1262.039 1086.008 1298.881
## 1759.571 1192.444 1122.197 1262.691 1085.011 1299.878
## 1759.714 1192.444 1121.551 1263.337 1084.023 1300.866
## 1759.857 1192.444 1120.911 1263.977 1083.044 1301.844
## 1760.000 1192.444 1120.277 1264.612 1082.074 1302.815
## 1760.143 1192.444 1119.648 1265.240 1081.112 1303.776
## 1760.286 1192.444 1119.025 1265.864 1080.159 1304.730
## 1760.429 1192.444 1118.406 1266.482 1079.213 1305.675
## 1760.571 1192.444 1117.793 1267.095 1078.275 1306.613
## 1760.714 1192.444 1117.185 1267.703 1077.345 1307.543
## 1760.857 1192.444 1116.582 1268.306 1076.423 1308.466
## 1761.000 1192.444 1115.983 1268.905 1075.508 1309.381
## 1761.143 1192.444 1115.390 1269.499 1074.599 1310.289
## 1761.286 1192.444 1114.800 1270.088 1073.698 1311.190
## 1761.429 1192.444 1114.216 1270.673 1072.804 1312.085
## 1761.571 1192.444 1113.635 1271.253 1071.916 1312.972
## 1761.714 1192.444 1113.059 1271.830 1071.035 1313.854
## 1761.857 1192.444 1112.487 1272.402 1070.160 1314.729
## 1762.000 1192.444 1111.919 1272.970 1069.291 1315.597
## 1762.143 1192.444 1111.355 1273.534 1068.428 1316.460
## 1762.286 1192.444 1110.794 1274.094 1067.572 1317.317
## 1762.429 1192.444 1110.238 1274.650 1066.721 1318.168
## 1762.571 1192.444 1109.686 1275.203 1065.876 1319.013
## 1762.714 1192.444 1109.137 1275.752 1065.036 1319.852
## 1762.857 1192.444 1108.591 1276.297 1064.202 1320.686
## 1763.000 1192.444 1108.049 1276.839 1063.374 1321.515
## 1763.143 1192.444 1107.511 1277.377 1062.550 1322.338
## 1763.286 1192.444 1106.976 1277.912 1061.732 1323.156
## 1763.429 1192.444 1106.444 1278.444 1060.919 1323.969
## 1763.571 1192.444 1105.916 1278.972 1060.111 1324.778
## 1763.714 1192.444 1105.391 1279.498 1059.308 1325.581
## 1763.857 1192.444 1104.869 1280.020 1058.509 1326.379
## 1764.000 1192.444 1104.350 1280.538 1057.716 1327.173
## 1764.143 1192.444 1103.834 1281.054 1056.927 1327.962
## 1764.286 1192.444 1103.321 1281.567 1056.142 1328.746
## 1764.429 1192.444 1102.811 1282.077 1055.362 1329.526
## 1764.571 1192.444 1102.304 1282.584 1054.587 1330.302
## 1764.714 1192.444 1101.800 1283.089 1053.816 1331.073
## 1764.857 1192.444 1101.298 1283.590 1053.049 1331.840
## 1765.000 1192.444 1100.800 1284.089 1052.286 1332.602
## 1765.143 1192.444 1100.304 1284.585 1051.527 1333.361
## 1765.286 1192.444 1099.810 1285.078 1050.773 1334.115
## 1765.429 1192.444 1099.320 1285.569 1050.022 1334.866
## 1765.571 1192.444 1098.831 1286.057 1049.276 1335.613
## 1765.714 1192.444 1098.346 1286.543 1048.533 1336.355
## 1765.857 1192.444 1097.863 1287.026 1047.794 1337.094
## 1766.000 1192.444 1097.382 1287.506 1047.059 1337.829
## 1766.143 1192.444 1096.904 1287.985 1046.328 1338.561
plot(gold.arima.f,include=100,xlab="Time",ylab="Gold Price")
Multiple regression shows a correlation between the variables
Still to do:
Arrange a forecats using alternative methods
Checking for Outliers and Structural Breaks