본문 바로가기
STATA

07. [STATA] 기초 통계 chi-squared, t-test

by Aubreyy 2021. 8. 6.

예시를 위해 auto 데이터를 사용하였다.

sysuse auto

 

그리고, 새로운 categorical 변수를 생성하였다. (04번 글 참고)

gen mpg2 = 1 if mpg >= 20
replace mpg2=0 if mpg2 == .
br

tab mpg2

label define mpg2 0 mpg_under_20 1 mpg_over_20
label value mpg2 mpg2

 

. tab mpg2

        mpg2 |      Freq.     Percent        Cum.
-------------+-----------------------------------
mpg_under_20 |         35       47.30       47.30
 mpg_over_20 |         39       52.70      100.00
-------------+-----------------------------------
       Total |         74      100.00

mpg < 20인것은 53개 행, mpg >= 20인 것은 39개 행이다.

 

그리고 오늘 사용할 변수인 foreign의 frequency를 확인해보았다.

tab foreign

. tab foreign

 Car origin |      Freq.     Percent        Cum.
------------+-----------------------------------
   Domestic |         52       70.27       70.27
    Foreign |         22       29.73      100.00
------------+-----------------------------------
      Total |         74      100.00

Domestic은 52개 행으로 70.27%를 차지하고, Foreign은 29.73%를 차지하고 있다.

 

Categorical Variables

Cross-tabulations

Row wise percentage

해당 code는 row 방향 %를 보여준다.

tab foreign mpg2, row

. tab foreign mpg2, row

+----------------+
| Key            |
|----------------|
|   frequency    |
| row percentage |
+----------------+

           |         mpg2
Car origin | mpg_under  mpg_over_ |     Total
-----------+----------------------+----------
  Domestic |        30         22 |        52 
           |     57.69      42.31 |    100.00 
-----------+----------------------+----------
   Foreign |         5         17 |        22 
           |     22.73      77.27 |    100.00 
-----------+----------------------+----------
     Total |        35         39 |        74 
           |     47.30      52.70 |    100.00 

해석: Domestic 중에 mpg<20인 것은 57.69%, mpg>=20 인 것은 42.31%이다. Foreign 중에서 mpg<20인 것은 22.73%, 그리고 mpg>=20인 것은 77.27%였다.

Column wise percentage

tab foreign mpg2, col

. tab foreign mpg2, col

+-------------------+
| Key               |
|-------------------|
|     frequency     |
| column percentage |
+-------------------+

           |         mpg2
Car origin | mpg_under  mpg_over_ |     Total
-----------+----------------------+----------
  Domestic |        30         22 |        52 
           |     85.71      56.41 |     70.27 
-----------+----------------------+----------
   Foreign |         5         17 |        22 
           |     14.29      43.59 |     29.73 
-----------+----------------------+----------
     Total |        35         39 |        74 
           |    100.00     100.00 |    100.00 

mpg < 20 중에 domestic은 85.71%이고 foreign은 14.29% 였다. mpg >=20 중에 domestic은 56.41%, foreign은 43.59%였다.

Pearson chi-squared test

tab foreign mpg2, chi2

. tab foreign mpg2, chi2

           |         mpg2
Car origin | mpg_under  mpg_over_ |     Total
-----------+----------------------+----------
  Domestic |        30         22 |        52 
   Foreign |         5         17 |        22 
-----------+----------------------+----------
     Total |        35         39 |        74 

          Pearson chi2(1) =   7.5822   Pr = 0.006

가정은 categorical variable 사이에 차이가 없다이다.

그런데 해당 결과를 보면 P-value < 0.05로 null hypothesis를 기각했으므로 차이가 있다라고 해석하면 된다.

 

Continuouse Variables

Independent sample t-test

여기서는 Domestic과 Foreign 차 사이의 유의미한 가격차이가 있는지 확인할 것이다.

ttest price, by (foreign)

 

. ttest price, by (foreign)

Two-sample t test with equal variances
------------------------------------------------------------------------------
   Group |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
---------+--------------------------------------------------------------------
Domestic |      52    6072.423    429.4911    3097.104    5210.184    6934.662
 Foreign |      22    6384.682    558.9942    2621.915     5222.19    7547.174
---------+--------------------------------------------------------------------
Combined |      74    6165.257    342.8719    2949.496    5481.914      6848.6
---------+--------------------------------------------------------------------
    diff |           -312.2587    754.4488               -1816.225    1191.708
------------------------------------------------------------------------------
    diff = mean(Domestic) - mean(Foreign)                         t =  -0.4139
H0: diff = 0                                     Degrees of freedom =       72

    Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
 Pr(T < t) = 0.3401         Pr(|T| > |t|) = 0.6802          Pr(T > t) = 0.6599

Domestic과 Foreign 사이에 차이가 없다는 것을 확인할 수 있다.

(STATA는 자동으로 variance가 equal, unequal인지 확인하고 계산해 주는 것 같다.)

 

Paired sample t-test

sysuse bpwide
ttest bp_before=bp_after

. ttest bp_before=bp_after

Paired t test
------------------------------------------------------------------------------
Variable |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
---------+--------------------------------------------------------------------
bp_bef~e |     120      156.45    1.039746    11.38985    154.3912    158.5088
bp_after |     120    151.3583    1.294234    14.17762    148.7956     153.921
---------+--------------------------------------------------------------------
    diff |     120    5.091667    1.525736     16.7136    2.070557    8.112776
------------------------------------------------------------------------------
     mean(diff) = mean(bp_before - bp_after)                      t =   3.3372
 H0: mean(diff) = 0                              Degrees of freedom =      119

 Ha: mean(diff) < 0           Ha: mean(diff) != 0           Ha: mean(diff) > 0
 Pr(T < t) = 0.9994         Pr(|T| > |t|) = 0.0011          Pr(T > t) = 0.0006

결과를 확인하면 bp 전과 후에 유의미한 차이가 있다. 후에 bp가 낮아진 것을 확인할 수 있다.

댓글