最近、統計のプログラムが必要になり、Rというプログラミング言語を習得することになりました。ウェブのアプリの開発では、php, javascript, htmlなどがメインなゆえに、それを外れたプログラミング言語を使用する機会は少なく、興味も手伝って取り組んでみました。

Rと言うと、もちろん、R Studioという無料のビジュアルな開発ツールがあります。それはそれでビジュアルでグラフの作成も可能であり、私もそちらでいくらか鍛てもらったのですが、実はコマンドラインでもRのプログラムは使用可能なのです。ということで、コマンドライン好きの私としては、あえてここではコマンドラインの方でRを紹介させていただきます。

インストール

まず、私のFedora 29での仮想環境では、以下のようにRのインストールが可能です。いやいや凄いパッケージの数。

$ yum install R
...
Transaction Summary	
Install  293 Packages	
	
Total download size: 299 M	
Installed size: 670 M	

実行は、単に、Rとタイプするだけ、

$ R

R version 3.5.3 (2019-03-11) -- "Great Truth"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-redhat-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

>

最後の > がRの対話型のプロンプトです。Laravelのプログラマーにとっては、tinkerのようなものです。行の最後にセミコロンが要らないことに注意を!

データとその統計

Rにはすでに、サンプルのデータがたくさんあります。data()はそのリストを表示してくれます。

$ R
> data()

AirPassengers           Monthly Airline Passenger Numbers 1949-1960
BJsales                 Sales Data with Leading Indicator
BJsales.lead (BJsales)
                        Sales Data with Leading Indicator
BOD                     Biochemical Oxygen Demand
CO2                     Carbon Dioxide Uptake in Grass Plants
ChickWeight             Weight versus age of chicks on different diets
DNase                   Elisa assay of DNase
...
women                   Average Heights and Weights for American Women

とたくさん出てきます。例えば、最後のwomenを見てみましょう。

> women
   height weight
1      58    115
2      59    117
3      60    120
4      61    123
5      62    126
6      63    129
7      64    132
8      65    135
9      66    139
10     67    142
11     68    146
12     69    150
13     70    154
14     71    159
15     72    164

これはアメリカの女性の身長と体重のデータです。単位はインチとパウンドですね。

さあ、ここでRが凄いのは、このデータを主要な統計値を一発で出してくれます。

> summary(women)
     height         weight     
 Min.   :58.0   Min.   :115.0  
 1st Qu.:61.5   1st Qu.:124.5  
 Median :65.0   Median :135.0  
 Mean   :65.0   Mean   :136.7  
 3rd Qu.:68.5   3rd Qu.:148.0  
 Max.   :72.0   Max.   :164.0 
Min. 最小値
1st Qu. 25パーセンタイル
Median 中央値(50パーセンタイル)
Mean 平均値
3rd Qu. 75パーセンタイル
Max. 最大値

これらの値のおかげで、このサンプルのデータのだいたいのバラツキみたいのが想像できます。ここでは、Heightにおいて中央値と平均値が同じなので、データの分布は対称ですね。

80パーセンタイルは、どうなのでしょう?

> quantile(women$height, 0.8)
 80% 
69.2 

と簡単に計算できます。引数として指定したwomen$heightは、以下のようにデータから、heightだけのデータを取ってきます。これはphpなどの言語では見慣れない$の使い方ですね。

> women$height
 [1] 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

グラフ

コンソールなので、グラフの作成は不可能と思いきや、txtplotというライブラリが使用できます。
シェルではなく、Rのコマンドラインから、install.packages()でライブラリをインストールします。この際は、rootユーザー権限で実行してください。ダウンロード元の選択が尋ねれられますが、1: 0-Cloud [https]を選択で十分です。

> install.packages('txtplot')			
Installing package into ‘/usr/lib64/R/library’			
(as ‘lib’ is unspecified)			
--- Please select a CRAN mirror for use in this session ---			
Secure CRAN mirrors			
			
1: 0-Cloud [https]                   2: Algeria [https]			
3: Australia (Canberra) [https]      4: Australia (Melbourne 1) [https]			
5: Australia (Melbourne 2) [https]   6: Australia (Perth) [https]			
7: Austria [https]                   8: Belgium (Ghent) [https]			
9: Brazil (PR) [https]              10: Brazil (RJ) [https]			
11: Brazil (SP 1) [https]            12: Brazil (SP 2) [https]			
13: Bulgaria [https]                 14: Chile 1 [https]			
..
Selection: 1
trying URL 'https://cloud.r-project.org/src/contrib/txtplot_1.0-3.tar.gz'
Content type 'application/x-gzip' length 6152 bytes
==================================================
downloaded 6152 bytes

* installing *source* package ‘txtplot’ ...
...

インストールしたら、以下のようにRの中で実行すると、

> library('txtplot')
> txtplot(women$weight, women$height)
   +-------+----------+----------+----------+----------+-------+
   |                                                       *   |
70 +                                            *     *        +
   |                                        *                  |
   |                                    *                      |
   |                               *                           |
   |                            *                              |
65 +                        *                                  +
   |                    *                                      |
   |                 *                                         |
   |              *                                            |
   |          *                                                |
60 +    *  *                                                   +
   |  *                                                        |
   +-------+----------+----------+----------+----------+-------+
          120        130        140        150        160       

とAscii文字でのグラフが表示されます!

最後に、Rのプロンプトから抜けるには、quitでも、exitでもコントロールCでもありません。q()なのです。

> q()
Save workspace image? [y/n/c]: y

By khino