LoginSignup
2
3

Rの便利なライブラリ・関数 と Rcmdrの便利な機能

Last updated at Posted at 2021-12-10

はじめに

Rの便利なライブラリ・関数 と Rcmdr(Rコマンダー)の便利な機能を整理しておく。

Rの便利なライブラリ・関数

作図デバイスを追加

windows()

作図デバイスを追加する。

ヒストグラム

箱ひげ図

points()

points()で、平均値などを箱ひげ図の中にポイントできる。

ドットチャート

ドットチャートは、データ数が小さいとき箱ひげ図の良い代りになる。

散布図行列

psych::pairs.panels(Dataset)

psychというライブラリを使って、相関係数も表示される散布図行列が描ける。
psychを使った散布図行列の描き方・読み方については、以下の記事でご説明していただいている。

相関係数

qgraph(cor(Dataset), edge.labels=T)

各変数間の相関関係をネットワーク図で表示できる。
cor(Dataset)は相関行列の作成。

qgraph(cor(Dataset), edge.labels=T)

管理図

グラフに縦線、横線、一次関数の線を描画

Rcmdrの便利な機能

[逐次モデル選択...]

重回帰分析で、ステップワイズ法を用いることができる。
ここでは、AIC(Akaike’s Informataion Criteriaon, 赤池情報量基準)を基準値として変数減少法により、モデルを作成する手順を示す。

※ステップワイズ法に関する参考文献
https://cogpsy.educ.kyoto-u.ac.jp/personal/Kusumi/datasem13/shinya.pdf
https://waidai-csc.jp/updata/2019/05/53729419335f743b3924577b76bd198b.pdf

image.png

image.png

Call:
lm(formula = 工数.H. ~ CountInput + CountLineCode + CountLineCodeExe + 
    CountOutput + Cyclomatic + CyclomaticStrict, data = Ensyu1)

Residuals:
    Min      1Q  Median      3Q     Max 
-2.7449 -0.6334 -0.0227  0.5296  4.4190 

Coefficients:
                  Estimate Std. Error t value Pr(>|t|)   
(Intercept)      -0.205274   0.253084  -0.811   0.4193   
CountInput        0.003567   0.036494   0.098   0.9223   
CountLineCode     0.143504   0.049047   2.926   0.0043 **
CountLineCodeExe -0.077245   0.064630  -1.195   0.2350   
CountOutput       0.094150   0.044211   2.130   0.0358 * 
Cyclomatic       -0.021502   0.098072  -0.219   0.8269   
CyclomaticStrict  0.122365   0.061585   1.987   0.0498 * 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.222 on 95 degrees of freedom
Multiple R-squared:  0.779,	Adjusted R-squared:  0.765 
F-statistic: 55.81 on 6 and 95 DF,  p-value: < 2.2e-16

image.png

image.png

Direction:  backward
Criterion:  AIC 

Start:  AIC=47.58
工数.H. ~ CountInput + CountLineCode + CountLineCodeExe + CountOutput + 
    Cyclomatic + CyclomaticStrict

                   Df Sum of Sq    RSS    AIC
- CountInput        1    0.0143 141.78 45.591
- Cyclomatic        1    0.0717 141.84 45.632
- CountLineCodeExe  1    2.1317 143.90 47.103
<none>                          141.77 47.581
- CyclomaticStrict  1    5.8915 147.66 49.734
- CountOutput       1    6.7676 148.54 50.337
- CountLineCode     1   12.7751 154.54 54.381

Step:  AIC=45.59
工数.H. ~ CountLineCode + CountLineCodeExe + CountOutput + Cyclomatic + 
    CyclomaticStrict

                   Df Sum of Sq    RSS    AIC
- Cyclomatic        1    0.0626 141.84 43.636
- CountLineCodeExe  1    2.1354 143.92 45.116
<none>                          141.78 45.591
- CyclomaticStrict  1    6.0167 147.80 47.830
- CountOutput       1    8.8907 150.67 49.794
- CountLineCode     1   12.9963 154.78 52.537

Step:  AIC=43.64
工数.H. ~ CountLineCode + CountLineCodeExe + CountOutput + CyclomaticStrict

                   Df Sum of Sq    RSS    AIC
- CountLineCodeExe  1    2.0840 143.93 43.124
<none>                          141.84 43.636
- CyclomaticStrict  1    6.8603 148.71 46.454
- CountOutput       1    9.6259 151.47 48.333
- CountLineCode     1   14.8096 156.66 51.765

Step:  AIC=43.12
工数.H. ~ CountLineCode + CountOutput + CyclomaticStrict

                   Df Sum of Sq    RSS    AIC
<none>                          143.93 43.124
- CountOutput       1     7.804 151.73 46.510
- CyclomaticStrict  1     8.764 152.69 47.152
- CountLineCode     1    57.146 201.08 75.228

Call:
lm(formula = 工数.H. ~ CountLineCode + CountOutput + CyclomaticStrict, 
    data = Ensyu1)

Coefficients:
     (Intercept)     CountLineCode       CountOutput  CyclomaticStrict  
        -0.04800           0.08949           0.08377           0.13006

[計算結果をデータとして保存]

重回帰分析により作成した式での予測値を実績値と比較したい場合に便利な機能。
予測値をデータセットに新たな変数として追加が可能。
image.png
image.png

予測値と実績値の散布図が簡単に作成可能。
散布図は予測値(fitted.RegModel)をx軸とするのがお作法。
image.png

Rでの分析結果の見方

回帰分析

全般的な参考記事

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3