# Re: st: gr tw (scatter if) (scatter if), by()

 From jpitblado@stata.com (Jeff Pitblado, Stata Corp.) To statalist@hsphsun2.harvard.edu Subject Re: st: gr tw (scatter if) (scatter if), by() Date Mon, 05 May 2003 13:15:47 -0500

```David Airey <david.airey@vanderbilt.edu> asks about using the -by()- option,
with multiple overlays and the -if- option:

> .scatter lens_average eye_nolens if parity <= 9, by(parity)
>
> gives a 3 x 3 graph of nine plots (parity 1, 2 to 9)
>
> .tw lfit lens_a eye_n if parity <= 9, by(parity)
>
> gives a 3 x 3 graph of nine plot
>
> .tw (scatter lens_average eye_nolens if parity <= 9) ///
> 	(lfit lens_average eye_nolens if parity <= 9), by(parity)
>
> gives the message:
>
> . tw (scatter lens_average eye_nolens if parity <= 9) ///
>  >         (lfit lens_average eye_nolens if parity <= 9), by(parity)
> no observations
>
> but completes a graph (3 x 4) leaving three holes in positions 10-12.
>
> Is this behavior intended?
>
> Given the first two graphs, I would have thought the by() option would
> travel through the overlay of the plots properly. It doesn't, at least
> not the way I intended.
>
> Maybe I should have simply done,
>
> preserve
> drop if parity > 9
> tw (scatter lens_average eye_nolens) ///
>     (lfit lens_average eye_nolens), by(parity)
> restore

So David would like to have each scatter plot in:

. tw scatter lens_average eye_nolens if parity <= 9, by(parity)

overlayed with the regression line(s) from (respectively):

. tw lfit lens_average eye_nolens if parity <= 9, by(parity)

And David tried:

. tw (scatter lens_average eye_nolens if parity <= 9) ///
(lfit lens_average eye_nolens if parity <= 9), by(parity)

but didn't get what he wanted.

In the first two graph commands the if-condition "if parity <= 9" applied to
the graph "globally", and thus restricted the data available to the -by()-
option.  In the third command, the "if parity <= 9" conditions are now
"local", thus the -by()- option is not restricted to only work with the data
that allowed by the (albeit common) if conditions.

The solution for David's problem is to specify the -if- restriction globally:

. tw (scatter lens_average eye_nolens) ///
(lfit    lens_average eye_nolens) ///
if parity <= 9, by(parity)

--Jeff