2017-03-15 20 views
0

年情報を使用して年齢を帰属させたいと思います。私は、次の特性を持つデータセットを持っています。年の順序に基づいて年齢を代入する

dat <- data.table(id = c(rep(1, 8), rep(2, 8)), 
        year = c(2007:2014, 2007:2014), 
        age = c(1, NA, 3, NA, NA, 5, 7, NA, NA, NA, 30, NA, 32, 35, NA, NA), 
        age_imp= c(1, 2, 3, 4, 5, 5, 7, 8, 28, 29, 30, 31, 32, 35, 36, 37) 
) 


    id year age age_imp 
1: 1 2007 1  1 
2: 1 2008 NA  2 
3: 1 2009 3  3 
4: 1 2010 NA  4 
5: 1 2011 NA  5 
6: 1 2012 5  5 
7: 1 2013 7  7 
8: 1 2014 NA  8 
9: 2 2007 NA  28 
10: 2 2008 NA  29 
11: 2 2009 30  30 
12: 2 2010 NA  31 
13: 2 2011 32  32 
14: 2 2012 35  35 
15: 2 2013 NA  36 
16: 2 2014 NA  37 

元の変数ageは常に(例えば、インタビューは前回のインタビュー、測定誤差などから一年未満に適用された)の年間の期間と一致していません、だから私はそれをそのまま保ちたい。 NAの行については、年単位でシーケンスを開始したいと考えています(例:age_imp)。

どのようにすればいいですか?

+0

新しい例では、あなたは両方の列を転嫁したいですか? – akrun

答えて

1

最初にNA以外の年齢を使用して線形方程式を作成し、最初にジャンプを処理せずに各ID内で線形補間します(&)。

次に、各IDのジャンプ/ステップがどこにあるかを特定します。

次に、ジャンプを考慮して、各グループ(つまりIDとステップのペア)を補間して外挿します。

詳しい説明をインライン..

#ensure order is correct before using shift 
setorder(dat, id, year) 

#' Fill NA by interpolating and extrapolating using a known point 
#' 
#' @param dt - data.table 
#' @param years - the xout that are required 
#' 
#' @return a numeric vector of ages given the years 
#' 
extrapolate <- function(dt, years) { 
    #find the first non NA entry 
    firstnonNA <- head(dt[!is.na(age)], 1) 

    #using linear equation y - y_1 = 1 * (x - x_1) 
    as.numeric(sapply(years, function(x) (x - firstnonNA$year) + firstnonNA$age)) 
} 

#interp and extrap age for years that are missing age assuming linearity without jumps 
dat[, imp1 := extrapolate(.SD, year), by="id"] 

#identifying when the age jumps up/down 
dat[, jump:=cumsum(
     (!is.na(age) & imp1!=age) | 
     (!is.na(age) & !is.na(shift(age)) & (age+1)!=shift(age)) 
    ), by="id"] 

#interp and extrap age for years taking into account jumps 
dat[, age_imp1 := extrapolate(.SD, year), by=c("id","jump")] 

#print results 
dat[,c("imp1","jump"):=NULL][] 

#check if the results are identical as requested 
dat[, identical(age_imp, age_imp1)] 
0

私は最終的にこの関数を作成:

impute.age <- function(age) { 
    if (any(is.na(age))) { 
    min.age <- min(age, na.rm = TRUE) 
    position <- which(age == min.age)[1] # ties 
    if (!is.na(position)) { 
    if (position > 1) { # initial values 
    for (i in 1:(position-1)) { 
     age[position - i] <- age[position] - i 
    } 
    } 
    missing <- which(is.na(age)) # missing data position 
    for (i in missing) { 
    age[i] = age[i-1] + 1 
    } 
    } else { age = as.numeric(NA) } 
} 
return(age) 
} 
関連する問題