Reputation: 10102
... and add columns for differences and t-statistics.
I learned how to make a quantile by quantile table of means and how to add a column/row of differences here (thanks to @lejohn).
Now instead of each row as one quantile of one variable, I would like each row to be a different variable and each cell would be the mean value for each variable for the individuals that fall in each column for the quantile of a given variable.
I can calculate the cell entries easily with tabstat
, but I would like the variables in the rows and the quantiles in the columns (tabstat
produces the transpose). I would also like the ability to difference columns (as in my first question) and calculate t-statistics for the cell differences.
I feel like the intermediate step is to reshape
to long data with three columns: id (here acc_d), variable name, and variable value. But I can't figure out how to do this and I may be stuck in an R paradigm.
Here is an example of the type table I would like to make
and here is some code with which I have been (unsuccesfully) tinkering
* generate data
clear
set obs 2000
generate acc = rnormal()
generate r1 = rnormal()
generate sar1 = rnormal()
generate arbrisk = rnormal()
* generate quantiles for for a and b
xtile acc_d = acc, nquantiles(10)
* form table (at least my attempts)
* w/ tabstat (but transposed and can't manipulate columns)
tabstat acc r1 sar1 arbrisk, stat(mean) by(acc_d) nototal
* my attempts to reshape fail, but I would want something like to following to use tabulate
* acc_d variable value
* 1 acc 0.01
* 1 r1 1.03
* 1 sar1 -0.03
* 1 arbrisk 0.05
* 2 acc 1.01
* 2 r1 2.03
* 2 sar1 0.03
* 2 arbrisk 1.05
Thanks!
Upvotes: 2
Views: 4419
Reputation: 10102
Here is a clunkier solution that creates two tables.
* generate data
clear
set obs 2000
generate acc = rnormal()
generate r1 = rnormal()
generate sar1 = rnormal()
generate arbrisk = rnormal()
* generate quantiles
xtile acc_d = acc, nquantiles(10)
* aggregate
collapse (mean) acc r1 sar1 arbrisk, by(date_y acc_d) cw
* relabel variables after collapse
label variable acc "Acc"
label variable r1 "R1"
label variable sar1 "SAR1"
label variable arbrisk "ArbRisk"
* main part of table
eststo clear
estpost tabstat acc r1 sar1 arbrisk if tin(1975, 2000) ///
, stat(mean) by(acc_d) columns(statistics) listwise nototal
esttab using tab_1a.tex ///
, booktabs replace main(mean) nonumbers noobs ///
label unstack nogaps not nomtitles nostar ///
eqlabels(, prefix("Acc "))
* add difference t-test
estpost ttest acc r1 sar1 if acc_d == 1 | acc_d == 10, by(acc_d)
esttab using tab_1a_ttest.tex, booktabs replace nonumbers noobs ///
label mtitles("Acc 1-Acc10") wide ///
varlabels(acc Acc r1 R1 sar1 SAR1)
Upvotes: 0
Reputation:
Here I would proceed a bit differently. I would first of all gather the information required to compute the difference and the t statistic
foreach v of varlist acc r1 sar1 arbrisk {
summarize `v' if acc_d == 1
local m_`v'_1 = r(mean)
local var_`v'_1 = r(Var)
local n_`v'_1 = r(N)
summarize `v' if acc_d == 10
local m_`v'_10 = r(mean)
local var_`v'_10 = r(Var)
local n_`v'_10 = r(N)
}
Then I would proceed by collapsing and transposing the data
collapse (mean) acc r1 sar1 arbrisk, by(acc_d)
xpose, clear varname
drop if _varname == "acc_d"
order _varname
forvalues n = 1 / 10 {
rename v`n' acc_d`n'
}
In a last step, I would add the difference and the t statistic:
generate diff_d10_d1 = .
generate tstat_d10_d1 = .
foreach v in acc r1 sar1 arbrisk {
replace diff = `m_`v'_10' - `m_`v'_1' if _varname == "`v'"
replace tstat = (`m_`v'_10' - `m_`v'_1') / sqrt((`var_`v'_10'/`n_`v'_10') + (`var_`v'_1'/`n_`v'_1')) if _varname == "`v'"
}
And finally print the results:
list, abb(12) noobs
Hope this helps.
Upvotes: 3