Triad sou.

R の model.frame オブジェクトについて

model.frame オブジェクト

model.frame オブジェクトは、R でデータとモデル式を統一的に扱うためのオブジェクトです。
実体は data.frame クラスのデータに、モデル式を含むいろいろな attributes が付加されたものです。

d  <- e <- rep(1, 10)
c  <- 1:10
b  <- factor(rep(1:2, 5))
a  <- c + as.numeric(b) + rnorm(10, 0, 0.2)
df <- data.frame(a=a, b=b, c=c, d=d, e=e)
formula <- a ~ b + c
mf <- model.frame(formula, data=df, weights=d, offset=e)
attributes(mf)
$names
[1] "a"         "b"         "c"         "(weights)"
[5] "(offset)" 

$terms
a ~ b + c
attr(,"variables")
list(a, b, c)
attr(,"factors")
  b c
a 0 0
b 1 0
c 0 1
attr(,"term.labels")
[1] "b" "c"
attr(,"order")
[1] 1 1
attr(,"intercept")
[1] 1
attr(,"response")
[1] 1
attr(,".Environment")
<environment: R_GlobalEnv>
attr(,"predvars")
list(a, b, c)
attr(,"dataClasses")
        a         b         c (weights)  (offset) 
"numeric"  "factor" "numeric" "numeric" "numeric" 

$row.names
 [1]  1  2  3  4  5  6  7  8  9 10

$class
[1] "data.frame"
model.response メソッド

model.frame オブジェクトから応答変数を取り出します。
引数 type で型を指定できます。

model.response(mf, type = "numeric")
> model.response(mf, type = "numeric")
        1         2         3         4         5
1.3296440 0.9986354 0.9287484 0.8723003 0.7622903
        6         7         8         9        10
0.8235902 1.1184641 1.0386115 1.2407432 0.9011047
model.matrix メソッド

model.frame オブジェクトから、デザイン行列を取り出します。

model.matrix(terms(mf), mf)
> model.matrix(terms(mf), mf)
   (Intercept) b2  c
1            1  0  1
2            1  1  2
3            1  0  3
4            1  1  4
5            1  0  5
6            1  1  6
7            1  0  7
8            1  1  8
9            1  0  9
10           1  1 10
attr(,"assign")
[1] 0 1 2
attr(,"contrasts")
attr(,"contrasts")$b
[1] "contr.treatment"

引数 contrasts で factor 型の変数に対する対比を変更することができます。

model.matrix(terms(mf), mf, contrasts = list(b="contr.sum"))
> model.matrix(terms(mf), mf, contrasts = list(b="contr.sum"))
   (Intercept) b1  c
1            1  1  1
2            1 -1  2
3            1  1  3
4            1 -1  4
5            1  1  5
6            1 -1  6
7            1  1  7
8            1 -1  8
9            1  1  9
10           1 -1 10
attr(,"assign")
[1] 0 1 2
attr(,"contrasts")
attr(,"contrasts")$b
[1] "contr.sum"
model.weights メソッド

model.frame オブジェクトから、応答変数に対する重みを取り出します。

model.weights(mf)
> model.weights(mf)
 [1] 1 1 1 1 1 1 1 1 1 1
model.offset メソッド

model.frame オブジェクトから、各個体に対するオフセットを取り出します。

model.offset(mf)
> model.offset(mf)
 [1] 1 1 1 1 1 1 1 1 1 1
model.extract メソッド

model.frame オブジェクトから、任意の変数等を取り出します。

model.extract(mf, "weights")
> model.extract(mf, "weights")
 1  2  3  4  5  6  7  8  9 10 
 1  1  1  1  1  1  1  1  1  1

取り出すことができるのは、model.response で取り出せる変数、model.offset で取り出せる変数、変数名に "( )" がついている変数っぽいです。

> mf
           a b  c (weights) (offset)
1   1.858072 1  1         1        1
2   4.273804 2  2         1        1
3   3.843513 1  3         1        1
4   6.106077 2  4         1        1
5   6.074054 1  5         1        1
6   7.847535 2  6         1        1
7   7.972882 1  7         1        1
8   9.801845 2  8         1        1
9   9.927260 1  9         1        1
10 11.554790 2 10         1        1