當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R modelr model_matrix 構建設計矩陣

這是 stats::model.matrix() 的一個薄包裝,返回一個 tibble。用它來確定如何將建模公式轉換為矩陣,然後轉換為方程。

用法

model_matrix(data, formula, ...)

參數

data

一個 DataFrame

formula

建模公式

...

其他參數傳遞給stats::model.matrix()

一點點。

例子

model_matrix(mtcars, mpg ~ cyl)
#> # A tibble: 32 × 2
#>    `(Intercept)`   cyl
#>            <dbl> <dbl>
#>  1             1     6
#>  2             1     6
#>  3             1     4
#>  4             1     6
#>  5             1     8
#>  6             1     6
#>  7             1     8
#>  8             1     4
#>  9             1     4
#> 10             1     6
#> # … with 22 more rows
model_matrix(iris, Sepal.Length ~ Species)
#> # A tibble: 150 × 3
#>    `(Intercept)` Speciesversicolor Speciesvirginica
#>            <dbl>             <dbl>            <dbl>
#>  1             1                 0                0
#>  2             1                 0                0
#>  3             1                 0                0
#>  4             1                 0                0
#>  5             1                 0                0
#>  6             1                 0                0
#>  7             1                 0                0
#>  8             1                 0                0
#>  9             1                 0                0
#> 10             1                 0                0
#> # … with 140 more rows
model_matrix(iris, Sepal.Length ~ Species - 1)
#> # A tibble: 150 × 3
#>    Speciessetosa Speciesversicolor Speciesvirginica
#>            <dbl>             <dbl>            <dbl>
#>  1             1                 0                0
#>  2             1                 0                0
#>  3             1                 0                0
#>  4             1                 0                0
#>  5             1                 0                0
#>  6             1                 0                0
#>  7             1                 0                0
#>  8             1                 0                0
#>  9             1                 0                0
#> 10             1                 0                0
#> # … with 140 more rows
源代碼:R/model_matrix.R

相關用法


注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Construct a design matrix。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。