Note that there are some explanatory texts on larger screens.

plurals
  1. POUse R or mysql to calculate time period returns?
    primarykey
    data
    text
    <p>I'm trying to calculate various time period returns (monthly, quarterly, yearly etc.) for each unique member (identified by <code>Code</code> in the example below) of a data set. The data set will contain monthly pricing information for a 20 year period for approximately 500 stocks. An example of the data is below:</p> <pre><code> Date Code Price Dividend 1 2005-01-31 xyz 1000.00 20.0 2 2005-01-31 abc 1.00 0.1 3 2005-02-28 xyz 1030.00 20.0 4 2005-02-28 abc 1.01 0.1 5 2005-03-31 xyz 1071.20 20.0 6 2005-03-31 abc 1.03 0.1 7 2005-04-30 xyz 1124.76 20.0 </code></pre> <p>I am fairly new to R, but thought that there would be a more efficient solution than looping through each <code>Code</code> and then each <code>Date</code> as shown here:</p> <pre><code>uniqueDates &lt;- unique(data$Date) uniqueCodes &lt;- unique(data$Code for (date in uniqueDates) { for (code in uniqueCodes) { nextDate &lt;- seq.Date(from=stock_data$Date[i], by="3 months",length.out=2)[2] curPrice &lt;- data$Price[data$Date == date] futPrice &lt;- data$Price[data$Date == nextDate] data$ret[(data$Date == date) &amp; (data$Code == code)] &lt;- (futPrice/curPrice)-1 } } </code></pre> <p>This method in itself has an issue in that <code>seq.Date</code> does not always return the final day in the month.</p> <p>Unfortunately the data is not uniform (the number of companies/codes varies over time) so using a simple row offset won't work. The calculation must match the <code>Code</code> and <code>Date</code> with the desired date offset. </p> <p>I had initially tried selecting the future dates by using the <code>seq.Date</code> function</p> <pre><code>data$ret = (data[(data$Date == (seq.Date(from = data$Date, by="3 month", length.out=2)[2])), "Price"] / data$Price) - 1 </code></pre> <p>But this generated an error as seq.Date requires a single entry.</p> <pre><code>&gt; Error in seq.Date(from = stock_data$Date, by = "3 month", length.out = &gt; 2) : 'from' must be of length 1 </code></pre> <p>I thought that R would be well suited to this type of calculation but perhaps not. Since all the data is in a mysql database I am now thinking that it might be faster/easier to do this calc directly in the database.</p> <p>Any suggestions would be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload