Packages to be installed:
library(pacman)
pacman::p_load(forecast)
p_load(tidyverse, lubridate, rio, pdfetch, tidyverse, readxl,dygraphs)
library(dygraphs)
# Secondary packages
pacman::p_load(tsibble, fable)
pacman::p_load(tsibbledata)
pacman::p_load(feasts, fpp3)
library(ggthemes)
library(tsbox) #convert xts to tsibble
library(TSstudio)
library(xts)
library(zoo)
library(ggthemes)
Import and preview the data
tungsten <- import("https://pubs.usgs.gov/sir/2012/5188/tables/tungsten.xlsx", skip = 2)
## New names:
## * `` -> ...2
head(tungsten); tail(tungsten)
## Year ...2 Price
## 1 1959 NA 13
## 2 1960 NA 19
## 3 1961 NA 17
## 4 1962 NA 12
## 5 1963 NA 9
## 6 1964 NA 15
## Year
## 54 Note: Annual average prices were derived from price changes reported in the following sources:
## 55 1959–66, tungsten ore (wolframite) in New York, “ordinary quality,” excluding duty, in American Metal Market.
## 56 1967–73, tungsten ore, domestic quote reflecting the U.S. Government's General Services Administration price, in American Metal Market's Metal Statistics 1972 and Metal Statistics 1974.
## 57 1974–76, tungsten ore, minimum 65% tungsten trioxide, European market, excluding duty, in U.S. Bureau of Mines Minerals Yearbook, converted from pounds sterling per metric ton unit as reported in Metal Bulletin.
## 58 1977–88, tungsten ore, minimum 65% tungsten trioxide, U.S. spot price, c.i.f., excluding duty, in Metals Week.
## 59 1989–2010, ammonium paratungstate, U.S. free market, in Metal Bulletin.
## ...2 Price
## 54 NA NA
## 55 NA NA
## 56 NA NA
## 57 NA NA
## 58 NA NA
## 59 NA NA
tungsten[1:2] = NULL #This step will remove the first and second columns because we need only one vector
str(tungsten)
## 'data.frame': 59 obs. of 1 variable:
## $ Price: num 13 19 17 12 9 15 23 38 43 43 ...
Convert the vector to the time-series variable
tungsten <- ts(tungsten, start=c(1959), end=c(2010), frequency=1)
Cpi Index:
- This helps to convert nominal prices of metals to real prices.
- There are lot of indexes available online . Based on the users and company’s needs
- The perfect index has to be picked.
cpi = pdfetch::pdfetch_FRED("CPIAUCSL") #download cpi all
head(cpi)
## CPIAUCSL
## 1947-01-31 21.48
## 1947-02-28 21.62
## 1947-03-31 22.00
## 1947-04-30 22.00
## 1947-05-31 21.95
## 1947-06-30 22.08
tail(cpi)
## CPIAUCSL
## 2021-04-30 266.832
## 2021-05-31 268.551
## 2021-06-30 270.981
## 2021-07-31 272.265
## 2021-08-31 273.012
## 2021-09-30 274.138
str(cpi)
## An 'xts' object on 1947-01-31/2021-09-30 containing:
## Data: num [1:897, 1] 21.5 21.6 22 22 21.9 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr "CPIAUCSL"
## Indexed by objects of class: [Date] TZ: UTC
## xts Attributes:
## NULL
cpi.ts = ts(cpi, start = c(1947,1), end = c(2021,7), frequency = 12)
Match the time-interval of tungsten and index.
- Both the nominal prices of metal and the index prices must start and end on the same time interval
cpi.annual = window(cpi.ts, start = c(1959,1), end = c(2010, 12), frequency = 1)