stata 教學
DESCRIPTION
Stata 教學. 第五講 兩個類別變數的關連分析. 列聯表 contingency tables. 我們可以用列聯表來展現兩個類別變數之間的關係。. 邊際分配 marginal distrib. . tabulate religion fethnicity if fethnicity < 4 您目前信甚麼教 | 您父親的籍貫是那裡? ? | 本省閩南 ? 本省客家 ? 大陸各省 ?| Total ---------------+---------------------------------+---------- - PowerPoint PPT PresentationTRANSCRIPT
©Ming-chi Chen 社會統計 Page.1
Stata 教學第五講
兩個類別變數的關連分析
©Ming-chi Chen 社會統計 Page.2
列聯表 contingency tables• 我們可以用列聯表來展現兩個類別變數之間的關係。. 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
邊際分配 marginal distrib.
列的邊際次數
欄column 列 row
©Ming-chi Chen 社會統計 Page.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條件機率 conditiona
l probabilities
©Ming-chi Chen 社會統計 Page.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
©Ming-chi Chen 社會統計 Page.5
期望次數與觀察次數• 就像所有的假設檢定一樣,我們分析如果虛無假設 H0 為真,會產生一個期望的值(比如說 u = 0 )。• 我們把這個期望值和實際觀察得到的值作比較。• 期望和觀察的差異是否大到讓我們必須要拒斥虛無假設?
©Ming-chi Chen 社會統計 Page.6
變數間獨立性的卡方檢定• 在兩變數獨立性檢定時,我們討論期望次數( expected frequencies )和觀察次數( obs
erved frequencies )的差別。• fo是實際觀察到次數• fe是預期觀察到次數
©Ming-chi Chen 社會統計 Page.7
fe 預期次數的計算• 我們首先要計算預期次數
總樣本數
列邊際次數欄邊際次數ef
©Ming-chi Chen 社會統計 Page.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
©Ming-chi Chen 社會統計 Page.9
fe 預期次數 Stata 的計算. 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
+--------------------+| Key ||-------------------- || frequency || exp. frequency |+--------------------+
©Ming-chi Chen 社會統計 Page.10
Chi-Squared Test Statistic• 變數間獨立性的卡方檢定是因為在大樣本時,檢定統計量呈現卡方分配而得名。
e
e
fff 2
02
‧x2 越大,我們越有信心拒斥兩變數互相獨立的虛無假設。
©Ming-chi Chen 社會統計 Page.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 遠大於此數,所以可以放心的拒絕兩變數互相獨立的虛無假設
族群和宗教信仰有關
©Ming-chi Chen 社會統計 Page.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‧所以報告中要列出變數的類別。‧卡方檢定把變數當作是名目尺度的變數來處理。
©Ming-chi Chen 社會統計 Page.13
調整殘值與 Stata• Stata 9標準運算不會報告調整殘值• 我們要去網上下載相關的 A-do檔案• 相關說明請見
http://ideas.repec.org/c/boc/bocode/s368901.html• 這個網站上還有很多其他有用的 ado檔案。• 如何在 Stata 上下載安裝這些由使用者自己寫的程式作為補充?• Host 這些 ado files 的地方叫 ssc (Statistical Softwar
e Components) 是在 Boston College Economic Department
• 先確認電腦已經連上線
©Ming-chi Chen 社會統計 Page.14
Stata 與 SSC• http://ideas.repec.org/s/boc/bocode.html• More pages of listings: 0|1|2|3|4|5到第四頁去
©Ming-chi Chen 社會統計 Page.15還有很多其他好用的 ado file
©Ming-chi Chen 社會統計 Page.16
Stata 與 SSC• 要下載 SSC 上面的 ado file ,在指令欄裡鍵入 ssc install tab_chi• 怎麼知道可以這樣做?• 去 Statalist server找, Statalist 是 Stata使用者的線上討論區,在這裡可以 post妳的問題,往往也可以得到解答,不過語氣要好一點。
©Ming-chi Chen 社會統計 Page.17
©Ming-chi Chen 社會統計 Page.18
Tab_chi 的使用• 其協助檔案不叫做 tab_chi ,而是 tabchi 。• Help tab_chi
©Ming-chi Chen 社會統計 Page.19
)190614381)(
19065121(283.386
283.386395
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
©Ming-chi Chen 社會統計 Page.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
©Ming-chi Chen 社會統計 Page.21
小樣本 Fisher’s exact test. 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
10 個格子的預期值小於 5
不準確
©Ming-chi Chen 社會統計 Page.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 。