[おまけ] 幾つかのTips、Q-Qプロット

統計モデル解析特論I/II : 第15回 (02/01/22)

 これまで資料に含めていたにも関わらず、 時間配分をミスって説明していなかったTipsについて、今回取り上げる。 加えてQ-Qプロットの原理についても説明する。
  1. 前回のアンケート: 8名

  2. [Tips 1] データをファイルから読み込む際に便利なコマンド: デリミタの指定等
     Excel 等で入力したデータを SAS に読み込ませる方法として、 csv 形式で保存してからSASで読み込む方法を以前紹介した。 これ以外にタブ区切り(*.txt)の形式のファイルや 固定長と呼ばれるファイルも読み込むことができ、 これらを読み込む際の、幾つかの便利なコマンドをまとめて紹介しておく。

    1. カンマ区切り(csv 形式)のファイルを読む場合 : *.csv (既に第10回で紹介済み)
        CSV 形式のファイルを読み込む場合、文字列の長さを指定しないと、 8文字(8バイト)しか読み込んでくれない。しかし、かと言って、input 文に 単に文字数を指定すると、カンマを超えて読み込もうとする。 また、欠損値が続くとそれ等を一つの欠損値として読み込んでしまう。 それらの欠点を一挙に解決するには以下の様式のプログラムを用いる。 続く欠損値を個々にバラして読ませ(infile 中の dsd)、 デリミタが出現するところまでの任意の長さの文字列を読み込む(input 中の : )ように指定する。
      
      data example2021;
        infile 'StatM21/foo1.csv'
          firstobs=2 
          dlm=',' dsd
          missover truncover
          encoding=sjis termstr=crlf 
      ;
        input No $ Univ : $30. SName : $40. Faculty : $50. Dept : $50.
              Center1 : $8. Center2 : $8. Sel1 : $8. Sel2 : $8.
              Book1 : $10. Book2 : $10.
              Vol0  VolS  VolT
              ZenKou $ ScoreS  ScoreT  KoKouSi
      ;
      

    2. タブ区切りのファイルを読む場合 : *.txt, タブコードは十六進数で"09"
      
      data example2021;
        infile 'StatM21/foo2.txt'
          firstobs=2
          dlm='09'x
          missover truncover
          encoding=sjis termstr=crlf
      ;
      

    3. 1レコード(1行)のレコード長が長い場合 : 一行の長さの指定、例えば 230バイトだと(Logical Record Length)
      
      data math;
        infile 'StatM21/foo3.csv'
          firstobs=2
          dlm=',' dsd
          lrecl=230
      ;
      

    4. 行末がそろってないデータの読み込み : 一行の長さを指定し、揃ってないことを明示(Truncate Overか)
      
      data math;
        infile 'StatM21/foo4.csv'
          firstobs=2
          dlm=',' dsd
          lrecl=230 truncover
      ;
      

    5. 固定長データの読み込み : カラム位置を指定して読み込む
      
      input
            UketsukeID     1-  6
            JyukenID       7- 11
            BirthDay      13- 20
            Area       $  32- 41
            s_scor01     103-104
            s_scor02     105-106
            s_scor03     107-108
      ;
      

  3. [Tips 2] 頻度集計の便利なオプション

    1. プログラム : Lesson 13-2 : les1302.sas
      
       /* Lesson 13-02 */
       /*    File Name = les1302.sas  01/18/22   */
      
      options nocenter linesize=78 pagesize=30;
      options locale='en_US';
      /* options locale='ja_JP'; */
      proc printto print = 'StatM21/les1302-Results.txt' new;
      
      data gakusei;
        infile 'StatM21/StudAll21d.csv'
          firstobs=9 dlm=',' dsd missover 
          encoding=sjis termstr=crlf;
        input sex $ shintyou taijyuu kyoui 
              jitaku : $10. kodukai carryer $ tsuuwa;
      
      /* if shintyou="." or taijyuu="." or kyoui="." then delete; */
      if carryer="DoCoMo"   then carryer="docomo";                     : 名称の不揃いを統一する
      if carryer="DoCoMo+w" then carryer="docomo+W";
      if carryer="Vodafone" then carryer="Softbank";
      if carryer="vodafone" then carryer="Softbank";
      if carryer="softbank" then carryer="Softbank";
      if carryer="au+willc" then carryer="au+YMobile";
      if carryer="Willcom"  then carryer="YMobile";
      if carryer="DDIp"     then carryer="YMobile";
      
      proc print data=gakusei(obs=5);
      run;
      
      title '*** 通常の頻度集計、クロス集計(アルファベット順になる) ***';
      proc freq data=gakusei;                          : オプションなし=アルファベット順
        tables sex jitaku carryer;
      run;
      proc freq data=gakusei;                          : オプションなし=アルファベット順
        tables sex*jitaku;
        tables sex*carryer;
        tables jitaku*carryer;
      run;
      
      title '*** 頻度の大きい順に表示 ***';
      proc freq data=gakusei order=freq;               : 頻度順に
        tables sex jitaku carryer;
      run;
      proc freq data=gakusei order=freq;               : 頻度順に
        tables sex*jitaku;
        tables sex*carryer;
        tables jitaku*carryer;
      run;
      
      title '*** 頻度の大きい順に表示(頻度のみ) ***';
      proc freq data=gakusei order=freq;                    : 頻度順に
        tables sex jitaku carryer / nopercent norow nocol;  : 頻度のみ
      run;
      proc freq data=gakusei order=freq;                    : 頻度順に
        tables sex*jitaku     / nopercent norow nocol;      : 頻度のみ
        tables sex*carryer    / nopercent norow nocol;      : 頻度のみ
        tables jitaku*carryer / nopercent norow nocol;      : 頻度のみ
      run;

    2. 出力
      • [結果タグ] html 形式: les1302-Results.html
      • [結果タグ] pdf 形式: les1302-Output.pdf
      • テキストファイル: les1302-Results.txt

      • 自宅生/下宿生の人数、自宅生/下宿生の人数。これらの組合わせの人数。
      • 頻度、割合(%)、累積頻度、累積割合(%)
      • 頻度、全体割合、縦割合、横割合
      
      *** 通常の頻度集計、クロス集計(アルファベット順にな  58
                                              Tuesday, February  1, 2022 10:46:37 AM
      The FREQ Procedure
                                      Cumulative    Cumulative
      sex    Frequency     Percent     Frequency      Percent
      --------------------------------------------------------
      F           141       29.81           141        29.81  
      M           332       70.19           473       100.00  
      
                      Frequency Missing = 125
      
                                            Cumulative    Cumulative
      jitaku       Frequency     Percent     Frequency      Percent
      --------------------------------------------------------------
      下宿生         185       34.84           185        34.84  
      自宅生         346       65.16           531       100.00  
      
                          Frequency Missing = 67
      
      *** 通常の頻度集計、クロス集計(アルファベット順にな  59
                                              Tuesday, February  1, 2022 10:46:37 AM
      The FREQ Procedure
                                           Cumulative    Cumulative
      carryer     Frequency     Percent     Frequency      Percent
      -------------------------------------------------------------
      J-PHONE           10        6.71            10         6.71  
      KDDI               1        0.67            11         7.38  
      No                 5        3.36            16        10.74  
      OCN                1        0.67            17        11.41  
      Softbank          22       14.77            39        26.17  
      UQ-mobil           1        0.67            40        26.85  
      YMobile            3        2.01            43        28.86  
      au                39       26.17            82        55.03  
      au+YMobi           1        0.67            83        55.70  
      docomo            65       43.62           148        99.33  
      docomo+w           1        0.67           149       100.00  
      
                         Frequency Missing = 449
      
      *** 通常の頻度集計、クロス集計(アルファベット順にな  60
                                              Tuesday, February  1, 2022 10:46:37 AM
      The FREQ Procedure
      Table of sex by jitaku
      sex       jitaku
      Frequency|
      Percent  |
      Row Pct  |
      Col Pct  |下宿    |自宅    |  Total
               |生      |生      |
      ---------+--------+--------+
      F        |     39 |     83 |    122
               |   9.35 |  19.90 |  29.26
               |  31.97 |  68.03 |
               |  26.17 |  30.97 |
      ---------+--------+--------+
      M        |    110 |    185 |    295
               |  26.38 |  44.36 |  70.74
               |  37.29 |  62.71 |
               |  73.83 |  69.03 |
      ---------+--------+--------+
      Total         149      268      417
                  35.73    64.27   100.00
      
      Frequency Missing = 181
      
      *** 通常の頻度集計、クロス集計(アルファベット順にな  61
                                              Tuesday, February  1, 2022 10:46:37 AM
      The FREQ Procedure
      Table of sex by carryer
      sex       carryer
      Frequency|
      Percent  |
      Row Pct  |
      Col Pct  |J-PHONE |KDDI    |No      |OCN     |Softbank|UQ-mobil|  Total
      ---------+--------+--------+--------+--------+--------+--------+
      F        |      4 |      0 |      1 |      0 |     10 |      0 |     56
               |   2.74 |   0.00 |   0.68 |   0.00 |   6.85 |   0.00 |  38.36
               |   7.14 |   0.00 |   1.79 |   0.00 |  17.86 |   0.00 |
               |  44.44 |   0.00 |  20.00 |      . |  45.45 |      . |
      ---------+--------+--------+--------+--------+--------+--------+
      M        |      5 |      1 |      4 |      0 |     12 |      0 |     90
               |   3.42 |   0.68 |   2.74 |   0.00 |   8.22 |   0.00 |  61.64
               |   5.56 |   1.11 |   4.44 |   0.00 |  13.33 |   0.00 |
               |  55.56 | 100.00 |  80.00 |      . |  54.55 |      . |
      ---------+--------+--------+--------+--------+--------+--------+
      Total           9        1        5        0       22        0      146
                   6.16     0.68     3.42     0.00    15.07     0.00   100.00
      (Continued)
      
      *** 通常の頻度集計、クロス集計(アルファベット順にな  62
                                              Tuesday, February  1, 2022 10:46:37 AM
      The FREQ Procedure
      Table of sex by carryer
      sex       carryer
      Frequency|
      Percent  |
      Row Pct  |
      Col Pct  |YMobile |au      |au+YMobi|docomo  |docomo+w|  Total
      ---------+--------+--------+--------+--------+--------+
      F        |      2 |     12 |      1 |     26 |      0 |     56
               |   1.37 |   8.22 |   0.68 |  17.81 |   0.00 |  38.36
               |   3.57 |  21.43 |   1.79 |  46.43 |   0.00 |
               |  66.67 |  30.77 | 100.00 |  40.00 |   0.00 |
      ---------+--------+--------+--------+--------+--------+
      M        |      1 |     27 |      0 |     39 |      1 |     90
               |   0.68 |  18.49 |   0.00 |  26.71 |   0.68 |  61.64
               |   1.11 |  30.00 |   0.00 |  43.33 |   1.11 |
               |  33.33 |  69.23 |   0.00 |  60.00 | 100.00 |
      ---------+--------+--------+--------+--------+--------+
      Total           3       39        1       65        1      146
                   2.05    26.71     0.68    44.52     0.68   100.00
      
      Frequency Missing = 452
      
      ≪中略≫
      
      *** 頻度の大きい順に表示 ***                                      65
                                              Tuesday, February  1, 2022 10:46:37 AM
      The FREQ Procedure
                                      Cumulative    Cumulative
      sex    Frequency     Percent     Frequency      Percent
      --------------------------------------------------------
      M           332       70.19           332        70.19  
      F           141       29.81           473       100.00  
      
                      Frequency Missing = 125
      
                                            Cumulative    Cumulative
      jitaku       Frequency     Percent     Frequency      Percent
      --------------------------------------------------------------
      自宅生            346       65.16           346        65.16  
      下宿生            185       34.84           531       100.00  
      
                          Frequency Missing = 67
      
      *** 頻度の大きい順に表示 ***                                      66
                                              Tuesday, February  1, 2022 10:46:37 AM
      The FREQ Procedure
                                           Cumulative    Cumulative
      carryer     Frequency     Percent     Frequency      Percent
      -------------------------------------------------------------
      docomo            65       43.62            65        43.62  
      au                39       26.17           104        69.80  
      Softbank          22       14.77           126        84.56  
      J-PHONE           10        6.71           136        91.28  
      No                 5        3.36           141        94.63  
      YMobile            3        2.01           144        96.64  
      KDDI               1        0.67           145        97.32  
      OCN                1        0.67           146        97.99  
      UQ-mobil           1        0.67           147        98.66  
      au+YMobi           1        0.67           148        99.33  
      docomo+w           1        0.67           149       100.00  
      
                         Frequency Missing = 449
      
      *** 頻度の大きい順に表示 ***                                      67
                                              Tuesday, February  1, 2022 10:46:37 AM
      The FREQ Procedure
      Table of sex by jitaku
      sex       jitaku
      Frequency|
      Percent  |
      Row Pct  |
      Col Pct  |自宅    |下宿    |  Total
               |生      |生      |
      ---------+--------+--------+
      M        |    185 |    110 |    295
               |  44.36 |  26.38 |  70.74
               |  62.71 |  37.29 |
               |  69.03 |  73.83 |
      ---------+--------+--------+
      F        |     83 |     39 |    122
               |  19.90 |   9.35 |  29.26
               |  68.03 |  31.97 |
               |  30.97 |  26.17 |
      ---------+--------+--------+
      Total         268      149      417
                  64.27    35.73   100.00
      
      Frequency Missing = 181
      
      *** 頻度の大きい順に表示 ***                                      68
                                              Tuesday, February  1, 2022 10:46:37 AM
      The FREQ Procedure
      Table of sex by carryer
      sex       carryer
      Frequency|
      Percent  |
      Row Pct  |
      Col Pct  |docomo  |au      |Softbank|J-PHONE |No      |YMobile |  Total
      ---------+--------+--------+--------+--------+--------+--------+
      M        |     39 |     27 |     12 |      5 |      4 |      1 |     90
               |  26.71 |  18.49 |   8.22 |   3.42 |   2.74 |   0.68 |  61.64
               |  43.33 |  30.00 |  13.33 |   5.56 |   4.44 |   1.11 |
               |  60.00 |  69.23 |  54.55 |  55.56 |  80.00 |  33.33 |
      ---------+--------+--------+--------+--------+--------+--------+
      F        |     26 |     12 |     10 |      4 |      1 |      2 |     56
               |  17.81 |   8.22 |   6.85 |   2.74 |   0.68 |   1.37 |  38.36
               |  46.43 |  21.43 |  17.86 |   7.14 |   1.79 |   3.57 |
               |  40.00 |  30.77 |  45.45 |  44.44 |  20.00 |  66.67 |
      ---------+--------+--------+--------+--------+--------+--------+
      Total          65       39       22        9        5        3      146
                  44.52    26.71    15.07     6.16     3.42     2.05   100.00
      (Continued)
      
      ≪中略≫
      
      *** 頻度の大きい順に表示(頻度のみ) ***                        72
                                              Tuesday, February  1, 2022 10:46:37 AM
      The FREQ Procedure
                          Cumulative
      sex    Frequency     Frequency
      ------------------------------
      M           332           332 
      F           141           473 
      
         Frequency Missing = 125
      
                                Cumulative
      jitaku       Frequency     Frequency
      ------------------------------------
      自宅生            346           346 
      下宿生            185           531 
      
             Frequency Missing = 67
      
      *** 頻度の大きい順に表示(頻度のみ) ***                        73
                                              Tuesday, February  1, 2022 10:46:37 AM
      The FREQ Procedure
                               Cumulative
      carryer     Frequency     Frequency
      -----------------------------------
      docomo            65            65 
      au                39           104 
      Softbank          22           126 
      J-PHONE           10           136 
      No                 5           141 
      YMobile            3           144 
      KDDI               1           145 
      OCN                1           146 
      UQ-mobil           1           147 
      au+YMobi           1           148 
      docomo+w           1           149 
      
            Frequency Missing = 449
      
      *** 頻度の大きい順に表示(頻度のみ) ***                        74
                                              Tuesday, February  1, 2022 10:46:37 AM
      The FREQ Procedure
      Table of sex by jitaku
      sex       jitaku
      Frequency|自宅    |下宿    |  Total
               |生      |生      |
      ---------+--------+--------+
      M        |    185 |    110 |    295
      ---------+--------+--------+
      F        |     83 |     39 |    122
      ---------+--------+--------+
      Total         268      149      417
      
      Frequency Missing = 181
      
      *** 頻度の大きい順に表示(頻度のみ) ***                        75
                                              Tuesday, February  1, 2022 10:46:37 AM
      The FREQ Procedure
      Table of sex by carryer
      sex       carryer
      Frequency|docomo  |au      |Softbank|J-PHONE |No      |YMobile |  Total
      ---------+--------+--------+--------+--------+--------+--------+
      M        |     39 |     27 |     12 |      5 |      4 |      1 |     90
      ---------+--------+--------+--------+--------+--------+--------+
      F        |     26 |     12 |     10 |      4 |      1 |      2 |     56
      ---------+--------+--------+--------+--------+--------+--------+
      Total          65       39       22        9        5        3      146
      (Continued)
      ≪後略≫
      

    3. [補足1] アンケート等のデータでは回答者の表記により 携帯電話会社の名前が大文字のものと小文字のものが混在していることも多い (表記の揺れ)。 本来であれば同じ会社を指すと考えられるので、そのような場合は、再定義すれば良い。 データパートに以下の再定義文を挿入する。 なお、社名や経営母体を変更した会社について、 どのように取り扱うかは別に検討する必要がある。
      
      ≪前略≫
      if carryer="DoCoMo"   then carryer="docomo";
      if carryer="DoCoMo+w" then carryer="docomo+W";
      if carryer="Vodafone" then carryer="Softbank";
      if carryer="vodafone" then carryer="Softbank";
      if carryer="softbank" then carryer="Softbank";
      if carryer="au+willc" then carryer="au+YMobile";
      if carryer="Willcom"  then carryer="YMobile";
      if carryer="DDIp"     then carryer="YMobile";
      ≪後略≫
      

    4. [補足2] SAS の出力の内、 「proc printto print = 'StatM21/les1302-Results.txt' new;」で 指定したファイルには「options nocenter linesize=78 pagesize=30;」 で表示エリアサイズに収まるように出力される (今回の場合で言えば一行78文字、一ページ30行)。 上記の出力でも、「SEX x JITAKU」のクロス表は、縦に分断されたように表示される。 これは、縦方向の行数が少ないので、クロス表が「小出し」に表示されるためである。 間にある 15行程の不要部分を削除すると、視覚的にも理解しやすい クロス表を得ることができ、配布資料はそのような編集作業後のものである。 皆さんもレポート作成時にはこの様な編集作業を行うと見易くなる。

    5. [補足3] 累積頻度の使い方の一つとして、頻度の高いもの順(降順)と言う指定もでき、 「上位 50% までのパターンを知りたい」と言うような時に使うことができる。 デフォルトではアルファベット順。 並び替えのオプションとしては、 freq(頻度の高い順) 以外に data(データセットに格納されている順)がある。
      
      ≪前略≫
      title '*** 頻度の大きい順に表示 ***';
      proc freq data=gakusei order=freq;
        tables sex jitaku carryer;
      run;
      proc freq data=gakusei order=freq;
        tables sex*jitaku;
        tables sex*carryer;
        tables jitaku*carryer;
      run;
      ≪後略≫
      

    6. [補足4] 頻度集計を行う際に、割合や周辺割合等が不要な場合は、 必要に応じて nopercent, norow, nocol を指定すれば良い。
      
      ≪前略≫
      title '*** 頻度の大きい順に表示(頻度のみ) ***';
      proc freq data=gakusei order=freq;
        tables sex jitaku carryer / nopercent norow nocol;
      run;
      proc freq data=gakusei order=freq;
        tables sex*jitaku     / nopercent norow nocol;
        tables sex*carryer    / nopercent norow nocol;
        tables jitaku*carryer / nopercent norow nocol;
      run;
      ≪後略≫
      

    7. [補足5] 上記の例では 二変量の組合わせまでを行ったが、 三変量以上の組合わせを行うことも可能である。 その際の出力はどのようになるかを予想し、その後実際に実行してみよ。 出力結果は何を表現し、また、予想は正しかったか?

      1. プログラム : Lesson 13-3 : les1303.sas
        
        ≪前略≫
        title '*** 3重クロス集計 ***';
        proc freq data=gakusei;
          tables sex*jitaku*carryer;
        run;
        

      2. 出力

  4. [Tips 3] 散布図行列(Scatterplot Matrix)

  5. Q-Qプロット導出の考え方

    回帰分析の中で「残差は正規分布を仮定して理論が構成されている」と紹介した。 また、統計検定の中では「分布が正規分布に従っているか」によって、 検定手法が異なることも紹介した。 これらの「正規分布に従っているか」を調べるための手法として Q-Qプロットがあり、「斜め右上がりの直線からの乖離具合で判断できる」と説明した。 どのような理由・原理からこの手法が導出されているかに 疑問(興味)に思った人も居たのではないかと思うので、 動画を交えて簡単に説明しておく。

    この手法は、平均を0に、分散を1に標準化した2つの分布を比較するものであり、基準の分布を標準正規正規分布(平均0,分散1)に取り、 調べたい変量の分布を対象分布として そのズレをプロットしたものである。 大まかな手順は以下の通り。 なお、確率側を比較するP-Pプロットもある。
    ちなみに、Qはquantile(分位点)、Pはprobability(確率)の頭文字を示している。

    1. それぞれの分布を標準化する
    2. それぞれの分布の累積分布を作る
    3. 測定点の確率に対する正規分布の分位点を求める
    4. 両者を縦軸と横軸に取ってプロットする
    5. 全く同じ分布であれば、斜め右上がりの直線上にプロットされる

  6. お役立ちサイト: マニュアル

  7. 連絡したければ

  8. 有意義な学生生活を