Hi there,
I have written some code to take the matrices saved from tabstat and calculate the standardised difference (calculated as the difference in means divided by pooled standard deviation) between a treatment and control group. I'm getting a type mismatch when I subscript the row containing the pooled standard deviation by name. tabstat outputs three matrices when the by() option is used with a variable with two groups: the matrices for the two groups ('Stat1' and 'Stat2') and the overall matrix ('StatTotal').
This first example results in a type mismatch on the line within the loop, because of using "sd" in the row subscript of matrix A:
This second example works as intended, and first saves the "sd" row of matrix A in matrix D, which is then used within the loop:
I would just like to understand why this is. Any pointers?
All the best,
Will
I have written some code to take the matrices saved from tabstat and calculate the standardised difference (calculated as the difference in means divided by pooled standard deviation) between a treatment and control group. I'm getting a type mismatch when I subscript the row containing the pooled standard deviation by name. tabstat outputs three matrices when the by() option is used with a variable with two groups: the matrices for the two groups ('Stat1' and 'Stat2') and the overall matrix ('StatTotal').
This first example results in a type mismatch on the line within the loop, because of using "sd" in the row subscript of matrix A:
Code:
tabstat var1 var2 var3 var4 var5, stat(n mean sd min max q) by(group) longstub save matrix A = r(StatTotal) matrix B = r(Stat2) matrix C = r(Stat1) local c = colsof(A) matrix Standardised_Difference = J(1,`c',0) forvalues j = 1/`c' { matrix Standardised_Difference[1,`j'] = (B["mean",`j'] - C["mean",`j']) / A["sd",`j'] } local rsubs : colnames A matrix colnames Standardised_Difference = `rsubs' matrix rownames Standardised_Difference = StDiff matrix list Standardised_Difference
Code:
tabstat var1 var2 var3 var4 var5, stat(n mean sd min max q) by(group) longstub save matrix A = r(StatTotal) matrix B = r(Stat2) matrix C = r(Stat1) matrix D = A["sd",1...] local c = colsof(A) matrix Standardised_Difference = J(1,`c',0) forvalues j = 1/`c' { matrix Standardised_Difference[1,`j'] = (B["mean",`j'] - C["mean",`j']) / D[1,`j'] } local rsubs : colnames A matrix colnames Standardised_Difference = `rsubs' matrix rownames Standardised_Difference = StDiff matrix list Standardised_Difference
All the best,
Will
Comment