impute_panel {COINr} | R Documentation |
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.
impute_panel(
iData,
time_col = NULL,
unit_col = NULL,
cols = NULL,
max_time = NULL
)
iData |
A data frame of indicator data, containing a time index column |
time_col |
The name of a column found in |
unit_col |
The name of a column found in |
cols |
Optionally, a character vector of names of columns to impute. If |
max_time |
The maximum number of time points to look backwards to impute from. E.g. if |
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:
.$iData_imp
: An iData
format data frame with missing data imputed using previous time points (where possible).
.$DataT
: A data frame in the same format as iData
, where each entry shows which time point each data point
came from.
# 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]