RiskTech
RiskTech

Reputation: 1165

Using a Function to plot in R

I have a dataframe called EWMA_SD252 3561 obs. of 102 variables (daily volatilities of 100 stocks since 2000), here is a sample :

     Data       IBOV     ABEV3    AEDU3    ALLL3
3000 2012-02-09 16.88756 15.00696 33.46089 25.04788
3001 2012-02-10 18.72925 14.55346 32.72209 24.93913
3002 2012-02-13 20.87183 15.25370 31.91537 24.28962
3003 2012-02-14 20.60184 14.86653 31.04094 28.18687
3004 2012-02-15 20.07140 14.56653 37.45965 33.47379
3005 2012-02-16 19.99611 16.80995 37.36497 32.46208
3006 2012-02-17 19.39035 17.31730 38.85145 31.50452

What i am trying to do is using a single command, to subset a interval from a particular stock using dates references and also plot a chart for the same interval, so far i was able to do the subset part but now i am stuck on plotting a chart, here is what i code so far :

Getting the Date Interval and the stock name :

    datas = function(x,y,z){
    intervalo_datas(as.Date(x,"%d/%m/%Y"),as.Date(y,"%d/%m/%Y"),z)
    } 

Subsetting the Data :

 intervalo_datas <- function(x,y,z){
 cbind(as.data.frame(EWMA_SD252[,1]),as.data.frame(EWMA_SD252[,z]))[EWMA_SD252$Data >= x    & EWMA_SD252$Data <= y,]
 } 

Now i am stuck, is it possible using a function to get ABEV3 data.frame and plot a chart using dates in X and volatility in y, using just the command bellow ?

ABEV3 = datas("09/02/2012","17/02/2012","ABEV3")

Upvotes: 1

Views: 104

Answers (3)

Carlos Cinelli
Carlos Cinelli

Reputation: 11617

You could use ggplot2 and reshape2 to make a function that automatically plots an arbitrary quantity of stocks:

plot_stocks <- function(data, date1, date2, stocks){
  require(ggplot2)
  require(reshape2)
  date1 <- as.Date(date1, "%d/%m/%Y") 
  date2 <- as.Date(date2, "%d/%m/%Y") 
  data <- data[data$Data > date1 & data$Data < date2,c("Data", stocks)] 
  data <- melt(data, id="Data")
  names(data) <- c("Data", "Stock", "Value")
  ggplot(data, aes(Data, Value, color=Stock)) + geom_line() 
}

Plotting one stock "ABEV3":

plot_stocks(EWMA_SD252, "09/02/2012", "17/02/2012", "ABEV3")

enter image description here

Plotting three stocks:

plot_stocks(EWMA_SD252, "09/02/2012", "17/02/2012", c("IBOV", "ABEV3", "AEDU3"))

enter image description here

You can further personalize your function adding other geoms, like geom_smooth etc.

Upvotes: 1

agstudy
agstudy

Reputation: 121626

I think you should use xts package. It is suitable :

  • manipluating time series specially financial time series
  • subsetting time series
  • plotting time series

So I would create an xts object using your data. Then I wrap the subset/plot in a single function like what you tried to do.

library(xts)
dat_ts <- xts(dat[,-1],as.Date(dat$Data))
plot_data <-
  function(start,end,stock)
    plot(dat_ts[paste(start,end,sep='/'),stock])

You can call it like this :

plot_data('2012-02-09','2012-02-14','IBOV')

enter image description here

Upvotes: 3

Gregor Thomas
Gregor Thomas

Reputation: 146249

(I'm assuming your EWMA_SD252 data.frame's Data column is already Date class. Convert it if it's not already.)

It looks like your trying to plot a particular column of your data.frame for a given date interval. It will be much easier for others to read your code (and you too in 6 months!) if you use variable names that are more descriptive than x, y, and z, e.g. date0, date1, column.

Let's rewrite your function. If EWMA_SD252 is already a data.frame, then you don't need to cbind individual columns of it into a data.frame. Giving a data argument makes things more flexible as well. All your datas function does is convert to Dates and call intervalo_datas, so we should wrap that up as well.

intervalo_datas <- function(date0, date1, column_name, data = EWMA_SD252) {
     if (!is.Date(date0)) date0 <- as.Date(date0, "%d/%m/%Y")
     if (!is.Date(date1)) date1 <- as.Date(date1,"%d/%m/%Y")
     cols <- c(1, which(names(data) == column_name))
     return(EWMA_SD252[EWMA_SD252$Data >= x & EWMA_SD252$Data <= y, cols])
} 

Now you should be able to get a subset this way

ABEV3 = intervalo_datas("09/02/2012", "17/02/2012", "ABEV3")

And plot like this.

plot(ABEV3[, 1], ABEV3[, 2])

If you want the subsetting function to also plot, just add the plot command before the return line (but define the subset first!). Using something like xts as agstudy recommends will simplify things and handle the dates better on the axis labels.

Upvotes: 1

Related Questions