capture program drop ttesttable *! version 1.3 11Feb2015 F. Chavez Juarez program define ttesttable , rclass version 9.2 syntax varlist(min=2 max=2 ) [if] [in] [, UNEQual TEX(str) HTML(str) XML(str) PREtable(str) POSTtable(str) force Reference(str) Format(str) TEXFormat(str)] marksample touse, strok preserve qui: keep if `touse' tokenize `varlist' tempvar GV gen `GV'=`2' capture: tostring `GV', replace qui: levelsof `GV', local(levels) qui: tab `GV' local K=r(r) if("`reference'"!=""){ local REFS=wordcount("`reference'") if((`REFS'>10 | `K'>100) & "`force'"==""){ di as error "Too many categories (max allowed = 10 (in the reference option) and 100 in the variable of interest)." _n "Make sure you use a categorical variable for the groups or use the option 'force'" exit } } ** CHECK if not excessively many groups if(`K'>10 & "`force'"=="" & "`reference'"=="") { di as error "Too many categories (max allowed = 10)." _n "Make sure you use a categorical variable for the groups or use the option 'force'" exit } tempvar v1 v2 local stats="p t diff" if("`reference'"==""){ foreach stat of local stats{ matrix cttab_`stat'=J(`K',`K',.) matrix colname cttab_`stat'=`levels' matrix rowname cttab_`stat'=`levels' } local I=0 // Standard case foreach i of local levels{ local I=`I'+1 local J=0 foreach j of local levels{ local J=`J'+1 if ("`j'"<"`i'"){ qui: g `v1'=`1' if `GV'=="`i'" qui: g `v2'=`1' if `GV'=="`j'" qui:ttest `v2'=`v1',unpaired `unequal' local pvalue=r(p) matrix cttab_p[`I',`J']=r(p) matrix cttab_t[`I',`J']=r(t) matrix cttab_diff[`I',`J']=(r(mu_1)-r(mu_2)) drop `v1' `v2' } } } } //end reference=="" else{ foreach stat of local stats{ matrix cttab_`stat'=J(`K',`REFS',.) matrix colname cttab_`stat'=`reference' matrix rowname cttab_`stat'=`levels' } // With a reference group foreach i of local levels{ local I=`I'+1 local J=0 foreach j of local reference{ local J=`J'+1 if("`i'"!="`j'"){ qui: g `v1'=`1' if `GV'=="`i'" qui: g `v2'=`1' if `GV'=="`j'" qui:ttest `v2'=`v1',unpaired `unequal' matrix cttab_p[`I',`J']=r(p) matrix cttab_t[`I',`J']=r(t) matrix cttab_diff[`I',`J']=(r(mu_1)-r(mu_2)) drop `v1' `v2' } } } } ** Start screen output (and TeX-output if required)** // Load columns if("`reference'"==""){ qui: levelsof `GV', local(columns) } else{ local columns="`reference'" } // Define formats if("`format'"==""){ local format="%6.3f" } if("`texformat'"==""){ local texformat="%6.3f" } // Get space needed local length=subinstr("`format'","%","",.) local length=subinstr("`length'","g","",.) local length=subinstr("`length'","f","",.) local length=subinstr("`length'","e","",.) local length=trim("`length'") qui: tokenize "`length'", parse(".") local length=`1'+`3' local colstep=3+max(12,`length') if("`tex'"!="") { local numcols=colsof(cttab_p) capture file open tex using `tex'.tex, replace write file write tex "`pretable'" _n file write tex "\begin{tabular}{l*{`numcols'}{l}}" _n "\toprule" _n } if("`html'"!=""){ local numcols=colsof(cttab_p) capture file open html using `html'.html, replace write file write html "
" } if("`xml'"!=""){ file write xml " | `value' | " } if("`xml'"!=""){ local value=abbrev("`col'",12) file write xml "|||
`value' | " } if("`xml'"!=""){ local value=abbrev("`row'",12) file write xml "" `format' (`value') "`stars' | " } if("`xml'"!="") { local value=cttab_diff[`i',`j'] file write xml _col(5) "" %6.0f (0) " | " if("`xml'"!="") file write xml _col(5) "
Note: Differences defined as column-line
* p<.1; ** p<.05; *** p<.01