第4講 數(shù)據(jù)操作和數(shù)據(jù)管理2_第1頁
第4講 數(shù)據(jù)操作和數(shù)據(jù)管理2_第2頁
第4講 數(shù)據(jù)操作和數(shù)據(jù)管理2_第3頁
第4講 數(shù)據(jù)操作和數(shù)據(jù)管理2_第4頁
第4講 數(shù)據(jù)操作和數(shù)據(jù)管理2_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、第五講 數(shù)據(jù)操作和數(shù)據(jù)管理二一、數(shù)據(jù)的追加與合并1D append . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Append datasets命令描述:把磁盤上的數(shù)據(jù)集追加到內(nèi)存數(shù)據(jù)集的末尾,其中內(nèi)存數(shù)據(jù)集稱作主數(shù)據(jù)集,磁盤數(shù)據(jù)集稱作追加數(shù)據(jù)集。數(shù)值型和字符型變量保留更精確的存儲類型。主數(shù)據(jù)集定義的變量標(biāo)簽和數(shù)據(jù)集標(biāo)簽不改變。進一步討論:如果追加數(shù)據(jù)集包含字符串變量,合并后的數(shù)據(jù)集在這個變量的附加數(shù)據(jù)將有數(shù)字缺失值;追加數(shù)據(jù)集的字符串變量的

2、內(nèi)容將被忽略。如果追加數(shù)據(jù)集包含數(shù)值變量,合并后的數(shù)據(jù)集將有在這個變量的附加數(shù)據(jù)的空字符串;追加數(shù)據(jù)集的數(shù)值變量的內(nèi)容被忽略。(這說明追加合并前需要保證變量類型一致)例1:use even,clearlistdescribe using odd (using的使用,summarize和list不能用)append using oddlist例2:(同例1,追加合并與變量的順序無關(guān))use odd,clearlistdescribe using evenappend using evenlist例3:(同例1,無論主數(shù)據(jù)集或追加數(shù)據(jù)集,都保留更精確的存儲類型)use even, clearapp

3、end using odddescribe例4:(添加值標(biāo)簽)use capoplistdescribe using ilpopdescribe using txpopappend using ilpop txpop, generate(state)label define statelab 0 "CA" 1 "IL" 2 "TX"label values state statelablist2D merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4、 . . . . . . . . . . . . . . . . . . . . Merge datasetsmerge Merge datasets (合并數(shù)據(jù)集。匹配于一個或多個指定的關(guān)鍵變量,合并主數(shù)據(jù)集和從數(shù)據(jù)集的對應(yīng)觀測值。append是為已有變量增加新觀測值)(合并從主數(shù)據(jù)集的第1個觀測值開始,對于主數(shù)據(jù)集的每1個觀測值,合并在從數(shù)據(jù)集中找到的對應(yīng)觀測值,記錄匹配或不匹配的結(jié)果。對主數(shù)據(jù)集的第2個觀測值重復(fù)上述操作,直到主數(shù)據(jù)集的匹配檢查全部完成。最后再檢查從數(shù)據(jù)集中未與主數(shù)據(jù)集匹配的觀測值。默認情況下,未匹配的觀測值保留在合并數(shù)據(jù)集中,無論他們來自主數(shù)據(jù)集或從數(shù)據(jù)集。) 參考文獻

5、:Gould, W. W. 2011a. Merging data, part 1: Merges gone bad. The Stata Blog: Not Elsewhere Classified.Gould, W. W. 2011b. Merging data, part 2: Multiple-key merges. The Stata Blog: Not Elsewhere Classified.1:1 merges原理關(guān)鍵變量同時出現(xiàn)在兩個數(shù)據(jù)集中,并且分別對應(yīng)唯一的觀測值。假設(shè)有兩個數(shù)據(jù)集:輸入:merge 1:1 id using filename輸入(面板數(shù)據(jù)的例子):mer

