Longitudinal Data Analysis: Stata Tutorial

2y ago
50 Views
4 Downloads
378.70 KB
11 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Melina Bettis
Transcription

Longitudinal Data Analysis: Stata TutorialPart A: Overview of StataI. Reading Data: useRead data that have been saved in Stata format. infileRead raw data and “dictionary” files. insheetRead spreadsheets saved as “CSV” files from a package such as Excel.II. Do Files What is a do file?A “do” file is a set of commands just as you would type them in one-by-oneduring a regular Stata session. Any command you use in Stata can be part of ado file. Do files are very useful, particularly when you have many commands toissue repeatedly, or to reproduce results with minor or no changes.Example: cd4-readin.do* Read in cd4.raw data and create stata data setlog using cd4-readin , replaceset memory 40minfile time cd4 age packs drugs sexpart cesd id using cd4gen timedays id"subject ID"time"years since seroconversion"timedays"days since seroconversion"cd4"CD4 Count"age"age (yrs) relative to arbitrary origin"packs"packs of cigarettes smoked per day"drugs"recreational drug use yes/no"sexpart "number of sexual partners"cesd"depression score relative to arbitrary origin"save cd4 , replaceclearlog closeYou can edit a do file anywhere then save as a file with the extension “.do”. InWindows or Mac, you can type doedit in Stata to edit any do files.

Where to put a do file?Put the do file in the working directory of Stata. How to run a do file?do mydofileExample: do cd4-readinIII. Ado files What is an ado file?An ado file is just a Stata program. You can use it as a command.A *.ado file usually contains a program called * in it.For example, the first non-comment line “autocor.ado” isprogram define autocor Where to put an ado file?Put them in your current directory, in your stata "ado" directory,or in a directory where Stata will know where to look for them.Use “adopath” to find out where Stata is looking for ado files.Here is an example in a Windows PC (Ado directory may bedifferent among different platforms). adopath[1] (UPDATES)[2] (BASE)[3] (SITE)[4][5] (PERSONAL)[6] (STBPLUS)[7] o\stbplus/""c:\ado/" How to run an ado file?Use the name of the program as a command as you use other default Statacommands.For example:. autocor cd4res timeyrs idIV. Convert data from wide to long or vice versa Two forms of data: wide and longDifferent models may require different forms of data in Stata. For instance, “logit”or “logistic” model in Stata prefers a wide format.

(wide form)-i------- x ij -------id 5000550060002120002200330030300020001000(long form)-i- -j-x 8201000 reshape converts data from one form to the other: From Wide to Long. reshape long inc, i(id) j(year) From Long to Wide. reshape wide inc, i(id) j(year) Examples: Cows Data. infile prot1-prot19 using cowslupins. gen id n. order id. list in 1/2Observation ot173.78prot183.77prot193.53Observation 1prot173.28prot183.42prot193.25. reshape long prot , i(id) j(week)(note: j 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19)Datawide- ------------------------------Number of obs.513- 27Number of variables3- 20j variable (19 values)week- (dropped)xij variables:prot- prot1 prot2 . --------------------------------. list in 1/20idweekprot

43.73.783.783.773.534.2. reshape wide prot, i(id) j(week)(note: j 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19)Datawide- ------------------------------Number of obs.513- 27Number of variables3- 20j variable (19 values)week- (dropped)xij variables:prot- prot1 prot2 . --------------------------------. list in 1/2Observation ot173.78prot183.77prot193.53Observation 1prot173.28prot183.42prot193.25

Part B: Longitudinal data analysis in StataI. Convert an ordinary dataset into a longitudinal dataset (cross-sectional timeseries data): use tsset vs. iis, tis “tsset” declares ordinary data to be time-series data, Simple time-series data: one panel Cross-sectional time-series data: multi-panel Each observation in a cross-sectional time-series (xt) dataset is anobservation on x for unit i (panel) at time t. For this course, we use cross-sectional time-series data. Syntax for “tsset” for cross-sectional time-series data:. tsset panel timevarExample:. infile time cd4 age packs drugs sexpart cesd id using cd4(2376 observations read). iis(i() has not been defined). tis(t() has not been defined). tsset id timetime variable must contain only integer valuesr(451);. list time in 1/10time1. -.7419582. -.2464073.2436694. -2.7296375. -2.2505136. -.2217667.2217668.7748129. 1.25667310. -1.240246. gen timedays round(time*365.25,1). list time timedays in 1/10time1. -.7419582. -.2464073.2436694. -2.7296375. -2.2505136. 8181283

