当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


R sparse.model.matrix 构造稀疏设计/模型矩阵

R语言 sparse.model.matrix 位于 Matrix 包(package)。

说明

根据公式和数据帧 (sparse.model.matrix) 或单个因子 (fac2sparse) 构建稀疏模型或 “design” 矩阵。

fac2[Ss]parse() 函数是实用程序,也在主要用户级函数 sparse.model.matrix() 内部使用。

用法

sparse.model.matrix(object, data = environment(object),
            contrasts.arg = NULL, xlev = NULL, transpose = FALSE,
            drop.unused.levels = FALSE, row.names = TRUE,
            sep = "", verbose = FALSE, ...)

fac2sparse(from, to = c("d", "l", "n"),
       drop.unused.levels = TRUE, repr = c("C", "R", "T"), giveCsparse)
fac2Sparse(from, to = c("d", "l", "n"),
       drop.unused.levels = TRUE, repr = c("C", "R", "T"), giveCsparse,
       factorPatt12, contrasts.arg = NULL)

参数

object

适当类的对象。对于默认方法,模型公式或术语对象。

data

使用 model.frame 创建的 DataFrame 。如果是另一种对象,则首先调用model.frame

contrasts.arg
对于sparse.model.matrix()

一个列表,其条目是适合输入到 contrasts 替换函数的对比,其名称是包含 factordata 的列的名称。

对于fac2Sparse()

字符串或 NULL 或(可强制转换为)"sparseMatrix" ,指定要应用于因子级别的对比度。

xlev

如果data 没有"terms" 属性,则用作model.frame 的参数。

transpose

逻辑指示是否应返回转置;如果无论如何使用转置,设置transpose = TRUE会更有效。

drop.unused.levels

未使用的因子是否应该下降?默认为sparse.model.matrix已更改为FALSE,2010-07,为了兼容R的标准(密集)model.matrix().

row.names

逻辑指示是否应使用行名称。

sep

根据变量名称及其级别构造列名称时,character 字符串传递给 paste()

verbose

逻辑或整数指示是否应打印(以及多少)进度输出。

...

传入或传出其他方法的进一步参数。

from

(对于 fac2sparse() :) 一个 factor

to

指示要返回的稀疏矩阵的“kind”的字符。默认值 "d" 适用于 double

giveCsparse

已弃用,替换为 repr ;逻辑指示结果是否必须是 CsparseMatrix

repr

character 字符串, "C""T""R" 之一,指定用于结果的稀疏表示,即来自超类 CsparseMatrixTsparseMatrixRsparseMatrix 之一。

factorPatt12

逻辑向量,例如 fp ,长度为 2;当fp[1]为真时,返回“contrasted” t(X);当 fp[2] 为 true 时,原始 (“dummy”) t(X) ,即 fac2sparse() 的结果。

稀疏矩阵,扩展 CsparseMatrix (对于 fac2sparse() 如果默认为 repr = "C" ;否则为 TsparseMatrixRsparseMatrix )。

对于 fac2Sparse() ,长度为 2 的 list,两个分量都具有相应的转置模型矩阵,其中相应的 factorPatt12 为 true。

fac2sparse()sparse.model.matrix() 的基本主力,返回模型矩阵的转置 (t )。

注意

MatrixModels 包中的 model.Matrix(sparse = TRUE) 如今可能比 sparse.model.matrix 更好,因为 model.Matrix 返回类 modelMatrix 的对象,并带有与模型变量相关的附加槽 assigncontrasts

例子


dd <- data.frame(a = gl(3,4), b = gl(4,1,12))# balanced 2-way
options("contrasts") # the default:  "contr.treatment"
sparse.model.matrix(~ a + b, dd)
sparse.model.matrix(~ -1+ a + b, dd)# no intercept --> even sparser
sparse.model.matrix(~ a + b, dd, contrasts = list(a="contr.sum"))
sparse.model.matrix(~ a + b, dd, contrasts = list(b="contr.SAS"))

## Sparse method is equivalent to the traditional one :
stopifnot(all(sparse.model.matrix(~    a + b, dd) ==
              Matrix(model.matrix(~    a + b, dd), sparse=TRUE)),
          all(sparse.model.matrix(~0 + a + b, dd) ==
              Matrix(model.matrix(~0 + a + b, dd), sparse=TRUE)))


(ff <- gl(3,4,, c("X","Y", "Z")))
fac2sparse(ff) #  3 x 12 sparse Matrix of class "dgCMatrix"
##
##  X  1 1 1 1 . . . . . . . .
##  Y  . . . . 1 1 1 1 . . . .
##  Z  . . . . . . . . 1 1 1 1

## can also be computed via sparse.model.matrix():
f30 <- gl(3,0    )
f12 <- gl(3,0, 12)
stopifnot(
  all.equal(t( fac2sparse(ff) ),
        sparse.model.matrix(~ 0+ff),
        tolerance = 0, check.attributes=FALSE),
  is(M <- fac2sparse(f30, drop= TRUE),"CsparseMatrix"), dim(M) == c(0, 0),
  is(M <- fac2sparse(f30, drop=FALSE),"CsparseMatrix"), dim(M) == c(3, 0),
  is(M <- fac2sparse(f12, drop= TRUE),"CsparseMatrix"), dim(M) == c(0,12),
  is(M <- fac2sparse(f12, drop=FALSE),"CsparseMatrix"), dim(M) == c(3,12)
 )

作者

Doug Bates and Martin Maechler, with initial suggestions from Tim Hesterberg.

也可以看看

model.matrix包装内stats, 底座的一部分R.

MatrixModels 中的model.Matrix;看注释'。

as(f, "sparseMatrix") (请参阅类文档 sparseMatrix 中的 coerce(from = "factor", ..) )为单个因子 f 生成转置稀疏模型矩阵(无对比)。

相关用法


注:本文由纯净天空筛选整理自R-devel大神的英文原创作品 Construct Sparse Design / Model Matrices。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。