Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: new tsb command

From   "Ng, Edmond" <>
To   "''" <>
Subject   Re: st: new tsb command
Date   Thu, 6 Jun 2013 22:49:22 +0000

Hi Margaret, 

The new user-written command, tsb, works similarly to Stata's own 'bootstrap'. It performs a two-stage bootstrap algorithm on a user supplied function, f(), with a shrinkage correction for possible overestimation due to second-stage sampling . The main difference is that the child function, f(), expected by 'tsb' is a Mata function. I have provided three such functions in the tsb package (package st0288 downloadable from the Stata Journal website) and their syntaxes are included in the accompanying Stata paper (Ng et al 2013 Stata Journal 13(1):pp141-164). 

To apply 'tsb' on the difference between the cluster-level treatment means you will have to write your own child Mata function to calculate this difference. To save you some time, I have provided the syntax for a new child function, meandiff(), for calculating this difference. I assumed your treatments are applied at the cluster-level.  Do test it and check if the results make sense to you before using it for analysis. 

Run the following syntax in Stata to create '' and save it into your personal directory. You are likely to have to modify the path for your personal directory (see 2nd line from the bottom). If not sure where this directory is on your computer, type 'sysdir' to find out. 

An example syntax for this new child function would be: 
> tsb cost, stats(&meandiff()) cluster(cluster) strata(int1)

Hope this is useful. 

BW, Edmond 

************ START OF MEANDIFF.DO ************ 
capture mata: mata drop meandiff()
real scalar function meandiff(transmorphic matrix data)  
version 12.1
real matrix cost, treat 
real scalar mc_ctl, mc_tmt
if (cols(data)!=3){
 _error("Function requires an input data matrix of 3 columns wide.") 
/* 1. Stop if treatment var has anything other than 2 levels */ 
if (rows(uniqrows(treat))<2) {
 _error("Treatment variable has <2 unique values.") 
 } else if (rows(uniqrows(treat))>2) {
 _error("Treatment variable has >2 unique values.") 
/* Calculate INB */ 
mc_ctl=mean(select(cost,treat:==uniqrows(treat)[1])) /* mean cost for ctl */ 
mc_tmt=mean(select(cost,treat:==uniqrows(treat)[2])) /* mean cost for tmt */ 
meandiff = mc_tmt-mc_ctl  
mata mosave meandiff(), dir("h:\stata\ado\personal") replace 
************ END OF MEANDIFF.DO ************ 

Edmond Ng

This email and any files transmitted with it are confidential. If you are not the intended recipient, any reading, printing, storage, disclosure, copying or any other action taken in respect of this email is prohibited and may be unlawful. 

If you are not the intended recipient, please notify the sender immediately by using the reply function and then permanently delete what you have received.Incoming and outgoing email messages are routinely monitored for compliance with the Department of Healths policy on the use of electronic communications. 

For more information on the Department of Healths email policy, click

The original of this email was scanned for viruses by the Government Secure Intranet virus scanning service supplied by Vodafone in partnership with Symantec. (CCTM Certificate Number 2009/09/0052.) On leaving the GSi this email was certified virus free.
Communications via the GSi may be automatically logged, monitored and/or recorded for legal purposes.

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index