Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice of handling time series in R?

Tags:

r

time-series

I am using R for some statistical analysis of time series. I have tried Googling around, but I can't seem to find any definitive answers. Can any one who knows more please point me in the right direction?

Example:

Let's say I want to do a linear regression of two time series. The time series contain daily data, but there might be gaps here and there so the time series are not regular. Naturally I only want to compare data points where both time series have data. This is what I do currently to read the csv files into a data frame:

library(zoo)
apples <- read.csv('/Data/apples.csv', as.is=TRUE)
oranges <- read.csv('/Data/oranges.csv', as.is=TRUE)
apples$date <- as.Date(apples$date, "%d/%m/%Y")
oranges$date <- as.Date(oranges$date, "%d/%m/%Y")
zapples <- zoo(apples$close,apples$date)
zoranges <- zoo(oranges$close,oranges$date)
zdata <- merge(zapples, zoranges, all=FALSE)
data <- as.data.frame(zdata)

Is there a slicker way of doing this?

Also, how can I slice the data, e.g., select the entries in data with dates within a certain period?

like image 837
c00kiemonster Avatar asked Feb 11 '11 02:02

c00kiemonster


People also ask

What is the best time series forecasting methods?

AutoRegressive Integrated Moving Average (ARIMA) models are among the most widely used time series forecasting techniques: In an Autoregressive model, the forecasts correspond to a linear combination of past values of the variable.

Which technique is used for time series Modelling in R?

Time Series Analysis Models and TechniquesBox-Jenkins ARIMA models: These univariate models are used to better understand a single time-dependent variable, such as temperature over time, and to predict future data points of variables. These models work on the assumption that the data is stationary.

Is R good for time series analysis?

R has many useful functions and packages for time series analysis. You'll find pointers to them in the task view for Time Series Analysis.

How do I smooth time series data in R?

You can then use the “SMA()” function to smooth time series data. To use the SMA() function, you need to specify the order (span) of the simple moving average, using the parameter “n”. For example, to calculate a simple moving average of order 5, we set n=5 in the SMA() function.


1 Answers

Try something along these lines. This assumes that the dates are in column 1. The dyn package can be used to transform lm, glm and many similar regression type functions to ones that accept zoo series. Write dyn$lm in place of lm as shown:

library(dyn) # also loads zoo
fmt <- "%d/%m/%Y"
zapples <- read.zoo('apples.csv', header = TRUE, sep = ",", format = fmt)
zoranges <- read.zoo('oranges.csv', header = TRUE, sep = ",", format = fmt)
zdata <- merge(zapples, zoranges)
dyn$lm(..whatever.., zdata)

You don't need all = FALSE since lm will ignore rows with NAs under the default setting of its na.action argument.

The window.zoo function can be used to slice data.

Depending on what you want to do you might also want to look at the xts and quantmod packages.

like image 91
G. Grothendieck Avatar answered Nov 15 '22 20:11

G. Grothendieck