From ae406539d8f94409d346db38abf22b65f4be1c15 Mon Sep 17 00:00:00 2001 From: Meiting-Wang <379446137@qq.com> Date: Mon, 27 Apr 2020 23:54:34 +0800 Subject: [PATCH] Initial commit --- .gitattributes | 2 + .gitignore | 30 ++++++++ LICENSE | 21 ++++++ stata.toc | 8 ++ wmttest.ado | 200 +++++++++++++++++++++++++++++++++++++++++++++++++ wmttest.pkg | 9 +++ 6 files changed, 270 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 stata.toc create mode 100644 wmttest.ado create mode 100644 wmttest.pkg diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3f73e9f --- /dev/null +++ b/.gitignore @@ -0,0 +1,30 @@ +# Created by https://www.gitignore.io/api/stata +# Edit at https://www.gitignore.io/?templates=stata + +### Stata ### +# .gitignore file for git projects containing Stata files +# Commercial statistical software: http://www.stata.com + +# Stata dataset and output files +*.dta +*.gph +*.log +*.smcl +*.stpr +*.stsem + +# Graphic export files from Stata +# Stata command graph export: http://www.stata.com/manuals14/g-2graphexport.pdf +# +# You may add graphic export files to your .gitignore. However you should be +# aware that this will exclude all image files from this main directory +# and subdirectories. +# *.ps +# *.eps +# *.wmf +# *.emf +# *.pdf +# *.png +# *.tif + +# End of https://www.gitignore.io/api/stata \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d22c1cc --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Meiting-Wang + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/stata.toc b/stata.toc new file mode 100644 index 0000000..ef0d55c --- /dev/null +++ b/stata.toc @@ -0,0 +1,8 @@ +v 1.0.0 +d Materials by Meiting-Wang +d School of Economics, South-Central University for Nationalities +d 2017110097@mail.scuec.edu.cn + +d 'wmttest': output ttest table to Word, LaTeX, Stata interface. + +p wmttest diff --git a/wmttest.ado b/wmttest.ado new file mode 100644 index 0000000..e31d296 --- /dev/null +++ b/wmttest.ado @@ -0,0 +1,200 @@ +* Feather: output Grouping t-means test to Stata interface, Word and LaTeX +* Author: Meiting Wang, Master, School of Economics, South-Central University for Nationalities +* Email: 2017110097@mail.scuec.edu.cn +* Created on Oct 24th, 2019 + + +program define wmttest +version 15.1 + +syntax varlist(numeric) [if] [in] [using/] , by(varname numeric) [ /// + replace append Statistics(string) TItle(string) Alignment(string) PAGE(string)] +/* +optional illustration: +1. varlist: only numeric variable names permitted. +2. by(): only a numeric variable name permitted, and this variable should be a binary variable. +3. statistics(): include N N1 N2 mean1 mean2 mean_diff se t p, and you can set +the format of every statistics, such as mean1(%9.3f) mean2(5). +4. title(): set the title for the reported table, Grouping t-means test as the default. +5. alignment(): only used in the LaTeX output, set the column format of the LaTeX +table, but it will not impact the column format in the Stata output table, dot as +the default. +6. page(): only used in the LaTeX output,set the extra package for the LaTeX code. +please don't need to add the package of booktabs array dcolumn, because the code +will automatic process these with the option of alignment(). +*/ + + +*--------设置默认格式------------ +*Stata界面显示和Word输出默认格式 +local N_default_fmt "%11.0f" +local others_default_fmt "%11.3f" + +*LaTeX默认输出格式 +local N_default_la_fmt "%11.0fc" +local others_default_la_fmt "%11.3fc" + +*默认下会输出的统计量(界面、Word、LaTeX) +if "`statistics'" == "" { + local statistics "N1 mean1 N2 mean2 mean_diff(star)" +} + + +*--------输入选项不合规的报错信息------- +if ("`replace'`append'"!="")&("`using'"=="") { + dis "{error:replace or append can't appear when you don't need to output result to a file.}" + exit +} + +if ("`replace'"!="")&("`append'"!="") { + dis "{error:replace and append cannot appear at the same time.}" + exit +} + +if (~ustrregexm("`using'",".tex"))&("`alignment'`page'"!="") { + dis "{error:alignment and page can only be used in the LaTeX output.}" + exit +} + + +*---------前期语句处理---------- +*普通选项语句的处理 +if "`using'" != "" { + local us_ing "using `using'" +} + +if "`title'" == "" { + local title "Grouping t-means test" +} //设置默认标题 + +local addnotes_stata "" +local addnotes_word "" +local addnotes_latex "" +if ustrregexm("`statistics'", "star") { + local addnotes_stata `"addnotes("* p < 0.10, ** p < 0.05, *** p < 0.01")"' + local addnotes_word `"addnotes("* \i p \i0 < 0.10, ** \i p \i0 < 0.05, *** \i p \i0 < 0.01")"' + local addnotes_latex `"addnotes("$^{*}\ p < 0.10,\ ^{**}\ p < 0.05,\ ^{***}\ p < 0.01$")"' +} //依据是否有star设定表格标注 + +*构建esttab中cells("")内部的语句 +//对`statistics'进行预处理 +tokenize "`statistics'", parse("()") +local statistics "" +local i = 1 +while "``i''" != "" { + if (mod(`i'+1,4)==0) { + local `i' = ustrregexra("``i''"," ","-") + } + if (mod(`i',4)==0) { + local `i' "``i'' " + } + local statistics "`statistics'``i''" + local `i' "" //置空`i' + local i = `i' + 1 +} +local statistics = ustrtrim("`statistics'") + +//对`statistics'进行正式处理 +tokenize "`statistics'" +local i = 1 +while "``i''" != "" { + local inp_`i' "``i''" + local inp_pure_`i' = ustrregexrf("``i''","\(.*","") + local `i' "" //将`i'置空 + local i = `i' + 1 +} //分解"`statistics'" +local stat_num = `i' - 1 //记录要输出的统计量的总数 + +local N "count" //以下几条local下面循环要用;这些也是该命令可以输出的全部统计量 +local N1 "N_1" +local N2 "N_2" +local mean1 "mu_1" +local mean2 "mu_2" +local mean_diff "b" +local se "se" +local t "t" +local p "p" + +local i = 1 +local st "" //界面显示和Word输出中cells("")内部的语句 +local stl "" //LaTeX输出中cells("")内部的语句 +local alignment_default "" //LaTeX输出中alignment()内部的默认语句 + +while "`inp_pure_`i''" != "" { + if ustrregexm("`inp_pure_`i''", "\bN\w*") { + local default_fmt "`N_default_fmt'" + local default_la_fmt "`N_default_la_fmt'" + local alignment_default "`alignment_default'>{$}c<{$}" + } + else { + local default_fmt "`others_default_fmt'" + local default_la_fmt "`others_default_la_fmt'" + local alignment_default "`alignment_default'D{.}{.}{-1}" + } + if ustrregexm("`inp_`i''", "\("){ + local fmt = ustrregexrf("`inp_`i''",".*\(","") //将左括号及之前的内容移除 + local fmt = ustrregexrf("`fmt'","\)","") //将右括号移除 + local fmt = ustrregexra("`fmt'","-+"," ") //将"-"号替换成空格 + tokenize "`fmt'" + local j = 1 + local fmt_st "" + while "``j''" != "" { + if "``j''" == "star" { + local fmt_st "`fmt_st'star " + } + else { + local fmt_st "`fmt_st'fmt(``j'') " + } + local `j' "" //将`j'置空 + local j = `j' + 1 + } + local fmt_st = ustrtrim("`fmt_st'") + if ustrregexm("`fmt_st'", "\d"){ + local st "`st'``inp_pure_`i'''(`fmt_st' label(`inp_pure_`i'')) " + local stl "`stl'``inp_pure_`i'''(`fmt_st' label(\multicolumn{1}{c}{`inp_pure_`i''})) " + } + else { + local st "`st'``inp_pure_`i'''(`fmt_st' fmt(`default_fmt') label(`inp_pure_`i'')) " + local stl "`stl'``inp_pure_`i'''(`fmt_st' fmt(`default_la_fmt') label(\multicolumn{1}{c}{`inp_pure_`i''})) " + } + } + else { + local st "`st'``inp_pure_`i'''(fmt(`default_fmt') label(`inp_pure_`i'')) " + local stl "`stl'``inp_pure_`i'''(fmt(`default_la_fmt') label(\multicolumn{1}{c}{`inp_pure_`i''})) " + } + local i = `i' + 1 +} +local st = ustrtrim("`st'") +local stl = ustrtrim("`stl'") + +*构建esttab中alignment()和page()内部的语句(LaTeX输出专属) +if "`page'" != "" { + local page ",`page'" +} +if "`alignment'" == "math" { + local page "array`page'" + local alignment "*{`stat_num'}{>{$}c<{$}}" +} +else { + local page "array,dcolumn`page'" + local alignment "`alignment_default'" +} +//加上array宏包可使得表格线之间的衔接没有空缺 + +*---------------------主程序---------------------------------- +qui estpost ttest `varlist' `if' `in', by(`by') +esttab, cells("`st'") compress /// + noobs nomti nonum starlevels(* 0.10 ** 0.05 *** 0.01) /// + `addnotes_stata' title(`title') //Stata 界面显示 +if ustrregexm("`us_ing'",".rtf") { + esttab `us_ing', cells("`st'") compress `replace'`append' /// + noobs nomti nonum starlevels(* 0.10 ** 0.05 *** 0.01) /// + `addnotes_word' title(`title') +} //Word 显示 +if ustrregexm("`us_ing'",".tex") { + esttab `us_ing', cells("`stl'") compress `replace'`append' /// + starlevels(\sym{*} 0.10 \sym{**} 0.05 \sym{***} 0.01) /// + noobs nomti nonum `addnotes_latex' title(`title') /// + booktabs width(\hsize) page(`page') alignment(`alignment') +} //LaTeX 显示 +end diff --git a/wmttest.pkg b/wmttest.pkg new file mode 100644 index 0000000..97a8540 --- /dev/null +++ b/wmttest.pkg @@ -0,0 +1,9 @@ +v 1.0.0 +d 'WMTTEST': wmttest +d +d output ttest table to Word, LaTeX, Stata interface. +d +d Distribution-Date: 20200427 +d License: MIT +d +F wmttest.ado