[R-sig-finance] Using data from yahooImport (fBasics)
Diethelm Wuertz
wuertz at itp.phys.ethz.ch
Thu Apr 6 13:10:32 CEST 2006
Owe Jessen wrote:
>Thanks to the help from this list, I'm on my way learning how to handle R.
>
>There are two things that bugger me:
>1. Using yahooImport I can easily get financial data. But how do I
>handle the data from there on?
>For example, I wanted to calculate the beta of DaimlerChrysler vs. the
>DAX. So I imported both datasets. How do I have to manipulate the
>objects so I can have a linear regression of the returns of DCX to the DAX?
>
>2. Using different data-sources, one usually gets timeseries of
>differing length. Whats the most economical way to make shure that one
>feeds the appropiate pieces to lm, plot and so on?
>
>Thanks in advance,
>Owe
>
>
>
>_______________________________________________
>R-sig-finance at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>
>
# DCX.DE
# ^GDAXI
require(fMultivar)
myFinCenter = "GMT"
# Download -> Data Slot -> As Time Series -> Close
# GET Closing Prices: 2005-06-01 until now - see help(yahooImport)
query = "s=^GDAXI&a=5&b=1&c=2005&d=0&q=31&f=2009&z=^GDAXI&x=.csv"
DAX.CLOSE = as.timeSeries(yahooImport(query)@data)[,"Close"]
query = "s=DCX.DE&a=5&b=1&c=2005&d=0&q=31&f=2009&z=DCX.DE&x=.csv"
DCX.CLOSE = as.timeSeries(yahooImport(query)@data)[,"Close"]
# Align to daily dates: --- So both time series will have afterwards the
same time stamps
# Consult help(timeSeries)
DAX.ALIGNED = alignDailySeries(DAX.CLOSE, method = "interp")
DCX.ALIGNED = alignDailySeries(DCX.CLOSE, method = "interp")
# Cut Common Piece from each: --- help(timeDate)
START = modify(c(start(DAX.ALIGNED), start(DCX.ALIGNED)), "sort")[2]
END = modify(c(end(DAX.ALIGNED), end(DCX.ALIGNED)), "sort")[1]
DAX.CUTTED = cutSeries(DAX.ALIGNED, from = START, to = END)
DCX.CUTTED = cutSeries(DCX.ALIGNED, from = START, to = END)
# Merge the two Series:
DCXDAX = mergeSeries(DCX.CUTTED, DAX.CUTTED at Data, units = c("DCX", "DAX"))
# Compute Return Series:
DCXDAX.RET = as.data.frame(returnSeries(DCXDAX))
# Compute Beta: --- linear Modelling:
c(Beta = lm(formula = DCX ~ DAX, data = DCXDAX.RET)$coef[2])
Beta.DAX
1.320997
Is that what you wanted ? Read my Paper about timeDate and timeSeries
objects under Rmetrics!
Regards Diethelm
More information about the R-sig-finance
mailing list