9. 1.25667310. -1.240246459-453. tsset id timedayspanel variable:time variable:id, 10002 to 41844timedays, -1092 to 1994, but with gaps. iisi() is id. tist() is timedays Alternative Way: iis & tisiis idtis timedays Some commands require tsset (built-in xt commands), others require iis andtis. For this course, mostly we are using iis and tis.II. xt commandsThe xt series of commands provide tools for analyzing cross-sectional time-series(panel) datasets: xtdesDescribe pattern of xt dataExample: Cows data. use cows. keep if (diet "barley"). drop if (prot .). xtdes, patterns(0)id:week:1, 2, ., 251, 2, ., 19Delta(week) 1; (19-1) 1 19(id*week uniquely identifies each observation)Distribution of T i:. xtdes, patterns(5)id:week:5%1425%1550%181, 2, ., 251, 2, ., 19Delta(week) 1; (19-1) 1 19(id*week uniquely identifies each observation)Distribution of T i:Freq.min12Percentmin12Cum. 5%14Pattern25%1550%18n T 75%19251995%19n T 75%19max19251995%19max19

--------------------------- --------------------1144.0044.00 1111111111111111111520.0064.00 11111111111111.28.0072.00 111111111111111111.28.0080.00 111111111111111.28.0088.00 1111111111111111.312.00 100.00 (other patterns)--------------------------- --------------------25100.00 XXXXXXXXXXXXXXXXXXX. xtdes//default number of patterns is 9id:week:1, 2, ., 251, 2, ., 19Delta(week) 1; (19-1) 1 19(id*week uniquely identifies each observation)Distribution of T i:min125%1425%1550%18n T 75%19251995%19max19Freq. PercentCum. Pattern--------------------------- --------------------1144.0044.00 1111111111111111111520.0064.00 11111111111111.28.0072.00 1111111111111111.28.0080.00 111111111111111111.28.0088.00 111111111111111.14.0092.00 11111111.1.111.14.0096.00 1.1111111111111111114.00 100.00 11111111.1111111111--------------------------- --------------------25100.00 XXXXXXXXXXXXXXXXXXXOther xt commands: xtsum Summarize xt dataPaul has a improved version: xtsumcorr. xttab xtregmodels xtdata xtlogit xtprobit xttobit xtpois xtnbregmodels xtclogTabulate xt dataFixed-, between- and random-effects, and population-averaged linearFaster specification searches with xt dataFixed-effects, random-effects, & population-averaged logit modelsRandom-effects and population-averaged probit modelsRandom-effects tobit modelsFixed-effects, random-effects, & population-averaged Poisson modelsFixed-effects, random-effects, & population-averaged negative binomialRandom-effects and population-averaged cloglog models

xtintregxtrchhxtglsxtgeeRandom-effects interval data regression modelsHildreth-Houck random coefficients modelsPanel-data models using GLSPopulation-averaged panel-data models using GEELook “help xt” in StataIII. Graphs for longitudinal data xtgraphA new command for summary graphs of xt data (cross-sectional time series data).Download the xtgraph.ado file from course website.Syntax:xtgraph varname [if] [in] , group(groupvar) av(avtype) bar(bartype)graph options xt optionsChoice of averagextgraph , av(avtype)The average types are am - arithmetic mean, the default gm - geometric mean hm - harmonic mean median - only with bars ci - default, iqr or rr.Choice of error barsxtgraph , bar(bar type)level(significance level)The bar types are ci - the default, significance set by level() se - standard error sd - standard deviation rr - reference range, level set by level() iqr -same as bar(rr) level(50) no - no bars

Examples:. xtgraph prot, av(median) bar(iqr) t1("median, iqr")median, iqrProtein content4.23.1451Week19. xtgraph prot, av(am) bar(se) t1("arithmetic mean, se")arithmetic mean, seProtein content3.962463.330281WeekRefer to xtgraph.pdf or xtgraph.hlp for help.19

