Commit bf560642 authored by Hopf, Konstantin's avatar Hopf, Konstantin
Browse files

L4 data preparation: time series interpolation examples

parent 7b1a76dc
---
title: "Examples for data preparation (EESYS-BIA, L04)"
author: "Konstantin Hopf <kostantin.hopf@uni-bamberg.de>"
output: html_document
---
```{r interpolate time series data}
# create a time series with recurrent pattern and random components
obs_window <- c(12,13,15,17,15,10)
X <- rep(obs_window, times=5)
set.seed(1211)
X <- X+ rnorm(length(X), mean = 1, sd=1)
# illustrate the result
plot(X, type="b", lwd=2)
#mark two observations as missing
X_mod1 <- X
X_mod1[c(24,25)] <- NA
# illustrate the result
plot(X, type="b", col="orange", lty="dashed", main="Time-series with missing values")
points(X_mod1, type="b", lwd=2)
legend("top", legend = c("Data", "True values"),
col=c("black", "orange"), lty=c("solid", "dashed"),
lwd=c(2,1), ncol=2)
# Approach 1: Linear interpolation
X_mod2 <- X_mod1
avg_dist <- (X_mod2[23] - X_mod2[26]) / 3
X_mod2[24] <- X_mod2[23] - avg_dist
X_mod2[25] <- X_mod2[23] - 2 * avg_dist
# illustrate the result
plot(X, type="b", col="orange", lty="dashed", main="Linear interpolation")
points(X_mod2, type="b", lwd=2)
legend("top", legend = c("Data (interpolated)", "True values"),
col=c("black", "orange"), lty=c("solid", "dashed"),
lwd=c(2,1), ncol=2)
# Approach 2: Time-series forecasting using Seasonal-Trend-Decomposition
# estimate the STL model
X_mod3 <- X_mod1
X_ts <- ts(X_mod3[1:(3*length(obs_window))], frequency = length(obs_window))
X_stl <- stl(X_ts, s.window = "periodic")
X_stl$time.series
# replace missing values
X_mod3[24] <- sum(X_stl$time.series[1+ 24 %% length(obs_window), 1:2])
X_mod3[25] <- sum(X_stl$time.series[1+ 25 %% length(obs_window), 1:2])
# illustrate the result
plot(X, type="b", col="orange", lty="dashed", main="Interpolation with time-series forecasting")
points(X_mod3, type="b", lwd=2)
legend("top", legend = c("Data (interpolated)", "True values"),
col=c("black", "orange"), lty=c("solid", "dashed"),
lwd=c(2,1), ncol=2)
```
```{r}
library(officer)
library(dplyr)
library(rvg)
plot1 <- plot_instr(code={
plot(X, type="b", col="orange", lty="dashed", main="Time-series with missing values")
points(X_mod1, type="b", lwd=2)
legend("top", legend = c("Data", "True values"),
col=c("black", "orange"), lty=c("solid", "dashed"),
lwd=c(2,1), ncol=2)
})
plot2 <- plot_instr(code={
plot(X, type="b", col="orange", lty="dashed", main="Linear interpolation")
points(X_mod2, type="b", lwd=2)
legend("top", legend = c("Data (interpolated)", "True values"),
col=c("black", "orange"), lty=c("solid", "dashed"),
lwd=c(2,1), ncol=2)
})
plot3 <- plot_instr(code={
plot(X, type="b", col="orange", lty="dashed", main="Interpolation with time-series forecasting")
points(X_mod3, type="b", lwd=2)
legend("top", legend = c("Data (interpolated)", "True values"),
col=c("black", "orange"), lty=c("solid", "dashed"),
lwd=c(2,1), ncol=2)
})
respres <- read_pptx() %>%
add_slide(layout = "Title and Content") %>%
ph_with(value = plot1, location = ph_location_fullsize()) %>%
add_slide(layout = "Title and Content") %>%
ph_with(value = plot2, location = ph_location_fullsize()) %>%
add_slide(layout = "Title and Content") %>%
ph_with(value = plot3, location = ph_location_fullsize()) %>%
print("Plot_export.pptx")
```
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment