1 / 22

Stata 教學

Stata 教學. 第五講 兩個類別變數的關連分析. 列聯表 contingency tables. 我們可以用列聯表來展現兩個類別變數之間的關係。. 邊際分配 marginal distrib. . tabulate religion fethnicity if fethnicity < 4 您目前信甚麼教 | 您父親的籍貫是那裡? ? | 本省閩南 ? 本省客家 ? 大陸各省 ?| Total ---------------+---------------------------------+----------

daquan-neal
Download Presentation

Stata 教學

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stata教學 第五講 兩個類別變數的關連分析 社會統計

  2. 列聯表contingency tables • 我們可以用列聯表來展現兩個類別變數之間的關係。 邊際分配marginal distrib. . tabulate religion fethnicity if fethnicity < 4 您目前信甚麼教 | 您父親的籍貫是那裡? ? | 本省閩南? 本省客家? 大陸各省?| Total ---------------+---------------------------------+---------- 佛教 | 395 64 53 | 512 道教 | 228 26 13 | 267 民間信仰或其他 | 609 83 55 | 747 基督教天主教 | 31 7 38 | 76 無宗教信仰 | 175 42 87 | 304 ---------------+---------------------------------+---------- Total | 1,438 222 246 | 1,906 欄column 列row 列的邊際次數 社會統計

  3. 列聯表contingency tables • 從這個列聯表裡我們不容易一眼看出兩個變數之間的關係。 • 轉換成百分比。 . tabulate religion fethnicity if fethnicity < 4, column nofreq 您目前信甚麼教 | 您父親的籍貫是那裡? ? | 本省閩南 本省客家 大陸各省 | Total ---------------+---------------------------------+---------- 佛教 | 27.47 28.83 21.54 | 26.86 道教 | 15.86 11.71 5.28 | 14.01 民間信仰或其他 | 42.35 37.39 22.36 | 39.19 基督教天主教 | 2.16 3.15 15.45 | 3.99 無宗教信仰 | 12.17 18.92 35.37 | 15.95 ---------------+---------------------------------+---------- Total | 100.00 100.00 100.00 | 100.00 條件機率conditional probabilities 社會統計

  4. 聯合分配 . tabulate religion fethnicity if fethnicity < 4, cell nofreq 您目前信甚麼教 | 您父親的籍貫是那裡? ? | 本省閩南? 本省客家? 大陸各省?| Total ---------------+---------------------------------+---------- 佛教 | 20.72 3.36 2.78 | 26.86 道教 | 11.96 1.36 0.68 | 14.01 民間信仰或其他 | 31.95 4.35 2.89 | 39.19 基督教天主教 | 1.63 0.37 1.99 | 3.99 無宗教信仰 | 9.18 2.20 4.56 | 15.95 ---------------+---------------------------------+---------- Total | 75.45 11.65 12.91 | 100.00 =395/1906 社會統計

  5. 期望次數與觀察次數 • 就像所有的假設檢定一樣,我們分析如果虛無假設H0為真,會產生一個期望的值(比如說u=0)。 • 我們把這個期望值和實際觀察得到的值作比較。 • 期望和觀察的差異是否大到讓我們必須要拒斥虛無假設? 社會統計

  6. 變數間獨立性的卡方檢定 • 在兩變數獨立性檢定時,我們討論期望次數(expected frequencies)和觀察次數(observed frequencies)的差別。 • fo是實際觀察到次數 • fe是預期觀察到次數 社會統計

  7. fe預期次數的計算 • 我們首先要計算預期次數 社會統計

  8. fe預期次數的計算 • Exp.本省閩南信奉佛教這一個格子(cell)的預期次數是(512*1438)/1906=386.3,也就是說如果族群跟宗教信仰無關的話我們會在這個格子裡觀察到386.3個人 您目前信甚麼教 | 您父親的籍貫是那裡? ? | 本省閩南? 本省客家? 大陸各省?| Total ---------------+---------------------------------+---------- 佛教 | 395 64 53 | 512 道教 | 228 26 13 | 267 民間信仰或其他 | 609 83 55 | 747 基督教天主教 | 31 7 38 | 76 無宗教信仰 | 175 42 87 | 304 ---------------+---------------------------------+---------- Total | 1,438 222 246 | 1,906 社會統計

  9. fe預期次數Stata的計算 +--------------------+ | Key | |-------------------- | | frequency | | exp. frequency | +--------------------+ . tabulate religion fethnicity if fethnicity < 4, expected 您目前信甚麼教 | 您父親的籍貫是那裡? ? | 本省閩南? 本省客家? 大陸各省?| Total ---------------+---------------------------------+---------- 佛教 | 395 64 53 | 512 | 386.3 59.6 66.1 | 512.0 ---------------+---------------------------------+---------- 道教 | 228 26 13 | 267 | 201.4 31.1 34.5 | 267.0 ---------------+---------------------------------+---------- 民間信仰或其他 | 609 83 55 | 747 | 563.6 87.0 96.4 | 747.0 ---------------+---------------------------------+---------- 基督教天主教 | 31 7 38 | 76 | 57.3 8.9 9.8 | 76.0 ---------------+---------------------------------+---------- 無宗教信仰 | 175 42 87 | 304 | 229.4 35.4 39.2 | 304.0 ---------------+---------------------------------+---------- Total | 1,438 222 246 | 1,906 | 1,438.0 222.0 246.0 | 1,906.0 社會統計

  10. Chi-Squared Test Statistic • 變數間獨立性的卡方檢定是因為在大樣本時,檢定統計量呈現卡方分配而得名。 ‧x2越大,我們越有信心拒斥兩變數互相獨立的虛無假設。 社會統計

  11. . tabulate religion fethnicity if fethnicity < 4, chi2 expected 您目前信甚麼教 | 您父親的籍貫是那裡? ? | 本省閩南? 本省客家? 大陸各省?| Total ---------------+---------------------------------+---------- 佛教 | 395 64 53 | 512 | 386.3 59.6 66.1 | 512.0 ---------------+---------------------------------+---------- 道教 | 228 26 13 | 267 | 201.4 31.1 34.5 | 267.0 ---------------+---------------------------------+---------- 民間信仰或其他 | 609 83 55 | 747 | 563.6 87.0 96.4 | 747.0 ---------------+---------------------------------+---------- 基督教天主教 | 31 7 38 | 76 | 57.3 8.9 9.8 | 76.0 ---------------+---------------------------------+---------- 無宗教信仰 | 175 42 87 | 304 | 229.4 35.4 39.2 | 304.0 ---------------+---------------------------------+---------- Total | 1,438 222 246 | 1,906 | 1,438.0 222.0 246.0 | 1,906.0 Pearson chi2(8) = 208.2023 Pr = 0.000 夠不夠大到足以拒斥虛無假設?透過查表ν=8,0.01的顯著水準的臨界值是20.09,此處的chi2遠大於此數,所以可以放心的拒絕兩變數互相獨立的虛無假設 族群和宗教信仰有關 社會統計

  12. 卡方檢定與變數類別 • 上述族群和宗教信仰之間獨立性的卡方檢定受到我們界定類別的影響,比如說如果我們把宗教界定為只有「佛教」和「非佛教」這兩個類別,會得到下面的結果。 tabulate rel fethnicity if fethnicity <4, chi2 | 您父親的籍貫是那裡? rel | 本省閩南? 本省客家? 大陸各省?| Total -----------+---------------------------------+---------- 佛教 | 395 64 53 | 512 非佛教 | 1,043 158 193 | 1,394 -----------+---------------------------------+---------- Total | 1,438 222 246 | 1,906 Pearson chi2(2) = 4.2467 Pr = 0.120 ‧所以報告中要列出變數的類別。 ‧卡方檢定把變數當作是名目尺度的變數來處理。 社會統計

  13. 調整殘值與Stata • Stata 9標準運算不會報告調整殘值 • 我們要去網上下載相關的A-do檔案 • 相關說明請見http://ideas.repec.org/c/boc/bocode/s368901.html • 這個網站上還有很多其他有用的ado檔案。 • 如何在Stata上下載安裝這些由使用者自己寫的程式作為補充? • Host這些ado files的地方叫ssc (Statistical Software Components)是在Boston College Economic Department • 先確認電腦已經連上線 社會統計

  14. Stata與SSC • http://ideas.repec.org/s/boc/bocode.html • More pages of listings: 0|1|2|3|4|5到第四頁去 社會統計

  15. 還有很多其他好用的ado file 社會統計

  16. Stata與SSC • 要下載SSC上面的ado file,在指令欄裡鍵入ssc install tab_chi • 怎麼知道可以這樣做? • 去Statalist server找,Statalist是Stata使用者的線上討論區,在這裡可以post妳的問題,往往也可以得到解答,不過語氣要好一點。 社會統計

  17. 社會統計

  18. Tab_chi的使用 • 其協助檔案不叫做tab_chi,而是tabchi。 • Help tab_chi 社會統計

  19. tab_chi的操作 . tabchi religion fethnicity if fethnicity < 4, r a observed frequency expected frequency raw residual adjusted residual --------------------------------------------------- 您目前信甚麼教 | 您父親的籍貫是那裡? ? | 本省閩南人 本省客家人 大陸各省市 ---------------+----------------------------------- 佛教 | 395 64 53 | 386.283 59.635 66.082 | 8.717 4.365 -13.082 | 1.047 0.703 -2.016 | 道教 | 228 26 13 | 201.441 31.099 34.461 | 26.559 -5.099 -21.461 | 4.072 -1.049 -4.224 社會統計

  20. 續上頁 | 民間信仰或其他 | 609 83 55 | 563.581 87.006 96.412 | 45.419 -4.006 -41.412 | 4.951 -0.586 -5.796 | 基督教天主教 | 31 7 38 | 57.339 8.852 9.809 | -26.339 -1.852 28.191 | -7.164 -0.676 9.843 | 無宗教信仰 | 175 42 87 | 229.356 35.408 39.236 | -54.356 6.592 47.764 | -7.901 1.286 8.912 --------------------------------------------------- Pearson chi2(8) = 208.2023 Pr = 0.000 likelihood-ratio chi2(8) = 169.6775 Pr = 0.000 社會統計

  21. 小樣本Fisher’s exact test 10個格子的預期值小於5 . tab religion fethnicity, chi2 expected 您目前信甚麼教 | 您父親的籍貫是那裡? ? | 本省閩南? 本省客家? 大陸各省? 原住民 其他 | Total ---------------+-------------------------------------------------------+---------- 佛教 | 395 64 53 3 2 | 517 | 387.0 59.7 66.2 3.2 0.8 | 517.0 ---------------+-------------------------------------------------------+---------- 道教 | 228 26 13 0 0 | 267 | 199.9 30.9 34.2 1.7 0.4 | 267.0 ---------------+-------------------------------------------------------+---------- 民間信仰或其他 | 609 83 55 0 0 | 747 | 559.2 86.3 95.7 4.7 1.2 | 747.0 ---------------+-------------------------------------------------------+---------- 基督教天主教 | 31 7 38 9 0 | 85 | 63.6 9.8 10.9 0.5 0.1 | 85.0 ---------------+-------------------------------------------------------+---------- 無宗教信仰 | 175 42 87 0 1 | 305 | 228.3 35.2 39.1 1.9 0.5 | 305.0 ---------------+-------------------------------------------------------+---------- Total | 1,438 222 246 12 3 | 1,921 | 1,438.0 222.0 246.0 12.0 3.0 | 1,921.0 Pearson chi2(16) = 347.8780 Pr = 0.000 不準確 社會統計

  22. . tabulate fethnicity religion, chi2 exact expected • 因為計算複雜,而記憶體default只有10m,出現下列訊息: • exceeded memory limits using exact(1); try again with larger #; see help tabulate for details • 加大memory • 先clear清掉所有在記憶體中的資料。 • Set memory 128m • 把資料叫回來 • use “C:\Documents and Settings\Ming-chi Chen\桌面\85q1_temp.dta”, clear • 不過既使是加大了memory,也提高了exact程序可運用的記憶體倍數,歷經了大約兩個小時還是沒有結果,於是直接關閉Stata。 社會統計

More Related