#1 Check your working directory
#2 Set your working directory to RStudio folder that you have created inside the ANLY 515 forlder
setwd(".")
#3 Download the 3 data set, and label them BTC, LTC, ETH. These data sets reperesnet daily pricesof three chryptocurrencies: Bitcoin, Litcoin, and Ethereum. Set the first column in each data set to the date format and the remaining columns in numerical format.
btc <- read_excel("btc.xlsx", col_types = c("date","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric"))
head(btc)
## # A tibble: 6 x 17
## date `txVolume(USD)` `adjustedTxVolu~ txCount
## <dttm> <dbl> <dbl> <dbl>
## 1 2009-01-03 00:00:00 NA NA NA
## 2 2009-01-04 00:00:00 NA NA NA
## 3 2009-01-05 00:00:00 NA NA NA
## 4 2009-01-06 00:00:00 NA NA NA
## 5 2009-01-07 00:00:00 NA NA NA
## 6 2009-01-08 00:00:00 NA NA NA
## # ... with 13 more variables: `marketcap(USD)` <dbl>, `price(USD)` <dbl>,
## # `exchangeVolume(USD)` <dbl>, `realizedCap(USD)` <dbl>,
## # generatedCoins <dbl>, fees <dbl>, activeAddresses <dbl>,
## # averageDifficulty <dbl>, paymentCount <dbl>,
## # `medianTxValue(USD)` <dbl>, medianFee <dbl>, blockSize <dbl>,
## # blockCount <dbl>
ltc <- read_excel("ltc.xlsx", col_types = c("date","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric"))
head(ltc)
## # A tibble: 6 x 17
## date `txVolume(USD)` `adjustedTxVolu~ txCount
## <dttm> <dbl> <dbl> <dbl>
## 1 2011-10-07 00:00:00 NA NA NA
## 2 2011-10-08 00:00:00 NA NA 0
## 3 2011-10-09 00:00:00 NA NA 0
## 4 2011-10-10 00:00:00 NA NA 0
## 5 2011-10-11 00:00:00 NA NA 0
## 6 2011-10-12 00:00:00 NA NA 0
## # ... with 13 more variables: `marketcap(USD)` <dbl>, `price(USD)` <dbl>,
## # `exchangeVolume(USD)` <dbl>, `realizedCap(USD)` <dbl>,
## # generatedCoins <dbl>, fees <dbl>, activeAddresses <dbl>,
## # averageDifficulty <dbl>, paymentCount <dbl>,
## # `medianTxValue(USD)` <dbl>, medianFee <dbl>, blockSize <dbl>,
## # blockCount <dbl>
eth <- read_excel("eth.xlsx", col_types = c("date","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric"))
head(eth)
## # A tibble: 6 x 16
## date `txVolume(USD)` `adjustedTxVolu~ txCount
## <dttm> <dbl> <dbl> <dbl>
## 1 2015-07-30 00:00:00 NA NA NA
## 2 2015-07-31 00:00:00 NA NA NA
## 3 2015-08-01 00:00:00 NA NA NA
## 4 2015-08-02 00:00:00 NA NA NA
## 5 2015-08-03 00:00:00 NA NA NA
## 6 2015-08-04 00:00:00 NA NA NA
## # ... with 12 more variables: `marketcap(USD)` <dbl>, `price(USD)` <dbl>,
## # `exchangeVolume(USD)` <dbl>, generatedCoins <dbl>, fees <dbl>,
## # activeAddresses <dbl>, `medianTxValue(USD)` <dbl>, medianFee <dbl>,
## # averageDifficulty <dbl>, paymentCount <dbl>, blockSize <dbl>,
## # blockCount <dbl>
#4 Create three new datasets that are subsets of the original datasets, to include dates only after “2015-08-31”. Name these datasets btcnew, ltcnew, ethnew,
btc <- na.omit(btc)
btcnew <- btc[btc$date>"2015-08-31",]
str(btcnew)
## Classes 'tbl_df', 'tbl' and 'data.frame': 1235 obs. of 17 variables:
## $ date : POSIXct, format: "2015-09-01" "2015-09-02" ...
## $ txVolume(USD) : num 2.90e+08 2.84e+08 2.47e+08 2.27e+08 1.85e+08 ...
## $ adjustedTxVolume(USD): num 1.26e+08 1.22e+08 1.14e+08 1.16e+08 8.93e+07 ...
## $ txCount : num 130256 149906 128536 103817 124071 ...
## $ marketcap(USD) : num 3.35e+09 3.32e+09 3.34e+09 3.31e+09 3.36e+09 ...
## $ price(USD) : num 230 228 229 227 230 ...
## $ exchangeVolume(USD) : num 20575200 18760400 17482000 20962400 20671400 ...
## $ realizedCap(USD) : num 4.07e+09 4.07e+09 4.06e+09 4.06e+09 4.06e+09 ...
## $ generatedCoins : num 3850 4000 3650 3900 3675 ...
## $ fees : num 25.3 53.9 28 25.1 27.8 ...
## $ activeAddresses : num 307795 290567 303103 265574 318521 ...
## $ averageDifficulty : num 5.43e+10 5.43e+10 5.43e+10 5.59e+10 5.70e+10 ...
## $ paymentCount : num 232345 1883742 339960 195974 276765 ...
## $ medianTxValue(USD) : num 47.7 110.4 45.9 44.8 19.6 ...
## $ medianFee : num 0.0001 0.000199 0.0001 0.0001 0.0001 ...
## $ blockSize : num 6.96e+07 1.21e+08 7.22e+07 5.55e+07 6.59e+07 ...
## $ blockCount : num 154 160 146 156 147 144 152 136 149 171 ...
## - attr(*, "na.action")= 'omit' Named int 1 2 3 4 5 6 7 8 9 10 ...
## ..- attr(*, "names")= chr "1" "2" "3" "4" ...
ltc <- na.omit(ltc)
ltcnew <- ltc[ltc$date>"2015-08-01",]
str(ltcnew)
## Classes 'tbl_df', 'tbl' and 'data.frame': 1265 obs. of 17 variables:
## $ date : POSIXct, format: "2015-08-02" "2015-08-03" ...
## $ txVolume(USD) : num 34902811 19112877 25961311 22250286 20001146 ...
## $ adjustedTxVolume(USD): num 13847683 7252869 9601632 10393075 9747818 ...
## $ txCount : num 3703 3850 3571 3188 3620 ...
## $ marketcap(USD) : num 1.69e+08 1.73e+08 1.73e+08 1.81e+08 1.81e+08 ...
## $ price(USD) : num 4.09 4.17 4.18 4.37 4.35 4.06 4.22 3.84 3.9 3.95 ...
## $ exchangeVolume(USD) : num 6950580 5494590 4323960 2565020 3879570 ...
## $ realizedCap(USD) : num 2.89e+08 2.89e+08 2.89e+08 2.89e+08 2.89e+08 ...
## $ generatedCoins : num 29950 30350 29250 28200 29500 ...
## $ fees : num 63.6 71.6 68.9 62.8 67.3 ...
## $ activeAddresses : num 12876 11251 11058 9564 10867 ...
## $ averageDifficulty : num 46370 46397 46397 46635 47712 ...
## $ paymentCount : num 11239 7231 9585 7600 8750 ...
## $ medianTxValue(USD) : num 108.4 73 140.6 65.5 109.4 ...
## $ medianFee : num 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 ...
## $ blockSize : num 2919355 3003790 2848820 2245739 2806986 ...
## $ blockCount : num 599 607 585 564 590 547 579 549 562 562 ...
## - attr(*, "na.action")= 'omit' Named int 1 2 3 4 5 6 7 8 9 10 ...
## ..- attr(*, "names")= chr "1" "2" "3" "4" ...
eth <- na.omit(eth)
ethnew <- eth[eth$date>"2015-08-01",]
str(ethnew)
## Classes 'tbl_df', 'tbl' and 'data.frame': 1264 obs. of 16 variables:
## $ date : POSIXct, format: "2015-08-07" "2015-08-08" ...
## $ txVolume(USD) : num 5684487 37899682 823606 1194050 1053130 ...
## $ adjustedTxVolume(USD): num 5684487 37899682 823606 1194050 1053130 ...
## $ txCount : num 2050 2881 1329 2037 4963 ...
## $ marketcap(USD) : num 1.70e+08 1.68e+08 4.27e+07 4.32e+07 4.27e+07 ...
## $ price(USD) : num 2.83 2.79 0.706 0.714 0.708 ...
## $ exchangeVolume(USD) : num 164329 674188 532170 405283 1463100 ...
## $ generatedCoins : num 27438 27943 27178 27817 28028 ...
## $ fees : num 37.3 68.1 14.1 31.2 11.3 ...
## $ activeAddresses : num 1086 826 732 1001 2342 ...
## $ medianTxValue(USD) : num 41.612 0.279 7.061 0.36 0.034 ...
## $ medianFee : num 0.00132 0.00124 0.00122 0.00123 0.00122 ...
## $ averageDifficulty : num 1.47e+12 1.59e+12 1.71e+12 1.84e+12 2.04e+12 ...
## $ paymentCount : num 1977 1973 1213 1451 3029 ...
## $ blockSize : num 3282693 3508878 3167541 3316883 3653834 ...
## $ blockCount : num 5189 5256 5123 5255 5280 ...
## - attr(*, "na.action")= 'omit' Named int 1 2 3 4 5 6 7 8
## ..- attr(*, "names")= chr "1" "2" "3" "4" ...
#5 Create 4 variables: date (represents dates of observation) , BTCPrice (price of Bitcoin), LTCPrice (Price of Litcoin), ETHPrice (Price of Ethereum)
date <- btcnew$date
BTCPrice <- btcnew$`price(USD)`
LTCPrice <- ltcnew$`price(USD)`
ETHPrice <- ethnew$`price(USD)`
#6 Check the format of these variable by using str() command
str(date)
## POSIXct[1:1235], format: "2015-09-01" "2015-09-02" "2015-09-03" "2015-09-04" "2015-09-05" ...
str(BTCPrice)
## num [1:1235] 230 228 229 227 230 ...
str(LTCPrice)
## num [1:1265] 4.09 4.17 4.18 4.37 4.35 4.06 4.22 3.84 3.9 3.95 ...
str(ETHPrice)
## num [1:1264] 2.83 2.79 0.706 0.714 0.708 ...
#7 Use date variable to create attribute “time” for BTCPrice, LTCPrice, and ETHPrice by using attr() function
attr(BTCPrice,"time") <- date
attr(LTCPrice,"time") <- date
attr(ETHPrice,"time") <- date
#8 Create three variables that represent daily returns on all three coins by using returnseries()(part of FRAPO package) function. Call these variables BTCRet, LTCRet, and ETHRet.
BTCRet <- returnseries(BTCPrice)
LTCRet <- returnseries(LTCPrice)
ETHRet <- returnseries(ETHPrice)
#9 Use date variable to create attribute “time” for BTCRet, LTCRet, and ETHRet
attr(BTCRet,"time") <- date
attr(LTCRet,"time") <- date
attr(ETHRet,"time") <- date
#10 Create a character variable CoinDates which extracts the dates from the BTCRet variable by using as.character(format(as.POSIXct(attr()),“%Y-%m-%d”)) function
CoinDates <- as.character(format(as.POSIXct(attr(BTCRet, "time")),"%Y-%m-%d"))
#11 Create time seriese called BTCReturns by using BTCRet varible and timeSeries() function
BTCReturns <- timeSeries(BTCRet, charvec = CoinDates)
#12 Rename the column of BTCReturns to “BTCReturns” by using colnames()
colnames(BTCReturns) <- "BTCReturns"
#13 Divide the output window into 2 by 2 matrix by using par() function #14 Generate a time series plot of Daily Returns of Bitcoin (requires fBasics library) #15 Generate a box plot of Returns of Bitcoin #16 Generate a acf and pacf of Bitcoin Returns.Make sure to omit missing values
par(mfrow=c(2,2))
seriesPlot(BTCReturns)
boxPlot(BTCReturns)
acf(BTCReturns,lag.max=20,na.action = na.omit,plot = TRUE)
pacf(BTCReturns, na.action = na.pass,plot = TRUE)
#17 Generate a QQ plot of Bitcoin. You may have to generate a variable that omits missing values. use na.omit() function
qqnormPlot(na.omit(BTCReturns))
#18 Generate acf and pcf of the absolute returns of Bitcoin returns
BTCabsreturn <- abs(BTCReturns)
acf(BTCabsreturn, na.action = na.pass, plot = TRUE)
pacf(BTCabsreturn, na.action = na.pass, plot = TRUE)
#19 Generate Volotility Clustering Plot of absolute daily returns of Bitcoin
plot(BTCabsreturn)
#Part II #20 Create new data set called PriceCoins that includes prices of all three coins by using cbind()
PriceCoins <- cbind(ETHPrice,LTCPrice,BTCPrice)
## Warning in cbind(ETHPrice, LTCPrice, BTCPrice): number of rows of result is
## not a multiple of vector length (arg 1)
#21 Check the names of the colums and the format of each of the variable
str(PriceCoins)
## num [1:1265, 1:3] 2.83 2.79 0.706 0.714 0.708 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:3] "ETHPrice" "LTCPrice" "BTCPrice"
#22 by using the package zoo create an element of class “zoo” labled PriceCoinsZoo, and includes Prices of all three Coints: BTCPrice“,”LTCPrice“, and”ETHPrice"
PriceCoinsZoo <- as.zoo(PriceCoins)[,c("ETHPrice","LTCPrice","BTCPrice")]
head(PriceCoinsZoo)
## ETHPrice LTCPrice BTCPrice
## 1 2.830000 4.09 230.26
## 2 2.790000 4.17 228.03
## 3 0.706136 4.18 229.32
## 4 0.713989 4.37 227.21
## 5 0.708087 4.35 230.20
## 6 1.060000 4.06 234.87
#23 Plot a time series graph of prices of all three coins
plot(PriceCoinsZoo)
#24) Create a variable that calculates daily returns (first difference of natural logs) on different coins.Call the variable ReturnCoins by using diff(log()) function
CoinsReturn <- diff(log(PriceCoinsZoo)) * 100
head(CoinsReturn)
## ETHPrice LTCPrice BTCPrice
## 2 -1.4235116 1.9371066 -0.9731906
## 3 -137.3989021 0.2395211 0.5641209
## 4 1.1059703 4.4451763 -0.9243708
## 5 -0.8300589 -0.4587164 1.3073796
## 6 40.3457220 -6.8992871 2.0083673
## 7 14.0581951 3.8652154 2.1315045
#25) Plot a time series graph of returns of all three coins
plot(CoinsReturn)
#26) Plot cross covariance functions between returns and between absolute returns of a) BTC and LTC, BTC and ETC, and ETC and LTC. You should have 6 graphs.
par(mfrow=c(3,2),mar=c(1, 2, 1, 1), oma=c(0, 0, 0, 0), mgp=c(2.4, 0.8, 0), las=1)
ccf(CoinsReturn[,3], CoinsReturn[,2])
ccf(CoinsReturn[,3], CoinsReturn[,1])
ccf(CoinsReturn[,1], CoinsReturn[,2])
ccf(CoinsReturn[,1], CoinsReturn[,3])
ccf(CoinsReturn[,2], CoinsReturn[,1])
ccf(CoinsReturn[,2], CoinsReturn[,3])
#27) Generate plots of rolling correlations between BTC<C, BTCÐ, and ETH<C
rollc <- function(x){ dim <- ncol(x)
rcor <- cor(x)[lower.tri(diag(dim), diag = FALSE)]
return(rcor)}
rcor <- rollapply(CoinsReturn, width = 250, rollc, align = "right", by.column = FALSE)
colnames(rcor) <- c("BITCOIN vs LITECOIN", "BITCOIN vs ETHEREUM", "LITECOIN vs ETHEREUM")
plot(rcor, main = "", xlab = "")