在R中对一个单一变量和多个国家使用线性回归法估算缺失数据

问题描述 投票:0回答:1

我已经使用该代码对观测数据点之间的数据进行了线性插值。

df2 <- df %>%
  group_by(iso3)%>%
  mutate_at(vars(anc4), list(inter = ~na.approx(., na.rm = FALSE)))%>%
  ungroup()%>%
  mutate_cond(is.na(anc4),anc4=inter)%>%
  dplyr::select(-c(inter))"

它给了我一个数据集,我在这里显示的是一个摘要,但我的数据集包含了194个国家2000年至2018年的数据。

   ID      iso3   year  anc4
   <chr>   <chr> <dbl> <dbl>
 1 AFG2000 AFG    2000  NA  
 2 AFG2001 AFG    2001  NA  
 3 AFG2002 AFG    2002  NA  
 4 AFG2003 AFG    2003  NA  
 5 AFG2004 AFG    2004  NA  
 6 AFG2005 AFG    2005  NA  
 7 AFG2006 AFG    2006  NA  
 8 AFG2007 AFG    2007  NA  
 9 AFG2008 AFG    2008  16.1
10 AFG2009 AFG    2009   9.9
11 AFG2010 AFG    2010  14.6
12 AFG2011 AFG    2011  18.6
13 AFG2012 AFG    2012  22.7
14 AFG2013 AFG    2013  17.8
15 AFG2014 AFG    2014  16.3
16 AFG2015 AFG    2015  17.8
17 AFG2016 AFG    2016  19.4
18 AFG2017 AFG    2017  20.9
19 AFG2018 AFG    2018  NA  
20 AGO2000 AGO    2000  39.8
21 AGO2001 AGO    2001  41.5
22 AGO2002 AGO    2002  43.1
23 AGO2003 AGO    2003  44.8
24 AGO2004 AGO    2004  46.4
25 AGO2005 AGO    2005  48.1
26 AGO2006 AGO    2006  49.8
27 AGO2007 AGO    2007  51.4
28 AGO2008 AGO    2008  53.1
29 AGO2009 AGO    2009  54.8
30 AGO2010 AGO    2010  56.4
31 AGO2011 AGO    2011  58.1
32 AGO2012 AGO    2012  59.7
33 AGO2013 AGO    2013  61.4
34 AGO2014 AGO    2014  NA  
35 AGO2015 AGO    2015  NA  
36 AGO2016 AGO    2016  NA  
37 AGO2017 AGO    2017  NA  
38 AGO2018 AGO    2018  NA 

我现在想做的是在国家层面使用线性回归向后和向前推断。我知道函数na.locf和na.approx,但找不到任何可以做到这一点的选项。小鼠或Amelia似乎没有办法做到这一点,因为它们需要协变量。我只有一个变量。

复杂的是,我有194个国家,所以我正在寻找可以为所有国家做这件事的东西。如果您能帮助我,我将感激不尽

我已经试过了,首先尝试提取斜率。

df_slope <- df2 %>%
  mutate(slope=NA)%>%
  group_by(iso3)%>%
  mutate_cond(is.na(slope),slope=lm(anc4 ~year,.)$coefficients[[2]])%>%
  ungroup()

...当然,这是不工作的... ...。

非常感谢您!

r linear-regression missing-data
1个回答
0
投票

由于你只有一个变量,你唯一能做的就是使用 列作为自变量(换言之,作为趋势值)来预测。祖先4

下面的代码,你可以找到与线性回归有关的投入,通过使用 base R.

df <- as.data.frame(df)

df_model <-  df[!is.na(df$anc4),]

predictions <- vector()

for(i in unique(df_model$iso3)) {

        temp <- df_model[df_model[,2]==i,]


        predictions <- c(predictions,predict(lm(anc4~year,temp),df[is.na(df$anc4) & df$iso3==i,]))


}

df[is.na(df$anc4),]$anc4 <- predictions

df

给。

        ID iso3 year      anc4
1  AFG2000  AFG 2000  8.781212
2  AFG2001  AFG 2001  9.471515
3  AFG2002  AFG 2002 10.161818
4  AFG2003  AFG 2003 10.852121
5  AFG2004  AFG 2004 11.542424
6  AFG2005  AFG 2005 12.232727
7  AFG2006  AFG 2006 12.923030
8  AFG2007  AFG 2007 13.613333
9  AFG2008  AFG 2008 16.100000
10 AFG2009  AFG 2009  9.900000
11 AFG2010  AFG 2010 14.600000
12 AFG2011  AFG 2011 18.600000
13 AFG2012  AFG 2012 22.700000
14 AFG2013  AFG 2013 17.800000
15 AFG2014  AFG 2014 16.300000
16 AFG2015  AFG 2015 17.800000
17 AFG2016  AFG 2016 19.400000
18 AFG2017  AFG 2017 20.900000
19 AFG2018  AFG 2018 21.206667
20 AGO2000  AGO 2000 39.800000
21 AGO2001  AGO 2001 41.500000
22 AGO2002  AGO 2002 43.100000
23 AGO2003  AGO 2003 44.800000
24 AGO2004  AGO 2004 46.400000
25 AGO2005  AGO 2005 48.100000
26 AGO2006  AGO 2006 49.800000
27 AGO2007  AGO 2007 51.400000
28 AGO2008  AGO 2008 53.100000
29 AGO2009  AGO 2009 54.800000
30 AGO2010  AGO 2010 56.400000
31 AGO2011  AGO 2011 58.100000
32 AGO2012  AGO 2012 59.700000
33 AGO2013  AGO 2013 61.400000
34 AGO2014  AGO 2014 63.058242
35 AGO2015  AGO 2015 64.719341
36 AGO2016  AGO 2016 66.380440
37 AGO2017  AGO 2017 68.041538
38 AGO2018  AGO 2018 69.702637
© www.soinside.com 2019 - 2024. All rights reserved.