Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: Re: Fill Missing values at the End


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Re: Fill Missing values at the End
Date   Thu, 9 Jan 2014 22:24:27 +0000

<>

Different versions of supposedly the same data remain puzzling.

I can get similar but not identical results. As I tend to trust Stata,
and on the whole my own code, and I can't see your code I suspect that
you are not carrying enough decimal places in your calculations.

Here is my revised code. With your rule it is better to separate
backward and forward calculations; otherwise they interfere with each
other.

In my version of your data -name- is string.

1. Find blocks of missing values. We have to -tsset- before using
-tsspell- (downloaded using -ssc inst tsspell-).

encode name, gen(NAME)
tsset NAME year
tsspell , c(missing(y))
egen max = max(_seq), by(name _spell)
drop _seq _end _spell

2. We need to identify first and last known values for each identifier.

gen ismissing = missing(y)
bysort ismissing name (year) : gen first = year[1] if y < .
by ismissing name : gen last = year[_N] if y < .
bysort name (first) : replace first = first[1]
bysort name (last) : replace last = last[1]

3. We are going to use the 1st and 5th known values and the
symmetrically placed values at the end. We do that by blanking out
2nd, 3rd, 4th values (and similarly at the end).
We use linear interpolation on the logarithms.

bysort ismissing name (year) : gen y_early = cond(inlist(_n, 2, 3, 4), ., ln(y))
bysort name (year): ipolate y_early year, gen(y_1) epolate
bysort ismissing name (year) : gen y_late = cond(inlist(_n, _N-3,
_N-2, _N-1), ., ln(y))
bysort name (year): ipolate y_late year, gen(y_2) epolate

4. Now we have the ingredients. We need to exponentiate whatever was logged.

gen y_new = y
replace y_new = exp(y_1) if year < first & max <= 5
replace y_new = exp(y_2) if year > last  & max <= 5
edit


Nick
[email protected]


