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

RE: st: reshape with j split


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: reshape with j split
Date   Mon, 15 Dec 2003 17:19:34 -0000

David Airey

> Nick [Cox] helped me figure out a reshape problem where "j" in
> the reshape
> command is split amongst several variables.
>
> > I think you're moving in the right direction
> > for a -reshape- to wide. As for the ANOVA,
> > your original data structure looks better.
> >
> > egen j = concat(s1*), p("_")
> > drop s1*
> > reshape wide s2peak, i(animal) j(j) string
>
> I was going from long to wide, to calculate a ratio of experimental
> conditions, and back to wide to calculate a mixed model on
> the ratios.
> What he suggested works nicely:
>
> Here I have a sample of the data set:
>
>       +----------------------------------------+
>       | s1level   s1s2de~y   animal   s2peak~e |
>       |----------------------------------------|
>    1. |       0         50   1_1_0F     773.75 |
>    2. |       0        100   1_1_0F    1001.63 |
>    3. |      75         50   1_1_0F      472.5 |
>   13. |       0         50   1_1_2F      309.5 |
>   14. |       0        100   1_1_2F    336.286 |
>   15. |      75         50   1_1_2F    442.625 |
>       +----------------------------------------+
>
> And I wanted to get to:
>
> animal s2peak0_50 s2peak0_100 s2peak75_50 s2peak75_100 s2peak85_50
> s2peak85_100
>
> The following code does just that and then returns to long format
> appropriate to the ANOVA.
>
> * reshape to wide
> egen j = concat(s1*), p("_")
> drop s1*
> reshape wide s2peak, i(animal) j(j) string
>
> . clist in 1/3
>
>         animal  s2~0_100  s2p~0_50  s~75_100  s2~75_50  s~85_100
> s2~85_50
>    1.   1_1_0F   1001.63    773.75   927.875     472.5   654.375
> 611.375
>    2.   1_1_1F   1101.38   1116.88   567.875   544.875       466
> 443.875
>    3.   1_1_2F   336.286     309.5       265   442.625
> 192.375
> 264.5
>
>
> * calculate ppi
> gen startle = (s2peakvalue0_50 + s2peakvalue0_100)/2
> gen ppi75_50 = s2peakvalue75_50 / startle * 100
> gen ppi85_50 = s2peakvalue85_50 / startle * 100
> gen ppi75_100 = s2peakvalue75_100 / startle * 100
> gen ppi85_100 = s2peakvalue85_100 / startle * 100
> drop s2* startle
>
> . clist in 1/3
>
>         animal   ppi75_50   ppi85_50  ppi75_100  ppi85_100
>    1.   1_1_0F   53.22819   68.87277   104.5272   73.71682
>    2.   1_1_1F   49.12656   40.02029   51.20027    42.0151
>    3.   1_1_2F   137.0811   81.91572   82.07057   59.57859
>
> *reshape to long
> reshape long ppi, i(animal) j(treatment) string
> gen str s1level_str = substr(treatment, 1, index(treatment,
> "_") - 1)
> gen str s1s2delay_str = substr(treatment, index(treatment,
> "_") + 1, .)
> encode s1level_str, generate(s1level)
> encode s1s2delay_str, generate(s1s2delay)
> drop *_str treatment
>
> . list, sep(4)
>
>       +----------------------------------------+
>       | animal        ppi   s1level   s1s2de~y |
>       |----------------------------------------|
>    1. | 1_1_0F   104.5272        75        100 |
>    2. | 1_1_0F   53.22819        75         50 |
>    3. | 1_1_0F   73.71682        85        100 |
>    4. | 1_1_0F   68.87277        85         50 |
>       |----------------------------------------|
>    5. | 1_1_1F   51.20027        75        100 |
>    6. | 1_1_1F   49.12656        75         50 |
>    7. | 1_1_1F    42.0151        85        100 |
>    8. | 1_1_1F   40.02029        85         50 |
>       |----------------------------------------|
>    9. | 1_1_2F   82.07057        75        100 |
>   10. | 1_1_2F   137.0811        75         50 |
>
> etc.

An alternative to

> gen str s1level_str = substr(treatment, 1, index(treatment,
> "_") - 1)
> gen str s1s2delay_str = substr(treatment, index(treatment,
> "_") + 1, .)
> encode s1level_str, generate(s1level)
> encode s1s2delay_str, generate(s1s2delay)

is

split treatment, p(_) destring

-split- is, broadly, speaking the inverse of -egen, concat()-.

(-split- doesn't fit within the -egen- framework, because
in general it can yield several variables.)

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/



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