You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Stata中按性别绘制分类变量年度百分比趋势线的技术问询

解决Stata按性别分组绘制分类变量年度百分比趋势线的问题

方法一:先计算分组实际百分比再绘图

这种方法直接基于原始数据计算每年、每性别下各分类的占比,直观展示真实分布趋势。

步骤1:计算分组百分比

在你生成的样本数据基础上执行以下代码:

* 计算每年、每性别下的总样本量
bysort year gender: gen total = _N
* 计算每年、每性别、每分类的样本量
bysort year gender problemcat: gen count = _N
* 转换为百分比
gen pct = (count / total) * 100
* 保留每组唯一值(避免重复绘图点)
bysort year gender problemcat: keep if _n == 1

步骤2:绘制分性别趋势线

用twoway line区分性别和分类的样式,生成合并趋势图:

twoway (line pct year if problemcat == 1 & gender == 1, lcolor(navy) lwidth(medium) label("男性-本年度有问题")) ///
       (line pct year if problemcat == 2 & gender == 1, lcolor(red) lwidth(medium) label("男性-之后有问题")) ///
       (line pct year if problemcat == 3 & gender == 1, lcolor(green) lwidth(medium) label("男性-无问题")) ///
       (line pct year if problemcat == 1 & gender == 2, lcolor(navy) lwidth(medium) lpattern(dash) label("女性-本年度有问题")) ///
       (line pct year if problemcat == 2 & gender == 2, lcolor(red) lwidth(medium) lpattern(dash) label("女性-之后有问题")) ///
       (line pct year if problemcat == 3 & gender == 2, lcolor(green) lwidth(medium) lpattern(dash) label("女性-无问题")), ///
       title("各问题分类年度百分比趋势(按性别分组)") ///
       ytitle("百分比(%)") ///
       xtitle("年份") ///
       legend(cols(2)) ///
       ylabel(0(20)100)

如果需要拆分两张独立的性别趋势图,可替换为以下代码:

twoway line pct year, by(gender) ///
       title("各问题分类年度百分比趋势") ///
       ytitle("百分比(%)") xtitle("年份") ///
       legend(cols(1))

方法二:基于回归模型分性别预测后绘图

若想展示模型拟合的趋势,可通过分性别生成预测值再绘图:

步骤1:重新加载数据并拟合交互模型

* 重新生成原始样本数据
clear 
set obs 100 
set seed 2803 
egen year = seq(), from(2000) to(2010)
egen problemcat = seq(), to(3) block(3)
egen gender = seq(), to(2) block(5)
label def cat 1 problemthisyear 2 problemsincethen 3 noproblem 
label val problemcat cat 

* 生成各分类的指示变量
gen problemthisyear = 1 if problemcat == 1
gen problemsincethen = 1 if problemcat == 2 
gen noproblem = 1 if problemcat == 3

* 拟合包含性别与年份交互项的模型(捕捉分性别趋势)
poisson problemthisyear c.year##i.gender, vce(robust) irr
* 分性别生成预测值
forvalues g = 1/2 {
    predict probyhat_`g', pr(1) if gender == `g'
}

* 对另外两个分类重复拟合与预测
poisson problemsincethen c.year##i.gender, vce(robust) irr
forvalues g = 1/2 {
    predict probsthat_`g', pr(1) if gender == `g'
}

poisson noproblem c.year##i.gender, vce(robust) irr
forvalues g = 1/2 {
    predict probnhat_`g', pr(1) if gender == `g'
}

步骤2:绘制预测趋势线

twoway (line probyhat_1 year, lcolor(navy) lwidth(medium) label("男性-本年度有问题")) ///
       (line probsthat_1 year, lcolor(red) lwidth(medium) label("男性-之后有问题")) ///
       (line probnhat_1 year, lcolor(green) lwidth(medium) label("男性-无问题")) ///
       (line probyhat_2 year, lcolor(navy) lwidth(medium) lpattern(dash) label("女性-本年度有问题")) ///
       (line probsthat_2 year, lcolor(red) lwidth(medium) lpattern(dash) label("女性-之后有问题")) ///
       (line probnhat_2 year, lcolor(green) lwidth(medium) lpattern(dash) label("女性-无问题")), ///
       title("各问题分类年度预测概率趋势(按性别分组)") ///
       ytitle("预测概率") ///
       xtitle("年份") ///
       legend(cols(2))

内容的提问来源于stack exchange,提问作者Sun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 08:30:22