On 9 January 2014 15:40, Sadia Khalid <[email protected]> wrote:
> Thanks Nick Cox for your reply.
> I am extremely sorry for this. That why i have posted this question again.
>
> Kindly Consider this data
>
> year name y
> 1990 Aqueela 50.1
> 1991 Aqueela 39.9
> 1992 Aqueela 93.4
> 1993 Aqueela 53.4
> 1994 Aqueela 5.9
> 1995 Aqueela 41.6
> 1996 Aqueela
> 1997 Aqueela
> 1998 Aqueela
> 1999 Aqueela
> 2000 Aqueela
> 1990 Abida 57.0
> 1991 Abida 44.3
> 1992 Abida 41.3
> 1993 Abida 92.3
> 1994 Abida 42.2
> 1995 Abida 56.0
> 1996 Abida
> 1997 Abida
> 1998 Abida
> 1999 Abida
> 2000 Abida
> 1990 Zubaida 68.5
> 1991 Zubaida 51.0
> 1992 Zubaida 41.7
> 1993 Zubaida 91.5
> 1994 Zubaida 40.7
> 1995 Zubaida
> 1996 Zubaida
> 1997 Zubaida
> 1998 Zubaida
> 1999 Zubaida
> 2000 Zubaida
>
> What I want to have
>
> year name y
> 1990 Aqueela 50.1
> 1991 Aqueela 39.9
> 1992 Aqueela 93.4
> 1993 Aqueela 53.4
> 1994 Aqueela 5.9
> 1995 Aqueela 41.6
> 1996 Aqueela 42.1
> 1997 Aqueela 42.5
> 1998 Aqueela 43.0
> 1999 Aqueela 43.4
> 2000 Aqueela 43.9
> 1990 Abida 57.0
> 1991 Abida 44.3
> 1992 Abida 41.3
> 1993 Abida 92.3
> 1994 Abida 42.2
> 1995 Abida 56.0
> 1996 Abida 59.3
> 1997 Abida 62.9
> 1998 Abida 66.7
> 1999 Abida 70.7
> 2000 Abida 75.0
> 1990 Zubaida 68.5
> 1991 Zubaida 51.0
> 1992 Zubaida 41.7
> 1993 Zubaida 91.5
> 1994 Zubaida 40.7
> 1995 Zubaida
> 1996 Zubaida
> 1997 Zubaida
> 1998 Zubaida
> 1999 Zubaida
> 2000 Zubaida
>
>
> Kindly do not consider the previous data .
> take this data into consideration.
> Like for the individual Aqueela values for the year 1996 – 2000 are
> missing. I will first calculate the growth rate of values from >
> 1991-1995. the formula for calculating the growth rate is
> =((41.6/39.9)^(1/(5-1))-1). The growth rate is 1.05%.now the value in
> 1996 = value in 1995*(1+growth rate).  And value in 1997= value in
> 1996 * (1+growth rate). And so on.
> Similarly for other individuals.
>
>
> Regards
> Sadia Khalid
>
> On 9 January 2014 18:00, Nick Cox <[email protected]> wrote:
>> <>
>>
>> In fact
>>
>> http://www.stata.com/statalist/archive/2014-01/msg00231.html
>>
>> is not even internally consistent.
>> Nick
>> [email protected]
>>
>>
>> On 9 January 2014 12:53, Nick Cox <[email protected]> wrote:
>>> This question gets more difficult to understand, not less. Let's focus
>>> on Abida. You say you want to get
>>>
>>> 1990 Abida 57.0
>>> 1991 Abida 44.3
>>> 1992 Abida 41.3
>>> 1993 Abida 92.3
>>> 1994 Abida 42.2
>>> 1995 Abida 82.7
>>> 1996 Abida 42.1
>>> 1997 Abida 42.5
>>> 1998 Abida 43.0
>>> 1999 Abida 43.4
>>> 2000 Abida 43.9
>>>
>>> whereas in http://www.stata.com/statalist/archive/2014-01/msg00231.html
>>> the data were stated to be
>>>
>>> 1990 Abida 57.0
>>> 1991 Abida 44.3
>>> 1992 Abida 41.3
>>> 1993 Abida 92.3
>>> 1994 Abida 42.2
>>> 1995 Abida 56.0
>>>
>>> and you said you wanted
>>>
>>> 1996 Abida 96.6
>>> 1997 Abida 113.0
>>> 1998 Abida 132.0
>>> 1999 Abida 154.4
>>> 2000 Abida 180.4
>>>
>>> What's up with 1995?
>>>
>>> What rule gets you from 82.7 to 42.1 (this post)?
>>>
>>> What rule gets you from 56.0 to 96.6 (this post)?
>>>
>>> There couldn't be good answers if the questions make no sense.
>>>
>>> Nick
>>> [email protected]
>>>
>>>
>>> On 9 January 2014 04:01, Sadia Khalid <[email protected]> wrote:
>>>> Sir
>>>> I am new to Stata and do not know how to write codes.
>>>>
>>>> Sir I have posted in the previous messages what i have and what i want to get.
>>>> year name y
>>>> 1990 Aqueela 50.1
>>>> 1991 Aqueela 39.9
>>>> 1992 Aqueela 93.4
>>>> 1993 Aqueela 53.4
>>>> 1994 Aqueela 5.9
>>>> 1995 Aqueela 41.6
>>>> 1996 Aqueela
>>>> 1997 Aqueela
>>>> 1998 Aqueela
>>>> 1999 Aqueela
>>>> 2000 Aqueela
>>>> 1990 Abida 57.0
>>>> 1991 Abida 44.3
>>>> 1992 Abida 41.3
>>>> 1993 Abida 92.3
>>>> 1994 Abida 42.2
>>>> 1995 Abida 56.0
>>>> 1996 Abida
>>>> 1997 Abida
>>>> 1998 Abida
>>>> 1999 Abida
>>>> 2000 Abida
>>>> 1990 Zubaida 68.5
>>>> 1991 Zubaida 51.0
>>>> 1992 Zubaida 41.7
>>>> 1993 Zubaida 91.5
>>>> 1994 Zubaida 40.7
>>>> 1995 Zubaida
>>>> 1996 Zubaida
>>>> 1997 Zubaida
>>>> 1998 Zubaida
>>>> 1999 Zubaida
>>>> 2000 Zubaida
>>>>
>>>>
>>>> What I want to get.
>>>> year name y
>>>> 1990 Aqueela 50.1
>>>> 1991 Aqueela 39.9
>>>> 1992 Aqueela 93.4
>>>> 1993 Aqueela 53.4
>>>> 1994 Aqueela 5.9
>>>> 1995 Aqueela 41.6
>>>> 1996 Aqueela 42.1
>>>> 1997 Aqueela 42.5
>>>> 1998 Aqueela 43.0
>>>> 1999 Aqueela 43.4
>>>> 2000 Aqueela 43.9
>>>> 1990 Abida 57.0
>>>> 1991 Abida 44.3
>>>> 1992 Abida 41.3
>>>> 1993 Abida 92.3
>>>> 1994 Abida 42.2
>>>> 1995 Abida 82.7
>>>> 1996 Abida 42.1
>>>> 1997 Abida 42.5
>>>> 1998 Abida 43.0
>>>> 1999 Abida 43.4
>>>> 2000 Abida 43.9
>>>> 1990 Zubaida 68.5
>>>> 1991 Zubaida 51.0
>>>> 1992 Zubaida 41.7
>>>> 1993 Zubaida 91.5
>>>> 1994 Zubaida 40.7
>>>> 1995 Zubaida
>>>> 1996 Zubaida
>>>> 1997 Zubaida
>>>> 1998 Zubaida
>>>> 1999 Zubaida
>>>> 2000 Zubaida
>>>>
>>>>
>>>> actually the code is not giving the above mentioed results.
>>>>
>>>> Please help me out.
>>>>
>>>>
>>>>
>>>>
>>>> Regards
>>>> Sadia Khalid
>>>> *
>>>> *   For searches and help try:
>>>> *   http://www.stata.com/help.cgi?search
>>>> *   http://www.stata.com/support/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index