6、ge 1:1 pid time using filenamem:1 merges原理關(guān)鍵變量同時出現(xiàn)在兩個數(shù)據(jù)集中,并且在主數(shù)據(jù)集中對應(yīng)多個觀測值,在從數(shù)據(jù)集中對應(yīng)唯一觀測值。假設(shè)有兩個數(shù)據(jù)集:merge m:1 region using filename1:m merges原理類似于merge m:1,只是轉(zhuǎn)換主數(shù)據(jù)集和從數(shù)據(jù)集的位置。merge 1:m region using filename需要注意的是,Stata不建議使用m:m merges,這種合并不是一個好主意。你需要首先處理數(shù)據(jù),然后使用1:m merge或 m:1 merge。序貫合并(Sequential merges)和

7、m:m merges同樣有風(fēng)險。排除故障m:m merges1.有時間變量:假設(shè)一個面板數(shù)據(jù),每個id變量/標(biāo)識符對應(yīng)4個觀測值。 merge m:m subjectid using filename (錯誤)merge 1:m subjectid time using filename(加入時間變量,正確)2.沒有時間變量:假設(shè)數(shù)據(jù)集沒有時間變量,但有一個序列的概念,記錄了觀測值的順序。首要目標(biāo)應(yīng)該是從當(dāng)前順序創(chuàng)建明確的序列變量來解決這個問題。sort subjectid, stable (不要省略stable選項,這保證觀測值在每個id中有同樣的順序。保存數(shù)據(jù)。在從數(shù)據(jù)集執(zhí)行相同的操作。)

8、by subjectid: gen seqnum = _nmerge 1:m subjectid seqnum using filename例1:(merge 1:1模式)use autosize,clearlistuse autoexpense,clearlist(仔細觀察數(shù)據(jù)集,關(guān)鍵變量make同時出現(xiàn)在兩個數(shù)據(jù)集中,并且分別對應(yīng)唯一的觀測值,所以應(yīng)當(dāng)使用merge 1:1模式。)use autosize,clearmerge 1:1 make using autoexpenselisttabulate _mergelist if _merge < 3drop if _merge &

9、lt; 3listuse autosize, clear merge 1:1 make using autoexpense, keep(match) nogenerate list例2:(merge m:1模式)use sforce, clearlistuse dollars,clear list(仔細觀察數(shù)據(jù)集,關(guān)鍵變量region同時出現(xiàn)在兩個數(shù)據(jù)集中,在主數(shù)據(jù)集sforce中,region對應(yīng)多個觀測值;在從數(shù)據(jù)集dollars中,region對應(yīng)唯一觀測值,所以應(yīng)當(dāng)使用merge m:1模式。)use sforce,clear merge m:1 region using dollar

10、slist例3-例6是關(guān)于重疊變量,不作為重點。例3:(對于匹配的id,主數(shù)據(jù)集的數(shù)據(jù)享有優(yōu)先保存權(quán),重疊變量x1和x2仍然是主數(shù)據(jù)集的值。)use overlap1, clear list, sepby(id)use overlap2,clearlist(仔細觀察數(shù)據(jù)集,id是關(guān)鍵變量,x1和x2是重疊變量。)use overlap1,clearmerge m:1 id using overlap2list, sepby(id)例4:(update選項,使用從數(shù)據(jù)集的值替換主數(shù)據(jù)集的缺失值)use overlap1, clearmerge m:1 id using overlap2, upd

11、atelist, sepby(id)例5:(replace選項,使用從數(shù)據(jù)集的值替換主數(shù)據(jù)集的對應(yīng)值)use overlap1, clearmerge m:1 id using overlap2, update replacelist, sepby(id)例6:use overlap1, clearmerge m:1 id using overlap2, update keep(3 4 5)list, sepby(id)二、數(shù)據(jù)集的轉(zhuǎn)置1D reshape . . . . . . . . . . . . . . . . . . . . Convert data from wide to lon

