具有聚合的散点图

Scatter plot with aggregation

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

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

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

Stata 中的可视化旨在可视化内存中的数据(或其子集),而不是内存中数据的转换。有人可能有这种情况的示例,但我想不出允许您动态转换数据的可视化命令。

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

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

(由于您没有提供样本数据进行测试,请保留任何拼写错误)