How to graph trajectoriesIn the lectures notes, Paul gave an example to draw trajectories usingsubjects picked based on ranking of within-subject statistics (the differencein the medians before and after HIV seroconversion).Other examples: A random set (trajectory1.do)*trajectory.do file for Stata 6.0clearuse cd4egen newid group(id)sum newiddrop idren newid idsort id timedaysgen pick 0local i 1while i' 8{set seed i'local r round(1 uniform()*369,1)gen cd4l i' cd4 if (id r')local i i' 1}ksm cd4 timedays, lowess gen(cd4smth) nographgraph cd4 cd4l1-cd4l7 cd4smth timedays, c(.LLLLLLL.) s(.iiiiiiio)pen(233333334) xlab ylabCD4 Countcd4smth3000200010000-100001000days since seroconversion2000

Ranking with the individual mean CD4 counts (trajectory2.do)*trajectory.do file for Stata 6.0clearuse cd4egen newid group(id)sum newiddrop idren newid idegen cd4mean mean(cd4), by(id)list id cd4 cd4mean in 1/10sort idquietly by id: replace cd4mean . if ( n 1)egen rnk rank(cd4mean)local i 1while i' 7{gen sub i' (rnk i'*25)sort id timedaysquietly by id: replace sub i' sub i'[1]gen cd4l i' cd4 if (sub i')drop sub i'local i i' 1}ksm cd4 timedays, lowess gen(cd4smth) nographgraph cd4 cd4l1-cd4l7 cd4smth timedays, c(.LLLLLLL.) s(.iiiiiiio)pen(233333334) xlab ylabCD4 Countcd4smth3000200010000-100001000days since seroconversion2000

Longitudinal Data Analysis: Stata Tutorial Part A: Overview of Stata I. Reading Data: use Read data that have been saved in Stata format. infile Read raw data and “dictionary” files. insheet Read spreadsheets saved as “CSV” files from a package such as Excel. II. Do Files What is a do file?

Related Documents:

Stata is available in several versions: Stata/IC (the standard version), Stata/SE (an extended version) and Stata/MP (for multiprocessing). The major difference between the versions is the number of variables allowed in memory, which is limited to 2,047 in standard Stata/IC, but can be much larger in Stata/SE or Stata/MP. The number of

Categorical Data Analysis Getting Started Using Stata Scott Long and Shawna Rohrman cda12 StataGettingStarted 2012‐05‐11.docx Getting Started Using Stata – May 2012 – Page 2 Getting Started in Stata Opening Stata When you open Stata, the screen has seven key parts (This is Stata 12. Some of the later screen shots .

There are several versions of STATA 14, such as STATA/IC, STATA/SE, and STATA/MP. The difference is basically in terms of the number of variables STATA can handle and the speed at which information is processed. Most users will probably work with the “Intercooled” (IC) version. STATA runs on the Windows, Mac, and Unix computers platform.

To open STATA on the host computer, click on the “Start” Menu. Then, when you look through “All Programs”, open the “Statistics” folder you should see a folder that says “STATA”. Click on the folde r and it will open up three STATA programs (STATA 10, STATA 11, and STATA 12). These are all the

STATA/IC, STATA/SE, and STATA/MP. The difference is basically in terms of the number of variables STATA can handle and the speed at which information is processed. Most users will probably work with the “Intercooled” (IC) version. STATA runs on the Windows (2000, 2003, XP, Vista, Server 2008, or Windows 7), Mac, and Unix computers platform.

Stata/MP, Stata/SE, Stata/IC, or Small Stata. Stata for Windows installation 1. Insert the installation media. 2. If you have Auto-insert Notification enabled, the installer will start auto-matically. Otherwise, you will want to navigate to your installation media and double-click on Setup.exe to start the installer. 3.

Stata/IC and Stata/SE use only one core. Stata/MP supports multiple cores, but only commands are speeded up. . I am using Stata 14 and not Stata 15) Setting up the seed using dataset lename. type can be F create creates a dataset with empty seeds for each variation. If option fill is used, then seeds are random numbers.

Introduction A description logic (DL) knowledge base (KB) consists of a terminological box (TBox), storing conceptual knowledge, and an assertion box (ABox), storing data. Typical applica-tions of KBs involve answering queries over incomplete data sources (ABoxes) augmented by ontologies (TBoxes) that provide additional information about the domain of interest as well as a convenient .