Buzz Burhans
>
> 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.
I can not hold all your detail in my head
at the same time, but I think I can move closer
to where you want to be.
I suspect you wouldt benefit from mapping to
a different data structure.
First, -rename- -enterPEN- and -exitPEN-
. rename enterPEN PEN1
. rename exitPEN PEN2
Now -reshape-
. reshape long PEN , i(farmID cowID)
You get something like
+---------------------------------+
| farmID cowID _j PEN |
|---------------------------------|
1. | 3 13 1 Mar/27/02 |
2. | 3 13 2 Apr/6/02 |
3. | 3 18 1 Jan/4/02 |
4. | 3 18 2 Jan/22/02 |
5. | 3 19 1 Jan/4/02 |
|---------------------------------|
6. | 3 19 2 Jan/18/02 |
7. | 3 20 1 Jan/4/02 |
8. | 3 20 2 Jan/22/02 |
9. | 3 22 1 Jan/18/02 |
10. | 3 22 2 Feb/4/02 |
|---------------------------------|
11. | 3 25 1 Mar/1/02 |
12. | 3 25 2 Mar/2/02 |
13. | 3 26 1 Mar/1/02 |
14. | 3 26 2 Mar/21/02 |
15. | 3 579 1 Mar/1/02 |
|---------------------------------|
16. | 3 579 2 Mar/12/02 |
17. | 3 580 1 Mar/8/02 |
18. | 3 580 2 Apr/1/02 |
19. | 3 581 1 Mar/1/02 |
20. | 3 581 2 Mar/24/02 |
|---------------------------------|
21. | 3 582 1 Mar/1/02 |
22. | 3 582 2 Mar/22/02 |
23. | 3 583 1 Mar/27/02 |
24. | 3 583 2 Apr/3/02 |
+---------------------------------+
Now let us mark 1 for each entry and -1
for each exit.
. gen change = cond(_j == 1, 1, -1)
If we cumulate that in day order we
get a tally #beasts at the end of
each day
. bysort farmID (PEN) : gen no = sum(change)
. bysort farmID PEN : replace no = no[_N]
. l
+-----------------------------------------------+
| farmID cowID _j PEN change no |
|-----------------------------------------------|
1. | 3 19 1 Jan/4/02 1 3 |
2. | 3 20 1 Jan/4/02 1 3 |
3. | 3 18 1 Jan/4/02 1 3 |
4. | 3 22 1 Jan/18/02 1 3 |
5. | 3 19 2 Jan/18/02 -1 3 |
|-----------------------------------------------|
6. | 3 18 2 Jan/22/02 -1 1 |
7. | 3 20 2 Jan/22/02 -1 1 |
8. | 3 22 2 Feb/4/02 -1 0 |
9. | 3 581 1 Mar/1/02 1 5 |
10. | 3 26 1 Mar/1/02 1 5 |
|-----------------------------------------------|
11. | 3 582 1 Mar/1/02 1 5 |
12. | 3 579 1 Mar/1/02 1 5 |
13. | 3 25 1 Mar/1/02 1 5 |
14. | 3 25 2 Mar/2/02 -1 4 |
15. | 3 580 1 Mar/8/02 1 5 |
|-----------------------------------------------|
16. | 3 579 2 Mar/12/02 -1 4 |
17. | 3 26 2 Mar/21/02 -1 3 |
18. | 3 582 2 Mar/22/02 -1 2 |
19. | 3 581 2 Mar/24/02 -1 1 |
20. | 3 13 1 Mar/27/02 1 3 |
|-----------------------------------------------|
21. | 3 583 1 Mar/27/02 1 3 |
22. | 3 580 2 Apr/1/02 -1 2 |
23. | 3 583 2 Apr/3/02 -1 1 |
24. | 3 13 2 Apr/6/02 -1 0 |
+-----------------------------------------------+
For each cow, you want to know how many were in
the pen when she left
. bysort farmID cowID (_j) : replace no = no[_N]
Throw away -change- and you -reshape- back.
. drop change
. reshape wide
(note: j = 1 2)
Data long -> wide
----------------------------------------------------------------------
-------
Number of obs. 24 -> 12
Number of variables 5 -> 5
j variable (2 values) _j -> (dropped)
xij variables:
PEN -> PEN1 PEN2
----------------------------------------------------------------------
-------
. l
+---------------------------------------------+
| farmID cowID PEN1 PEN2 no |
|---------------------------------------------|
1. | 3 13 Mar/27/02 Apr/6/02 0 |
2. | 3 18 Jan/4/02 Jan/22/02 1 |
3. | 3 19 Jan/4/02 Jan/18/02 3 |
4. | 3 20 Jan/4/02 Jan/22/02 1 |
5. | 3 22 Jan/18/02 Feb/4/02 0 |
|---------------------------------------------|
6. | 3 25 Mar/1/02 Mar/2/02 4 |
7. | 3 26 Mar/1/02 Mar/21/02 3 |
8. | 3 579 Mar/1/02 Mar/12/02 4 |
9. | 3 580 Mar/8/02 Apr/1/02 2 |
10. | 3 581 Mar/1/02 Mar/24/02 1 |
|---------------------------------------------|
11. | 3 582 Mar/1/02 Mar/22/02 2 |
12. | 3 583 Mar/27/02 Apr/3/02 1 |
+---------------------------------------------+
Some details remain ambiguous. Are you counting the cows
_on each day_ before or after entries? before or after
exits?
My presumption remains that the tool of choice
is -by:- but I have added the presumption that these
matters are better done with a long data structure.
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/