R中Interrupted time series分析:合并多机构异质时间序列数据集
问题
我正在使用R开展中断时间序列分析(Interrupted Time Series Analysis, ITS),评估一项干预措施对结局变量的有效性,该干预已在10家不同机构实施。最初计划对各机构分别分析,但希望合并10家机构的数据集进行整体分析,却遇到问题:各机构的基线时长、干预实施日期及干预持续时长均存在差异。
我尝试将数据框转换为时间序列后用merge函数合并,但因intervention列不一致出现问题。我认为缩短基线或干预时长会引入数据偏倚,特此求教如何对齐此类时间序列数据以开展ITS分析?
样本数据
# Dataset 1 time <- c("1", "2", "3", "4", "5", "6", "7", "8") week <- c("32", "33", "34", "35", "36", "37", "38", "39") year <- c("2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020") outcome <- c(56, 76, 86, 96, 73, 87, 47, 98) intervention <- c(0, 0, 0, 1, 1, 1, 1, 1) organisation1_df <- data.frame(time, week, year, outcome, intervention) # Dataset 2 time <- c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12") week <- c("12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23") year <- c("2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020") outcome <- c(33, 44, 45, 34, 45, 34, 33, 32, 32, 32, 33, 43) intervention <- c(0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1) organisation2_df <- data.frame(time, week, year, outcome, intervention)
数据预览
> organisation1_df time week year outcome intervention 1 1 32 2020 56 0 2 2 33 2020 76 0 3 3 34 2020 86 0 4 4 35 2020 96 1 5 5 36 2020 73 1 6 6 37 2020 87 1 7 7 38 2020 47 1 8 8 39 2020 98 1 > organisation2_df time week year outcome intervention 1 1 12 2020 33 0 2 2 13 2020 44 0 3 3 14 2020 45 0 4 4 15 2020 34 0 5 5 16 2020 45 0 6 6 17 2020 34 0 7 7 18 2020 33 0 8 8 19 2020 32 1 9 9 20 2020 32 1 10 10 21 2020 32 1 11 11 22 2020 33 1 12 12 23 2020 43 1
尝试过的代码
merged <- merge(organisation1, organisation2, by="time", all = T)
该代码因intervention列不一致导致合并后数据无法用于分析。
解决方案
你不需要把数据合并成宽格式,而是应该采用长格式(long format)整合所有机构的数据,同时创建相对时间变量来对齐干预节点,再用面板数据模型进行ITS分析,这样既保留所有原始数据,又能避免偏倚。
步骤1:为每个机构添加标识并整合数据
首先给每个机构的数据集添加唯一标识,然后用bind_rows合并成一个长格式数据框:
library(dplyr) # 添加机构标识 organisation1_df <- organisation1_df %>% mutate(org_id = "org1") organisation2_df <- organisation2_df %>% mutate(org_id = "org2") # 合并所有机构数据 combined_data <- bind_rows(organisation1_df, organisation2_df) # 将time转换为数值型,方便后续计算 combined_data <- combined_data %>% mutate(time = as.numeric(time))
步骤2:创建相对时间变量
相对时间变量以干预实施的时间点为0,干预前的时间为负数,干预后的时间为正数。首先需要确定每个机构的干预起始时间(即intervention从0变为1的第一个time值):
# 提取每个机构的干预起始时间 intervention_start <- combined_data %>% filter(intervention == 1) %>% group_by(org_id) %>% summarise(start_time = min(time)) # 合并到总数据,并计算相对时间 combined_data <- combined_data %>% left_join(intervention_start, by = "org_id") %>% mutate(relative_time = time - start_time)
步骤3:构建合并ITS模型
对于多机构的ITS分析,推荐使用混合效应模型(mixed-effects model),可以控制机构间的异质性,同时估计干预的整体效应。这里用lme4包示例:
library(lme4) # 构建模型:包含相对时间的线性趋势、干预分组、干预后的趋势变化,以及机构的随机截距和随机斜率 its_model <- lmer(outcome ~ relative_time + intervention + relative_time:intervention + (1 + relative_time | org_id), data = combined_data) # 查看模型结果 summary(its_model)
关键说明
- 长格式数据保留了所有机构的原始观测,不会因为截断数据引入偏倚。
- 相对时间变量统一了不同机构的干预节点,让模型能正确识别干预前后的趋势变化。
- 混合效应模型中的随机效应可以捕捉不同机构在基线水平和时间趋势上的差异,同时估计干预的平均效应。
内容的提问来源于stack exchange,提问作者lb_2230
相关产品推荐
相关产品推荐

