Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- > head(df2)
- # A tibble: 20 x 2
- `Date (US)` Value
- <dttm> <dbl>
- 1 2016-01-02 02:24:00 243814121.
- 2 2016-01-04 16:48:00 243821146.
- 3 2016-01-29 21:36:00 243837845.
- 4 2016-01-30 21:36:00 243838871.
- 5 2016-02-04 21:36:00 243841925.
- 6 2016-02-06 04:48:00 243843313.
- rm(list=ls())
- #http://sma.epfl.ch/~lbelzile/math342/pract_sup.html
- library(readxl)
- library(forecast)
- library(imputeTS)
- library(xts)
- library(zoo)
- library(data.table)
- library(xlsx)
- #import, combine, and sort data
- df1 <- read_excel("C:/Users/PC/Desktop/Time_Series/upwork-data.xlsx")
- df2 <- read_excel("C:/Users/PC/Desktop/Time_Series/upwork-data.xlsx", sheet = 2)
- df <- rbind(df1,df2)
- df <- df[order(df$`Date (US)`),]
- #plot irregular time series
- itimeseries <- as.xts(x = df$Value, order.by = df$`Date (US)`)
- plot(itimeseries, grid.ticks.on = "year", yaxis.right = FALSE)
- #plot interpolation
- plot(na.interpolation(itimeseries))
- #get interpolated values
- df$interp <- na.interpolation(itimeseries)
- #use only those interpolated values where data are avalailable within a day on either side
- df$dateplus <- df$`Date (US)` + 24*60*60 # time moved up 24 hours
- df$dateminus <- df$`Date (US)` - 24*60*60 #time moved back 24 hours
- setDT(df)[, laguptime:= shift(df$"Date (US)")][] #time moved by 1 row down
- setDT(df)[, lagdowntime:= shift(df$"Date (US)",type="lead")][] #time moved by 1 row up
- df$new <- ifelse(df$lagdowntime < df$dateplus | df$laguptime > df$dateminus , df$interp, NA)
- #keep only regular midnight values
- df$time <- strftime(df$`Date (US)`, format="%H:%M:%S")
- df$new <- ifelse(df$time == "00:00:00" | df$time == "01:00:00", df$new, NA)
- df3 <- df[which(df$time == "00:00:00" | df$time == "01:00:00"),]
- #make regular time series of all midnights
- timeseries <- as.xts(x = df3$new, order.by = df3$`Date (US)`)
- plot(timeseries, grid.ticks.on = "year", yaxis.right = FALSE)
- #visualize missing data
- plotNA.distributionBar(timeseries)
Add Comment
Please, Sign In to add comment