[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: RE: generate variance variable |

Date |
Tue, 17 Dec 2002 12:14:07 -0000 |

Jan Bayer > > We have repeatedly determined a whole bunch of "phenotypic > measures" on > a group of individuals, over a period of three years now. I > would like > to use Stata to perform analyses of the data, but I'm a real newbee. > > In my datalist I have one line per subject (individual) for > every single > time the individual was tested. In columns, I have the > numerical values > for the different phenotypic measures. The same subject > (individual) > does show up several times in the datalist (several lines), and > therefore, for every subject we have multiple independent > determinations > of the same phenotypic values. > > Is it possible to generate new variables containing the > variance of the > measurements, per subject (individual) ? > See help on -egen- which provides a canned -sd()- function. So you could just . bysort id : egen sdpheno = sd(pheno) . gen varpheno = sdpheno^2 But better is to have your own function. Typing . which _gsd shows you where the defining file is on your system. Here is a copy. *! version 3.1.0 30jun1998 program define _gsd version 6 syntax newvarname =/exp [if] [in] [, BY(varlist)] tempvar touse mean quietly { gen byte `touse'=1 `if' `in' sort `touse' `by' by `touse' `by': gen double `mean' = /* */ sum(`exp')/sum((`exp')!=.) if `touse'==1 by `touse' `by': gen `typlist' `varlist' = /* */ sqrt(sum(((`exp')-`mean'[_N])^2)/(sum((`exp')!=.)-1)) /* */ if `touse'==1 & sum(`exp'!=.) by `touse' `by': replace `varlist' = `varlist'[_N] } end The program for a variance could just be therefore ================================ begin _gvar.ado *! _gvar NJC 17 Dec 2002 *! _gsd 3.1.0 30jun1998 program define _gvar version 6 syntax newvarname =/exp [if] [in] [, BY(varlist)] tempvar touse mean quietly { gen byte `touse'=1 `if' `in' sort `touse' `by' by `touse' `by': gen double `mean' = /* */ sum(`exp')/sum((`exp')!=.) if `touse'==1 by `touse' `by': gen `typlist' `varlist' = /* */ sum(((`exp')-`mean'[_N])^2)/(sum((`exp')!=.)-1) /* */ if `touse'==1 & sum(`exp'!=.) by `touse' `by': replace `varlist' = `varlist'[_N] } end ============================== end _gvar.ado What to do: 1. Copy and paste the code above into a text file _gvar.ado in what your Stata calls STBPLUS. Type . sysdir to see where this ado. The underscore _ is essential. 2. Within Stata, your commands are then of the form . bysort id : egen varpheno = var(pheno) Nick n.j.cox@durham.ac.uk * * 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/

**References**:**st: generate variance variable***From:*Jan Bayer <bayer@cephb.fr>

- Prev by Date:
**st: RE: Re: how to extract numeric part of a string** - Next by Date:
**st: RE: RE: Re: how to extract numeric part of a string** - Previous by thread:
**st: generate variance variable** - Index(es):

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