R 如何分割在Amelia中创建的多重插补数据集?

R 如何分割在Amelia中创建的多重插补数据集?,r,missing-data,data-management,R,Missing Data,Data Management,我使用Amelia插补缺失值,从而创建了5个多重插补数据集。现在,我想拆分这个多数据集,例如,一组用于year=>1990,另一组用于year=Amelia返回一个包含数据帧列表的对象(对于每个插补)。您可以使用str()查看此对象的结构 编辑:我刚刚重读了你的问题,我发现你实际上在寻找更具体的东西。您可以将上面的内容应用于每个数据帧的子集,例如lappy(a.out$imputations,function(x)x[x$year>1990,])。我不确定您希望如何组合这些插补数据集(按大于/小

我使用Amelia插补缺失值,从而创建了5个多重插补数据集。现在,我想拆分这个多数据集,例如,一组用于year=>1990,另一组用于year=Amelia返回一个包含数据帧列表的对象(对于每个插补)。您可以使用
str()
查看此对象的结构

编辑:我刚刚重读了你的问题,我发现你实际上在寻找更具体的东西。您可以将上面的内容应用于每个数据帧的子集,例如
lappy(a.out$imputations,function(x)x[x$year>1990,])
。我不确定您希望如何组合这些插补数据集(按大于/小于1990年的年份划分),但如果您只想将所有行附加在一起,则
rbind()
将起到作用(如果不想让我知道您希望如何做,我可能会推荐一种解决方案):

df1(1990年,])) >df2头(df1) 年国家关税政策pop gdp.pc intresmi签署五份usheg imp1.11 1991斯里兰卡26.9000 5 1724700597.6987 2.285213 1.00000012.8 0.2589872 imp1.12 1992斯里兰卡25.0000 5 17405000 618.3329 2.877877 0.515665 13.1 0.2623017 imp1.13 1993斯里兰卡24.2000 5 17628420 652.6205 4.280361 0.000000 13.2 0.2812928 imp1.14 1994斯里兰卡26.0000 5 17865000 680.0408 4.389912 0.000000 13.20.2783585 imp1.15 1995斯里兰卡20.0000 5 18112000 707.6591 3.995919 0.000000 13.20.2627195 imp1.16 1996斯里兰卡20.5646 5 18300000727.0039 3.676763 0.00000013.20.2681700 >主管(df2) 年国家关税政策pop gdp.pc intresmi签署五份usheg imp1.1 1981斯里兰卡30.56693 6 14988000 461.0236 1.937347 0 12.4 0.2593112 imp1.2 1982斯里兰卡22.39382 5 15189000 473.7634 1.964430 0 12.5 0.2558008 imp1.3 1983斯里兰卡41.30000 5 15417000 489.2266 1.663936 1 12.3 0.2655022 imp1.4 1984斯里兰卡26.81580 5 15599000 508.1739 2.797462 0 12.3 0.2988009 imp1.5 1985斯里兰卡31.000005 1583700525.5609 2.259116 0 12.3 0.2952431 imp1.6 1986斯里兰卡17.76314 5 16117000 538.9237 1.832549 0 12.5 0.2886563
太棒了!非常感谢你,大卫!我的想法太复杂了,到处都是指责论者。。。这是一种非常好且简单的分割数据的方法。
data(freetrade)
freetrade$year #splitting variable

#Imputation of missing data
a.out <- amelia(freetrade, m=5, ts="year", cs="country")

#split of created dataset?
> library(Amelia)
> data(freetrade)
> 
> a.out <- amelia(freetrade, m=5, ts="year", cs="country")
-- Imputation 1 --

  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

-- Imputation 2 --

  1  2  3  4  5  6  7  8  9 10 11 12 13

-- Imputation 3 --

  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19

-- Imputation 4 --

  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

-- Imputation 5 --

  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20


