返り値を抽出する

  • Rの関数は色々な情報を返してくる
  • その中のある値を使って次の処理をしたい、ということはよくある
  • が、不勉強だったために、どうやって、その「使いたい値」を取り出せばよいのかがわからず困ることがあった
  • そのために、こんな調べものをした
  • "typeof()"関数や"str()"関数というもので、オブジェクトの「何たるか」やオブジェクトの「構成」を確認できることがわかった
  • それを使って、たとえば"lm()"関数の返り値の中がどうなっているかを確認しよう
  • こちらの"lm()"処理の結果を"lm.out"に入れている
  • さて、typeof()で中身を見るとリストであるとわかる
  • lm.outをそのまま表示させるとかいつまんだ情報が「見やすく」表示される
  • 見やすいけれど、「使い」にくい
> lm.out

Call:
lm(formula = log(pro.cont[c(2:length(pro.cont))], 10) ~ log((r -     1), 10))

Coefficients:
     (Intercept)  log((r - 1), 10)  
          1.9426            0.9584 
  • summary()関数にかけるのもよい手だが
> summary(lm.out)

Call:
lm(formula = log(pro.cont[c(2:length(pro.cont))], 10) ~ log((r - 
    1), 10))

Residuals:
        1         2         3         4         5         6 
 0.039235  0.006266 -0.061634 -0.022623 -0.009042  0.047798 

Coefficients:
                 Estimate Std. Error t value Pr(>|t|)    
(Intercept)       1.94256    0.01964   98.91 6.26e-08 ***
log((r - 1), 10)  0.95837    0.02092   45.82 1.36e-06 ***
---
Signif. codes:  0***0.001**0.01*0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 0.04543 on 4 degrees of freedom
Multiple R-squared: 0.9981,     Adjusted R-squared: 0.9976 
F-statistic:  2100 on 1 and 4 DF,  p-value: 1.357e-06 
  • これまた、見やすいけれど使いにくい
  • というわけで、返り値がどうなっているのかを確認して、「使」えるようにする
> typeof(lm.out)
[1] "list"
  • リストとわかる
  • str()関数にかけると
