# Dealing with different time series data in Machine Learning

I am trying to create a stock market model based on fundamental variables for the US economy. I am using R. Some of the variables I am looking to include are: GDP, Unemployment Rate, Initial Claims, etc... Given that some of these indicators have different time frames from weekly to quarterly will I get error messages because of NA values if I try different models?

Below is the output of the df I created showing all the NA values.

            Var1 Var2 Var3     Var4
2013-12-08     NA  NA 358000     NA
2013-12-15     NA  NA 368000     NA
2013-12-22     NA  NA 339000     NA
2013-12-29     NA  NA 344000     NA
2014-01-01    6.6 144     NA 317602
2014-01-05     NA  NA 333000     NA
2014-01-12     NA  NA 329000     NA
2014-01-19     NA  NA 334000     NA
2014-01-26     NA  NA 345000     NA
2014-02-01    6.7 197     NA 317760
2014-02-02     NA  NA 328000     NA
2014-02-09     NA  NA 343000     NA
2014-02-16     NA  NA 330000     NA
2014-02-23     NA  NA 351000     NA
2014-03-01    6.7 192     NA 317920
2014-03-02     NA  NA 325000     NA
2014-03-09     NA  NA 319000     NA
2014-03-16     NA  NA 323000     NA
2014-03-23     NA  NA 310000     NA
2014-03-30     NA  NA 326000     NA


Is there a model that can deal with all the different time frames (daily, weekly, monthly)? Or should I try to consolidate the time frames into the highest order (i.e. monthly)?

Any insight or assistance would be greatly appreciated.

• This is my first question on this exchange.
• auto.arima and Arima form forecast package can both deal with multiple seasonal components, see here for example – David Arenburg Jul 2 '14 at 7:50
• Var3 is the variable you want to model and other variables are the covariates whose effect on Var3 you want to estimate? – mpiktas Jul 2 '14 at 13:19

Well first of all, what would you be using if the data were all present at every observation? Whatever you had in mind, you could basically do the same thing but create an indicator variable for whether the observation exists and include it along with an interaction between the indicator and the actual variable. That's kind of a cheap trick to handle missing data like that, but it works with a linear model.

As for this kind of time series data, usually the simplest version of this model looks something like an ARMA model with an additional term that is a linear combination of the covariates you've got. So basically it's an ARMA model where the mean difference is linear in the covariates. But of course you can make it more complex than that; you could make the difference depend on the previous value times the covariate, or you could make the variance depend on the covariate. But that's a whole set of decisions you'd have to make even if all the data were there.

As for what R packages to use, that I actually don't know although I think most of the standard packages will incorporate covariates like this if you do it right.

I agree with Mike's answer: You need to know how you want to use the data you're collecting. If you're trying to analyze daily market data you should try to get as much data at that frequency as you can because munging data together with different frequencies is a challenge. The same goes for weekly, monthly, etc.

Having said that, you could take your weekly data and calculate a 4 wk moving average so it will line up with your monthly data, but your quarterly GDP data is going to have even fewer observations so you need to know how you want to handle that circumstance. You could fill in the dataset with current GDP data so you have 3 months of repeating GDP values.

There are other techniques I guess you could use (interpolation, etc.), but ultimately it goes back to making sure you have a clear understanding of how you want to make this analysis work for you.

Hope this helps.

• Thanks @Tony_Frame. This is the method I was going to try first but wanted to see if anyone knew of a different way. – 22Kane_HI Apr 22 '14 at 12:41

This is a common issue with incorporating economic data, they just aren't collected very often! Basically, you have two choices:

1. Some time series models will let you forecast at higher frequencies than they were trained on, i.e. fit a model to monthly data then forecast daily prices.

2. Fill in the blanks to bring all of your data into the same timeframe. There are nice na.approx and na.locf methods in the zoo package for this. The indicator trick can work well here too.

A not-very-basic third option is solve the open research of question of the correct multiple timeframe model for incorporating macro-economic data into fundamental stock price models. As a hint, try tree models like random forests.