【问题标题】:Scatter plot with aggregation聚合散点图
【发布时间】:2021-11-24 08:20:03
【问题描述】:

我刚开始使用 Stata,所以我搜索了很多,但找不到任何用于在绘图中聚合值的参数。

scatter x y if z==0 || scatter x y if z==1

但是,我不想显示所有观察结果,而是想显示 x 的 y 平均值。 有没有办法做到这一点,还是我需要单独计算平均值 40 次(对于 20 x 和 2 z)?

【问题讨论】:

    标签: stata


    【解决方案1】:

    Stata 中的可视化旨在可视化内存中的数据(或其子集),而不是内存中数据的转换。有人可能有一个例子说明这种情况,但我想不出一个可视化命令可以让你动态转换数据。

    但是,我可以帮助您展示我将如何在 Stata 中进行此转换,这样您就无需计算 40 次的平均值,以及如何将数据集恢复到转换前的状态。

    preserve  // After restore below the dataset will be reverted to how it was here
    
      * Calculate the mean of x by each level of y and z. This aggregates the data
      * so that the new unit of observation is the combination of the units in y and z.
      collapse (mean) x, by(y z)  // (mean) is redundant as it is the default, but included for clarity
    
      * Generate the plot
      scatter x y if z==0 || scatter x y if z==1
    
    restore // Restore the data to its state before the aggregation
    

    (保留任何错别字,因为您没有提供样本数据进行测试)

    【讨论】:

    • 另一种选择是 egen mean = mean(x), by(y z) 后跟 egen tag = tag(y z)scatter mean y if tag & z == 0 等等。这消除了对preserverestore 的任何需求,尽管这两种方式各有利弊。
    猜你喜欢
    • 2014-08-24
    • 1970-01-01
    • 2020-05-15
    • 2015-02-18
    • 2013-02-20
    • 1970-01-01
    • 2021-05-01
    • 2013-05-18
    • 1970-01-01
    相关资源
    最近更新 更多