👇 連享會 · 推文導航 | www.lianxh.cn
理論 + 實證:從「讀懂模型」到「折騰模型」
🎦 理論模型構建專題
📅 2021 年 10 月 2-3 日 (周六-周日)
🔑 郭凱明副教授 (中山大學)
🍓 課程主頁:https://gitee.com/lianxh/emodel ,招聘助教 8 名
🌴報名連結: http://junquan18903405450.mikecrm.com/QdtTXkm
理論模型可以簡潔、凝練地抽離出經濟現象的本質,使我們能夠進行更深層次的思考和分析。然而,建立理論模型並非易事,若能將 理論和實證有機結合,那更加難能可貴了。
為此,我們邀請到了中山大學嶺南學院郭凱明副教授,與大家一同學習理論模型的構建。郭老師一直專注於經濟轉型與中國經濟方面的研究,發表論文近 40 篇,其中《經濟研究》7 篇。
郭老師將從模型設定初衷、最基本的假設條件入手,通過討論各種可能的建模思路和彎路,讓學生不自覺中已經建立起理論分析的思維模式。最終的目標是:讓學生不僅能「讀懂模型」,還能「折騰模型」—— 可以自己修改甚至新設模型。
掃碼直達課程主頁:
作者: 袁子晴 (香港大學)
郵箱: yzq0612@foxmail.com
目錄
1. 問題背景
2. Stata 實操
2.1 Stata 外部命令介紹
2.2 命令安裝及初始化
3. Stata 繪製流程圖
4. Latex 編譯流程圖
5. 參考文獻
6. 相關推文
溫馨提示: 文中連結在微信中無法生效。請點擊底部「閱讀原文」。或直接長按/掃描如下二維碼,直達原文:
⭕ 隨後,可以在 連享會:B站 搜索「Latex 流程圖」查看本文的視頻版。
1. 問題背景
在研究過程中,我們往往需要藉助流程圖來直觀清楚地展示實驗步驟、技術路線、邏輯推演等內容,如下圖所示。視頻演示一個簡化的例子,可以點擊 Overleaf的案例1項目地址 查看編譯後的流程圖。通常來說,利用命令可以精準控制圖形的形狀和位置,對於結構性較強的圖形,利用命令畫圖比手工繪圖更值得推薦。
LaTeX 作為論文排版神器,本身有一些命令可以繪製簡單的圖形,但繪製複雜圖形則需要使用一些宏包,其中 TikZ 是最常用的宏包之一。但是 LaTex 繪圖學習成本高且代碼繁瑣,所以本文將介紹一個 Stata 命令 flowchart 幫助我們生成可以直接在 LaTex 中編譯的流程圖文件。
2. Stata 實操2.1 Stata 外部命令介紹
首先通過 ssc install flowchart 來安裝外部命令 flowchart ,使用 flowchart 繪製流程圖時,其命令結構如下:
flowchart [命令] [, 選項]主要命令:
init :命令格式為 init using filename.data —— 初始化創建用於存儲繪圖數據的文件(指定文件名或包含文件路徑的文件名),該文件後續由 LaTeX 的 datatool 包用來繪圖;
writerow(..) :按行 (Row) 來創建流程圖中的每個區塊 (Block);
connect :將流程圖中的每個區塊箭頭連接起來;
finalize :按照指定模板,將流程圖以 TikZ 代碼的形式輸出,以便後續 LaTeX 編譯;
setup:安裝其他依賴包 ( texdoc 和 sjlatex ) 及輔助文件至當前工作路徑來確保命令正常運行,選項 [, update] 用來更新 flowchart 命令的安裝;
debug:命令格式為 debug, [on off logreset info check tikz] —— 打開或關閉調試功能,並產生一個 DebugLog.log 文件,可以指定選項 tikz,在最終的 TikZ 文件中產生調試字符串。
2.2 命令安裝及初始化首先在 Stata 中運行如下命令:
ssc install flowchart
cd "工作路徑"
flowchart setup // 輔助文件會自動下載到上述指定的工作路徑中3. Stata 繪製流程圖
步驟如下:
指定存儲繪圖數據的文件名,文件後綴為 .data,例如 flowchart init using "..\Data\Subanalysis Data\methods--fig-flowchart.data";
從左到右依次指定該行每個區塊的名稱和內容,例如 flowchart writerow ...;
將不同區塊之間用箭頭連接起來,例如 flowchart connect;
有以下幾種格式:
[rowname_center] --> [rowname_left] :水平箭頭連接左右區塊[rowname_center] --> [rowname_center] :垂直箭頭連接上下區塊。[rowname_left] --> [rowname_left] :垂直箭頭連接上下區塊。, arrow(angle) :這個選項使箭頭成90度角。在空白行中使用這個選項。用下劃線連接每一行的區塊,然後用這種方式連接與之對應的列方向。在 LaTex 中將 .tikz 為結尾的文件進行編譯。
案例 1:
* TESTS: This is a barebones test file for running tests. Use this as a template
* of the flowchart syntax to follow.
cd "工作路徑"
*-- 第一步:指定存儲繪圖數據的文件名,文件後綴為 .data---*
flowchart init using "filename.data"
*-- 第二步:從左到右依次指定該行每個區塊的名稱和內容-*
* | TEST1: Row with 2 blocks.
flowchart writerow(rownametest1): "lblock1_line1" 46 "This is one line, \\ of a block." ///
"lblock1_line2" 43 "This is another line, of a block" ///
"lblock1_line3" 3 "This is another line, of a block", ///
"rblock1_line1" 97 "This is one line, of a block." ///
"rblock1_line2" 33 "This is another line, of a block" ///
"rblock1_line3" 44 "This is another line, of a block"
* | TEST2: Row with No center-block (a center-block appears on the left)
flowchart writerow(rownametest2): Flowchart_Blank, ///
"rblock1_line1" 97 "This is one line, of a block." ///
"rblock1_line2" 33 "This is another line, of a block" ///
"rblock1_line3" 44 "This is another line, of a block"
* | TEST3: Row with No left-block (a left-block appears on the right)
flowchart writerow(rownametest3): "lblock1_line1" 46 "This is one line, \\ of a block." ///
"lblock1_line2" 43 "This is another line, of a block" ///
"lblock1_line3" 3 "This is another line, of a block", Flowchart_Blank
* 需要注意的是該行左邊是沒有區塊的,只有右邊有區塊,所以逗號前面用 flowchart_blank 代表空白
* | TEST4: Row with No center-block and a Singleton Lead-Line in the left-block
flowchart writerow(rownametest4): Flowchart_Blank, "rblock1_line1" 97 "This is one line, \\ of a block."
* | TEST5: Row with Singleton Lead-Line in the center-block and No left-block
flowchart writerow(rownametest5): "lblock1_line1" 46 "This is one line, \\ of a block.", Flowchart_Blank
* 第三步:將不同區塊之間用箭頭連接起來 -*
* | CONNECTIONS: Use the block orientation to connect arrows to the appropriate blocks
flowchart connect rownametest1_center rownametest1_left
flowchart connect rownametest1_left rownametest2_left
flowchart connect rownametest1_center rownametest3_center
flowchart connect rownametest3_center rownametest5_center
flowchart connect rownametest2_left rownametest4_left
* 第四步:將上述繪圖輸出為以 .tikz 為結尾的文件 -*
* | FINALIZE: This writes the files and generates the 'tikzpicture'
flowchart finalize, template("figure-flowchart.texdoc") output("figure.tikz")案例 2:
*-- 第一步:指定存儲繪圖數據的文件名,文件後綴為 .data---*
flowchart init using "methods--figure-flowchart.data"
*-- 第二步:從左到右依次指定該行每個區塊的名稱和內容-*
* 命令格式: flowchart writerow(行名): [區塊1] , [區塊2],同一行從左到右不同區塊之間用逗號隔開
* 區塊內部格式: "區塊名" n= "區塊內文本"
* 首先生成一行,行名為 enrollment;
* 生成該行左邊第一個區塊,區塊名為 referred,後面是該區塊內容 Referred (n=173),數字173,最終呈現為 (n=173),在本例中代表子樣本容量;
* 用逗號分隔後,生成右邊第二個區塊,區塊名為 referred_excluded, 後面全部是該區塊的文本內容,該區塊內部由三行組成,分別指定了每行的名稱、子樣本容量和文本內容。
flowchart writerow(enrollment): ///
"referred" 173 "Referred", ///
"referred_excluded" 17 "Excluded" ///
"referred_excluded_nopartic" 9 "a) Did not wish to participate" ///
"referred_excluded_noshow" 5 "b) Did not show for interview" ///
"referred_excluded_other" 3 "c) Other reasons"
flowchart writerow(assessment): ///
"assessed" 156 "Assessed for Eligibility", ///
"assessed_excluded" 54 "Excluded" ///
"assessed_excluded_inclusioncritunmet" 22 "a) Inclusion criteria not met" ///
"assessed_excluded_exclusioncritmet" 13 "b) Exclusion criteria met" ///
"assessed_excluded_unsuitedgroup" 7 "c) Not suited for waitlist group" ///
"assessed_excluded_unsuitedtx" 2 "d) Not suited for intervention" ///
"assessed_excluded_othertx" 3 "e) Sought other treatment" ///
"assessed_excluded_other" 7 "f) Other reasons"
flowchart writerow(random): "randomized" 102 "Randomized", flowchart_blank // Blank Row
flowchart writerow(allocgroup): ///
"alloc_interventiongroup" 51 "Allocated to Intervention group", ///
"alloc_waitlistgroup" 51 "Allocated to Wait-list control group"
flowchart writerow(allocdetails): ///
"intervention_received" 49 "Received intervention" ///
"intervention_unreceived" 2 "Did not receive intervention" ///
"intervention_unreceived_exclusioncrit" 1 "With exclusionary criteria" ///
"intervention_unreceived_notime" 1 "Could not find time to participate", ///
"waitlist_stayedon" 48 "Stayed on wait-list" ///
"waitlist_didnotstay" 3 "Did not stay on wait-list" ///
"waitlist_didnotstay_selfinduced" 2 "Lost motivation" ///
"waitlist_didnotstay_leftarea" 1 "Was offered treatment elsewhere"
flowchart writerow(postmeasurement): ///
"postintervention_lost" 5 "Post-intervention measurement" ///
"postintervention_lost_droppedout" 2 "Dropped out of the intervention" ///
"postintervention_lost_nomeasurement" 3 "Did not complete measurement", ///
"postwaitlist_lost" 6 "Post-wait-list measurement" ///
"postwaitlist_lost_droppedout" 3 "Dropped out of the wait-list" ///
"postwaitlist_lost_nomeasurement" 3 "Did not complete measurement" ///
flowchart writerow(wlistintervention): flowchart_blank, ///
"postwaitlist_intervention_allocated" 48 "Allocated to intervention" ///
"postwaitlist_intervention_received" 46 "Received intervention" ///
"postwaitlist_intervention_didnotreceive" 2 "Did not receive intervention" ///
"postwaitlist_intervention_dnr_lowmotivation" 1 "Reported low motivation" ///
"postwaitlist_intervention_dnr_notime" 1 "Could not find time to participate"
flowchart writerow(measurement3monpostint): ///
"intervention_3monthfollowup" 9 "3-months follow-up measurement: \\ \h Loss to follow-up", ///
"postwaitlist_postintervention_losstofollowup" 5 "Post-intervention measurement: \\ \h Loss to follow-up" ///
"postwaitlist_postintervention_losstofollowup_droppedout" 2 "Dropped out of the intervention" ///
"postwaitlist_postintervention_losstofollowup_incomplete" 3 "Did not complete measurement"
* 需要注意的是該行左邊是沒有區塊的,只有右邊有區塊,所以逗號前面用 flowchart_blank 代表空白
flowchart writerow(wlist3mon): flowchart_blank, ///
"postwaitlist_3monthfollowup" 2 "3-months follow-up measurement \\ \h Did not complete measurement"
flowchart writerow(analyzed): ///
"intervention_analyzed" 51 "Analyzed in Intervention group", ///
"postwaitlist_analyzed" 51 "Analyzed in Wait-list control group"
* 第三步:將不同區塊之間用箭頭連接起來 -*
flowchart connect enrollment_center enrollment_left
flowchart connect enrollment_center assessment_center
flowchart connect assessment_center assessment_left
flowchart connect assessment_center random_center
flowchart connect random_center allocgroup_center
flowchart connect random_center allocgroup_left, arrow(angled)
flowchart connect allocgroup_center allocdetails_center
flowchart connect allocgroup_left allocdetails_left
flowchart connect allocdetails_center postmeasurement_center
flowchart connect allocdetails_left postmeasurement_left
flowchart connect postmeasurement_center measurement3monpostint_center
flowchart connect measurement3monpostint_center analyzed_center
flowchart connect postmeasurement_left wlistintervention_left
flowchart connect wlistintervention_left measurement3monpostint_left
flowchart connect measurement3monpostint_left wlist3mon_left
flowchart connect wlist3mon_left analyzed_left
* 第四步:將上述繪圖輸出為以 .tikz 為結尾的文件 -*
flowchart finalize, template("figure-flowchart.texdoc") output("methods--figure-flowchart.tikz")4. Latex 編譯流程圖
在進行 LaTex 編譯之前,我們需要做以下準備工作:
LaTex 工作路徑下需要放置:繪圖文件 methods--figure-flowchart.tikz,以及 LaTex 在使用 datatool 宏包編譯的過程中還需要的 methods--figure-flowchart.dataLaTex 編譯的主文件 main.tex 裡面在正文開始前需要加載相應的宏包以便後續調用LaTex 在 figure 環境中加載以 .tikz 結尾的繪圖文件Latex 編譯過程中整體的文件結果如下:
main.tex 主要編譯文件,在此基礎上進行編譯;methods--figure-flowchart.tikz Stata 輸出的繪圖文件;methods--figure-flowchart.data Stata 存儲繪圖數據的文件,在LaTex 使用datatool 宏包編譯的過程中需要。methods--figure-flowchart.tex 編譯子文件,在 figure 環境中加載以 .tikz 結尾的繪圖文件;案例 1:
\documentclass{article}
% 加載所需宏包及自定義命令---%
%% Package Declarations
\usepackage[utf8]{inputenc} % Document Encoding
\renewcommand{\rmdefault}{ppl} % Font Changes - PPL = Palatino
% Layout Management 排版管理宏包
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{multicol}
% Design 版面設計宏包
\usepackage{xcolor} % Design - Custom Colors
\usepackage{graphicx} % Design - Graphics Packages
% Figures, Diagrams, and Other Graphics 生成圖表所需宏包
\usepackage{tikz} % TikZ Package - Generates graphics (i.e., flowcharts)
\usetikzlibrary{shapes,arrows}
\newcommand*{\h}{\hspace{5pt}}% For indentation
\newcommand*{\hh}{\h\h}% Double indentation
\usepackage{datatool} % DataTool Package - Loads Subanalysis Data to generate flowchart 數據工具包--加載子分析數據以生成流程圖
\DTLsetseparator{ = }% Delimiter
\newcommand{\symbcomma}{,} % Work-Around: Put a comma into a number with \symbcomma without breaking the Stata code
% Biostatistics 生物統計學宏包
\usepackage{amsmath} % Equations - Handler
\usepackage{amsfonts} % Equations - Display
\usepackage{amssymb} % Equations - Symbols
\usepackage{fontenc}
%---%
\title{LaTex-flowchart 演示}
\author{}
\date{}
%-- 正文 %
\begin{document}
% 插入流程圖子文件
\input{figure.tex}
\end{document}
Figure.tex 是包含了繪圖文件和繪圖數據的子文檔,這裡需要修改兩個地方,一是加載的數據文件,即filename.data,二是 LaTeX 的 input 命令插入的流程圖文件 figure.tikz\begin{figure}
\begin{center}
%% Load Subanalysis Data for Figure into \figvalue{} commands
\DTLloaddb[noheader, keys={subanakey,subanavalue}]{subanalysis-flowchart}{"filename.data"}
\newcommand{\figvalue}[1]{\DTLfetch{subanalysis-flowchart}{subanakey}{#1}{subanavalue}}
% Style - Setting the typeface to sans serif and the font size to small
% (the scope of this style is local to the environment)
\sffamily
\footnotesize
\input{figure.tikz}
\caption{Subject disposition flowchart of analytic sample data.} \label{fig:StudyFlowchart}
\end{center}
\end{figure}
最後,在 main.tex 文檔上進行編譯,可以點擊 Overleaf 的案例1項目地址 查看編譯後的流程圖,即文章開頭演示的流程圖。案例 2:
此外,作者提供的案例2的編譯過程在此不再贅述,可以直接點擊瀏覽該項目,最終的流程圖如下圖所示:
5. 參考文獻Github Stata-LaTex 項目地址: https://github.com/arlionn/flowchart在線編譯網站 OverLeaf: https://www.overleaf.com
6. 相關推文
Note:產生如下推文列表的 Stata 命令為:
Stata結果輸出:Excel結果表變身LaTeX表格Stata結果輸出:用esttab生成帶組別名稱的LaTeX回歸表格
lianxh latex
安裝最新版 lianxh 命令:
ssc install lianxh, replaceNew! Stata 搜索神器:lianxh 和 songbl GIF 動圖介紹
搜: 推文、數據分享、期刊論文、重現代碼 ……
👉 安裝:
. ssc install lianxh
. ssc install songbl
👉 使用:
. lianxh DID 倍分法
. songbl all🎦 🎦 🎦
🎧 項目申報書專題:自科、社科和教育部基金項目
📅 2021 年 9 月 19-21 日
🔑 遲老師;孫老師;張老師
🎤 講授方式:網絡直播,每天 7 小時(8:00-12:00;14:00-17:00)🍓 課程主頁:https://gitee.com/lianxh/kt
🍏 關於我們直通車: 👉【百度一下:連享會】即可直達連享會主頁。亦可進一步添加 「知乎」,「b 站」,「面板數據」,「公開課」 等關鍵詞細化搜索。