Hi everyone, I have a question about producing table in Stata. What I'm doing in the following example code is to run regressions first and then collect the p-values of each coefficient. Then I calculate a new statistic, sharpened-q value, based on these p-values. q-value is stored in a variable named bky06_qval. In the table, I want to show 1) coefficients 2) t-statistic 3) sharpened q-value. Can anybody help? Also, is there an easier way to extract p-values from the regression results? Thanks a ton!
dependent var | |
independent var | coefficient |
t-statistic | |
sharpened-q value |
Code:
sysuse auto, clear * regression 1 reg price mpg rep78 headroom mat m1 = r(table) forval i = 1/3 { scalar p1_`i' = m1[4, `i'] } eststo reg1 * regression 2 reg trunk mpg rep78 headroom mat m2 = r(table) forval i = 1/3 { scalar p2_`i' = m2[4, `i'] } eststo reg2 * calculate sharpened q-value; taken from Michael Anderson’s code (2008) quietly gen float pval = . display "***********************************" display "Please paste the vector of p-values that you wish to test into the variable 'pval'" display "After pasting, type 'q' to resume" display "***********************************" replace pval = p1_1 if _n == 1 replace pval = p1_2 if _n == 2 replace pval = p1_3 if _n == 3 replace pval = p2_1 if _n == 4 replace pval = p2_2 if _n == 5 replace pval = p2_3 if _n == 6 * Collect the total number of p-values tested quietly sum pval local totalpvals = r(N) * Sort the p-values in ascending order and generate a variable that codes each p-value's rank quietly gen int original_sorting_order = _n quietly sort pval quietly gen int rank = _n if pval~=. * Set the initial counter to 1 local qval = 1 * Generate the variable that will contain the BKY (2006) sharpened q-values gen bky06_qval = 1 if pval~=. * Set up a loop that begins by checking which hypotheses are rejected at q = 1.000, then checks which hypotheses are rejected at q = 0.999, then checks which hypotheses are rejected at q = 0.998, etc. The loop ends by checking which hypotheses are rejected at q = 0.001. while `qval' > 0 { * First Stage * Generate the adjusted first stage q level we are testing: q' = q/1+q local qval_adj = `qval'/(1+`qval') * Generate value q'*r/M gen fdr_temp1 = `qval_adj'*rank/`totalpvals' * Generate binary variable checking condition p(r) <= q'*r/M gen reject_temp1 = (fdr_temp1>=pval) if pval~=. * Generate variable containing p-value ranks for all p-values that meet above condition gen reject_rank1 = reject_temp1*rank * Record the rank of the largest p-value that meets above condition egen total_rejected1 = max(reject_rank1) * Second Stage * Generate the second stage q level that accounts for hypotheses rejected in first stage: q_2st = q'*(M/m0) local qval_2st = `qval_adj'*(`totalpvals'/(`totalpvals'-total_rejected1[1])) * Generate value q_2st*r/M gen fdr_temp2 = `qval_2st'*rank/`totalpvals' * Generate binary variable checking condition p(r) <= q_2st*r/M gen reject_temp2 = (fdr_temp2>=pval) if pval~=. * Generate variable containing p-value ranks for all p-values that meet above condition gen reject_rank2 = reject_temp2*rank * Record the rank of the largest p-value that meets above condition egen total_rejected2 = max(reject_rank2) * A p-value has been rejected at level q if its rank is less than or equal to the rank of the max p-value that meets the above condition replace bky06_qval = `qval' if rank <= total_rejected2 & rank~=. * Reduce q by 0.001 and repeat loop drop fdr_temp* reject_temp* reject_rank* total_rejected* local qval = `qval' - .001 } quietly sort original_sorting_order esttab reg* using "test.tex"
Comment