2013-11-22 30 views
0

一特定值增加觀察,看看我的數據集的一些變量:與可變

firm_id year dyrstr Lack total_workers 
2432 2002 1980  29 
2432 2003 1980  23 
2432 2005 1980 1 283 
2432 2006 1980  56 
2432 2007 1980  21 
2433 2004 2001  42 
2433 2006 2001 1 29 
2433 2008 2001 1 100 
2434 2002 2002  21 
2434 2003 2002  55 
2434 2004 2002  22 
2434 2005 2002  24 
2434 2006 2002  17 
2434 2007 2002  40 
2434 2008 2002  110 
2434 2009 2002  158 
2434 2010 2002  38 
2435 2002 2002  80 
2435 2003 2002  86 
2435 2004 2002  877 
2435 2005 2002  254 
2435 2006 2002  71 
2435 2007 2002  116 
2435 2008 2002  118 
2435 2009 2002  1165 
2435 2010 2002  67 
2436 2002 1992  24 
2436 2003 1992  25 
2436 2004 1992  22 
2436 2005 1992  23 
2436 2006 1992  21 
2436 2007 1992  100 
2436 2008 1992  73 
2436 2009 1992  23 
2436 2010 1992  40 
2437 2002 2002  30 
2437 2003 2002  31 
2437 2004 2002  21 
2437 2006 2002 1 56 
2437 2007 2002  20 

的變量:

  1. firm_id是公司
  2. 的標識符
  3. 是觀察年份
  4. dyrstr是一個堅定的
  5. 缺乏等於1的創始年,如果在今年缺少觀測之前(例如數據集中的三線,缺乏等於1,因爲ID爲2432的公司,還有在2004年)
  6. total_workers沒有觀察工人的數量

我想填在空間中,即我想創建新的觀察,我告訴你下面的(僅考慮公司與ID 2432):

firm_id year dyrstr Lack total_workers 
2432 2002 1980  29 
*2432* *2004* *1980*  *156* 
2432 2003 1980  23 
2432 2005 1980 1 283 
2432 2006 1980  56 
2432 2007 1980  21 

,我已經把變量的值中的行星號是新創建的觀察。這個觀察結果應該是前面觀察的一個副本,但需要進行一些修改。

  • firm_id應保持一樣前行
  • 應該是從上線一年加一個
  • dyrstr應該
  • 之前保持不變,如行
  • 缺少:這裏沒關係這個變量有哪個值
  • 個total_workers等於0.5 *
  • 所有其他變量
之前我的數據集(我沒有在這裏列出)的應該保持相同的線(連續觀察先前的觀測值+值的值)

我讀了關於命令expand的一些信息,但help expand對我沒有什麼幫助。希望你們中的一個能幫助我!

+0

應該年份的範圍相同(即理想情況下,你會觀察所有公司10年),還是公司的年度變化? – SOConnell

+0

儘管Stata上的Stata用戶似乎很樂意幫助解決這類問題,但我也注意到,按照一般Stack Overflow標準,這是**不是一個好問題,因爲它不顯示任何嘗試提供給您的提示碼。 –

+0

對不起! –

回答

5

我的建議取決於使用expand,這反過來只需要關於要添加的觀測數量的信息。我忽略了你的變量Lack,因爲Stata本身可以計算出差距。我輸入total_workers的步驟基於使用內置命令ipolate,因此可用於超過1年的間隔,這在您的示例中未顯示。如此估計的工人人數不一定是一個整數。

對於其他插值程序,請查看cipolate,csipolate,pchipolate,所有可通過ssc desc cipolate(或等效)訪問。

這種操作取決於獲取sort的順序是否正確,我認爲即使有經驗也不是微不足道的,所以在爲類似問題獲得代碼時,要爲錯誤的開始做好準備;用list陳述胡言亂語;並在一個好的玩具示例數據集上工作(如您在此提供的那樣)。

. clear 

