Saturday, April 13, 2013

Failing Upwards in R: Time Series

My plan was to learn R through individual projects. I wanted to look at a bag of various commodity producers and see how/if correlate. I've spent 7 of the last 24 hours trying to figure out time series and how to plot them in R. Here's what I have:

The index in xts is the date. You can call the dates using index(ofinterest). The output from that seems to be a list, at least, that's the interpretation a Python experience gives you because you can choose a (subindexed?) element by foo=index(ofinterest) and foo[i] calls the ith element in foo. That's a list in Python.

The problem is that given two time series that start at different times, such as Series 1 with range 1980-2000 and Series 2 with range 1990-200, I can't overlay them on the same graph. I've hit it two ways:

1. Merged the date series (the intervals were diffferent) into a single series so that all y points had an x coordinate, and then tried to plot S1 and S2 with respect to that single date series. Obvious problem results in that x-y isn't one-to-one and the plotter doesn't like that. Possible solution is to insert nulls to equate x and y lengths. I don't know how to go about that yet. Start simpler, maybe.

2. Merged the xts series for each ticker and pray it plots. Merge doesn't work like I thought it did. I thought merge would create a single series of closing prices, but it seems to have created a matrix of submatrices. Likely incorrect on that.

I think I'm starting a bit ambitious for someone with limited experience, but I don't know a simpler project of interest. Date formatting in all languages still confuses me, so that's not a great sign. 

No comments:

Post a Comment