Gold Price Vs Oil Price

Setting directory

setwd("D:/DATA/BAYU/05. OTHERS/Gold Price")
setwd("Data")
getwd()
## [1] "D:/DATA/BAYU/05. OTHERS/Gold Price/Data"

Read data

gp <-read.csv("Data/Goldvsoil1.csv")
head(gp)
##       Month Gold.Price Crude.Oil.Price
## 1 01-Jun-09      $935          $69.78 
## 2 01-Jul-09      $939          $64.39 
## 3 01-Aug-09      $956          $71.25 
## 4 01-Sep-09      $996          $69.50 
## 5 01-Oct-09    $1,040          $75.85 
## 6 01-Nov-09    $1,176          $78.21

Modifikasi Data

library(lubridate)
## 
## Attaching package: 'lubridate'

## The following object is masked from 'package:base':
## 
##     date
library(stringr)
gp$Month <- dmy(gp$Month)
gp[,2]<-gsub(",","",gp[,2]) #Menghilangkan koma agar bisa di convert ke numeric
gp[,2]<-str_sub(gp$Gold.Price,3) #Menghilangkan $ agar bisa di convert ke numeric
gp[,2]<-as.numeric(gp[,2])
gp[,3]<-str_sub(gp$Crude.Oil.Price,3)#Menghilangkan $ agar bisa di convert ke numeric
gp[,3]<-as.numeric(gp[,3])
head(gp)
##        Month Gold.Price Crude.Oil.Price
## 1 2009-06-01        935           69.78
## 2 2009-07-01        939           64.39
## 3 2009-08-01        956           71.25
## 4 2009-09-01        996           69.50
## 5 2009-10-01       1040           75.85
## 6 2009-11-01       1176           78.21

Grafik

plot(gp$Gold.Price,gp$Crude.Oil.Price)

Model Regresi

model_gp<-lm(formula=Gold.Price~Crude.Oil.Price,data=gp)
summary(model_gp)
## 
## Call:
## lm(formula = Gold.Price ~ Crude.Oil.Price, data = gp)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -367.98 -116.95    1.32   86.25  432.17 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     971.1604    53.5534  18.134  < 2e-16 ***
## Crude.Oil.Price   4.7553     0.7019   6.775 5.03e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 165.3 on 119 degrees of freedom
## Multiple R-squared:  0.2784, Adjusted R-squared:  0.2723 
## F-statistic:  45.9 on 1 and 119 DF,  p-value: 5.034e-10
plot(lm(formula=Gold.Price~Crude.Oil.Price,data=gp))

abline(a=971.1604,b=4.7553)

library(ggplot2)
## Registered S3 methods overwritten by 'ggplot2':
##   method         from 
##   [.quosures     rlang
##   c.quosures     rlang
##   print.quosures rlang
ggplot(as.data.frame(gp),aes(x=gp$Gold.Price,y=gp$Crude.Oil.Price))

ggplot(gp,aes(x=gp$Gold.Price,y=gp$Crude.Oil.Price)) +
  geom_point(aes(size=gp$Gold.Price)) +
  geom_smooth()+ #+
  #geom_abline(slope = coef(mpg.hp)[2] ,intersect(coef(mpg.hp)[1]),color="gray", lty=2) 
  
  #chart furniture
  labs(
    title = "Gold Price Vs Crude Oil Price",
    #subtitle= "taken from mtcar data sets",
    x = "Gold Price",
    y= "Crude Oil Price"
      
  )
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Prediksi Gold Price

crude_oil_price_predict <- data.frame(Crude.Oil.Price=c(100:150))
gold_price_predict <- predict(model_gp, data.frame(Crude.Oil.Price=crude_oil_price_predict))
#gold_price_predict
df_predict<-data.frame(x=crude_oil_price_predict,y=gold_price_predict)
df_predict
##    Crude.Oil.Price        y
## 1              100 1446.689
## 2              101 1451.444
## 3              102 1456.199
## 4              103 1460.954
## 5              104 1465.710
## 6              105 1470.465
## 7              106 1475.220
## 8              107 1479.976
## 9              108 1484.731
## 10             109 1489.486
## 11             110 1494.241
## 12             111 1498.997
## 13             112 1503.752
## 14             113 1508.507
## 15             114 1513.262
## 16             115 1518.018
## 17             116 1522.773
## 18             117 1527.528
## 19             118 1532.284
## 20             119 1537.039
## 21             120 1541.794
## 22             121 1546.549
## 23             122 1551.305
## 24             123 1556.060
## 25             124 1560.815
## 26             125 1565.571
## 27             126 1570.326
## 28             127 1575.081
## 29             128 1579.836
## 30             129 1584.592
## 31             130 1589.347
## 32             131 1594.102
## 33             132 1598.858
## 34             133 1603.613
## 35             134 1608.368
## 36             135 1613.123
## 37             136 1617.879
## 38             137 1622.634
## 39             138 1627.389
## 40             139 1632.145
## 41             140 1636.900
## 42             141 1641.655
## 43             142 1646.410
## 44             143 1651.166
## 45             144 1655.921
## 46             145 1660.676
## 47             146 1665.431
## 48             147 1670.187
## 49             148 1674.942
## 50             149 1679.697
## 51             150 1684.453

Grafik Prediksi Gold Price

ggplot(df_predict,aes(x=df_predict$y,y=df_predict$Crude.Oil.Price)) +
  geom_point(aes(size=df_predict$Crude.Oil.Price)) +
  geom_smooth()+ #+
  #geom_abline(slope = coef(mpg.hp)[2] ,intersect(coef(mpg.hp)[1]),color="gray", lty=2) 
  
  #chart furniture
  labs(
    title = "Gold Price Prediction",
    #subtitle= "taken from mtcar data sets",
    x = "Gold Price",
    y= "Crude Oil Price"
      
  )
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'