. input firm_id year dyrstr total_workers 

     firm_id  year  dyrstr total_w~s 
    1.  2432  2002 1980 29 
    2.  2432  2003 1980 23 
    3.  2432  2005 1980 283 
    4.  2432  2006 1980 56 
    5.  2432  2007 1980 21 
    6.  2433  2004 2001 42 
    7.  2433  2006 2001 29 
    8.  2433  2008 2001 100 
    9.  2434  2002 2002 21 
10.  2434  2003 2002 55 
11.  2434  2004 2002 22 
12.  2434  2005 2002 24 
13.  2434  2006 2002 17 
14.  2434  2007 2002 40 
15.  2434  2008 2002 110 
16.  2434  2009 2002 158 
17.  2434  2010 2002 38 
18.  2435  2002 2002 80 
19.  2435  2003 2002 86 
20.  2435  2004 2002 877 
21.  2435  2005 2002 254 
22.  2435  2006 2002 71 
23.  2435  2007 2002 116 
24.  2435  2008 2002 118 
25.  2435  2009 2002 1165 
26.  2435  2010 2002 67 
27.  2436  2002 1992 24 
28.  2436  2003 1992 25 
29.  2436  2004 1992 22 
30.  2436  2005 1992 23 
31.  2436  2006 1992 21 
32.  2436  2007 1992 100 
33.  2436  2008 1992 73 
34.  2436  2009 1992 23 
35.  2436  2010 1992 40 
36.  2437  2002 2002 30 
37.  2437  2003 2002 31 
38.  2437  2004 2002 21 
39.  2437  2006 2002 56 
40.  2437  2007 2002 20 
41. end 

. scalar N = _N 

. bysort firm_id (year) : gen gap = year - year[_n-1] 
(6 missing values generated) 

. expand gap 
(6 missing counts ignored; observations not deleted) 
(4 observations created) 

. gen orig = _n <= scalar(N) 

. bysort firm_id (year) : replace total_workers = . if !orig 
(4 real changes made, 4 to missing) 

. bysort firm_id (year orig) : replace year = year[_n-1] + 1 if _n > 1 & year != year[_n-1] + 1 
(4 real changes made) 

. bysort firm_id (year): ipolate total_workers year , gen(total_workers2) 

. list, sepby(firm_id) 

     +------------------------------------------------------------+ 
     | firm_id year dyrstr total_~s gap orig total_~2 | 
     |------------------------------------------------------------| 
    1. | 2432 2002  1980   29  .  1   29 | 
    2. | 2432 2003  1980   23  1  1   23 | 
    3. | 2432 2004  1980   .  2  0  153 | 
    4. | 2432 2005  1980  283  2  1  283 | 
    5. | 2432 2006  1980   56  1  1   56 | 
    6. | 2432 2007  1980   21  1  1   21 | 
     |------------------------------------------------------------| 
    7. | 2433 2004  2001   42  .  1   42 | 
    8. | 2433 2005  2001   .  2  0  35.5 | 
    9. | 2433 2006  2001   29  2  1   29 | 
    10. | 2433 2007  2001   .  2  0  64.5 | 
    11. | 2433 2008  2001  100  2  1  100 | 
     |------------------------------------------------------------| 
    12. | 2434 2002  2002   21  .  1   21 | 
    13. | 2434 2003  2002   55  1  1   55 | 
    14. | 2434 2004  2002   22  1  1   22 | 
    15. | 2434 2005  2002   24  1  1   24 | 
    16. | 2434 2006  2002   17  1  1   17 | 
    17. | 2434 2007  2002   40  1  1   40 | 
    18. | 2434 2008  2002  110  1  1  110 | 
    19. | 2434 2009  2002  158  1  1  158 | 
    20. | 2434 2010  2002   38  1  1   38 | 
     |------------------------------------------------------------| 
    21. | 2435 2002  2002   80  .  1   80 | 
    22. | 2435 2003  2002   86  1  1   86 | 
    23. | 2435 2004  2002  877  1  1  877 | 
    24. | 2435 2005  2002  254  1  1  254 | 
    25. | 2435 2006  2002   71  1  1   71 | 
    26. | 2435 2007  2002  116  1  1  116 | 
    27. | 2435 2008  2002  118  1  1  118 | 
    28. | 2435 2009  2002  1165  1  1  1165 | 
    29. | 2435 2010  2002   67  1  1   67 | 
     |------------------------------------------------------------| 
    30. | 2436 2002  1992   24  .  1   24 | 
    31. | 2436 2003  1992   25  1  1   25 | 
    32. | 2436 2004  1992   22  1  1   22 | 
    33. | 2436 2005  1992   23  1  1   23 | 
    34. | 2436 2006  1992   21  1  1   21 | 
    35. | 2436 2007  1992  100  1  1  100 | 
    36. | 2436 2008  1992   73  1  1   73 | 
    37. | 2436 2009  1992   23  1  1   23 | 
    38. | 2436 2010  1992   40  1  1   40 | 
     |------------------------------------------------------------| 
    39. | 2437 2002  2002   30  .  1   30 | 
    40. | 2437 2003  2002   31  1  1   31 | 
    41. | 2437 2004  2002   21  1  1   21 | 
    42. | 2437 2005  2002   .  2  0  38.5 | 
    43. | 2437 2006  2002   56  2  1   56 | 
    44. | 2437 2007  2002   20  1  1   20 | 
     +------------------------------------------------------------+ 
