lab4#
lab3: solutions
data: transplants.dta donors_recipients.dta donors.dta
zoom: M T W Th F
This lab is optional; you are NOT required to complete these questions. Please use this lab as an opportunity to review the course material and prepare yourself for the homework questions. Sample responses to the lab questions will be provided separately.
In lectures 3 & 4, we discussed how you can define your own “program”. It’s an awesome tool that allows us to automate a specific task. If you think a specific part of your code will be used multiple times, you might as well put that into a program. In this lab, we will practice customizing our programs.
Start Stata, open your do-file editor, lay out a template for your basic .do file structure using
qui {,if 0 {, and andif 1. Loadtransplants.dtain yourif 2block or wherever you feel it fits best.Write a program called
mymean. This program will takevarlistas a user input, and calculate the mean value of each variable, and display the values.capture program drop mymean program define mymean syntax varlist foreach var in `varlist' { quietly sum `var' display r(mean) } end
Modify your program
mymeanso that when anifargument is supplied,mymeanwould only include the observations that meet the condition specified by theifargument. In other words, if the user typesmymean height if age>65, the programmymeanwill calculate the mean only among patients older than 65.capture program drop mymean program define mymean syntax varlist [if] foreach var in `varlist' { quietly sum `var' `if' display r(mean) } end
Further modify your program
mymeanto include the optionsd. When the optionsdis supplied,mymeanwill display the standard deviation along with the mean. This version ofmymeanshould still be able to accommodate theifargument.capture program drop mymean program define mymean syntax varlist [if], [sd] foreach var in `varlist' { quietly sum `var' `if' display r(mean) if "`sd'" != "" { display r(sd) } } end // The answer above is in the simplest possible form for clarity. In practice, I will arrange the outputs a little bit better. capture program drop mymean program define mymean syntax varlist [if], [sd] foreach var in `varlist' { quietly sum `var' `if' if "`sd'" != "" { display "`var': " r(mean) " (" r(sd) ")" } else { display "`var': " r(mean) } } end
Further modify your program
mymeanto include the optiondigits(), with a number in the parenthesis. When the optiondigits()is supplied,mymeanwill round up the mean (and the standard deviation, if applicable) in units ofdigits(). Ifdigits()is NOT supplied, round in units of 0.001. (Hint: use the Stata functionround())capture program drop mymean program define mymean syntax varlist [if], [sd] [digits(real 0.001)] foreach var in `varlist' { quietly sum `var' `if' display round(r(mean), `digits') if "`sd'" != "" { display round(r(sd), `digits') } } end
Did you make
if,sd, anddigits()optional arguments? That is, your program should run whether or not these arguments are supplied. To do so, simply surround each argument with brackets. For example,[sd]I’d like to draw your attention to the merge command. It’s hard to write a question around
merge, but it’s a really important command in practice. For instance, we used it in theif 4 {code-block of chapter:r(mean)merge 1:1 fake_id using donors_recipients
This is the code from the lecture. We are merging
transplants.dtawithdonors_recipients.dta. We are merging observations with the samefake_id, and expect that there will be only one observation perfake_idin both datasets.We want to study if death (
died==1) is associated with several predictor variables:bmi,prev_ki,age,peak_pra, orgender. Run logistic regression betweendiedand each of the predictor variables usingforeachloop. At each run, save the name and the regression coefficient of the predictor variable into an external Stata dataset file namedoutput.dta.postfile output str30 name coef using output // you may add ", replace" to allow overwriting output.dta foreach var in bmi prev_ki age peak_pra gender { quietly logistic died `var' post output ("`var'") (_b[`var']) } postclose output
You have all your commands in your do file, right? Run your do file from the beginning and make sure your do file does exactly the same thing.