[R-SIG-Finance] column transposition with xts
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Sep 16 16:14:58 CEST 2009
zoo's lag function supports vector lags so try this
where x is your xts object:
z <- as.zoo(Cl(x))
zz <- lag(z, -seq(0, 20))
zz is a zoo object and you can use as.xts(zz) to convert it back to
xts if needed.
Note that lag.zoo follows the R convention of having negative lag mean
move the lagged series forward in time where lag.xts uses the opposite
convention.
On Mon, Sep 14, 2009 at 6:53 AM, Aleks Clark <aleks.clark at gmail.com> wrote:
> I'm working with SVMs and have recently migrated to using TTR and thus xts,
> but I've run into the following problem:
>
> For each vector in an SVMs training set, I need to have a given amount of
> historical data available as features for the vector. A simple example would
> be to have the past 20 close prices. So I'd take a matrix like this:
> O H L C
> 1
> 2
> 3
> 4
> 5
> ...
> N
>
> and output something like this to give to my SVM:
>
> O, H, L, C, C-1, C-2, C-3, etc
> 1
> 2
> 3
> 4
> 5
>
> obviously at the top of the matrix, the historical Close cells would be
> empty until there was enough data. In the pre-xts days, I accomplished this
> using sapply() and t(), a probably hackish solution, but all I was able to
> come up with. However, I am now running into problems with indices using
> this technique, and was wondering if there was a better way to perform this
> transformation, which really only amounts to a simple copy of data. Possible
> solutions that occur to me include some sort of "index shift" and then a
> merge, but I've been unable to get anything to work.
>
>
> --
> Aleks Clark
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>
More information about the R-SIG-Finance
mailing list