Transcript STATA

STATA 基礎教學
洪仲林
[email protected]
使用Stata的時機
•
•
•
•
需要處理大量資料
需要處理重複工作
需要較新的統計或計量方法
投稿專業期刊
Stata 版本
• Small、IC、SE、MP、LAB
– Small為教學用
– IC以上為研究用
– MP為多核心電腦用
– LAB 多人使用
Stata 11 視窗
工具列
快捷列
歷史指令視窗
變數視窗
執行結果視窗
指令輸入視窗
快捷列
變數管理視窗
執行結果記錄
搜尋
程式撰寫視窗
資料編輯視窗
如何輸入資料一
• 按快捷列資料編輯視窗,開啟工作表,直
接複製貼上
• 直接輸入資料
如何輸入資料二
• 使用點選輸入
– 工具列---File---Import
• 使用指令輸入
– 在指令輸入視窗鍵入
– Insheet using 檔名
變數名稱與標籤處理
• rename 舊變數名稱 新變數名稱
– 例:rename o gender
• 新增變數標籤
– 例: label variable gender “性別”
• 新增樣本標籤
– 例: label define genderlb 1 “男” 2 “女”
• 顯示樣本標籤
– 例:label values gender genderlb
刪除或保留資料
• drop 刪除變數
– 例:drop gender
• keep 保留變數
– 例:keep smoke drink
• 刪除或保留變數加上條件
– 例:drop if numofprivate<1 (沒給錢的小孩刪除)
– 例:keep if smoke==0 & drink==0 & gender==1
(挑丈夫)
資料描述一
• list 變數名稱 in if (顯示資料)
– 例: list age-marr in 1/100
• summarize 變數名稱 (樣本數、平均數、標
準差、最大最小值)
– 例: summarize education- exercise
資料描述二
• tab 某一個變數
– 例: tab gender
• tab 某兩個變數
– 例: tab gender education, column or row
• tab 某兩個變數, summarize(某一個變數)
– 例: tab gender education, summarize( marr)
• table某三個變數(類似樞紐分析)
– 例: table gender education marr
資料處理一
• gen 變數名稱=條件
– 例:gen biryear=2010-age (計算出生年)
• gen 變數名稱=條件 if 條件
– 例:gen income=1 if wageincome==1 |
retireincome==1 | rentincome==1 (歸類有收入)
– replace income=0 if income=. (歸類無收入)
• gen age5 = autocode(age, 5, 67, 87)
– 例:generate age5 = autocode(age, 5, 67, 87)(五
歲分成一群)
資料處理二
• destring 文字改數字
– destring , gen(變數名稱) or replace
• tostring 數字改文字
– tostring , gen(變數名稱) or replace
資料處理三
• sort 排序
– sort gender education
• gsort 降幕或升幕
– gsort gender –education
• 儲存檔案
– save female,replace
• 開啟檔案
– use female, clear
資料處理四
• merge
– sort gender education
• append
資料分析
• 線性迴歸
– regress 獨立變數 相依變數
• 單因子變異數分析
– oneway age marr
Do-File
• 將重複性的程式儲存於檔案中,類似批次
檔功能
– 可點選快捷列中的程式撰寫視窗後進行撰寫
– 也可於記事本中撰寫好之後,再貼到Stata中