Martan
Martan

Reputation: 605

Scatter plot with aggregation

I am getting started with Stata, so I searched quite a bit, but could not find any argument for aggregating values in a plot.

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

enter image description here

However, instead of showing all observations, I want to show mean values of y for x. Is there a way to do this, or do I need to compute mean individually 40 times (for 20 x and 2 z)?

Upvotes: 0

Views: 789

Answers (1)

TheIceBear
TheIceBear

Reputation: 3255

Visualizations in Stata is meant to visualize the data in memory (or subsets of it) but not transformations of the data in memory. Someone might have an example of where this is the case, but I cannot think of a visualization command that allows you to transform the data on the fly.

I can however help you show how I would do this transformation in Stata so that you do not need to calculate the mean 40 times and how you return the dataset to its state before the transformation.

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

(Reservation for any typos as you did not provide a sample data to test on)

Upvotes: 2

Related Questions