> str(a.out)
List of 12
 $ imputations:List of 5
  ..$ imp1:'data.frame':    171 obs. of  10 variables:
  .. ..$ year    : int [1:171] 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 ...
  .. ..$ country : chr [1:171] "SriLanka" "SriLanka" "SriLanka" "SriLanka" ...
  .. ..$ tariff  : num [1:171] 30.6 22.4 41.3 26.8 31 ...
  .. ..$ polity  : num [1:171] 6 5 5 5 5 5 5 5 5 5 ...
  .. ..$ pop     : num [1:171] 14988000 15189000 15417000 15599000 15837000 ...
  .. ..$ gdp.pc  : num [1:171] 461 474 489 508 526 ...
  .. ..$ intresmi: num [1:171] 1.94 1.96 1.66 2.8 2.26 ...
  .. ..$ signed  : num [1:171] 0 0 1 0 0 0 0 1 0 0 ...
  .. ..$ fiveop  : num [1:171] 12.4 12.5 12.3 12.3 12.3 ...
  .. ..$ usheg   : num [1:171] 0.259 0.256 0.266 0.299 0.295 ...
  ..$ imp2:'data.frame':    171 obs. of  10 variables:
  .. ..$ year    : int [1:171] 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 ...
  .. ..$ country : chr [1:171] "SriLanka" "SriLanka" "SriLanka" "SriLanka" ...
  .. ..$ tariff  : num [1:171] 33.6 59.7 41.3 18.2 31 ...
  .. ..$ polity  : num [1:171] 6 5 5 5 5 5 5 5 5 5 ...
  .. ..$ pop     : num [1:171] 14988000 15189000 15417000 15599000 15837000 ...
  .. ..$ gdp.pc  : num [1:171] 461 474 489 508 526 ...
  .. ..$ intresmi: num [1:171] 1.94 1.96 1.66 2.8 2.26 ...
  .. ..$ signed  : num [1:171] 0 0 1 0 0 0 0 1 0 0 ...
  .. ..$ fiveop  : num [1:171] 12.4 12.5 12.3 12.3 12.3 ...
  .. ..$ usheg   : num [1:171] 0.259 0.256 0.266 0.299 0.295 ...
  ..$ imp3:'data.frame':    171 obs. of  10 variables:
  .. ..$ year    : int [1:171] 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 ...
  .. ..$ country : chr [1:171] "SriLanka" "SriLanka" "SriLanka" "SriLanka" ...
  .. ..$ tariff  : num [1:171] 48.5 32.9 41.3 47.2 31 ...
  .. ..$ polity  : num [1:171] 6 5 5 5 5 5 5 5 5 5 ...
  .. ..$ pop     : num [1:171] 14988000 15189000 15417000 15599000 15837000 ...
  .. ..$ gdp.pc  : num [1:171] 461 474 489 508 526 ...
  .. ..$ intresmi: num [1:171] 1.94 1.96 1.66 2.8 2.26 ...
  .. ..$ signed  : num [1:171] 0 0 1 0 0 0 0 1 0 0 ...
  .. ..$ fiveop  : num [1:171] 12.4 12.5 12.3 12.3 12.3 ...
  .. ..$ usheg   : num [1:171] 0.259 0.256 0.266 0.299 0.295 ...
  ..$ imp4:'data.frame':    171 obs. of  10 variables:
  .. ..$ year    : int [1:171] 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 ...
  .. ..$ country : chr [1:171] "SriLanka" "SriLanka" "SriLanka" "SriLanka" ...
  .. ..$ tariff  : num [1:171] 18.4 45.5 41.3 16.9 31 ...
  .. ..$ polity  : num [1:171] 6 5 5 5 5 5 5 5 5 5 ...
  .. ..$ pop     : num [1:171] 14988000 15189000 15417000 15599000 15837000 ...
  .. ..$ gdp.pc  : num [1:171] 461 474 489 508 526 ...
  .. ..$ intresmi: num [1:171] 1.94 1.96 1.66 2.8 2.26 ...
  .. ..$ signed  : num [1:171] 0 0 1 0 0 0 0 1 0 0 ...
  .. ..$ fiveop  : num [1:171] 12.4 12.5 12.3 12.3 12.3 ...
  .. ..$ usheg   : num [1:171] 0.259 0.256 0.266 0.299 0.295 ...
  ..$ imp5:'data.frame':    171 obs. of  10 variables:
  .. ..$ year    : int [1:171] 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 ...
  .. ..$ country : chr [1:171] "SriLanka" "SriLanka" "SriLanka" "SriLanka" ...
  .. ..$ tariff  : num [1:171] 15.3 44.4 41.3 40.1 31 ...
  .. ..$ polity  : num [1:171] 6 5 5 5 5 5 5 5 5 5 ...
  .. ..$ pop     : num [1:171] 14988000 15189000 15417000 15599000 15837000 ...
  .. ..$ gdp.pc  : num [1:171] 461 474 489 508 526 ...
  .. ..$ intresmi: num [1:171] 1.94 1.96 1.66 2.8 2.26 ...
  .. ..$ signed  : num [1:171] 0 0 1 0 0 0 0 1 0 0 ...
  .. ..$ fiveop  : num [1:171] 12.4 12.5 12.3 12.3 12.3 ...
  .. ..$ usheg   : num [1:171] 0.259 0.256 0.266 0.299 0.295 ...
  ..- attr(*, "class")= chr [1:2] "mi" "list"
 $ m          : num 5
 $ missMatrix : logi [1:171, 1:10] FALSE FALSE FALSE FALSE FALSE FALSE ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:10] "year" "country" "tariff" "polity" ...
 $ overvalues : NULL
 $ theta      : num [1:9, 1:9, 1:5] -1 -0.08456 -0.03404 -0.00193 0.06483 ...
 $ mu         : num [1:8, 1:5] -0.08456 -0.03404 -0.00193 0.06483 -0.11178 ...
 $ covMatrices: num [1:8, 1:8, 1:5] 0.7881 -0.1869 -0.0531 0.2121 -0.0819 ...
 $ code       : num 1
 $ message    : chr "Normal EM convergence."
 $ iterHist   :List of 5
  ..$ : num [1:15, 1:3] 44 34 25 28 26 25 24 22 20 14 ...
  ..$ : num [1:13, 1:3] 44 27 24 22 22 21 18 17 14 11 ...
  ..$ : num [1:19, 1:3] 44 34 29 27 26 26 25 24 23 21 ...
  ..$ : num [1:15, 1:3] 44 34 27 28 23 24 23 23 19 19 ...
  ..$ : num [1:20, 1:3] 44 32 30 27 24 23 23 23 23 21 ...
 $ arguments  :List of 22
  ..$ idvars      : NULL
  ..$ logs        : NULL
  ..$ ts          : num 1
  ..$ cs          : num 2
  ..$ empri       : NULL
  ..$ tolerance   : num 1e-04
  ..$ polytime    : NULL
  ..$ splinetime  : NULL
  ..$ lags        : NULL
  ..$ leads       : NULL
  ..$ intercs     : logi FALSE
  ..$ sqrts       : NULL
  ..$ lgstc       : NULL
  ..$ noms        : NULL
  ..$ ords        : NULL
  ..$ priors      : NULL
  ..$ autopri     : num 0.05
  ..$ bounds      : NULL
  ..$ max.resample: num 100
  ..$ startvals   : num 0
  ..$ overimp     : NULL
  ..$ emburn      : num [1:2] 0 0
  ..- attr(*, "class")= chr [1:2] "ameliaArgs" "list"
 $ orig.vars  : chr [1:10] "year" "country" "tariff" "polity" ...
 - attr(*, "class")= chr "amelia"
