Skip to content
Back to formatted view

Raw Message

Message-ID: <CAG7y0gonZfsPx-m+9m=dq37V=0X2axwjYiBa0iZ7vMaC_pPBYw@mail.gmail.com>
Date: 2015-08-03T23:51:08Z
From: Aaron Goldenberg
Subject: aggregate an xts by factors
In-Reply-To: <CAG7y0grCoiXz1dzdtN0BupyB=uV5OLmNYEbYv=ta4JXOEcLKAg@mail.gmail.com>

Alternatively, I could have two separate time series, one for each factor.

On Mon, Aug 3, 2015 at 7:48 PM, Aaron Goldenberg <aaron at quantrisktrading.com
> wrote:

> This seems like a simple issue but I cannot get it to work for some
> reason. I have a time series object that has daily returns of several
> stocks that I would like to aggregate by qualitative factor.  In my toy
> example, for each day, I would like to have two entries, one for the sum of
> the returns of my two computer stocks and another for my financial stock.
> Then I would like to calculate the cumulative sum of the returns for each
> factor. What am I not doing correctly?
>
> library(quantmod)
> getSymbols("AAPL", src="yahoo", from="2015-07-01")
> AAPL <- dailyReturn(AAPL$AAPL.Adjusted)
> AAPL <- cbind(data.frame(AAPL), sector="Computer")
> AAPL <- as.xts(AAPL)
> getSymbols("GOOG", src="yahoo", from="2015-07-01")
> GOOG <- dailyReturn(GOOG$GOOG.Adjusted)
> GOOG <- cbind(data.frame(GOOG), sector="Computer")
> GOOG <- as.xts(GOOG)
> getSymbols("GS", src="yahoo", from="2015-07-01")
> GS <- dailyReturn(GS$GS.Adjusted)
> GS <- cbind(data.frame(GS), sector="Financial")
> GS <- as.xts(GS)
> combined <- rbind(AAPL, GOOG, GS)
> #combined <- period.sum(combined$daily.returns, endpoints(combined,
> on="days"))
> combined <- aggregate(combined, by=combined$sector, sum)
>
>
>
>

	[[alternative HTML version deleted]]