我想在运行
pstest
后使用选项 both
保存 Stata psmatch2
命令的输出。我在生成数百个表格的循环中使用 pstest
,因此从屏幕复制和粘贴单个表格是不切实际的。我在其他地方看到过这个问题,但我还没有看到可行的解决方案。据我所知,putdocx
不起作用。
如果为
psmatch2
(logit n(5) ai(5) ate common
) 和 both
选择选项 logit、k-近邻匹配(其中 k = 5)、Abadie 和 Imbens 标准误、平均治疗效果和共同支持,则我的解决方案有效为后续 pstest 命令选择的选项,如下所示 pstest, both atu
。是否有更好、更灵活的解决方案,如果这些选项中的任何一个发生变化,不需要重新编写我的代码?
需要明确的是,我并不是在寻找任何人来编辑我的代码。我的解决方案有效,但如果指定不同的选项,则很长且难以修改。更好的答案会更简单,更短,并且足够灵活,可以在选择不同选项时显示结果。使用
putdocx
的解决方案将是理想的选择。
如果可能的话,我建议 Stata 考虑要求包的作者以某种方式使屏幕上的任何输出可检索,也许使用
return list
或 ereturn list
。只能通过这种方式检索使用 pstest 生成的最后一个表。我相信,pstest 将输出保存在临时内存中的两个表中的第一个表中,创建第二个表时该输出会丢失。用户可以通过 return list
获得第二个的输出。如果我对此有误,请告诉我。
此代码有效,但它更多的是一种解决方法而不是解决方案。在没有更好的东西的情况下,我希望它能帮助任何与我遇到类似困境的人。
* Most of this code is from the pstest.ado by Leuven and Sianesi (2003)
clear all
set more off
* Define the program to generate output from the Stata command pstest
cap program drop pstest_tab
program define pstest_tables
args exog atu
/* _weight is conditional on ATU option */
qui replace _weight = _support if _treated==0
local i = 0
local cont_cnt = 0 /* counting continuous vars */
local cont_varbef = 0 /* counting continuous vars w/ excessive var ratio*/
local cont_varaft = 0
qui g _bias0 = .
qui g _biasm = .
qui g sumbias = .
qui g sumbias0 = .
scalar wd_num = `:word count `exog''
mat A = J(wd_num,13,.)
mat R = J(1,16,.)
qui count if _treated==1 & esample==1
scalar Flowu = invF(r(N)-1, r(N)-1, 0.025)
scalar Fhighu = invF(r(N)-1, r(N)-1, 0.975)
qui count if _treated==1 & _support==1 & esample==1
scalar Flowm = invF(r(N)-1, r(N)-1, 0.025)
scalar Fhighm = invF(r(N)-1, r(N)-1, 0.975)
foreach v of local exog {
local ++i
/* Calculate stats for varlist */
qui sum `v' if _treated==1
scalar m1u = r(mean)
scalar v1u = r(Var)
mat A[`i',1] = r(mean)
qui sum `v' if _treated==0
scalar m0u = r(mean)
scalar v0u = r(Var)
mat A[`i',2] = r(mean)
qui sum `v' [iw=_weight] if _treated==1 & _support==1 & esample==1
scalar m1m = r(mean)
scalar v1m = r(Var)
mat A[`i',3] = r(mean)
qui sum `v' if _treated==0 & _support==1 & esample==1
scalar m0m = r(mean)
scalar v0m = r(Var)
mat A[`i',4] = r(mean)
/* Get Var ratio */
scalar v_ratiobef = v1u/v0u
scalar v_ratioaft = v1m/v0m
mat A[`i',12] = v1u/v0u
mat A[`i',13] = v1m/v0m
/* Get Var ratio */
capture assert `v'==0 | `v'==1 | `v'==., fast
if (_rc) {
local cont_cnt = `cont_cnt' +1
scalar v_ratiobef = v1u/v0u
if v_ratiobef>Fhighu | v_ratiobef<Flowu {
local cont_varbef = `cont_varbef' +1
}
scalar v_ratioaft = v1m/v0m
if v_ratioaft>Fhighm | v_ratioaft<Flowm {
local cont_varaft = `cont_varaft' +1
}
}
/* Standardised bias before matching */
scalar bias = 100*(m1u - m0u)/sqrt((v1u + v0u)/2)
mat A[`i',5] = bias
qui replace _bias0 = bias in `i'
qui replace sumbias0 = abs(bias) in `i'
/* Standardised bias after matching */
scalar biasm = 100*(m1m - m0m)/sqrt((v1u + v0u)/2)
mat A[`i',6] = biasm
qui replace _biasm = biasm in `i'
qui replace sumbias = abs(biasm) in `i'
/* Reduction in absolute bias */
mat A[`i',7] = -100*(abs(biasm) - abs(bias))/abs(bias)
/* t-tests before matching */
qui regress `v' _treated
mat A[`i',8] = _b[_treated]/_se[_treated]
mat A[`i',9] = 2*ttail(e(df_r),abs(_b[_treated]/_se[_treated]))
/* t-tests after matching */
qui regress `v' _treated [iw=_weight] if _support==1 & esample==1
mat A[`i',10] = _b[_treated]/_se[_treated] //taft
mat A[`i',11] = 2*ttail(e(df_r),abs(_b[_treated]/_se[_treated])) //paft
scalar vrb = 100*`cont_varbef'/`cont_cnt'
scalar vra = 100*`cont_varaft'/`cont_cnt'
}
/* Get overall stats for the second table */
qui probit _treated `exog' if esample==1
qui predict double index0 if e(sample), xb
mat R[1,1] = e(r2_p) // r2bef
mat R[1,3] = e(chi2) // chibef new
mat R[1,5] = chi2tail(e(df_m), e(chi2)) // probbef
qui probit _treated `exog' [iw=_weight] if _support==1 & esample==1
qui predict double indexm if e(sample), xb
mat R[1,2] = e(r2_p) // r2aft
mat R[1,4] = e(chi2) // chibef
mat R[1,6] = chi2tail(e(df_m), e(chi2)) //probaft
qui replace _bias0 = bias in `i'
qui replace sumbias0 = abs(bias) in `i'
qui replace _biasm = biasm in `i'
qui replace sumbias = abs(biasm) in `i'
qui sum sumbias0, detail
mat R[1,7] = r(mean) // meanbiasbef
mat R[1,9] = r(p50) // medbiasbef
qui sum sumbias, detail
mat R[1,8] = r(mean) // meanbiasaft
mat R[1,10] = r(p50) // medbiasaft
qui sum index0 if _treated==1 & esample==1
scalar mi1 = r(mean)
scalar vi1 = r(Var)
qui sum index0 if _treated==0 & esample==1
scalar mi0 = r(mean)
scalar vi0 = r(Var)
mat R[1,11] = 100*(mi1 - mi0)/sqrt((vi1 + vi0)/2) // ibiasbef
mat R[1,13] = vi1/vi0 // iratiobef
qui sum indexm [iw=_weight] if _treated==1 & _support==1 & esample==1
scalar mi1 = r(mean)
scalar vi1 = r(Var)
qui sum indexm [iw=_weight] if _treated==0 & _support==1 & esample==1
scalar mi0 = r(mean)
scalar vi0 = r(Var)
mat R[1,12] = 100*(mi1 - mi0)/sqrt((vi1 + vi0)/2) // ibiasaft
mat R[1,14] = vi1/vi0 // iratioaft
mat R[1,15] = 100*`cont_varbef'/`cont_cnt'
mat R[1,16] = 100*`cont_varaft'/`cont_cnt'
end
* Get example dataset
webuse cattaneo2, clear
* Keep only the used variables
keep mbsmoke foreign alcohol mage medu fage fedu bweight
* Run psmatch2
qui psmatch2 mbsmoke foreign alcohol mage medu fage fedu, out(bweight) logit n(5) ai(5) ate common
qui gen esample=e(sample)
mat P=J(1,13,.)
local xvars foreign alcohol mage medu fage fedu
pstest_tables "`xvars'" "`r(atu)'"
* Run the pstest_tables function
qui psmatch2 mbsmoke foreign alcohol mage medu fage fedu, out(bweight) logit n(5) ai(5) ate common
pstest, both atu
mat li A
mat li R
matrix colnames R = "R2(U)" "R2(M)" "LRchi2(U)" "p>chi2(U)" "MeanBias(U)" "MedBias(U)" "B(U)" ///
"R(U)" "%Var(U)" "LRchi2(M)" "p>chi2(M)" "MeanBias(M)" "MedBias(M)" "B(M)" "R(M)" "%Var(M)"
* Reshape the matrix to resemble the tables from pstest
mat B = A[1..6,1..4]
mata : M = colshape(st_matrix("B"),2)
mata : st_matrix("B",M)
mat C = A[1..6,5..6]
mata: M = colshape(st_matrix("C"),1)
mata : st_matrix("C",M)
mat D = J(12,1,.)
mat D[2,1] = A[1,7]
mat D[4,1] = A[2,7]
mat D[6,1] = A[3,7]
mat D[8,1] = A[4,7]
mat D[10,1] = A[5,7]
mat D[12,1] = A[6,7]
mat E = A[1..6,8..11]
mata: M = colshape(st_matrix("E"),2)
mata : st_matrix("E",M)
mat F = A[1..6,12..13]
mata: M = colshape(st_matrix("F"),1)
mata : st_matrix("F",M)
mat G = B,C,D,E,F
mata: M = colshape(st_matrix("R"),2)
mata: st_matrix("R",M)
mat R = R'
mat li R
matrix colnames R = "Ps R2" "LR chi2" "p>chi2" "MeanBias" "MedBias" "B" "R" "%Var"
matrix rownames R = "Unmatched" "Matched"
matrix colnames G = "Treated" "Control" "%bias" "%red_bias" "t" "p>|t|" "V(T)/V(C)"
matrix rownames G = "foreign (U)" "foreign (M)" "alcohol (U)" "alcohol (M)" "mage (U)" ///
"mage (M)" "medu (U)" "medu (M)" "fage (U)" "fage (M)" "fedu (U)" "fedu (M)"
estout mat(G, fmt(3))
mat li R, format(%4.3f)
qui psmatch2 mbsmoke foreign alcohol mage medu fage fedu, out(bweight) logit n(5) ai(5) ate common
pstest, both atu
我目前正在使用一些匹配技术并有同样的问题。我发现的一个快捷方式是使用“asdoc”。
我用过: “asdoc pstest v1 v2 v3”
asdoc 在您的工作目录中创建了一个名为 myfile 的 Word 文档,从那里您可以将表格转换为乳胶。我还没有找到更好的方法,但我想它是否对你有用。 祝你工作顺利! :)
asdoc
缺少处理pstest
表的专用模块。因此,它将 pstest
结果写入日志文件,然后将该日志转换为表。虽然此表包含所有必要的信息,但其结构和格式可能并不理想。
为了解决这个限制,我在
pstest
(asdocx
的高级版本)中为asdoc
开发了一个特定的模块。对于那些感兴趣的人,他们可能喜欢访问 asdocx 网站上的 pstest 页面。