Hello,
I have been setting up a custom descriptive statistics table for personal data use. Reading through the forums, I have been able to get the formatting close to my liking. However, I am having trouble remapping (?) the p-value of dichotomous variables, so that the value can be seen when excluding the 0.var level.
The layout is set using (roweq#var#`depvar') to provide a vertical format. This is necessary, as a horizontal format becomes difficult to read and compare when the depvar has more than two levels.
using example code:
Code "Layout " shows the p-value in the first instance, but it is missing for dichotomous variables in the second instance when the "absent" level is removed.
Ideally, the "Test" value would be aligned with the roweq level (bolded title), though setting it just below the last value of i.var would also work. I have tried the code below without success.
I have been setting up a custom descriptive statistics table for personal data use. Reading through the forums, I have been able to get the formatting close to my liking. However, I am having trouble remapping (?) the p-value of dichotomous variables, so that the value can be seen when excluding the 0.var level.
The layout is set using (roweq#var#`depvar') to provide a vertical format. This is necessary, as a horizontal format becomes difficult to read and compare when the depvar has more than two levels.
using example code:
Code:
clear all webuse nhanes2 svyset gen index = inrange(hlthstat,1,3) label var index "Index Subpopulation" *---------------------- * macro for continuous variables local convars age weight * factor variables, all local factorvars diabetes rural race region * Dependent variable local depvar sex * variables specified in options -continuous()- and -factor()- do not * need to be specified in the varlist unless you want a special variable * order that is otherwise too difficult to get using the options dtable, by(`depvar', tests nomissing) svy subpop(index) column(by(hide)) /// continuous(`convars', statistics(total mean sd semean p50 p25 p75)) /// factor(`factorvars', /// statistics(fvfrequency fvpercent) /// test(svywald) /// ) *------------------------------------------------------------ * Add -roweq- tag to nest vars into groups; -roweq- is a special * dimension that will grab variable labels for it's levels that match * variable names in the current frame foreach c of local convars { collect addtags roweq[`c'], fortags(var[`c']) * hide this factor variable's title since we plan to use this * -roweq- level to title this variable in the header collect style header var[`c'], title(hide) level(hide) // hides the unbolded title of the variable local ccall `ccall' `c' } foreach var of varlist `factorvars' { levelsof `var' // get levels of each variable * Dichotomous variables if r(r) == 2 { collect addtags roweq[`var'], fortags(var[i.`var']) * hide this factor variable's title since we plan to use this * -roweq- level to title this variable in the header collect style header `var', title(hide) level(hide) local fcall `fcall' 1.`var' } * All other factor variables if r(r) > 2 { collect addtags roweq[`var'], fortags(var[i.`var']) collect style header `var', title(hide) level(label) local fcall `fcall' i.`var' } } *------------------------------- * Shows the bolded title collect style header roweq, title(hide) level(label) * bold all the column headers collect style cell cell_type[column-header], font(arial, bold) * bold the levels of -roweq- collect style cell roweq#cell_type[row-header], font(arial, bold) * unbold the variable names/labels collect style cell var#cell_type[row-header], font(arial, nobold) collect style row stack, truncate(head) // Composite results *------------------------------- * stack non-missing counts and factor level frequencies collect composite define col1 = total fvfrequency, trim collect label levels result col1 "Total" * stack means and factor level percentages collect composite define col2 = mean fvpercent, trim collect label levels result col2 "Mean/%" // * stack means and factor level percentages collect composite define col3 = regress svywald, trim collect label levels result col3 "Test" // Format *------------------------------- * Formatting: show custom label for results in the header collect label levels result total "Total (N)" sd "SD" semean "SE Mean" p50 "p50" p25 "p25" p75 "p75", modify collect style header result, level(label) title(hide) * Changing the format of result cells collect style cell result[total sd p50 p25 p75], nformat(%12.2gc) collect style cell result[mean fvpercent], nformat(%9.2fc) *------------------------------- * Setting autolevels for ease of use collect style autolevels result, clear // removes existing levels for results collect style autolevels result col1 col2 sd seamean frequency percent p50 p25 p75 col3 *------------------------------- * Add notes to the bottom using collect collect notes 1: "Continuous variables tested via Regress" collect notes 2: "Factor variables tested via Adjusted Wald Test" // Layout *------------------------------- * uses only the present status (ie var == 1) of dichotomous variables, but missing p-value collect layout (roweq#var[`ccall' `fcall']#`depvar') (result) * shows all, including absent status (var == 0) of dichotomous variables collect layout (roweq#var#`depvar') (result) * publish our table to MS Excel collect export ztable1.xlsx, replace
Ideally, the "Test" value would be aligned with the roweq level (bolded title), though setting it just below the last value of i.var would also work. I have tried the code below without success.
Code:
collect remap `depvar'[_dtable_test]=`depvar'[0.`var'] // causes the p-values to be lost on preview collect remap sex[_dtable_test]=roweq // error, as already has tag at roweq collect remap sex[_dtable_test]=sex[.m] // only moves within the levels of sex, not by var/roweq
Comment