# imputing missing values of finance data

I have 5 years stock closing price of company almost every data with some missing values in between having 1443 data points,when i create time-series object in R with frequency 365 it creates 1834 data points.

How do I impute missing values in original data for missing values? Or is there any way in R to account for missing values and only create ts values for 1443 data points which are present?

R command used:

st_ts = ts(stocks[,2],
start = c(2010,1),
end = c(2015,9),
frequency = 365)

• What are the intervals of your data? For example is it daily or hourly? 1443 is more than 5 years of daily data. Why are you using the number 365? The stock market isn't open on weekends or holidays. – Brian O'Donnell Mar 10 '16 at 21:34
• You could consider doing some sort of linear interpolation. I'm sure there are packages for this, or you could code it by hand which probably isn't impossible. – dsaxton Mar 10 '16 at 21:54
• It depends on why they are missing. For example, if you are including Saturday and Sunday, they are not missing. If there was no trade, they are also not missing, there was just nothing to report. Still, there is a difference in how they would be handled. Likewise, it is also different if you believe there were trades, but the data is not in the feed. Why it is missing matters. – Dave Harris Apr 10 '18 at 3:50

One of the packages I find very useful is ImputeTS.

This package has various ways of imputation and it is very easy to use.

E.g. Spline Interpolation

st_ts_imputed   <- na.interpolation(st_ts, option = "spline")


The package file can help you with the implementation of the methods.

Other Packages to look at

missForest
VIM
Amelia
DMwR
mice
missMDA
RandomForest
yaImpute
CoImp
mix
mi
Hmisc


These are a lot of different packages and methods, but in some instance certain methods work better than other. So this is tools you can use to impute the missing values present in your data.

to impute you can use the zoo package in R if you want an linear interpolation of your data.

Usage could be like this:

#make time series (you already have that using your ts command) from certain columns
ts<-zoo(your_data[,c(1,5)])

#this command already linearly interpolates the NAs
ts2<-na.approx(ts, x=z132\$variable_for_time_steps)


Cheers