Question Details

No question body available.

Tags

r time-series

Answers (2)

November 3, 2025 Score: 0 Rep: 1,211 Quality: Low Completeness: 80%

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 pivotlonger(-year) %>% ggplot(aes(x = year, y = value, color = name)) + geomline() + geompoint() + # highlight original values geompoint(data = ~filter(.x, name == "Variable"), size = 3) + labs(x = "Year", y = "Value", color = "Variable") + guides(color = guidelegend(reverse = T)) #> Warning: Removed 2 rows containing missing values or values outside the scale range #> (geomline()). #> Warning: Removed 8 rows containing missing values or values outside the scale range #> (geompoint()). #> Removed 8 rows containing missing values or values outside the scale range #> (geompoint()).

Created on 2025-11-03 with reprex v2.1.1

November 4, 2025 Score: 0 Rep: 70,620 Quality: Low Completeness: 70%

The na.spline function in the zoo package will take a zoo object and replace the NAs using a cubic spline interpolation.

We could run df$Varspline = zoo::na.spline(zoo::zoo(df$Variable, df$year)) and then see this works nicely with the data, interpolating between the known points and extrapolating beyond:

ggplot(df, aes(year, Varspline)) + 
  geomline(color = "gray70") +
  geompoint(shape = 21) +
  geom_point(aes(y = Variable))

enter image description here