2
$\begingroup$

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)
$\endgroup$
  • 2
    $\begingroup$ 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. $\endgroup$ – Brian O'Donnell Mar 10 '16 at 21:34
  • $\begingroup$ 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. $\endgroup$ – dsaxton Mar 10 '16 at 21:54
  • $\begingroup$ 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. $\endgroup$ – Dave Harris Apr 10 '18 at 3:50
2
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.