{"id":124,"date":"2012-06-27T22:57:58","date_gmt":"2012-06-27T19:57:58","guid":{"rendered":"http:\/\/ekqvist.goeuropeinfo.com\/rbloggerqvist\/?p=124"},"modified":"2012-06-27T22:57:58","modified_gmt":"2012-06-27T19:57:58","slug":"r-and-vix-index","status":"publish","type":"post","link":"https:\/\/science.ekqvist.fi\/blogi\/r-and-data-visualization\/r-and-vix-index\/","title":{"rendered":"R and VIX index"},"content":{"rendered":"<h1>Using R as visualizing Volatility Index VIX<\/h1>\n<p>On September 22, 2003, the CBOE began disseminating price level information using revised methodology for the CBOE Volatility Index, VIX. A spreadsheet with more than 13 years of price history data using this new methodology is <a href=\"http:\/\/www.cboe.com\/publish\/ScheduledTask\/MktData\/datahouse\/vixcurrent.csv\" target=\"_blank\">now available<\/a>.<\/p>\n<h2>VIX?<\/h2>\n<p>In 1993, the Chicago Board Options Exchange\u00ae (CBOE\u00ae) introduced the CBOE Volatility Index\u00ae, VIX\u00ae, and it quickly became the benchmark for stock market volatility. It is widely followed and has been cited in hundreds of news articles in the Wall Street Journal, Barron&#8217;s and other leading financial publications. Since volatility often signifies financial turmoil, VIX is often referred to as the &#8220;investor fear gauge&#8221;.<br \/>\nSource: http:\/\/www.cboe.com\/micro\/vix\/faq.aspx#1<\/p>\n<p>So, I found this example from <a href=\"http:\/\/margintale.blogspot.fi\/2012\/04\/ggplot2-time-series-heatmaps.html\" target=\"_blank\">there<\/a>. I change it little bit. In this exampele it use whole time series you will found <a href=\"http:\/\/www.cboe.com\/publish\/ScheduledTask\/MktData\/datahouse\/vixcurrent.csv\" target=\"_blank\">there<\/a>. Now let&#8217;s look at closer this amazing approach&#8230;<br \/>\n<code><br \/>\n#required library<br \/>\nrequire(quantmod)<br \/>\nrequire(ggplot2)<br \/>\nrequire(reshape2)<br \/>\nrequire(plyr)<br \/>\nrequire(scales)<br \/>\n<\/code><code><br \/>\n# download data source<br \/>\ninput &lt;- read.table(\"http:\/\/www.cboe.com\/publish\/ScheduledTask\/MktData\/datahouse\/vixcurrent.csv\", header=TRUE, sep=\",\", na.strings=\"NA\", dec=\".\", strip.white=TRUE)<br \/>\n<\/code><code><br \/>\n# creating dataframe<br \/>\ndat&lt;-data.frame(date=index(VIX),VIX)<br \/>\n#look what we get<br \/>\nhead(dat)<br \/>\n<\/code><code><br \/>\n#date conversation<br \/>\ninput$date &lt;- as.Date(input$Date, \"%m\/%d\/%Y\")<br \/>\n<\/code><code><br \/>\n## below some example do to that<br \/>\n## read in date\/time info in format 'm\/d\/y h:m:s'<br \/>\n## dates &lt;- c(\"02\/27\/92\", \"02\/27\/92\", \"01\/14\/92\", \"02\/28\/92\", \"02\/01\/92\")<br \/>\n## times &lt;- c(\"23:03:20\", \"22:29:56\", \"01:03:30\", \"18:21:03\", \"16:56:26\")<br \/>\n## x &lt;- paste(dates)<br \/>\n## y &lt;- strptime(x, \"%m\/%d\/%y\")<br \/>\n<\/code><code><br \/>\n# extract year<br \/>\ninput$year&lt;-as.numeric(as.POSIXlt(input$date)$year+1900)<br \/>\n<\/code><code><br \/>\n# and\u00a0 the month too OK<br \/>\ninput$month&lt;-as.numeric(as.POSIXlt(input$date)$mon+1)<br \/>\n<\/code><code><br \/>\n# monts into right order and giving factor name<br \/>\ninput$monthf&lt;-factor(input$month,levels=as.character(1:12),labels=c(\"Jan\",\"Feb\",\"Mar\",\"Apr\",\"May\",\"Jun\",\"Jul\",\"Aug\",\"Sep\",\"Oct\",\"Nov\",\"Dec\"),ordered=TRUE)<br \/>\n<\/code><code><br \/>\n# week days<br \/>\ninput$weekday = as.POSIXlt(input$date)$wday<br \/>\n<\/code><code><br \/>\n# week day ordering and gibing factor name (weekdays)<br \/>\ninput$weekdayf&lt;-factor(input$weekday,levels=rev(1:7),labels=rev(c(\"Mon\",\"Tue\",\"Wed\",\"Thu\",\"Fri\",\"Sat\",\"Sun\")),ordered=TRUE)<br \/>\n<\/code><code><br \/>\n# yearmonth<br \/>\ninput$yearmonth&lt;-as.yearmon(input$date)<br \/>\ninput$yearmonthf&lt;-factor(input$yearmonth)<br \/>\n<\/code><code><br \/>\n# week of year by rows<br \/>\ninput$week &lt;- as.numeric(format(input$date,\"%W\"))<br \/>\n<\/code><code><br \/>\n# making week to start at number 1<br \/>\ninput&lt;-ddply(input,.(yearmonthf),transform,monthweek=1+week-min(week))<br \/>\n<\/code><code><br \/>\n# Ok, Now we are ready to plot<br \/>\n<\/code><code><br \/>\nP&lt;- ggplot(input, aes(monthweek, weekdayf, fill = VIX.Close)) +<br \/>\ngeom_tile(colour = \"white\") + facet_grid(year~monthf) + scale_fill_gradient(low=\"yellow\", high=\"red\") +<br \/>\nopts(title = \"Heatmap - view of future expected stock market volatility (risk)\") + xlab(\"Week of Month\") + ylab(\"\")<br \/>\n<\/code><code><br \/>\n#show the plot<br \/>\nP<br \/>\n<\/code><code><br \/>\n# save the picture<br \/>\n# example - output graph to jpeg file<br \/>\njpeg(\"heatmaps.jpg\")<br \/>\nP<br \/>\ndev.off()<br \/>\n<\/code><code><br \/>\n...and output will look like this:<\/code><\/p>\n<p><a href=\"http:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/heatmaps.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-126\" title=\"heatmaps\" src=\"http:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/heatmaps-300x300.jpg\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/heatmaps-300x300.jpg 300w, https:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/heatmaps-150x150.jpg 150w, https:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/heatmaps.jpg 480w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>That&#8217;s it,<\/p>\n<p>Cheers, Marko<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using R as visualizing Volatility Index VIX On September 22, 2003, the CBOE began disseminating price level information using revised methodology for the CBOE Volatility Index, VIX. A spreadsheet with more than 13 years of price history data using this new methodology is now available. VIX? In 1993, the Chicago Board Options Exchange\u00ae (CBOE\u00ae) introduced <a class=\"read-more-excerpt\" href=\"https:\/\/science.ekqvist.fi\/blogi\/r-and-data-visualization\/r-and-vix-index\/\">[&#8230;] Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"gallery","meta":[],"categories":[19,22],"tags":[48,53],"_links":{"self":[{"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/posts\/124"}],"collection":[{"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/comments?post=124"}],"version-history":[{"count":0,"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/posts\/124\/revisions"}],"wp:attachment":[{"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/media?parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/categories?post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/tags?post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}