1

I`m trying to visualise data of the following form:

         date volaEUROSTOXX    volaSA volaKENYA25    volaNAM volaNIGERIA
1   10feb2012    0.29844454 0.1675901 0.007862087 0.12084170  0.10247617
2   17feb2012    0.31811157 0.2260064 0.157017220 0.33648935  0.22584127
3   24feb2012    0.30013672 0.1039974 0.083863921 0.11694768  0.16388161

To do so, I first converted the date (stored as a character in the original data frame) into a date-format. Which works just fine:

vola$date <- as.Date(vola$date)

str(vola$date)
 Date[1:543], format: "2012-02-10" "2012-02-17" "2012-02-24" "2012-03-02" "2012-03-09"

However, if I now try to graph my data by using the chart.TimeSeries command, I get the following:

chart.TimeSeries(volatility_annul_stringdate,lwd=2,auto.grid=F,ylab="Annualized Log Volatility",xlab="Time",
                 main="Log Volatility",lty=1,
                 legend.loc="topright")
Error in if (class(x) == "numeric") { : the condition has length > 1

I tried:

  • Converting my date variable (in the date format) further into a time series object:
vola$date <- ts(vola$date, frequency=52, start=c(2012,9)) #returned same error from above
  • Converting the whole data set using its-command:
vol.xts <- xts(vola, order.by= vola$date, unique = TRUE ) # which then returned:
order.by requires an appropriate time-based object 
#even though date is a time-series 

What am I doing wrong? I am rather new to RStudio.. I really want to use the chart.TimeSeries command. Can someone help me?

Thanks in advance!

My MRE:


library(PerformanceAnalytics)

vola <- structure(list(date_2 = c("2012-02-10", "2012-02-17", "2012-02-24", 
"2012-03-02"), volaEUROSTOXX = c(0.298444539308548, 0.318111568689346, 
0.300136715173721, 0.299697518348694), volaKENYA25 = c(0.00786208733916283, 
0.157017216086388, 0.0838639214634895, 0.152377054095268), volaNAM = c(0.120841704308987, 
0.336489349603653, 0.116947680711746, 0.157027021050453), volaNIGERIA = c(0.102476172149181, 
0.225841268897057, 0.163881614804268, 0.317349642515182), volaSA = c(0.167590111494064, 
0.226006388664246, 0.103997424244881, 0.193037077784538), date = structure(c(1328832000, 
1329436800, 1330041600, 1330646400), tzone = "UTC", class = c("POSIXct", 
"POSIXt"))), row.names = c(NA, -4L), class = c("tbl_df", "tbl", 
"data.frame"))

vola <- subset(vola, select = -c(date))
vola$date_2 <- as.Date(vola$date_2)
chart.TimeSeries(vola,lwd=2,auto.grid=F,ylab="Annualized Log Volatility",xlab="Time",
                 main="Log Volatility",lty=1,
                 legend.loc="topright")

#This returns the above mentioned error message. 

#Thus, I tried the following:
vola$date_2 <- ts(vola$date_2, frequency=52, start=c(2012,9))

chart.TimeSeries(vola,lwd=2,auto.grid=F,ylab="Annualized Log Volatility",xlab="Time",
                 main="Log Volatility",lty=1,
                 legend.loc="topright")

#Which returned a different error (as described above)

#And I tried:
vol.xts <- xts(vola, order.by= vola$date_2, unique = TRUE )
#This also returned an error message. 
#My intention was to then run:

#chart.TimeSeries(vol.xts,lwd=2,auto.grid=F,ylab="Annualized Log Volatility",xlab="Time",
                 main="Log Volatility",lty=1,
                 legend.loc="topright")
stefan
  • 90,330
  • 6
  • 25
  • 51
jugross
  • 11
  • 2
  • Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). If you want to share a snippet of your data then do so via `dput(NAME_OF_DATASET)` or `dput(head(NAME_OF_DATASET, 5))`. For more on that see the referenced link. – stefan Jul 14 '22 at 09:28
  • 1
    Thanks stefan! I hope my MRE satisfies at least the minimum criteria to reproduce my problem.. – jugross Jul 14 '22 at 10:21
  • Great. Almost perfect. Better: Include the package (especially if it is a non-standard one). Also you mixed up the code with comments or text. But for the first time ...(; – stefan Jul 14 '22 at 10:36

1 Answers1

1

The documentation of PerformanceAnalytics::chart.TimeSeries is a bit vague. The issue is that when passing a dataframe you have to set the dates as row.names. To this end I first converted your data (which is a tibble) to a data.frame. Afterwards I add the dates as rownames and drop the date column:

library(PerformanceAnalytics)

vola <- as.data.frame(vola)
vola <- subset(vola, select = -c(date))
row.names(vola) <- as.Date(vola$date_2)
vola$date_2 <- NULL

chart.TimeSeries(vola,
  lwd = 2, auto.grid = F, ylab = "Annualized Log Volatility", xlab = "Time",
  main = "Log Volatility", lty = 1,
  legend.loc = "topright"
)

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Amazing! Thanks! I`ll work on my documentation.. ;) – jugross Jul 14 '22 at 10:45
  • 1
    Haha. Did not mean your documenation. The documentation of `PerformanceAnalytics::chart.TimeSeries` is a bit vague. Made an edit to make this clear. (: – stefan Jul 14 '22 at 11:29