Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I understand that you want to include in your regression only those groups for which there are exactly 4 observations. If this is the case, then one solution is to count the number of observations per group and condition the regression using <code>if</code>:</p> <pre><code>clear all set more off webuse nlswork xtset idcode list idcode year in 1/50, sepby(idcode) bysort idcode: gen counter = _N xtreg ln_w grade age c.age#c.age ttl_exp c.ttl_exp#c.ttl_exp tenure /// c.tenure#c.tenure 2.race not_smsa south if counter == 12, be </code></pre> <p>In this example the regression is conditioned to groups with 12 observations. The <code>xtreg</code> command gives (among other things):</p> <blockquote> <p>Number of obs = 1881</p> <p>Number of groups = 158</p> </blockquote> <p>which you can compare with the result of running the regression without the <code>if</code>:</p> <blockquote> <p>Number of obs = 28091</p> <p>Number of groups = 4697</p> </blockquote> <p>As commented by @NickCox, if you don't mind losing observations you can <code>drop</code> or <code>keep</code> (un)desired groups:</p> <pre><code>bysort idcode: drop if _N != 4 </code></pre> <p>or</p> <pre><code>bysort idcode: keep if _N == 4 </code></pre> <p>followed by an unconditional <code>xtreg</code> (i.e. with no <code>if</code>).</p> <p>Notice that both approaches count missings, so you may need to account for that.</p> <p>On the other hand, you might want to think about why you want to discard that data in your analysis.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload