import pandas.io.data as web
from datetime import datetime
import pandas as pd
ticker = 'HMC'
start = datetime(2014, 1, 1)
end = datetime(2014, 12, 19)
df=web.DataReader(ticker, 'yahoo', start, end)
df.reset_index(level=0, inplace=True) # R's data frame doesn't like it when the index is a date so convert date to column
df.head()
Date | Open | High | Low | Close | Volume | Adj Close | |
---|---|---|---|---|---|---|---|
0 | 2014-01-02 | 41.10 | 41.13 | 40.60 | 40.69 | 179400 | 40.69 |
1 | 2014-01-03 | 40.68 | 40.82 | 40.50 | 40.69 | 191900 | 40.69 |
2 | 2014-01-06 | 41.08 | 41.38 | 40.66 | 40.80 | 277000 | 40.80 |
3 | 2014-01-07 | 40.67 | 40.81 | 40.56 | 40.71 | 207000 | 40.71 |
4 | 2014-01-08 | 40.72 | 40.72 | 40.50 | 40.65 | 258100 | 40.65 |
%load_ext rpy2.ipython
%R -i df
%%R -w 480 -h 300 -u px # instead of px, you can also choose 'in', 'cm', or 'mm'
df = as.data.frame(df) # ensure dataframe that was passed in from Python will be R's dataframe type
library(ggplot2)
p = ggplot(df, aes(df$Date, df$Adj.Close))
p = p +
geom_line() +
ggtitle("HMC Closing Price") +
xlab("Date") +
ylab("Price")
print(p)
%%R
library(forecast)
HoltWinters(df$Adj.Close)
Error in decompose(ts(x[1L:wind], start = start(x), frequency = f), seasonal) : time series has no or less than 2 periods
%%R
hwm = HoltWinters(df$Adj.Close, gamma=FALSE)
hwf = forecast.HoltWinters(hwm, h=20)
summary(hwf)
%R print(summary(hwf))
<DataFrame - Python:0x08E26D50 / R:0x095AD9A8> [FloatVe..., FloatVe..., FloatVe..., FloatVe..., FloatVe...] Point Forecast: <class 'rpy2.robjects.vectors.FloatVector'> <FloatVector - Python:0x08E26A08 / R:0x082BF9E8> [30.309197, 30.288394, 30.267591, ..., 29.955544, 29.934741, 29.913938] Lo 80: <class 'rpy2.robjects.vectors.FloatVector'> <FloatVector - Python:0x08E263A0 / R:0x0875ADD8> [29.793228, 29.557593, 29.371186, ..., 27.709635, 27.623839, 27.539461] Hi 80: <class 'rpy2.robjects.vectors.FloatVector'> <FloatVector - Python:0x08E26800 / R:0x08ED1B38> [30.825166, 31.019194, 31.163995, ..., 32.201454, 32.245643, 32.288415] Lo 95: <class 'rpy2.robjects.vectors.FloatVector'> <FloatVector - Python:0x08E26300 / R:0x0778FE98> [29.520090, 29.170731, 28.896658, ..., 26.520722, 26.400521, 26.282489] Hi 95: <class 'rpy2.robjects.vectors.FloatVector'> <FloatVector - Python:0x05D5E850 / R:0x07D84198> [31.098303, 31.406057, 31.638523, ..., 33.390366, 33.468961, 33.545387]
%R print(head(df))
<DataFrame - Python:0x08E21198 / R:0x0869EED8> [POSIXct, Array, Array, ..., Array, Array, Array] Date: <class 'rpy2.robjects.vectors.POSIXct'> <POSIXct - Python:0x08E1A918 / R:0x09FBCDF8> [1388638800.000000, 1388725200.000000, 1388984400.000000, 1389070800.000000, 1389157200.000000, 1389243600.000000] Open: <class 'rpy2.robjects.vectors.Array'> <Array - Python:0x08E1A210 / R:0x09FBCDA0> [41.100000, 40.680000, 41.080000, 40.670000, 40.720000, 40.700000] High: <class 'rpy2.robjects.vectors.Array'> <Array - Python:0x08CCEC60 / R:0x09FBCD48> [41.130000, 40.820000, 41.380000, 40.810000, 40.720000, 40.750000] ... Date: <class 'rpy2.robjects.vectors.Array'> <Array - Python:0x067E1210 / R:0x09FBCC98> [40.690000, 40.690000, 40.800000, 40.710000, 40.650000, 40.730000] Open: <class 'rpy2.robjects.vectors.Array'> <Array - Python:0x08CCF2B0 / R:0x0847A1D0> [ 179400, 191900, 277000, 207000, 258100, 170800] High: <class 'rpy2.robjects.vectors.Array'> <Array - Python:0x08E125F8 / R:0x09FBCC40> [40.690000, 40.690000, 40.800000, 40.710000, 40.650000, 40.730000]
%%R
plot.forecast(hwf)