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


R hardhat model_matrix 構建設計矩陣

model_matrix()stats::model.matrix() 的更嚴格版本。值得注意的是,model_matrix() 永遠不會刪除行,結果將是一個 tibble。

用法

model_matrix(terms, data)

參數

terms

用於構造模型矩陣的術語對象。這通常是從 model_frame() 的相應調用返回的術語對象。

data

用於構建設計矩陣的小標題。這通常是從相應的 model_frame() 調用返回的小標題。

包含設計矩陣的小標題。

細節

下麵解釋了與 stats::model.matrix() 相比一些參數差異的基本原理:

  • contrasts.arg :全局設置對比度參數 options("contrasts") ,或直接使用 stats::contrasts() 為感興趣的因子分配對比度。請參閱示例部分。

  • xlev :不允許,因為 model.frame() 從未被調用,因此沒有必要。

  • ... :不允許,因為 model.matrix() 的默認方法不使用它,而 lm 方法使用它來傳遞潛在的偏移量和權重,這在安全帽中的處理方式有所不同。

例子

# ---------------------------------------------------------------------------
# Example usage

framed <- model_frame(Sepal.Width ~ Species, iris)

model_matrix(framed$terms, framed$data)
#> # 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
#> # ℹ 140 more rows

# ---------------------------------------------------------------------------
# Missing values never result in dropped rows

iris2 <- iris
iris2$Species[1] <- NA

framed2 <- model_frame(Sepal.Width ~ Species, iris2)

model_matrix(framed2$terms, framed2$data)
#> # A tibble: 150 × 3
#>    `(Intercept)` Speciesversicolor Speciesvirginica
#>            <dbl>             <dbl>            <dbl>
#>  1             1                NA               NA
#>  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
#> # ℹ 140 more rows

# ---------------------------------------------------------------------------
# Contrasts

# Default contrasts
y <- factor(c("a", "b"))
x <- data.frame(y = y)
framed <- model_frame(~y, x)

# Setting contrasts directly
y_with_contrast <- y
contrasts(y_with_contrast) <- contr.sum(2)
x2 <- data.frame(y = y_with_contrast)
framed2 <- model_frame(~y, x2)

# Compare!
model_matrix(framed$terms, framed$data)
#> # A tibble: 2 × 2
#>   `(Intercept)`    yb
#>           <dbl> <dbl>
#> 1             1     0
#> 2             1     1
model_matrix(framed2$terms, framed2$data)
#> # A tibble: 2 × 2
#>   `(Intercept)`    y1
#>           <dbl> <dbl>
#> 1             1     1
#> 2             1    -1

# Also, can set the contrasts globally
global_override <- c(unordered = "contr.sum", ordered = "contr.poly")

rlang::with_options(
  .expr = {
    model_matrix(framed$terms, framed$data)
  },
  contrasts = global_override
)
#> # A tibble: 2 × 2
#>   `(Intercept)`    y1
#>           <dbl> <dbl>
#> 1             1     1
#> 2             1    -1
源代碼:R/model-matrix.R

相關用法


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