【问题标题】:Scatter clusters in Stata 12Stata 12 中的分散集群
【发布时间】:2025-12-09 19:50:01
【问题描述】:

我创建了两个不同的集群,一个使用 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)

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

【问题讨论】:

    标签: plot graph stata scatter


    【解决方案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)
    

    【讨论】: