I thought I wouldn't have any further questions about dtable and collect for the moment but have two more. I'm sorry to be taking up so much time and space on Statalist of late. I hope other Stata users are get some benefit from my posts.
Here's the code and the output along the way.
The sample frequency is in _dtable_stats. The following command also confirms this.
Now for the rest of the code and the table produced.
Question 1: How do I display the sample frequency in the header for the Intervention and Control groups?
Question 2: What is the difference between the dimensions colname and var? When I use var instead of colname for the row component, results are shown for only 2 levels of my variable blinding. There genuinely are only 2 levels for Day=0 but there are 3 levels for the other days.
Kind regards,
Suzanna
Here's the code and the output along the way.
Code:
*_______________________________________________________________________________
*Creating the example dataset.
*Reading in the dataset.
use https://www.stata-press.com/data/r18/exercise.dta,clear
keep if inlist(day, 0, 4, 6, 8)
*Replacing the variable program with group_allocation, where 1=intervention and 2=control.
gen group_allocation=program
lab var group_allocation "Group allocation"
lab def grplbl 1 Intervention 2 Control
lab val group_allocation grplbl
drop program
gen blinding=2
replace blinding=3 if _n/3==int(_n/3)
bysort day: replace blinding=1 if _n/4==int(_n/4) & day!=0
lab def blindlbl 1 "Change or lack of change from baseline" ///
2 "Participant informed assessor" ///
3 "Guess"
lab val blinding blindlbl
*_______________________________________________________________________________
*Creating the table.
*Clearing any collections that may be in Stata's memory.
collect clear
*Creating a separate descriptive table for each level of time.
*For time=1, including the sample frequency in the column header.
dtable i.blinding if day==0, ///
by(group_allocation,nototal) ///
sample(, statistic(frequency) place(seplabels)) ///
sformat("n=%s" frequency) ///
title(Table 5: Blinding of assessor) ///
titlestyles(font(Calibri, size(12) bold)) ///
name(Day0)
forval i=4(2)8 {
dtable i.blinding if day==`i', ///
by(group_allocation,nototal) ///
nosample name(Day`i')
}
*Combining the above collections.
collect combine comb = Day0 Day4 Day6 Day8
collect query composite _dtable_stats
OUTPUT:
Composite definition
Collection: comb
Composite: _dtable_stats
Elements: frequency
fvfrequency
fvpercent
Delimiter: " "
Trim: on
Override: off
Code:
. collect label list result
Collection: comb
Dimension: result
Label: Result
Level labels:
frequency Frequency
fvfrequency Factor-variable frequency
fvpercent Factor-variable percent
Code:
*Setting the cell percentages to integers.
collect style cell result[fvpercent], nformat(%3.0f)
*Adding a border to separate each level of time
collect style cell colname[3.blinding], border( bottom, width(1))
*Creating the final table.
collect layout (collection#colname) (group_allocation#result)
OUTPUT:
Table 5: Blinding of assessor
----------------------------------------------------------------
Group allocation
Intervention Control
----------------------------------------------------------------
Day0
blinding
Participant informed assessor 11 (69%) 14 (67%)
Guess 5 (31%) 7 (33%)
----------------------------------------------------------------
Day4
blinding
Change or lack of change from baseline 4 (25%) 5 (24%)
Participant informed assessor 8 (50%) 12 (57%)
Guess 4 (25%) 4 (19%)
----------------------------------------------------------------
Day6
blinding
Change or lack of change from baseline 2 (12%) 7 (33%)
Participant informed assessor 9 (56%) 10 (48%)
Guess 5 (31%) 4 (19%)
----------------------------------------------------------------
Day8
blinding
Change or lack of change from baseline 4 (25%) 5 (24%)
Participant informed assessor 8 (50%) 10 (48%)
Guess 4 (25%) 6 (29%)
----------------------------------------------------------------
Question 2: What is the difference between the dimensions colname and var? When I use var instead of colname for the row component, results are shown for only 2 levels of my variable blinding. There genuinely are only 2 levels for Day=0 but there are 3 levels for the other days.
Code:
. collect layout (collection#var) (group_allocation#result)
Collection: comb
Rows: collection#var
Columns: group_allocation#result
Table 1: 16 x 2
Table 5: Blinding of assessor
-------------------------------------------------------
Group allocation
Intervention Control
-------------------------------------------------------
Day0
blinding
Participant informed assessor 11 (69%) 14 (67%)
Guess 5 (31%) 7 (33%)
Day4
blinding
Participant informed assessor 8 (50%) 12 (57%)
Guess 4 (25%) 4 (19%)
Day6
blinding
Participant informed assessor 9 (56%) 10 (48%)
Guess 5 (31%) 4 (19%)
Day8
blinding
Participant informed assessor 8 (50%) 10 (48%)
Guess 4 (25%) 6 (29%)
-------------------------------------------------------
Suzanna
Comment