[R-sig-finance] Using data from yahooImport (fBasics)

Dirk Eddelbuettel edd at debian.org
Fri Apr 7 14:20:36 CEST 2006


On 7 April 2006 at 11:58, Diethelm Wuertz wrote:
| Why you should definitely use Rmetrics to solve Owe Jessens problem:
[...]
| When you have downloaded the data,
| you can do it with default settings in 6 lines !!!!
| 
| Note you have several alternatives to align the data, and to
| compute the returns. Check the arguments of the functions
| alignDailySeries() and returnSeries().

Ajay has point as zoo can really do this compactly:


stopifnot(require(tseries), quiet=TRUE) 	# also loads zoo these days
simpleBeta <- function(ysymb="F", xsymb="^GSPC", startdate="2005-01-01") {
  Y <- get.hist.quote(instrument=ysymb, start=startdate, quote="Close")
  X <- get.hist.quote(instrument=xsymb, start=startdate, quote="Close")
  rets <- diff(log(merge(Y,X,all=FALSE)))
  fit <- lm(Close.Y ~ Close.X, data=rets)
  return(coef(fit)[2])
}

One command each for data retrieval [ and is there a way to not get the
verbose output from downloading ? ], the return transformation and merge
really can be as compact as one compact, fit, and then coefficient
extraction. 

Obviously, this even fits in one call without really saving anything:

reallyShortSimpleBeta <- function(ysymb="F", xsymb="^GSPC",
                                  startdate="2005-01-01") {
  return( coef( lm(Close.Y ~ Close.X,
                   data = diff( log (  merge(
                     Y=get.hist.quote(instrument=ysymb,
                                    start=startdate, quote="Close"),
                     X=get.hist.quote(instrument=xsymb,
                                    start=startdate, quote="Close"),
                     all=FALSE)))))[2] )
}

Now, what is the deal with the Frankfurt data at data Yahoo? 
For regressions of Daimler or Deutsche Bank, ie
  > simpleBeta("DCX.DE", "^GDAXI")
  > simpleBeta("DBK.DE", "^GDAXI")
I get only 200 datapoints whereas US data (with the same start data request
of Jan 1, 2005) gets me the full 317 points...

Anyway, hope this helps.

Cheers, Dirk

-- 
Hell, there are no rules here - we're trying to accomplish something. 
                                                  -- Thomas A. Edison



More information about the R-sig-finance mailing list