0

我已经搜索了论坛,但没有找到任何可以回答或提供有关如何在论坛上做我想做的事情的提示。

我有暴露数据的年度测量值,我希望根据每个人进入研究的情况计算个人水平的年平均值。对于每一行,一年暴露分配应包括从加入研究前的最后一个月开始的前 12 个月的数据。例如,样本数据中的第一个人于 2002 年 2 月 7 日加入研究。他的暴露将包括 2002 年 1 月(年平均为 18)和 2001 年 2 月至 2001 年 12 月(年平均为 19)的贡献。此人的时间加权平均值为 (1/12*18) + (11/12*19)。同一个人的两年平均暴露量将从 2002 年 1 月延长到 2000 年 2 月。

同样,对于 2004 年 12 月加入研究的最后一个人,将包括 2004 年 11 个月和 2003 年 1 个月的贡献,他的年平均暴露量将是 (11/12*5) 从 2004 年和 (1/12*6) 推导出来来自 2003 年的年平均值。

我如何计算从进入研究之日起的 1、2 和 5 年平均暴露量?我怎样才能以我所描述的方式使用滞后?

从此链接访问示例数据

https://drive.google.com/file/d/0B_4NdfcEvU7La1ZCd2EtbEdaeGs/view?usp=sharing

4

1 回答 1

2

这不是一个优雅的答案。但是,我想留下我尝试过的东西。我首先整理了数据框。我想确定哪一年将是每个科目的关键年份。所以,我创建了id. variable来自原始数据集中的列名(例如 pol_2000)。entryYear来自entry您的数据。entryMonth也来自entrycheck创建的目的是确定哪一年是每个参与者的基准年。在我的下一步中,我为使用getMyRowsSOfun 包中的每个参与者提取了六行。在下一步中,我使用了lapply并按照您在问题中描述的那样进行数学运算。对于两年/五年平均值的计算,我将总值除以年(2 或 5)。我不确定最终输出会是什么样子。所以我决定为每个科目使用基准年,并在其中添加了三列。

library(stringi)
library(SOfun)
devtools::install_github("hadley/tidyr")
library(tidyr)
library(dplyr)


### Big thanks to BondedDust for this function
### http://stackoverflow.com/questions/6987478/convert-a-month-abbreviation-to-a-numeric-month-in-r

mo2Num <- function(x) match(tolower(x), tolower(month.abb))


### Arrange the data frame.
ana <- foo %>%
       mutate(id = 1:n()) %>%
       melt(id.vars = c("id","entry")) %>%
       arrange(id) %>%
       mutate(variable = as.numeric(gsub("^.*_", "", variable)),
              entryYear = as.numeric(stri_extract_last(entry, regex = "\\d+")),
              entryMonth = mo2Num(substr(entry, 3,5)) - 1,
              check = ifelse(variable == entryYear, "Y", "N"))

### Find a base year for each subject and get some parts of data for each participant.
indx <- which(ana$check == "Y")
bob <- getMyRows(ana, pattern = indx, -5:0)


### Get one-year average
cathy <- lapply(bob, function(x){
    x$one <- ((x[6,6] / 12) * x[6,4]) + (((12-x[5,6])/12) * x[5,4])
    x 
})

one <- unnest(lapply(cathy, `[`, i = 6, j = 8))

### Get two-year average
cathy <- lapply(bob, function(x){
    x$two <- (((x[6,6] / 12) * x[6,4]) + x[5,4] + (((12-x[4,6])/12) * x[4,4])) / 2
    x 
})

two <- unnest(lapply(cathy, `[`, i = 6, j =8))


### Get five-year average
cathy <- lapply(bob, function(x){
    x$five <- (((x[6,6] / 12) * x[6,4]) + x[5,4] + x[4,4] + x[3,4] + x[2,4] + (((12-x[2,6])/12) * x[1,4])) / 5 
    x 
})

five <- unnest(lapply(cathy, `[`, i =6 , j =8))

### Combine the results with the key observations
final <- cbind(ana[which(ana$check == "Y"),], one, two, five)
colnames(final) <- c(names(ana), "one", "two", "five")

#   id     entry variable value entryYear entryMonth check       one       two      five
#6   1 07feb2002     2002    18      2002          1     Y 18.916667 18.500000 18.766667
#14  2 06jun2002     2002    16      2002          5     Y 16.583333 16.791667 17.150000
#23  3 16apr2003     2003    14      2003          3     Y 15.500000 15.750000 16.050000
#31  4 26may2003     2003    16      2003          4     Y 16.666667 17.166667 17.400000
#39  5 11jun2003     2003    13      2003          5     Y 13.583333 14.083333 14.233333
#48  6 20feb2004     2004     3      2004          1     Y  3.000000  3.458333  3.783333
#56  7 25jul2004     2004     2      2004          6     Y  2.000000  2.250000  2.700000
#64  8 19aug2004     2004     4      2004          7     Y  4.000000  4.208333  4.683333
#72  9 19dec2004     2004     5      2004         11     Y  5.083333  5.458333  4.800000
于 2014-10-25T03:17:23.033 回答