Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: RE: counting across individuals within groups


From   Buzz Burhans <[email protected]>
To   [email protected]
Subject   Re: st: RE: counting across individuals within groups
Date   Thu, 30 Jan 2003 13:19:17 -0500

thanks Nick,

I still can't get what I need. What you suggested gives me a count of all the cows on a farm that have entered the pen cumulative as of the date they enter, and unconditioned on whether the other cows that entered the pen on a given farm are still present in the pen when a particular cow leaves. Note that farm and pen are different, all cows with the same farmid are present on the farm, but only a subset are present in the pen on any given date, and the cohort present on the day any given cow exits the pen is conditional on the dates the other cows entered and exited. It is a count of that subset cohort that I need, and presence in the cohort may be different for each "ExitiingCow" and is conditional on several factors relative to each unique "ExitingCow" as follows:

1. farmid is matched with farmid of "ExitingCow"
AND
2. subset cohort cows must have enterPen dates < exitPen dates of the particular ExitingCow I am making the count for (i.e. cohort cows entered the pen before the ExitingCow left it)
AND
3. subset cohort cows must have exitPen dates > exitPen date of the ExitingCow (i.e. cohort cows had to still be present in the pen when ExitCow left.

that is, cohort cows have to be on the same farm, enter the pen before ExitCow leaves it, and still be there when ExitCow leaves it.

If it helps to make sense of what I am after, I am interested in the association of population density in a pen at the day of exit and outcomes for cows that are in certain management pens, i.e. maternity or treatment pens. I have dates each cow on a farm entered or left the pens in question, what I need is to figure out a count of the cohort present at each cows exit from the pen.

. list farmid cowid enterPEN exitPEN enteredsofar Present if farmid ==3, table clean

farmid cowid enterPEN exitPEN entere~r Count(CowsinPenOnHerExitDate)
50. 3 19 01/04/02 01/18/02 1 3
51. 3 18 01/04/02 01/22/02 2 2
52. 3 20 01/04/02 01/22/02 3 2
54. 3 22 01/18/02 02/04/02 5 1
55. 3 581 03/01/02 03/24/02 11 2
56. 3 25 03/01/02 03/02/02 7
57. 3 26 03/01/02 03/21/02 9
58. 3 582 03/01/02 03/22/02 10
59. 3 579 03/01/02 03/12/02 8
60. 3 580 03/08/02 04/01/02 12
61. 3 583 03/27/02 04/03/02 13
62. 3 13 03/27/02 04/06/02 14

The entere~r is the var that resulted from my test of your code. The "Count" is what I am after. For example, look at cowid 581. 10 cows entered before she exits, but 4 cows that entered before she did exited before she exits, so they are not in her cohort at exit. Of the 6 other cows that entered before she exited, 4 not only entered, they also exited before she did, so they are also not there when she exits. Only cow 580 entered before she exited, but had not exited yet, so the cohort at her exit is 581 and 580.

Thanks for following my ruminations.

Buzz Burhans
Department of Animal Science
Cornell University


At 04:45 PM 1/30/03 +0000, you wrote:

Buzz Burhans ruminated
>
> I would appreciate help with creating the following value,
> I can't seem to
> get it right:
>
> I have data with 4 vars in each record as follows:
>
> 1. farmID     is an ID variable for farm
> 2. cowID      is an ID variable for cow (within farmID)
> 3  enterPen   is the date on which a cow enters Pen
> 4  exitPen    is the date on which the cow exits Pen
>
> There are multiple cows within each farm, and their
> enterPen and exitPen
> dates vary individually.
>
> What I need is a variable that contains the count of cows
> within the same
> farmID which have entered, but not yet exited the Pen on the date an
> individual cow exits the Pen,
>
> i.e.a count of  "other cows" within the same farmID whose
> enterPen date is
> < exitPen date for each individual cow (within the same
> farmID) and whose
> exitPen date (the "other cows" ) is > the exitPen date for
> the individual
> cow ...
>
> I can't seem to get the ifs to work correctly across
> different records,
> probably because I'm not very good at looping.
>

There may be a way to approach this with -st-
but -st- is not my daily bread, so I will
pass on that and try a direct approach.

I don't think -if- enters here, nor need
any looping. I surmise that you can get exactly
what you want with -by-. It's just that I
don't understand exactly what you want.

Any cow is still within the farm until the
day it exits. I don't see that -enterPen-
is relevant so long as -enterPen- <= -exitPen-.

A step towards what you want is, I think,

bysort farmID (exitpen) : gen enteredsofar = _n
qui by farmID exitpen :
        replace enteredsofar = enteredsofar[_N]

The number of other cows appears to be central,
which means subtract 1 from this, I guess.

Nick
[email protected]

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index