Octave で統計解析

# Octave で統計解析


```matlab
x = [2, 3, 2, 3, 4, 5, 3, 2, 3, 2, 1];
y = [3, 2, 2, 1, 2, 3, 3, 2, 5, 4, 2];

data = [45.51 43.38 56.91 47.32 55.20;
        56.74 61.85 70.44 57.09 55.59;
        32.62 34.17 47.24 43.64 42.45;
        53.43 59.83 47.97 52.18 46.99;
        37.41 36.15 37.70 26.06 30.12;
        42.85 43.91 44.75 53.73 50.74;
        66.06 52.68 39.87 61.52 68.19;
        62.09 62.36 62.01 62.93 59.58;
        50.81 58.48 40.50 48.10 42.55;
        52.48 47.21 52.61 47.42 48.60];
```

## 1.　合計 `sum()`

    usage:
    sum (x)
    sum(x, dim)
    sum(…, "native")
    sum(…, "double")
    sum(…, "extra")


```matlab
sum(data)
```

    ans =
    
       500.00   500.02   500.00   499.99   500.01
    


## 2.　累積 `prod()`

    usage:
    prod(x)
    prod(x, dim)
    prod(…, "native")
    prod(…, "double")


```matlab
prod(data)
```

    ans =
    
       7.8905e+16   7.8969e+16   8.0733e+16   7.6062e+16   7.8593e+16
    


## 3.　平均値 `mean()`

    usage:
    mean(x)
    mean(x, dim)
    mean(x, opt)
    mean(x, dim, opt)
    
    opt="a" 算術平均（デフォルト）
    opt="g" 幾何平均
    opt="h" 調和平均


```matlab
mean(x)
```

    ans = 2.7273



```matlab
mean(data)
```

    ans =
    
       50.000   50.002   50.000   49.999   50.001
    



```matlab
mean(data, dim=2)
```

    ans =
    
       49.664
       60.342
       40.024
       52.080
       33.488
       47.196
       57.664
       61.794
       48.088
       49.664
    



```matlab
mean(data, opt="g")
```

    ans =
    
       48.945   48.949   49.057   48.766   48.926
    



```matlab
mean(data, opt="h")
```

    ans =
    
       47.843   47.861   48.178   47.229   47.760
    


## 4.　不偏分散，分散 `var()`

    usage:
    var(x)
    var(x, opt)
    var(x, opt, dim)
    
    opt=0 不偏分散
    opt=1 分散


```matlab
var(x)
```

    ans = 1.2182



```matlab
var(data), var(data, opt=1)
```

    ans =
    
       111.11   111.11   111.12   111.09   111.14
    
    ans =
    
        99.995   100.002   100.005    99.985   100.023
    



```matlab
var(data, opt=0, dim=2)
```

    ans =
    
        36.3476
        37.5938
        40.0300
        26.1738
        26.6820
        22.7304
       134.4062
         1.6617
        50.9126
         7.1996
    


## 5.　標準偏差 `std()`
    usage:
    std(x)
    std(x, opt)
    std(x, opt, dim)
    
    opt=0 不偏分散の平方根， opt=1 分散の平方根


```matlab
std(x)
```

    ans = 1.1037



```matlab
std(data) # opt=0, dim=1
```

    ans =
    
       10.541   10.541   10.541   10.540   10.542
    



```matlab
std(data, opt=1) # dim=1
```

    ans =
    
        9.9997   10.0001   10.0003    9.9993   10.0011
    



```matlab
std(data, opt=0, dim=2)
```

    ans =
    
        6.0289
        6.1314
        6.3269
        5.1160
        5.1655
        4.7676
       11.5934
        1.2891
        7.1353
        2.6832
    



```matlab
std(data, opt=1, dim=2)
```

    ans =
    
        5.3924
        5.4841
        5.6590
        4.5759
        4.6201
        4.2643
       10.3694
        1.1530
        6.3820
        2.3999
    


