Answer to: Interpolate data using indicator in time series
Score: 0
You can use a linear regression to predict the missing values:
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
df <- tibble(
year = 2015:2025,
Variable = c(1000, NA, NA, NA, 1200, NA, NA, NA, 1500, NA, NA),
Indicator = c(700, 730, 775, 790, 815, 870, 910, 930, 970, 980, 1030)
)
df2 <- df %>%
mutate(Prediction = if_else(
is.na(Variable),
predict(lm(Variable ~ year + Indicator, data = .), newdata = .),
Variable
))
library(ggplot2)
library(tidyr)
df2 |>
pivot_longer(-year) %>%
ggplot(aes(x = year, y = value, color = name)) +
geom_line() +
geom_point() +
# highlight original values
geom_point(data = ~filter(.x, name == "Variable"),
size = 3) +
labs(x = "Year", y = "Value", color = "Variable") +
guides(color = guide_legend(reverse = T))
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_line()`).
#> Warning: Removed 8 rows containing missing values or values outside the scale range
#> (`geom_point()`).
#> Removed 8 rows containing missing values or values outside the scale range
#> (`geom_point()`).
Created on 2025-11-03 with reprex v2.1.1
View Question ↗
Question
Parent Entity
Score: 2 • Views: 119
Site: stackoverflow
Other Comments / Reviews
SaaS Metrics