impute_panel {COINr}R Documentation

Impute panel data


Given a data frame of panel data, with a time-index column time_col and a unit ID column unit_col, imputes other columns using the entry from the latest available time point.


  time_col = NULL,
  unit_col = NULL,
  cols = NULL,
  max_time = NULL



A data frame of indicator data, containing a time index column time_col, a unit code column unit_col, and other numerical columns to be imputed.


The name of a column found in iData to be used as the time index column. Must point to a numeric column.


The name of a column found in iData to be used as the unit code/ID column. Must point to a character column.


Optionally, a character vector of names of columns to impute. If NULL (default), all columns apart from time_col and unit_col will be imputed where possible.


The maximum number of time points to look backwards to impute from. E.g. if max_time = 1, if an NA is found at time t, it will only look for a replacement value at t-1 but not in any time points before that. By default, searches all time points available.


This presumes that there are multiple observations for each unit code, i.e. one per time point. It then searches for any missing values in the target year, and replaces them with the equivalent points from previous time points. It will replace using the most recently available point.


A list containing:


# Copy example panel data
iData_p <- ASEM_iData_p

# we introduce two NAs: one for NZ in 2022 in LPI indicator
iData_p$LPI[iData_p$uCode == "NZ" & iData_p$Time == 2022] <-  NA
# one for AT, also in 2022, but for Flights indicator
iData_p$Flights[iData_p$uCode == "AT" & iData_p$Time == 2022] <- NA

# impute: target only the two columns where NAs introduced
l_imp <- impute_panel(iData_p, cols = c("LPI", "Flights"))
# get imputed df
iData_imp <- l_imp$iData_imp

# check the output is what we expect: both NAs introduced should now have 2021 values
iData_imp$LPI[iData_imp$uCode == "NZ" & iData_imp$Time == 2022] ==
  ASEM_iData_p$LPI[ASEM_iData_p$uCode == "NZ" & ASEM_iData_p$Time == 2021]

iData_imp$Flights[iData_imp$uCode == "AT" & iData_imp$Time == 2022] ==
  ASEM_iData_p$Flights[ASEM_iData_p$uCode == "AT" & ASEM_iData_p$Time == 2021]

[Package COINr version 1.1.7 Index]