> sapply(a.out$imputations,function(x) head(x$year))
     imp1 imp2 imp3 imp4 imp5
[1,] 1981 1981 1981 1981 1981
[2,] 1982 1982 1982 1982 1982
[3,] 1983 1983 1983 1983 1983
[4,] 1984 1984 1984 1984 1984
[5,] 1985 1985 1985 1985 1985
[6,] 1986 1986 1986 1986 1986
> df1 <- do.call(rbind,lapply(a.out$imputations,function(x) x[x$year > 1990,]))
> df2 <- do.call(rbind,lapply(a.out$imputations,function(x) x[x$year < 1990,]))
> head(df1)
        year  country  tariff polity      pop   gdp.pc intresmi   signed fiveop     usheg
imp1.11 1991 SriLanka 26.9000      5 17247000 597.6987 2.285213 1.000000   12.8 0.2589872
imp1.12 1992 SriLanka 25.0000      5 17405000 618.3329 2.877877 0.515665   13.1 0.2623017
imp1.13 1993 SriLanka 24.2000      5 17628420 652.6205 4.280361 0.000000   13.2 0.2812928
imp1.14 1994 SriLanka 26.0000      5 17865000 680.0408 4.389912 0.000000   13.2 0.2783585
imp1.15 1995 SriLanka 20.0000      5 18112000 707.6591 3.995919 0.000000   13.2 0.2627195
imp1.16 1996 SriLanka 20.5646      5 18300000 727.0039 3.676763 0.000000   13.2 0.2681700
> head(df2)
       year  country   tariff polity      pop   gdp.pc intresmi signed fiveop     usheg
imp1.1 1981 SriLanka 30.56693      6 14988000 461.0236 1.937347      0   12.4 0.2593112
imp1.2 1982 SriLanka 22.39382      5 15189000 473.7634 1.964430      0   12.5 0.2558008
imp1.3 1983 SriLanka 41.30000      5 15417000 489.2266 1.663936      1   12.3 0.2655022
imp1.4 1984 SriLanka 26.81580      5 15599000 508.1739 2.797462      0   12.3 0.2988009
imp1.5 1985 SriLanka 31.00000      5 15837000 525.5609 2.259116      0   12.3 0.2952431
imp1.6 1986 SriLanka 17.76314      5 16117000 538.9237 1.832549      0   12.5 0.2886563