12、g form and vice versareshape Convert data from wide to long form and vice versa(轉(zhuǎn)換數(shù)據(jù)從寬格式到長格式,反之亦然)示例:使用命令reshape之前,需要確定數(shù)據(jù)的格式是長還是寬,還要確定邏輯觀測值(i)和次級觀測值(j),如面板數(shù)據(jù)?;久睿簉eshape long inc, i(id) j(year) (從寬到長)( i()可代表一個id變量或多個id變量。多個id變量的例子是每個醫(yī)院的醫(yī)院id和病人id,i(hid pid)。hid和pid的唯一組合定義了組間變量)reshape wide inc, i(i

13、d) j(year) (從長到寬)例1:觀測數(shù)據(jù)Xij,其中i是邏輯觀測變量,或組間標(biāo)識符。j是次級觀測變量,或組內(nèi)標(biāo)識符。寬形數(shù)據(jù)由邏輯觀測變量組織,所有數(shù)據(jù)存儲在一行觀測值中。長形數(shù)據(jù)由次級觀測變量組織,數(shù)據(jù)存儲在多行觀測值中。 有寬形數(shù)據(jù),記錄個人ID、性別和19801982年的年收入。有兩個Xij變量。use reshape1,clearlistreshape long inc ue, i(id) j(year) (寬轉(zhuǎn)長。原始的寬形數(shù)據(jù)沒有year變量,轉(zhuǎn)換后的長形數(shù)據(jù)有新變量year)list, sep(3)reshape wide inc ue, i(id) j(year) (長

14、轉(zhuǎn)寬)list例2:(變量id不唯一識別觀測值,也就是說,在寬形數(shù)據(jù)中變量i()必須唯一)use reshape2, clearlistreshape long inc, i(id) j(year)例3:(在長形數(shù)據(jù)中,變量i(id)不必唯一,但j(year)在i()中必須唯一識別觀測值。)use reshapexp1,clearlistreshape wide inc, i(id) j(year)reshape error例4:use reshape6,clearlist in 1/4reshape wide inc, i(id) j(year) (錯誤)reshape wide inc u

15、e, i(id) j(year) (正確,ue在i(id)中不是常量)總之,有三種情況,reshape將拒絕數(shù)據(jù)轉(zhuǎn)換:1.寬形數(shù)據(jù),i(id)不唯一。 2.長形數(shù)據(jù),j(year)在i()中不唯一。 3.長形數(shù)據(jù),未指定的變量在i()中不是常量。例5:(Xij變量中缺少了變量ue)use reshape1,clearlistreshape long inc, i(id) j(year)list, sep(3)reshape wide(返回原始的寬形數(shù)據(jù))reshape long inc ue, i(id) j(year)list, sep(3)小結(jié):如果得到一個意想不到的結(jié)果,如何撤消它: 1

16、. 如果輸入reshape long語句,輸入reshape wide(不帶參數(shù))撤消它。 2. 如果輸入reshape wide語句,輸入reshape long(不帶參數(shù))撤消它。例6:(缺失的變量被視為變量有缺失值)use reshape1, cleardrop ue81listreshape long inc ue, i(id) j(year)list, sep(3)reshape wide inc ue, i(id) j(year)list, sep(3) (數(shù)據(jù)集發(fā)生了變化:建立ue81變量,包含全部缺失值)例7:use reshape3, clearlistreshape lon

17、g incr ue, i(id) j(year) (通配符代表數(shù)字。在寬形數(shù)據(jù)中,incr表示變量名inc#r ;在長形數(shù)據(jù)中,incr表示incr )list, sep(3)reshape wide incr ue, i(id) j(year)list, sep(3)例8:(選項string允許j()指定字符變量)use reshape4, clearlistreshape long inc, i(id) j(sex) string (sex是字符變量)list, sep(2)reshape wide inc, i(id) j(sex) string listreshape long inc

18、, i(id) j(sex) string (通配符代表字符。符號的位置取決于變量名)list, sep(2)例9:第二層嵌套數(shù)據(jù)可能包含兩個以上的j()變量,例如同時具有year變量和sex變量。數(shù)據(jù)的邏輯觀測值能用四種形式來表示:第一,長長形第二,長year,寬sex形第三,寬year,長sex形第四,寬寬形 From Toyear sex year sex Commandlong long long wide reshape wide inc, i(hid year) j(sex) stringlong wide long long reshape long inc, i(hid year) j(sex) stringlong long wide long reshape wide inc, i(hid sex) j(year)wide long long lon

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論