我有一个面板数据集,其中包含 60 个国家的 10 个变量,跨越 18 年(2000-2017 年),我有很多缺失的数据。
Country Year Broadband
Albania 2000 NA
Albania 2001 NA
Albania 2002 NA
Albania 2003 NA
Albania 2004 NA
Albania 2005 272
Albania 2006 NA
Albania 2007 10000
Albania 2008 64000
Albania 2009 92000
Albania 2010 105539
Albania 2011 128210
Albania 2012 160088
Albania 2013 182556
Albania 2014 207931
Albania 2015 242870
Albania 2016 263874
Albania 2017 NA
Algeria 2000 NA
Algeria 2001 NA
Algeria 2002 NA
Algeria 2003 18000
Algeria 2004 36000
我想使用 R 中的 na.approx 函数进行插值(并使用 rule = 2 进行推断),但仅限于每个国家/地区。例如,在此示例数据集中,我想插入 Albania 2006 的值,并推断 Albania 2000-2004 和 2017 的值。但我想确保 Albania 2017 的值不使用 Albania 2016 和 Algeria 2003 进行插值。对于阿尔及利亚 2000-2002,我希望使用阿尔及利亚 2003 和 2004 的数据来推断这些值。我尝试了以下代码:
data <- group_by(data, country)
data$broadband <- na.approx(data$broadband, maxgap = Inf, rule = 2)
data <- as.data.frame(data)
并尝试了不同的 maxgap 值,但似乎没有一个能解决我的问题。我假设通过使用 group_by 函数它可以正常工作,但事实并非如此。有谁知道任何解决方案?
编辑:我想到的唯一方法是使用以下代码将数据集拆分为每个唯一国家/地区的单独数据集:
mylist <- split(data, data$country)
alb <- mylist[1]
alb <- as_data_frame(alb)
alg <- mylist[2]
alg <- as_data_frame(alg)
ang <- mylist[3]
ang <- as_data_frame(ang)
然后在单独的数据集上一次使用一个 na.approx 函数。
编辑2:
我已经尝试了下面 Markus 建议的解决方案,但它似乎不起作用。这是使用您建议的安哥拉值编码的结果:
Country Year Broadband Broadband_imp
Algeria 2014 1599692 1599692
Algeria 2015 2269348 2269348
Algeria 2016 2858906 2858906
Angola 2000 NA 2451556.286
Angola 2001 NA 2044206.571
Angola 2002 NA 1636856.857
Angola 2003 NA 1229507.143
Angola 2004 NA 822157.429
Angola 2005 NA 414807.714
Angola 2006 7458 7458
Angola 2007 11700 11700
如您所见,安哥拉 2000-2005 年的估算值似乎是使用阿尔及利亚的值计算的,因为估算值远高于安哥拉 2006 年 7458 的值。
编辑 3:这是我使用的完整代码 -
data <- read_excel("~/Documents/data.xlsx")
> dput(head(data))
structure(list(continent = c("Europe", "Europe", "Europe", "Europe",
"Europe", "Europe"), country = c("Albania", "Albania", "Albania",
"Albania", "Albania", "Albania"), Year = c(2000, 2001, 2002,
2003, 2004, 2005), `Individuals Using Internet, %, WB` = c(0.114097347,
0.325798377, 0.390081273, 0.971900415, 2.420387798, 6.043890864
), `Secure Internet Servers, WB` = c(NA, 1, NA, 1, 2, 1), `Mobile Cellular
Subscriptions, WB` = c(29791,
392650, 851000, 1100000, 1259590, 1530244), `Fixed Broadband Subscriptions,
WB` = c(NA,
NA, NA, NA, NA, 272), `Trade, % GDP, WB` = c(55.9204287230026,
57.4303612453301, 63.9342407411882, 65.4406219482911, 66.3578254370479,
70.2953012017195), `Air transport, freight (million ton-km)` = c(0.003,
0.003, 0.144, 0.088, 0.099, 0.1), `Air Transport, registered carrier
departures worldwide, WB` = c(3885,
3974, 3762, 3800, 4104, 4309), `FDI, net, inflows, % GDP, WB` =
c(3.93717707227928,
5.10495722596557, 3.04391445388559, 3.09793068135411, 4.66563777108359,
3.21722676118428), `Number of Airports, WFB` = c(10, 11, 11,
11, 11, 11), `Currently under EU Arms Sanctions` = c(0, 0, 0,
0, 0, 0), `Currently under EU Economic Sanctions` = c(0, 0, 0,
0, 0, 0), `Currently under UN Arms Sanctions` = c(0, 0, 0, 0,
0, 0), `Currently under UN Economic Sanctions` = c(0, 0, 0, 0,
0, 0), `Currently under US Arms Embargo` = c(0, 0, 0, 0, 0, 0
), `Currently under US Economic Sanctions` = c(0, 0, 0, 0, 0,
0)), .Names = c("continent", "country", "Year", "Individuals Using Internet,
%, WB",
"Secure Internet Servers, WB", "Mobile Cellular Subscriptions, WB",
"Fixed Broadband Subscriptions, WB", "Trade, % GDP, WB", "Air transport,
freight (million ton-km)",
"Air Transport, registered carrier departures worldwide, WB",
"FDI, net, inflows, % GDP, WB", "Number of Airports, WFB", "Currently under EU
Arms Sanctions",
"Currently under EU Economic Sanctions", "Currently under UN Arms Sanctions",
"Currently under UN Economic Sanctions", "Currently under US Arms Embargo",
"Currently under US Economic Sanctions"), row.names = c(NA, -6L
), class = c("tbl_df", "tbl", "data.frame"))
data_imputed <- data %>%
group_by(country) %>%
mutate(broadband_imp = na.approx(broadband, maxgap=Inf, rule = 2))