본문 바로가기
STATA

03. [STATA] 변수 확인하기

by Aubreyy 2021. 8. 1.

예시 데이터로는 STATA에 내장되어 있는 auto 데이터를 사용하였습니다.

 

데이터 불러오기

sysuse auto

 

Auto 데이터를 불러오면 아래와 같은 결과문을 확인할 수 있습니다.

. sysuse auto
(1978 automobile data)

 

변수 제대로 불러왔는지 확인

그리고 오른쪽 위에는 다음의 변수들이 불려온 것을 확인할 수 있습니다. 사용해야 하는 변수들이 전부 있는지 확인을 합니다. (오른쪽 위에 아무것도 안보인다면 Window-Variables 클릭하면 볼 수 있습니다.)

 

변수 타입 확인

변수가 적절한 타입으로 import 되었는지도 확인해야 합니다. 예를 들어 평균을 내야 하는데 숫자형식이 아니면 계산을 할 수 없겠죠?

describe
desc

 

. desc

 Observations:            74                  1978 automobile data
    Variables:            12                  13 Apr 2020 17:45
                                              (_dta has notes)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Variable      Storage   Display    Value
    name         type    format    label      Variable label
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
make            str18   %-18s                 Make and model
price           int     %8.0gc                Price
mpg             int     %8.0g                 Mileage (mpg)
rep78           int     %8.0g                 Repair record 1978
headroom        float   %6.1f                 Headroom (in.)
trunk           int     %8.0g                 Trunk space (cu. ft.)
weight          int     %8.0gc                Weight (lbs.)
length          int     %8.0g                 Length (in.)
turn            int     %8.0g                 Turn circle (ft.)
displacement    int     %8.0g                 Displacement (cu. in.)
gear_ratio      float   %6.2f                 Gear ratio
foreign         byte    %8.0g      origin     Car origin
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sorted by: foreign

 

데이터 직접 확인

browse
br
edit
ed

 

browse, edit 둘 다 데이터를 직접 확인할 수 있습니다. 단, 차이점은 edit은 데이터를 수정할 수 있다는 것입니다.

 

변수 값 탐색

codebook

 

codebook으로 확인할 수 있는 것은 다음과 같습니다.

데이터 형식, Missing value 갯수, 변수별 category 갯수, label

변수 중 몇 가지만 꼽아서 코드북을 보며 해석해보겠습니다.

. codebook

-----------------------------------------------------------------------------------------------------------------------------------
make                                                          Make and model
-----------------------------------------------------------------------------------------------------------------------------------

                  Type: String (str18), but longest is str17

         Unique values: 74                        Missing "": 0/74

              Examples: "Cad. Deville"
                        "Dodge Magnum"
                        "Merc. XR-7"
                        "Pont. Catalina"

               Warning: Variable has embedded blanks.

make 변수를 보면 차종이 총 74개가 있고, 그 예시로는 Cad.Deville, Dodge Magnum, Merc, XR-7, Pont. Catalina가 있습니다. missing value는 없네요.


-----------------------------------------------------------------------------------------------------------------------------------
price                                                             Price
-----------------------------------------------------------------------------------------------------------------------------------

                  Type: Numeric (int)

                 Range: [3291,15906]                  Units: 1
         Unique values: 74                        Missing .: 0/74

                  Mean: 6165.26
             Std. dev.:  2949.5

           Percentiles:     10%       25%       50%       75%       90%
                           3895      4195    5006.5      6342     11385

가격대를 보면 3291에서 15906사이입니다. 평균 가격은 6165 정도네요. 


-----------------------------------------------------------------------------------------------------------------------------------
rep78                                                         Repair record 1978
-----------------------------------------------------------------------------------------------------------------------------------

                  Type: Numeric (int)

                 Range: [1,5]                         Units: 1
         Unique values: 5                         Missing .: 5/74

            Tabulation: Freq.  Value
                            2  1
                            8  2
                           30  3
                           18  4
                           11  5
                            5  .

repair record는 총 다섯번까지 있네요. 5개 데이터가 missing입니다. 1번고친 차는 2개, 2번 고친 차는 8개입니다.


-----------------------------------------------------------------------------------------------------------------------------------
foreign                                                       Car origin
-----------------------------------------------------------------------------------------------------------------------------------

                  Type: Numeric (byte)
                 Label: origin

                 Range: [0,1]                         Units: 1
         Unique values: 2                         Missing .: 0/74

            Tabulation: Freq.   Numeric  Label
                           52         0  Domestic
                           22         1  Foreign

Domestic 차량은 총 52개, Foreign차량은 22개입니다.

 

데이터 요약

평균, SD, Min, Max 등을 한눈에 파악할 수 있습니다. 코드북으로도 확인할 수 있지만 다음의 코드를 활용하면 변수끼리 비교할 수 있습니다.

summarize
sum


    Variable |        Obs        Mean    Std. dev.       Min        Max
-------------+---------------------------------------------------------
        make |          0
       price |         74    6165.257    2949.496       3291      15906
         mpg |         74     21.2973    5.785503         12         41
       rep78 |         69    3.405797    .9899323          1          5
    headroom |         74    2.993243    .8459948        1.5          5
-------------+---------------------------------------------------------
       trunk |         74    13.75676    4.277404          5         23
      weight |         74    3019.459    777.1936       1760       4840
      length |         74    187.9324    22.26634        142        233
        turn |         74    39.64865    4.399354         31         51
displacement |         74    197.2973    91.83722         79        425
-------------+---------------------------------------------------------
  gear_ratio |         74    3.014865    .4562871       2.19       3.89
     foreign |         74    .2972973    .4601885          0          1

 

Frequency 확인하기

codebook을 통해서도 확인할 수 있지만 다음의 코드로도 확인할 수 있습니다.

tab foreign
tab foreign, nola

 

. tab foreign

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

. tab foreign, nola

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

 

 

'STATA' 카테고리의 다른 글

05. [STATA] 두 개 테이블 합치기  (0) 2021.08.03
04. [STATA] 변수 생성, 수정, missing value, recoding  (0) 2021.08.02
01. [STATA] 로그, 코드 관리  (0) 2021.07.31
02. [STATA] 데이터 불러오기  (0) 2021.07.31
00. STATA 소개  (0) 2021.07.28

댓글