herding with high low volatility

Hello everyone, I have to comapre herding between 5 traditionnal asset and 5 crypto currencies during high and low volatility days, I have the feeling that my code is repetitive, could you give me some feedback please?

library(dplyr) ## This package help us manipulate and analyse data easily
library(tibble) ## This is to improve the basic data.frame structure, making it more simple and consistent.
library(tidyr) ## This is to clean and organise messy data by reshaping it.
library(readr) ## This is to read and import data from text files efficiently like csv file
library(xts) ## This is for handling time-series data
library(zoo) ## Useful for working with irregular time series data.
library(PerformanceAnalytics) ## Analyse and visualise investment performance.

We can estimate herding considering a high and low volatility for the traditionnal and the crypto currencies markets.

CLASSIC$Date = as.POSIXct(CLASSIC$Date,format="%d/%m/%Y", tz = "")
CLASSIC.xts <- xts(CLASSIC[,-1], order.by=CLASSIC[,1]) # We convert CLASSIC into an xts object
CLASSIC.zoo <- as.zoo(CLASSIC.xts) # We convert CLASSIC.xts into a zoo object

CRYPTO$Date = as.POSIXct(CRYPTO$Date,format="%d/%m/%Y", tz = "")
CRYPTO.xts <- xts(CRYPTO[,-1], order.by=CRYPTO[,1]) # We convert CRYPTO into an xts object
CRYPTO.zoo <- as.zoo(CRYPTO.xts) # We convert CRYPTO.xts into a zoo object

#Function to create the cross-sectional absolute deviation (CSAD) and the average absolute market return (Rm)
exchange.herd = function(return)
Rm = rowMeans(return)
temp_dif =abs(return-Rm)
temp_sum = rowSums(temp_dif)
CSAD = temp_sum / ncol(return)
CSAD = cbind (CSAD, Rm)
return (CSAD)

returnCLASSIC = Return.calculate(CLASSIC.xts , method = "log") #We compute the log returns of CLASSIC assets
returnCRYPTO = Return.calculate(CRYPTO.xts , method = "log") #We compute the log returns of crypto assets


fCRYPTO = exchange.herd(returnCRYPTO) #We run the crypto object into the herding function
head (fCRYPTO)

CSAD.CRYPTO = fortify.zoo(fCRYPTO) # We convert the resulting object into a fortify dataframe (to simplify further calculations)
CSAD.CRYPTO$Rm2 = CSAD.CRYPTO$Rm^2 # We calculate Rm^2
CSAD.CRYPTO = CSAD.CRYPTO[-c(1),] # We remove the first row with NAs
head (CSAD.CRYPTO) # We display the first 6 rows
tail (CSAD.CRYPTO) #We display the last 6 rows

We reassign the columns as Y and Xs to look better in the regression model

y.CRYPTO = CSAD.CRYPTO$CSAD #dependent variable
x1.CRYPTO = abs (CSAD.CRYPTO$Rm) #independent variable 1
x2.CRYPTO = CSAD.CRYPTO$Rm2 #independent variable 2

linearMod.CRYPTO <- lm(y.CRYPTO ~x1.CRYPTO + x2.CRYPTO) # We build the linear regression model
print(linearMod.CRYPTO) #We print the linear regression model
summary(linearMod.CRYPTO) #We print the summary of the linear regression model

#We do the same process for CLASSIC assets
fCLASSIC = exchange.herd(returnCLASSIC)
head (fCLASSIC)

CSAD.CLASSIC = fortify.zoo(fCLASSIC)



Calculate standard historical volatility from yields

volatility_CLASSIC <- apply(returnCLASSIC, 1, sd)
volatility_CRYPTO <- apply(returnCRYPTO, 1, sd)

Calculate volatility threshold automatically

threshold_CLASSIC <- median(volatility_CLASSIC)
threshold_CRYPTO <- median(volatility_CRYPTO)

Create a binary variable to indicate days of high/low volatility

high_volatility_CLASSIC <- ifelse(volatility_CLASSIC > threshold_CLASSIC, 1, 0)
high_volatility_CRYPTO <- ifelse(volatility_CRYPTO > threshold_CRYPTO, 1, 0)

Add the binary variable high_volatility to the data frame

CSAD.CLASSIC$HighVolatility <- high_volatility_CLASSIC
CSAD.CRYPTO$HighVolatility <- high_volatility_CRYPTO

Fit of linear regression models for high/low volatility

linearMod_high_volatility_Stocks <- lm(CSAD ~ abs(Rm) + Rm2, data = subset(CSAD.CLASSIC, HighVolatility == 1))
linearMod_low_volatility_Stocks <- lm(CSAD ~ abs(Rm) + Rm2, data = subset(CSAD.CLASSIC, HighVolatility == 0))
linearMod_high_volatility_Crypto <- lm(CSAD ~ abs(Rm) + Rm2, data = subset(CSAD.CRYPTO, HighVolatility == 1))
linearMod_low_volatility_Crypto <- lm(CSAD ~ abs(Rm) + Rm2, data = subset(CSAD.CRYPTO, HighVolatility == 0))

Print model summaries for Stocks

print("high volatility Linear regression :")
print("Low volatility linear regression :")

Print model summaries for Cryptos

print("high volatility Linear regression :")
print("Low volatility linear regression :")

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.