> str(lm.out)
List of 12
 $ coefficients : Named num [1:2] 1.943 0.958
  ..- attr(*, "names")= chr [1:2] "(Intercept)" "log((r - 1), 10)"
 $ residuals    : Named num [1:6] 0.03924 0.00627 -0.06163 -0.02262 -0.00904 ...
  ..- attr(*, "names")= chr [1:6] "1" "2" "3" "4" ...
 $ effects      : Named num [1:6] -5.4833 2.0816 -0.0734 -0.0353 -0.0228 ...
  ..- attr(*, "names")= chr [1:6] "(Intercept)" "log((r - 1), 10)" "" "" ...
 $ rank         : int 2
 $ fitted.values: Named num [1:6] 0.96 1.47 2.06 2.5 3.01 ...
  ..- attr(*, "names")= chr [1:6] "1" "2" "3" "4" ...
 $ assign       : int [1:2] 0 1
 $ qr           :List of 5
  ..$ qr   : num [1:6, 1:2] -2.449 0.408 0.408 0.408 0.408 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:6] "1" "2" "3" "4" ...
  .. .. ..$ : chr [1:2] "(Intercept)" "log((r - 1), 10)"
  .. ..- attr(*, "assign")= int [1:2] 0 1
  ..$ qraux: num [1:2] 1.41 1.19
  ..$ pivot: int [1:2] 1 2
  ..$ tol  : num 1e-07
  ..$ rank : int 2
  ..- attr(*, "class")= chr "qr"
 $ df.residual  : int 4
 $ xlevels      : list()
 $ call         : language lm(formula = log(pro.cont[c(2:length(pro.cont))], 10) ~ log((r -      1), 10))
 $ terms        :Classes 'terms', 'formula' length 3 log(pro.cont[c(2:length(pro.cont))], 10) ~ log((r - 1), 10)
  .. ..- attr(*, "variables")= language list(log(pro.cont[c(2:length(pro.cont))], 10), log((r - 1), 10))
  .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:2] "log(pro.cont[c(2:length(pro.cont))], 10)" "log((r - 1), 10)"
  .. .. .. ..$ : chr "log((r - 1), 10)"
  .. ..- attr(*, "term.labels")= chr "log((r - 1), 10)"
  .. ..- attr(*, "order")= int 1
  .. ..- attr(*, "intercept")= int 1
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. ..- attr(*, "predvars")= language list(log(pro.cont[c(2:length(pro.cont))], 10), log((r - 1), 10))
  .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
  .. .. ..- attr(*, "names")= chr [1:2] "log(pro.cont[c(2:length(pro.cont))], 10)" "log((r - 1), 10)"
 $ model        :'data.frame':  6 obs. of  2 variables:
  ..$ log(pro.cont[c(2:length(pro.cont))], 10): num [1:6] 1 1.48 2 2.48 3 ...
  ..$ log((r - 1), 10)                        : num [1:6] -1.024 -0.492 0.124 0.581 1.113 ...
  ..- attr(*, "terms")=Classes 'terms', 'formula' length 3 log(pro.cont[c(2:length(pro.cont))], 10) ~ log((r - 1), 10)
  .. .. ..- attr(*, "variables")= language list(log(pro.cont[c(2:length(pro.cont))], 10), log((r - 1), 10))
  .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. ..$ : chr [1:2] "log(pro.cont[c(2:length(pro.cont))], 10)" "log((r - 1), 10)"
  .. .. .. .. ..$ : chr "log((r - 1), 10)"
  .. .. ..- attr(*, "term.labels")= chr "log((r - 1), 10)"
  .. .. ..- attr(*, "order")= int 1
  .. .. ..- attr(*, "intercept")= int 1
  .. .. ..- attr(*, "response")= int 1
  .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. .. ..- attr(*, "predvars")= language list(log(pro.cont[c(2:length(pro.cont))], 10), log((r - 1), 10))
  .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
  .. .. .. ..- attr(*, "names")= chr [1:2] "log(pro.cont[c(2:length(pro.cont))], 10)" "log((r - 1), 10)"
 - attr(*, "class")= chr "lm"
  • 「なるほどね」
> lm.out$df.residual
[1] 4
  • と出せるし、
> 
> lm.out$qr
$qr
  (Intercept) log((r - 1), 10)
1  -2.4494897      -0.75653693
2   0.4082483       2.17205292
3   0.4082483      -0.09295765
4   0.4082483      -0.30342208
5   0.4082483      -0.54808399
6   0.4082483      -0.74998342
attr(,"assign")
[1] 0 1

$qraux
[1] 1.408248 1.190847

$pivot
[1] 1 2

$tol
[1] 1e-07

$rank
[1] 2

attr(,"class")
[1] "qr"
  • だから、
> lm.out$qr$qr
  (Intercept) log((r - 1), 10)
1  -2.4494897      -0.75653693
2   0.4082483       2.17205292
3   0.4082483      -0.09295765
4   0.4082483      -0.30342208
5   0.4082483      -0.54808399
6   0.4082483      -0.74998342
attr(,"assign")
[1] 0 1
  • と、一段、深く降りて行って
> str(lm.out$qr$qr)
 num [1:6, 1:2] -2.449 0.408 0.408 0.408 0.408 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:6] "1" "2" "3" "4" ...
  ..$ : chr [1:2] "(Intercept)" "log((r - 1), 10)"
 - attr(*, "assign")= int [1:2] 0 1
  • で、これが、さらに"dimnames"つきの"List of 2"とわかるから
> dim(lm.out$qr$qr)
[1] 6 2
  • dimension情報を与えて、次のように、ある値をスカラーで取り出せる
> lm.out$qr$qr[3,2]
[1] -0.09295765