logging - Normal and log return plotting in R and corresponding returns -
i have data sheet, lot of var. companies dax index. i'm trying time series bmw , siemens , log return them. sorry, can not attach pics... sample:
positions allianz.hldg. commerzbank dresdner.bank bmw schering basf 01/01/1973 155.51 147.41 18.40 103.97 36.88 14.96 01/02/1973 155.51 147.41 18.40 109.05 37.44 15.43 01/03/1973 160.58 149.14 18.80 109.83 37.79 15.61 01/04/1973 162.27 152.05 18.91 110.81 37.86 15.85 01/05/1973 164.30 152.05 18.89 109.44 37.44 15.75 01/08/1973 164.30 152.25 18.99 109.05 37.79 15.80 the datas 197x, need datas 02.01.1989, till end of sheet.
the other extract data 1996 (bmw, siemens). , calculate corresponding returns on specific year. i'm totally stuck , appreciate help. thanks!
please me how calculate in r.
you can quantmod package. calling table above df,
library(quantmod) df$positions <- as.date(df$positions, format="%m/%d/%y") rownames(df) <- df$positions bmw.xts <- xts(df$bmw,order.by=df$positions) periodreturn(bmw.xts,period="daily") # daily.returns # 1973-01-01 0.000000000 # 1973-01-02 0.048860248 # 1973-01-03 0.007152682 # 1973-01-04 0.008922881 # 1973-01-05 -0.012363505 # 1973-01-08 -0.003563596 periodreturn(bmw.xts,period="daily", type="log") # daily.returns # 1973-01-01 0.000000000 # 1973-01-02 0.047704097 # 1973-01-03 0.007127223 # 1973-01-04 0.008883307 # 1973-01-05 -0.012440569 # 1973-01-08 -0.003569961 periodreturn(bmw.xts,period="daily", subset="1973-01-03::") # daily.returns # 1973-01-03 0.007152682 # 1973-01-04 0.008922881 # 1973-01-05 -0.012363505 # 1973-01-08 -0.003563596 the way table set up, first column, containing dates, loaded column positions, not want. nevertheless left way avoid confusion. positions timestamp in example. these have converted dates as.date(...) function. then, create xts time series, bmw.xts bmw column. can use quantmod function periodreturn(...) generate returns. subset= string formatted "start::end". if end missing, time series taken last row.
finally, example of plotting
library(ggplot2) library(reshape2) # melt(...) basf.xts <- xts(df$basf,order.by=df$positions) returns <- data.frame(df$positions, periodreturn(bmw.xts,period="daily",type="log"), periodreturn(basf.xts,period="daily",type="log")) colnames(returns)=c("date","bmw","basf") gg <- melt(returns, id="date", variable.name="stock", value.name="log.return") ggplot(gg, aes(x=date, y=log.return, color=stock))+ geom_point()+ geom_line() 
Comments
Post a Comment