'Create a customised table with means, standard deviation, regression coefficients and pvalues
I have a dataset for an experiment and want to create a table with the following structure : column 1 : mean for full sample. col 2 : sd for full sample. col 3 / 4 : mean for treated / control group. col 5 : coefficient of regression for outcome on treatment status (and control variables), with stars of significance. col 6 : p value of regression coefficient.
I already have a table but have two issues with it. First my table adds the sd for treated and control groups as well, which I do not want. Secondly the regression coefficient in the table doesn't correspond to the coefficient I see when I perform the regression on the side, a random number pops from godknowswhere. As a result, my pvalue is also messed up.
Here's the table. It's highly customised and designed for LaTeX export, hence the weird syntax. There is usually more than one varlist, hence the "bigglobal", I've scaled it down for easier testing.
global varlist "var1 var2 var3"
global samples "FullSample treated control"
global bigglobal "varlist"
foreach men in black {
cap log close
cap log using "$export_table/Table.tex", text replace
di "\begin{table}[!h]"
di "\centering"
di "\small"
di "\begin{threeparttable}[b]"
di "\caption{\label{Stat_des} \textsc{Table for treated and controls}}"
di "\begin{tabular}{lcccccccc} "
di "\hline"
di "\hline"
di "{\bf } & {\bf All } & "" & {\bf Treated} & "" & {\bf Controls} & "" & {\bf } & {\bf } \\"
di "\cline{2-9}"
di " & Mean & SD & Mean & SD & Mean & SD & Difference & p-value \\"
di " Variables & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\"
di "\hline"
di " & & & & & & & &\\"
foreach glob in $bigglobale {
local lab_varlist "label"
dis "{\bf `lab_`glob'' }&&&&&&&& \\"
foreach var in $`glob' {
local lab_var1 "label1"
local lab_var2 "label2"
local lab_var3 "label3"
local lab_FS "Full Sample"
local lab_treated "\% Treated"
local lab_control "\% Control"
dis "`lab_`var'' "
foreach samp in $samples{
quiet: sum `var' if `samp' ==1
quiet: local mean`samp'_`var' = r(mean)
quiet: local sd`samp'_`var' = r(sd)
dis "& " %4.3f = `mean`samp'_`var''
dis "& " %4.3f = `sd`samp'_`var''
}
quiet: reg `var' treated control1 control2 control3, robust
quiet: matrix beta=e(b)
quiet: matrix var=e(V)
quiet: local dif_`var'=beta[1,1]
quiet: local sedif_`var'=var[1,1]
quiet: local pvaldif_`var'=2*(1-normal(abs(`dif_`var''/`sedif_`var'')))
quiet: if `pvaldif_`var'' < .001 local stardif_`var' `"***"'
quiet: else if `pvaldif_`var'' < .05 local stardif_`var' `"**"'
quiet: else if `pvaldif_`var'' < .01 local stardif_`var' `"*"'
quiet: else local stardif_`var' `""'
dis "& " %4.3f = `dif_`var'' "`stardif_`var'' "
dis " & ("%4.3f = `pvaldif_`var'' ") \\"
}
dis "\\"
}
dis "\hline"
quiet : count
quiet : local tot=r(N)
quiet : count if control==1
quiet : local control=r(N)
quiet : count if treated==1
quiet : local treated=r(N)
dis "Number of observations & `tot' & "" & `treated' & ""& `control' & "" && \\"
dis "\hline"
dis "\hline"
dis "\end{tabular}"
dis "\end{threeparttable}"
dis "\end{table}"
cap log close
}
I've tried to perform the regession on the side and this time it stores the correct coefficient, but for some reason it doesn't work when I write the same code in a table
reg y x1 x2 x3, robust
matrix beta=e(b)
matrix list e(b)
gen coeff = beta[1,1]
dis coeff
I think I've located the issue, something wrong happens more or less in the middle of the table, where all the "quiet" are. In addition, I've also tried to add and if condition where I display sd
dis "& " %4.3f = `sd`samp'_`var'' if samp == FullSample
But that doesn't work, so I left it as that and added columns for the sd of treated and control. I'd like to get rid of it at some point.
I cannot show my real data but here is an example with the auto dataset. It reports the same coefficients and pvalues as when I perform the regression on the side.
sysuse auto
gen FS = 1
gen domestic = (foreign ==0)
global Variables "mpg price"
global samples "FS foreign domestic"
global bigglobale "Variables"
foreach men in black {
cap log close
cap log using "$export_table/AttemptAuto.tex", text replace
di "\begin{table}[!h]"
di "\centering"
di "\small"
di "\begin{threeparttable}[b]"
di "\caption{\label{Stat_des} \textsc{Descriptive statistics for domestic and Foreign : Auto}}"
di "\begin{tabular}{lcccccccc} "
di "\hline"
di "\hline"
di "{\bf } & {\bf All } & "" & {\bf Foreign} & "" & {\bf Domestic} & "" & {\bf } & {\bf } \\"
di "\cline{2-9}"
di " & Mean & SD & Mean & SD & Mean & SD & Difference & p-value \\"
di " Variables & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\"
di "\hline"
di " & & & & & & & &\\"
foreach glob in $bigglobale {
local lab_Variables "Car characteristics"
dis "{\bf `lab_`glob'' }&&&&&&&& \\"
foreach var in $`glob' {
local lab_mpg "Miles per gallon"
local lab_price "Price"
local lab_FS "Full Sample"
local lab_foreign "\% foreign"
local lab_domestic "\% domestic"
dis "`lab_`var'' "
foreach samp in $samples{
quiet: sum `var' if `samp' ==1
quiet: local mean`samp'_`var' = r(mean)
quiet: local sd`samp'_`var' = r(sd)
dis "& " %4.3f = `mean`samp'_`var''
dis "& " %4.3f = `sd`samp'_`var''
}
quiet: reg `var' foreign, robust
quiet: matrix beta=e(b)
quiet: matrix var=e(V)
quiet: local dif_`var'=beta[1,1]
quiet: local sedif_`var'=var[1,1]
quiet: local pvaldif_`var'=2*(1-normal(abs(`dif_`var''/`sedif_`var'')))
quiet: if `pvaldif_`var'' < .001 local stardif_`var' `"***"'
quiet: else if `pvaldif_`var'' < .05 local stardif_`var' `"**"'
quiet: else if `pvaldif_`var'' < .01 local stardif_`var' `"*"'
quiet: else local stardif_`var' `""'
dis "& " %4.3f = `dif_`var'' "`stardif_`var'' "
dis " & ("%4.3f = `pvaldif_`var'' ") \\"
}
dis "\\"
}
dis "\hline"
quiet : count
quiet : local tot=r(N)
quiet : count if foreign==1
quiet : local foreign=r(N)
quiet : count if domestic==1
quiet : local domestic=r(N)
dis "Number of observations & `tot' & "" & `foreign' & ""& `domestic' & "" && \\"
dis "\hline"
dis "\hline"
dis "\end{tabular}"
dis "\end{threeparttable}"
dis "\end{table}"
cap log close
}
drop FS
drop domestic
reg mpg foreign, robust
matrix beta=e(b)
matrix list e(b)
matrix var=e(V)
matrix list e(V)
gen coeff = beta[1,1]
dis coeff
gen vari = var[1,1]
di vari
gen sd = sqrt(vari)
di sd
gen pval = 2*(1-normal(abs(coeff/vari)))
di pval
drop coeff
drop vari
drop pval
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