+0

非常感謝您的回答。這很棒! –

1

如果像在您的示例數據庫中那樣,對於任何給定公司沒有連續幾年缺失,以下方法可行。我還假設變量Lack是數字,最終結果是一個不平衡的面板(在您的問題中,您沒有具體說明這一點)。

* Expand database 
expand 2 if Lack == 1, gen(x) 
gsort firm_id year -x 

* Substitution rules 
replace year = year - 1 if x == 1 
replace total_workers = (total_workers[_n-1] + total_workers[_n+1])/2 if x == 1 

list, sepby(firm_id) 

expand線可以重新寫爲expand Lack + 1, gen(x),但也許更直觀的方式。

對於在其中連續有多年缺少更普遍的情況,下面應該讓你開始假設Lack規定的連續三年缺數下。例如,如果某家公司從2006年到2009年有一個跳躍,那麼2009年的觀察值爲Lack = 2

* Expand database 
expand Lack + 1, gen(x) 
gsort firm_id year -x 

* Substitution rules 
replace year = year[_n-1] + 1 if x == 1 

現在你只需要拿出你的total_workers插補規則:

replace total_workers = ... 

如果Lack是一個字符串,轉換爲使用real爲數字。

+0

謝謝你的回答。不幸的是,一些公司連續幾年缺失。 –

+0

這很好。答案的第二部分涵蓋連續失蹤的幾年。 –

1

你已經獲得了答案,但我不得不做之前相似,始終使用cross命令,如下所示。說我用你的數據集已經&繼續下面的代碼:

tempfile master year 
save `master' 
preserve 
keep year 
duplicates drop 
save `year' 

restore 
//next two lines set me up to correct for different year ranges by firm; if year ranges were standard, this would be omitted 
bys firm_id: egen minyear=min(year) 
bys firm_id: egen maxyear=max(year) 
keep firm_id minyear maxyear 
duplicates drop 
cross using `year' 
merge m:1 firm_id year using `master', assert(1 3) nogen 
drop if year<minyear | year>maxyear //this adjusts for years outside the earliest and latest years observed by firm; if year ranges standard, again omitted 

然後從這裏開始,在@NickCox的精神用ipolate命令。

我對使用expandcross的任何優點/缺點特別感興趣。 (除了我在這裏使用的具體情況具體取決於每年觀察到的> 0記錄以構建交叉數據集,如果我創建的tempfile文件不同,可以省略此記錄。)

+0

'expand'和'cross'的優點和缺點:我的答案的早期版本使用'fillin',它與'cross'有關,但是我必須做你做的事情,刪除在開頭添加的觀察結果,不屬於的時期結束。它部分歸結爲風格偏好。 –

+0

@ NickCox - 很高興知道 - 謝謝! – SOConnell