在Stata 12中散布集群

时间:2017-10-04 17:54:00

标签: plot graph stata scatter

我创建了两个不同的集群,一个使用kmeans(分区方法),另一个使用完整链接(分层)。

使用分层方法的群集:

cluster completelinkage area age, name(hcm_5) measure(L2)
cluster generate c1 = group(5), name(hcm_1)

使用分区方法的群集:

cluster kmeans area age, k(5) name(pcm_1)

这会创建两个不同的群集。现在我想直观地看到创建的集群。我想做一个散点图,并根据pcm_1和c1的值显示不同的颜色。但是,我只能为一个语句创建一个分散检查。

例如:

scatter ycoord xcoord if pcm_1==1, mcolor(red)
scatter ycoord xcoord if pcm_1==2, mcolor(green)
... 
scatter ycoord xcoord if pcm_1==5, mcolor(purple)

如何在同一个图表中获取所有这些散点图,并根据每个观察值中变量的值使用不同的颜色?

1 个答案:

答案 0 :(得分:1)

这是如何做到这一点的基本思路:

ssc install labutil
sysuse auto 
separate price , by(rep78) gen(price_)
labvarch price_*, after("== ")
tw scatter price_* weight
drop price_*

如果您将每个群集的y值存储在他们自己的变量中,则可以更轻松地一次性绘制它们。

您也可以像这样手动完成:

tw (scatter price weight if rep78==1) (scatter price weight if rep78==2) (scatter price weight if rep78==3) (scatter price weight if rep78==4) (scatter price weight if rep78==5)