Lab 5#
Part I#
Start Stata, open your do-file editor, write the header, and load
transplants.dta
.ctr_id
indicates the ID of the transplant center where the patient received the transplant. Count the number of recipients at each center, and store in a new variablevolume
.bysort ctr_id: gen volume=_N
List
ctr_id
andvolume
to see how many patients each center has. Maybe let’s try this:list ctr_id volume
Whelp. This is not what we wanted. Generate a variable
ctr_tag
that “tags” one observation per center.egen ctr_tag=tag(ctr_id)
Now
list ctr_id
andvolume
, but just for one record per center.list ctr_id volume if ctr_tag==1
Calculate the mean age of the patients at each center, and store in a new variable
mean_age
.bys ctr_id: egen mean_age=mean(age)
For each primary diagnosis subgroup (use variable
dx
), run a regression with age as the predictor and peak PRA (peak_pra
) as the outcome.forvalue i=1/9 { regress peak_pra age if dx==`i' }
Now let’s make the output cleaner. Count the number of cases within each diagnosis group. If there are more than 500 cases, run the regression and display the output. If not, display “There are fewer than 500 cases.”
forvalue i=1/9 { qui count if dx==`i' if r(N)>500 { regress peak_pra age if dx==`i' } else { di "There are fewer than 500 cases." } }
Define a program called
reg_pra
. This program will perform the same tasks as described in Question 8, but the regression will take one or more variables specified by the user as the predictor.capture program drop reg_pra program define reg_pra syntax varlist forvalue i=1/9 { qui count if dx==`i' if r(N)>500 { regress peak_pra `varlist' if dx==`i' } else { di "There are fewer than 500 cases." } } end
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.
Part II#
lab4#
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 and consider using conditional code-blocks (
if 2
for instance) as you answer each question in this lab. That way each block has some autonomy and you can “silence” it (if 0
) while you run other code-blocks.Write a program called
mymean
. This program will takevarlist
as 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
mymean
so that when anif
argument is supplied,mymean
would only include the observations that meet the condition specified by theif
argument. In other words, if the user typesmymean height if age>65
, the programmymean
will 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
mymean
to include the optionsd
. When the optionsd
is supplied,mymean
will display the standard deviation along with the mean. This version ofmymean
should still be able to accommodate theif
argument.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
mymean
to include the optiondigits()
, with a number in the parenthesis. When the optiondigits()
is supplied,mymean
will 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 the week 4. Reference these notes when necessary in the future.merge 1:1 fake_id using donors_recipients
This is the code from the lecture. We are merging
transplants.dta
withdonors_recipients.dta
. We are merging observations with the samefake_id
, and expect that there will be only one observation perfake_id
in 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 betweendied
and each of the predictor variables usingforeach
loop. 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.