我正在尝试计算不同国家/地区时间序列的估算值。这段代码以前运行良好,但现在估算的值都是错误的……我无法弄清楚问题所在,我已经尝试了我能想到的一切。
我们的规则是:
- 在时间序列末尾缺少的值将被赋予该序列中最后一个已知值。
- 在时间序列开始时缺失的值被赋予该序列中的第一个已知值。
- 如果时间序列中间缺少值,则使用线性外推。
# load library for imputation
library(zoo)
# expand table to show NAs
output_table_imp = expand(output_table, transport_mode, year, country_code)
output_table_imp = full_join(output_table_imp, output_table)
# add imputated values
output_table_imp <- output_table_imp %>%
group_by(transport_mode, country_code) %>%
mutate(fatalities_imp= na.approx(fatalities,na.rm=FALSE)) %>% # linear interpolation
mutate(fatalities_imp= na.locf.default(fatalities_imp,na.rm=FALSE)) %>% # missing values at the end of a time series (copy last non-NA value)
mutate(fatalities_imp= na.locf(fatalities_imp,fromLast=TRUE, na.rm=FALSE)) %>% # missing values at the start of a time series (copy first non-NA value)
我的数据框由几列组成:transport_mode、country_code、year、fatities。我不确定如何在这里分享我的数据?这是一张有 3600 个观测值的大表……