Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: RE: coding problem: looping through a list of ID's


From   Jeph Herrin <junk@spandrel.net>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: coding problem: looping through a list of ID's
Date   Mon, 24 Nov 2008 13:40:48 -0500


I'm sure this is a very interesting problem, but it is hard to see
quite what you want or where you are starting. You say you reshaped
the data twice, which may have helped you or may have distorted your
observations.

Assuming your original data looks like this:

 ID   dov   height
 1
 1
 .
 .
 2
 2
 .


and what you want is average annual increase, then

  bysort ID (dov) : gen rate=(height-height[_n-1])/(dov-dov[_n-1])
  bysort ID: egen average=mean(rate)

The first line will give you the rate of increase/day for each subject
at each visit relative to the last (note that -rate- will be missing
for the first visit). The second line then averages this rate over all
visits.

Note that the rate will be in height/day, you will want to multiply
by 365.25 to get growth/year.

Hope this helps.

Jeph



Leny Mathew wrote:
Figured as much, but at this point any comments would be useful for me!!

When I got this data set each patient had a different number of visits
over the period of time and not everyone visits over whole period. I
tried writing this code for that but couldn't get it to loop over
patient id, so had to create a max(number of visits) per person and
use that value as a break point. The I changed the data into wide
format and when I reshaped it back to long, I ended up with the
everybody having the maximum of 148 visits. This was a boon in a sense
because I didn't have to worry about the max number of visits per
person (a problem as each loop takes an awful lot of time!) Some
people would have age as missing in some rows but I figured that it
wouldn't be a problem in the loop as that difference would be set to
missing and that wouldn't be an issue.
Since I needed to create an age interval I thought that age could be
used in the same way as I would use date, just that I would set 1 year
instead of the number of days elapsed.

The way this is set up I'm trying to take the difference (within each
patient) of age[i]- age[j] where i takes all values 148 through 1 and
j does the same. So I can calculate the all age differences and if the
age difference is between approximately 1 year ( 0.95, 1.5) then I can
calculate the height difference for those ages.

The data is as follows:

pt_id  counter num  timec age     dov
1       1          148   1        10.1    1 jan 01
1       2          148   2        12.1    1 jan 03
.
.
1      147        148  147     .          .
1      148        148  148     16.1    ..
2      149        148   1        5.0     2 Jan 98
2      150        148   2        5.5     2 Jun 98
.
.
.



****************************************************************************************************************

On Mon, Nov 24, 2008 at 10:34 AM, Nick Cox <n.j.cox@durham.ac.uk> wrote:
I don't know how you propose to withdraw this if anyone objects. For the
record, I can't see any violation of protocol here, just a question that
looks unlikely to get much of an answer.

You may be lucky, but few people want to wade through code as convoluted
as this, especially when you advertise in advance that it is wrong.

I note that despite your statement that "not everybody has 148
measurements" you are jumping by 148 in your code. That sounds like a
recipe for problems.

I note also that you are not using dates in your calculations, but
counters.

I suggest you start again and show us the structure of the data and
define more precisely how height increase over a year is to be defined
from measurements irregular in time.

Nick
n.j.cox@durham.ac.uk

Leny Mathew
                         I've have been using stata for a while but
am a novice when it comes to loops and macros. I'm hoping that someone
on the list could help with with the following problem. I have a data
set with 74 patients each with 148 measurements of height over a
period of 10 years. The height measurements for all cases are not
necessarily at the same time and not everybody has 148 measurements.
The number 148 is a result of changing the data set into the long
form.
I'm trying to calculate the the the increase in height for a one year
(approximately) interval for each case. I developed the following code
for this purpose, but am not able to get it to work perfectly. It
loops though each patient, but at the end, 'phv' end up being the same
for everyone in the data.  Also, this code might be a totally
convoluted way of doing this and I'm hoping that someone could give me
some pointers on how to improve/ revamp this completely.
If this posting is a violation of the list protocol on type of
posting, please feel free to let me know and I'll take it off. I've
spent quite some time tweaking this and am out of ideas.
I'm using stata10.1 Any suggestions are much appreciated.

Note: 'dov'  is date of visit, used to create age.

sort pt_id dov
by pt_id: gen timec=_n

gen counter=_n

sort pt_id dov
by pt_id: gen num=_N

local j=counter
local i=1
local k=1
while pt_id <75 {
                display pt_id[_n]
                while (pt_id[`j']==pt_id[`j'+1]) & timec < 148{
                local value= max(`j'+147, num[pt_id])
                                                while (`value' >
max(`j',1)) & (`value' > timec[`j']) & (pt_id[`j']==`k') {
                                                replace phv=
(pt_ht[`value']-pt_ht[counter[`i']]) if
((age[`value']-age[counter[`i']]) >0.95 &
(age[`value']-age[counter[`i']]) < 1.5) & (pt_id[`j']==`k')
                                                local i= `i'+1
                                                if `i'==`value' local
value= `value'-1
                                                if `i'==`value'+1 &
pt_id[`j']==1 local i=1
                                                if `i'==`value'+1 &
(pt_id[`j'] >1) local i=(`k'-1)*148
                                                }
                 local i=`i'+148
                 local j=`j'+148
                 display `j'
                 local k=`k'+1
                 display `k'
                 continue
                 if k >74 break
                 }
}


**I tried to use phv[`value'] so that it would replace the value at
the certain row, but that gave me an error that weights are not
allowed. (I'm sure that that must have been a violation of stata
rules!)

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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