## 6.　尖度 `skewness()`

    usage:
    skewness(x)
    skewness(x, flag)
    skewness(x, flag, dim)

    flag=1 標本尖度（デフォルト），flag=0 不偏推定値
    
    s1 = mean((x - mean(x)).^3 / std(x).^3
    s0 = sqrt(n * (n - 1)) / (n - 2) * mean((x - mean(x)).^3) / std(x).^3


```matlab
skewness(x), skewness(x, flag=0)
```

    ans = 0.5609
    ans = 0.6536



```matlab
skewness(data)
```

    ans =
    
      -0.1430  -0.2036   0.6687  -0.9911  -0.1520
    



```matlab
skewness(data, flag=0)
```

    ans =
    
      -0.1696  -0.2414   0.7930  -1.1753  -0.1802
    


## 7.　尖度 `kurtosis()`

    usage:
    kurtosis(x)
    kurtosis(x, flag)
    kurtosis(x, flag, dim)

    flag=1 標本歪度（デフォルト），flag=0 不偏推定値

    k1 = mean((x - mean(x)).^4 / std(x).^4
    k0 = 3 + (n-1)/(n-2)/(n-3) * ((n + 1) * k1 - 3*(n-1))


```matlab
kurtosis(x), kurtosis(x, flag=0)
```

    ans = 2.9166
    ans = 3.6943



```matlab
kurtosis(data)
```

    ans =
    
       2.0954   1.6057   2.4006   3.7860   2.8210
    



```matlab
kurtosis(data, flag=0)
```

    ans =
    
       2.3651   1.4994   2.9046   5.3537   3.6478
    


## 8.　中央値 `median()`

    usage:
    median(x)
    median(x, dim)


```matlab
median(x)
```

    ans = 3



```matlab
median(data)
```

    ans =
    
       51.645   49.945   47.605   50.140   49.670
    


## 9.　最小値 `min()`

    usage:
    min(x)
    min(x, [], dim)
    [w, iw] = min(x)
    min(x, y)
    
    戻り値を指定するとき，最小値 w のある場所（インデックス）が iw で返される。


```matlab
min(x)
```

    ans = 1



```matlab
min(data)
```

    ans =
    
       32.620   34.170   37.700   26.060   30.120
    



```matlab
[w,iw] = min(data)
```

    w =
    
       32.620   34.170   37.700   26.060   30.120
    
    iw =
    
       3   3   5   5   5
    


## 10.　最大値 `max()`

    usage:
    max(x)
    max(x, [], dim)
    [w, iw] = max(x)
    max(x, y)


```matlab
max(x)
```

    ans = 5



```matlab
max(data)
```

    ans =
    
       66.060   62.360   70.440   62.930   68.190
    



```matlab
[w,iw] = max(data)
```

    w =
    
       66.060   62.360   70.440   62.930   68.190
    
    iw =
    
       7   8   2   8   7
    


## 11.　範囲 `range()`

    usage:
    range(x)
    range(x, dim)


```matlab
range(x), max(x) - min(x)
```

    ans = 4
    ans = 4



```matlab
range(data)
```

    ans =
    
       33.440   28.190   32.740   36.870   38.070
    


## 12.　四分偏差 `iqr()`

この関数で計算される四分偏差は「第 3 四分位数 - 第 1 四分位数」 $Q_3 - Q_1$ である。統計学では $(Q_3 - Q_1) / 2$ である。

    usage:
    iqr(x)
    iqr(x, dim)


```matlab
iqr(data), quantile(data, 0.75) - quantile(data, 0.25)
```

    ans =
    
       13.8900   16.4500   16.4100    9.7700   13.0400
    
    ans =
    
       13.8900   16.4500   16.4100    9.7700   13.0400
    


## 13.　平均平方 `meansq()`

    usage:
    meansq(x)
    meansq(x, dim)


```matlab
meansq(x), mean(x.^2)
```

    ans = 8.5455
    ans = 8.5455


## 14.　クオンタイル `quantile()`

    usage:
    q = quantile(x)
    q = quantile(x, p)
    q = quantile(x, p, dim)
    q = quantile(x, p, dim, method)
    
    p: デフォルトで [0, 0.25, 0.5, 0.75, 1]
    method: R で採用されている 9 通りの方法。



```matlab
quantile(x)
```

    ans =
    
       1   2   3   3   5
    



```matlab
quantile(data)
```

    ans =
    
       32.620   34.170   37.700   26.060   30.120
       42.850   43.380   40.500   47.320   42.550
       51.645   49.945   47.605   50.140   49.670
       56.740   59.830   56.910   57.090   55.590
       66.060   62.360   70.440   62.930   68.190
    


Octave では method=5 がデフォルトであるが， R と同じにするなら method=7 にする。


```matlab
quantile(data, p=0:0.25:1, dim=1, method=7)
```

    ans =
    
       32.620   34.170   37.700   26.060   30.120
       43.515   43.513   41.562   47.345   43.660
       51.645   49.945   47.605   50.140   49.670
       55.913   59.492   55.835   56.250   55.493
       66.060   62.360   70.440   62.930   68.190
    


## 15.　基礎統計量 `statistics()`

    usage:
    statistics(x)
    statistics(x, dim)

以下の統計量を返す。

1. 最小値
1. 第1四分位数
1. 中央値
1. 第3四分位数
1. 最大値
1. 平均値
1. 標準偏差
1. 尖度.
1. 歪度


```matlab
statistics(x') % statistics(x) はエラーになる
```

    ans =
    
       1.0000
       2.0000
       3.0000
       3.0000
       5.0000
       2.7273
       1.1037
       0.5609
       2.9166
    



```matlab
statistics(data)
```

    ans =
    
       32.6200   34.1700   37.7000   26.0600   30.1200
       43.5150   43.5125   41.5625   47.3450   43.6600
       51.6450   49.9450   47.6050   50.1400   49.6700
       55.9125   59.4925   55.8350   56.2500   55.4925
       66.0600   62.3600   70.4400   62.9300   68.1900
       50.0000   50.0020   50.0000   49.9990   50.0010
       10.5406   10.5410   10.5412   10.5401   10.5421
       -0.1430   -0.2036    0.6687   -0.9911   -0.1520
        2.0954    1.6057    2.4006    3.7860    2.8210
    


## 16.　順位付け `ranks()`

同順位の場合には，平均順位になる。

usage: ranks(x, dim)


```matlab
ranks(x)
```

    ans =
    
     Columns 1 through 8:
    
        3.5000    7.5000    3.5000    7.5000   10.0000   11.0000    7.5000    3.5000
    
     Columns 9 through 11:
    
        7.5000    3.5000    1.0000
    



```matlab
ranks(data)
```

    ans =
    
        4    3    8    3    7
        8    9   10    8    8
        1    1    5    2    2
        7    8    6    6    4
        2    2    1    1    1
        3    4    4    7    6
       10    6    2    9   10
        9   10    9   10    9
        5    7    3    5    3
        6    5    7    4    5