[R-SIG-Finance] Specifying data by date
Josh Ulrich
josh.m.ulrich at gmail.com
Thu Jul 10 00:24:01 CEST 2008
Hi James,
The xts package was created with this problem in mind. You can index
xts objects using POSIX-style character strings, with ranges separated
by either ":" or "/". period.apply can give you the standard
deviation of a series by month. The xts vignette describes all this -
and much more - in thorough detail.
require(fImport)
require(xts)
> df <- yahooSeries('QQQQ', returnClass=c("data.frame"))
trying URL 'http://chart.yahoo.com/table.csv?s=QQQQ&a=6&b=10&c=2007&d=6&e=09&f=2008&g=d&x=.csv'
Content type 'text/csv' length 200 bytes
opened URL
downloaded 12 Kb
> x <- as.xts(df)
> sd(x['2008-01','QQQQ.Close'])
QQQQ.Close
2.20967
> sd(x['2008-01/2008-02','QQQQ.Close'])
QQQQ.Close
2.110933
> period.apply(x$QQQQ.Close, INDEX=endpoints(x,'months'), FUN=function(x) sd(x))
x
2007-07-31 0.8553352
2007-08-31 0.8954604
2007-09-28 1.0446701
2007-10-31 0.8624363
2007-11-30 1.8838374
2007-12-31 0.8676462
2008-01-31 2.2096702
2008-02-29 0.6359294
2008-03-31 0.9582882
2008-04-30 1.0761549
2008-05-30 0.6399203
2008-06-30 1.3976623
2008-07-08 0.6155458
>
Best,
Josh
--
http://quantemplation.blogspot.com
On Wed, Jul 9, 2008 at 4:55 PM, James <j at jtoll.com> wrote:
> Hi,
>
> I have a dataframe with data from the yahooSeries function, I am trying to
> figure out if there is a way to calculate the standard deviation on a
> specific time period by simply entering a beginning and end date. For
> example, could I calculate the standard deviation for the month of March
> 2007, by simply giving a beginning date and an end date? Or better yet, the
> SD for March for the past 20 years.
>
> Really, I guess I just need a way to coax the appropriate range of data from
> the dataframe into a vector so that I can perform the necessary calculation.
> Any suggestions? Thanks.
>
> James
>
> _______________________________________________
> 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