Stata: 外部命令的搜索、安裝與使用
作者:游萬海 || 連玉君 || (知乎 | 簡書 | 碼雲)
Stata連享會 精彩推文1 || 精彩推文2
Stata 軟體的一大特點是其開放性。用戶可以修改 Stata 官方提供的命令 (其實就是一些以 .ado 為後綴的文本文件),為己所用。同時,全球的 Stata 用戶也在日夜耕耘,共享他們編寫的新命令——外部命令,比如大家熟悉的 outreg2
(Roy Wada),estout
(Ben Jann) 和 winsor2
(Lian Yu-jun ) 等熱門命令。
遺憾的是,這些外部命令分散在互聯網的各個角落,我們需要一些海納百川的本領。這就是本文地主要目的:大家介紹外部命令的搜索、安裝及使用方法。
大家在演示教材中的例子時,經常發現為何相同的命令,教材可以得到結果,而自己運行卻出現錯誤,如最常見是扎眼的紅色信息 command fuzzydid is unrecognized
常用的外部命令來源:
- Statistical Software Components (SSC) archive (http://www.repec.org)
- Stata Journal ( https://www.stata-journal.com/)
- Stata Technical Bulletin (STB) (https://www.stata.com/products/stb/)
- github (https://github.com/)
一些最熱門的命令:
輸入 ssc hot, n(10)
可以查看 SSC 上最熱門的 10 個命令:
輸入 github hot, number(5)
可以查看 Github 上最熱門的 5 個命令。
1. 路徑設置
為了方便命令的管理,下面先介紹一些基本的設置。
下載的外部命令通常為 .ado 格式,為了將下載的命令與自編的命令放在不同的路徑下,找到 Stata 的安裝路徑,可以看到一個 【ado 】文件夾。裡面有【base】 文件、【plus】 文件及 【personal】 文件(後兩個文件夾不一定有自帶,可以自己手動創建)。 - 【base】文件夾用於存儲 Stata 自帶的基礎命令 - 【plus】文件夾用於存儲外部命令 - 【personal】用於存儲自己編寫的命令和 dofiles。
為了達到這一目的,需要對系統默認路徑進行一些簡單的設置。在 Stata 的安裝路徑下(如:【D:stata15】)新建一個 profile.do 文件,將如下命令放到此文件中:
adopath + "D:stata15adoplus"
sysdir set PLUS "D:stata15adoplus" // 外部命令的存放位置
sysdir set PERSONAL "D:stata15adopersonal" // 個人文件夾位置
cd `c(sysdir_personal)
(Note: 連老師在這裡-連玉君的 profile.do 文檔 分享了他的 profile.do 文檔。https://gitee.com/arlionn/StataProfile )
2. 外部命令的下載
根據外部命令的來源不同,所使用的命令也不盡一致,這裡主要介紹 ssc
, search,net
,findit
和 github
等命令。
2.1 ssc 命令
ssc
是 Statistical Software Components (http://www.repec.org) 的縮寫,用於操作存放在該網站上的外部命令,包括安裝 ( ssc install
)、移除 ( ssc uninstall
)、描述 ( ssc describe
)、顯示最近更新 ( ssc new
)、顯示最熱門 ( ssc hot
)。例如:
. ado dir //顯示已安裝ado文件
[1] package outreg2 from http://fmwww.bc.edu/repec/bocode/o
OUTREG2: module to arrange regression outputs into an illustrative table
[2] package estout from http://fmwww.bc.edu/repec/bocode/e
ESTOUT: module to make regression tables
. ssc install winsor2, all replace //安裝winsor2,基本用法:ssc install newprogramname
checking winsor2 consistency and verifying not already installed...
installing into D:stata15adoplus...
installation complete.
若要查看某一具體命令是否安裝,可以利用
. ado, find(winsor2)
[3] package winsor2 from http://fmwww.bc.edu/repec/bocode/w
WINSOR2: module to winsorize data
要查看 ssc
上最熱門的命令,可以通過
. ssc hot, n(10) //顯示排名前10的命令
Top 10 packages at SSC
Aug 2018
Rank # hits Package Author(s)
----------------------------------------------------------------------
1 331271.0 findname Nicholas J. Cox
2 19504.6 outreg2 Roy Wada
3 18223.6 estout Ben Jann
4 11066.7 distinct Gary Longton, Nicholas J. Cox
5 7746.3 winsor Nicholas J. Cox
6 6881.7 winsor2 Lian Yu-jun
7 6598.7 ivreg2 Mark E Schaffer, Steven Stillman, Christopher F Baum
8 6579.3 ivreg210 Mark E Schaffer, Steven Stillman, Christopher F Baum
9 6571.1 ivreg28 Mark E Schaffer, Steven Stillman, Christopher F Baum
10 6561.8 ivreg29 Christopher F Baum, Mark E Schaffer, Steven Stillman
----------------------------------------------------------------------
(Click on package name for description)
從上面結果可以看出,連老師編寫的 winsor2
也榜上有名 (^^此處應有掌聲)。另外,還有 ssc new
, ssc describe
, ssc type
和 ssc copy
等用法,具體可以通過 help ssc
查看相關例子。
2.2 search 命令
與 ssc
命令相比,search
搜索的範圍更廣,該命令語法如下:
.search word [word ...] [, search_options]
word 表示待搜索的關鍵詞,比如想了解面板數據模型的相關命令,可以 search panel data model
。對於怎麼取關鍵詞,可以 help searchadvice
。可以看出,搜索結果中既有 Stata 手冊中的命令,也有外部命令,甚至還包括 Stata 官方的培訓、書籍、視頻和 FAQ 等信息。
search 命令的選項 search_options 主要包括:all
,net
,faq
,manual
,sj
等,對應上文提到的各種類型的信息,以便於我們實現精準搜索。
- (1) search + all: 通常為默認選項,根據幫助文件的描述:
search, all
is the best way to search for information on a topic across all sources, including the system help, the FAQs at the Stata website, the Stata Journal,and all Stata-related Internet sources including user-written additions
從上面的表述可以發現,當定義 all
時,其搜索範圍很廣,包括軟體自帶的系統文件,Stata 網站的常見問題集,Stata journal 期刊及其他網路相關資料。因此,與 ssc
不同,search
命令不僅可以搜索外部命令,也能搜索其他相關文檔資料。
- (2) search + net: 與
net search
等價,根據幫助文件描述:
net search
searches the Internet for user-written additions to Stata, including, but not limited to, user-written additions published in the Stata Journal (SJ) and in the Stata Technical Bulletin (STB)
從上述描述可知,通過該命令可以搜索發布在 Stata Journal (SJ) 和Stata Technical Bulletin (STB)上的相關資料。資料不僅包括 ado 命令 文件,還包括幫助文件 (help files) 和數據集 (datasets).
- (3) search + sj: 僅搜索 Stata Journal 和 Stata Technical Bulletin 上的資源。
- (4) search + faq: 僅搜索到發布在 Stata 官網 http://www.stata.com 中的 FAQS 條目下的資源。
- (5) search + manual: 僅搜索 Stata 電子手冊文檔上的資源。 例子:
. search linear regression, all
. search linear regression, net
. search linear regression, sj
. search linear regression, faq
. search linear regression, manual
2.3 net 命令
net
命令的用法與 search
相似,功能也較多,詳細可以 help net
查看。本文主要介紹以下幾種: - (1) net search word [word ...] [, search_options]
該命令與上面介紹的 search + net
等價; (2) net install
語法如下:
. net install pkgname [, all replace force from(directory_or_url)]
該命令可以用於從特定的網站安裝外部 ado 文件,比如
.net install github, from("https://haghish.github.io/github/")
運行上述命令得到
. net install github, from("https://haghish.github.io/github/")
checking github consistency and verifying not already installed...
installing into D:stata15adoplus...
installation complete.
使用 ado describe
命令可以查看已安裝命令詳情:
. ado describe github
--------------------------------------------------------
[4] package github from https://haghish.github.io/github
--------------------------------------------------------
TITLE
GITHUB: search, install, and uninstall Stata packages with a particular {break}
DESCRIPTION/AUTHOR(S)
version (release) as well as their dependencies from
{browse "http://www.github.com/haghish/github":GitHub} website
Distribution-Date: 20161214
INSTALLATION FILES
ffindall.ado
ffindall.sthlp
ggithub.ado
ggithub.dlg
ggithub.sthlp
ggithubcheck.ado
ggithubconfirm.ado
ggithubdependency.ado
ggithublist.ado
ggithubmake.ado
ggithuboutput.ado
ggithubquery.ado
ggithubsearch.ado
ggithubsearchsteps.ado
mmake.ado
mmake.dlg
INSTALLED ON
1 Nov 2018
--------------------------------------------------------
- (3)
net sj vol-issue [insert]
這個用法很強大,有時候我們想把 Stata Journal 某一期所涉及的外部命令都下載到本地,比如想安裝 2018年第 3 期文中的所有文件,可以使用如下命令:
. net sj 18-3
這等價於
. net from "http://www.stata-journal.com/software/sj18-3"
2.4 findit 命令
findit + keyword
等價於 search keyword [keyword ...], all
可以搜索的資料包括: 系統文件 the system help, the FAQs, the Stata Journal, and all Stata-related Internet sources including user-written additions. 如我們想了解 Stata 中有關面板單位根檢驗方面命令與資料,可以執行如下命令:
. findit panel unit root
2.5 github命令
gitHub 是一個面向開源及私有軟體項目的託管平台,因為只支持 git 作為唯一的版本庫格式進行託管,故名 gitHub。在 GitHub 中,用戶可以十分輕易地找到海量的開源代碼。
目前,越來越多學者將程序託管到該平台,包括 Python,R 和 Stata 等各種軟體。為了更方便地安裝託管在github 上的命令,E. F. Haghish 開發了 github
命令。github
可以實現搜索、安裝、移除等功能。
為了使用這一外部命令,首先要通過以下命令進行安裝
. net install github, from("https://haghish.github.io/github/")
完成 github 安裝,通過 help github
,可以發現其語法如下
. github [ subcommand ] [ keyword | username/repository ] [, options]
這裡主要介紹 github search
和 github install
:
- (1)
github search
: 該命令可以對託管到 github 平台的 Stata 相關命令進行搜索,比如我們想知道在 github 平台上有哪些面板數據模型相關命令,可以輸入:
. github search panel data model, in(all)
根據上面返回的結果,點擊相應藍色部分的命令可以在 github 上查看相應的項目,包括:倉庫、作者主頁等,點擊 Install 可以在線安裝該命令的相關文件。
- (2)
github install
: 該命令主要用於安裝託管於 github 平台的外部命令。比如我們想了解在 Stata 中是否有實現動態報告的相關命令(類似於 R 裡面的 knitr 包),可以輸入命令
. github search dynamic report, in(all)
此時,點擊 Install 會自動安裝最新版本,若想安裝此前的某個版本,則可以使用 github install
命令的 version()
選項加以控制:
. github install haghish/MarkDoc , version("3.8.0")
之後在界面上可以看到
. github install haghish/MarkDoc , version("3.8.0")
checking markdoc consistency and verifying not already installed...
installing into D:stata15adoplus...
installation complete.
Checking package dipendencies
markdoc package has no dependency
這也是發佈於 github 上的命令區別於 SSC 上的命令的主要區別:Github 可以非常高效地進行版本控制。
3. 總結
可將本部分內容歸結為兩點: - 其一,當知道外部包的具體命令寫法時,通常可以利用 ssc install
, net install
,github install
等命令直接安裝; - 其二,若只知道該命令的大體功能或關鍵詞,而不知道具體名稱,可以通過 findit
,search
,github search
等命令進行搜索,在返回的結果中查找安裝。 - 其三,為了保證外部命令能夠被 Stata 自動檢索到,需要在 profile.do 文檔中設定文件路徑。
關於我們
- 【Stata 連享會(公眾號:StataChina)】由中山大學連玉君老師團隊創辦,旨在定期與大家分享 Stata 應用的各種經驗和技巧。
- 公眾號推文同步發佈於 CSDN-Stata連享會 、簡書-Stata連享會 和 知乎-連玉君Stata專欄。可以在上述網站中搜索關鍵詞
Stata
或Stata連享會
後關注我們。 - 點擊推文底部【閱讀原文】可以查看推文中的鏈接並下載相關資料。
- Stata連享會 精彩推文1 || 精彩推文2
聯繫我們
- 歡迎賜稿: 歡迎將您的文章或筆記投稿至
Stata連享會(公眾號: StataChina)
,我們會保留您的署名;錄用稿件達五篇
以上,即可免費獲得 Stata 現場培訓 (初級或高級選其一) 資格。 - 意見和資料: 歡迎您的寶貴意見,您也可以來信索取推文中提及的程序和數據。
- 招募英才: 歡迎加入我們的團隊,一起學習 Stata。合作編輯或撰寫稿件五篇以上,即可免費獲得 Stata 現場培訓 (初級或高級選其一) 資格。
- 聯繫郵件: [email protected]
往期精彩推文
- Stata連享會推文列表1
- Stata連享會推文列表2
- Stata連享會 精彩推文1 || 精彩推文2
http://qr06.cn/B9EOq8 (二維碼自動識別)
http://weixin.qq.com/r/7Ujm-tfEHIpjrZOd9x3- (二維碼自動識別)
推薦閱讀: