基于R语言按性别、AgeGroup1计算2011-2017年直接标准化死亡率及95%CI并绘图
在R语言中计算直接标准化死亡率(DSR)及95%置信区间并绘制折线图
1. 依赖包加载
加载数据处理、标准化率计算及绘图所需的R包:
library(dplyr) library(epitools) library(ggplot2)
2. 数据准备
假设你的原始数据集命名为mort_data,包含字段:year(2011-2017年)、sex(性别)、AgeGroup1(年龄组)、deaths(死亡数)、population(人口数);同时需要标准人口数据集standard_pop,包含与原始数据匹配的AgeGroup1及对应standard_pop(标准人口数)。
先统一年龄组顺序,避免计算偏差:
# 替换为你的实际年龄组顺序 mort_data$AgeGroup1 <- factor(mort_data$AgeGroup1, levels = c("<1", "1-4", "5-14", "15-24", "25-34", "35-44", "45-54", "55-64", "65+")) standard_pop$AgeGroup1 <- factor(standard_pop$AgeGroup1, levels = levels(mort_data$AgeGroup1))
3. 计算直接标准化死亡率及95%置信区间
按性别、年份分组,批量计算DSR及置信区间(转换为每10万人口单位):
dsr_results <- mort_data %>% group_by(sex, year) %>% summarise( dsr_output = list(directadj(count = deaths, pop = population, stdpop = standard_pop$standard_pop, conf.level = 0.95)), dsr = dsr_output[[1]]$rate[1] / 1000, lower_ci = dsr_output[[1]]$lower[1] / 1000, upper_ci = dsr_output[[1]]$upper[1] / 1000 ) %>% select(-dsr_output)
注:若directadj返回的率为百分比,除以1000可转换为每10万人口死亡率,可根据需求调整转换系数。
4. 绘制折线图(带置信区间)
用ggplot2绘制趋势折线图,展示不同性别各年份的DSR及置信区间:
ggplot(dsr_results, aes(x = year, y = dsr, color = sex, group = sex)) + geom_line(size = 1) + geom_point(size = 2) + geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci), width = 0.2) + scale_x_continuous(breaks = 2011:2017) + labs( x = "年份", y = "直接标准化死亡率(每10万人口)", color = "性别", title = "2011-2017年不同性别直接标准化死亡率趋势" ) + theme_bw() + theme( plot.title = element_text(hjust = 0.5), legend.position = "top" )
可通过修改geom_line、geom_errorbar参数或scale_color_manual自定义折线样式、颜色。
内容的提问来源于stack exchange,提问作者skpak
相关产品推荐
相关产品推荐

