Analysis in R: Maybe not just interpolating missing values in time series? The “imputeTS” package

RAnalytics
スポンサーリンク

This is the “imputeTS” package for interpolating missing values in time series. It is easier to use R commands to manipulate missing values, but you may want to try this package first.

Package version is 3.3. Checked with R version 4.2.2.

スポンサーリンク

Install Package

Run the following command.

#Install Package
install.packages("imputeTS")

Example

See the command and package help for details.

#Loading the library
library("imputeTS")

#Create Data
#Converting to time series data with the ts command
TestData <- ts(c(2,3,4,5,6,NA,7,8))
TestData
Time Series:
Start = 1 
End = 8 
Frequency = 1 
[1]  2  3  4  5  6 NA  7  8

#Interpolate missing values in linear or in splines: na_interpolation command
#Specified by option
na_interpolation(TestData, option = "linear")
Time Series:
Start = 1 
End = 8 
Frequency = 1 
[1] 2.0 3.0 4.0 5.0 6.0 6.5 7.0 8.0

#Interpolate missing values by the value
#before or after the missing value: na_locf command
#locf: interpolate by previous value,nocb: interpolate by backward value
na_locf(TestData, option = "locf")
Time Series:
Start = 1 
End = 8 
Frequency = 1 
[1] 2 3 4 5 6 6 7 8

#Interpolate missing values by mean, median, mode: na_mean command
#option:mean,median,mode
na_mean(TestData, option = "mean")
Time Series:
Start = 1 
End = 8 
Frequency = 1 
[1] 2 3 4 5 6 5 7 8

#Randomly interpolate missing values: na_random command
#upper_bound: lower bound, upper_bound: upper bound
na_random(TestData, lower_bound = 100, upper_bound = 1000)
Time Series:
Start = 1 
End = 8 
Frequency = 1 
[1]   2.0000   3.0000   4.0000   5.0000   6.0000 911.4419   7.0000   8.0000

#Remove NA: na_remove command
na_remove(TestData)
Time Series:
Start = 1 
End = 7 
Frequency = 1 
[1] 2 3 4 5 6 7 8

#Replace NA: na_replace command
na_replace(TestData, fill = 99999)
Time Series:
Start = 1 
End = 8 
Frequency = 1 
[1]  2     3     4     5     6  99999  7  8

I hope this makes your analysis a little easier !!

Copied title and URL