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 |
使用 |
contrasts.arg |
|
xlev |
如果 |
transpose |
逻辑指示是否应返回转置;如果无论如何使用转置,设置 |
drop.unused.levels |
未使用的因子是否应该下降?默认为 |
row.names |
逻辑指示是否应使用行名称。 |
sep |
|
verbose |
逻辑或整数指示是否应打印(以及多少)进度输出。 |
... |
传入或传出其他方法的进一步参数。 |
from |
(对于 |
to |
指示要返回的稀疏矩阵的“kind”的字符。默认值 |
giveCsparse |
已弃用,替换为 |
repr |
|
factorPatt12 |
逻辑向量,例如 |
值
稀疏矩阵,扩展 CsparseMatrix
(对于 fac2sparse()
如果默认为 repr = "C"
;否则为 TsparseMatrix
或 RsparseMatrix
)。
对于 fac2Sparse()
,长度为 2 的 list
,两个分量都具有相应的转置模型矩阵,其中相应的 factorPatt12
为 true。
fac2sparse()
是 sparse.model.matrix()
的基本主力,返回模型矩阵的转置 (t
)。
注意
MatrixModels
包中的 model.Matrix(sparse = TRUE)
如今可能比 sparse.model.matrix
更好,因为 model.Matrix
返回类 modelMatrix
的对象,并带有与模型变量相关的附加槽 assign
和 contrasts
。
例子
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 sparseMatrix 从非零项构建一般稀疏矩阵
- R sparseQR-class 稀疏 QR 分解
- R sparseVector-class 稀疏向量类
- R sparseMatrix-class 虚拟类“sparseMatrix”——稀疏矩阵之母
- R sparseLU-class 稀疏 LU 分解
- R sparseVector 从非零条目构造稀疏向量
- R spMatrix 三元组的稀疏矩阵构造函数
- R solve-methods 函数求解矩阵包中的方法
- R symmetricMatrix-class 包矩阵中对称矩阵的虚拟类
- R symmpart-methods 矩阵的对称部分和偏斜(对称)部分
- R dtrMatrix-class 三角形稠密数值矩阵
- R facmul-methods 乘以矩阵因式分解的因数
- R updown-methods 更新和降级稀疏 Cholesky 分解
- R bdiag 构建分块对角矩阵
- R printSpMatrix 灵活格式化和打印稀疏矩阵
- R all.equal-methods 函数 all.equal() 的矩阵封装方法
- R boolmatmult-methods 布尔算术矩阵乘积:%&% 和方法
- R ltrMatrix-class 三角密集逻辑矩阵
- R Hilbert 生成希尔伯特矩阵
- R nearPD 最近正定矩阵
- R lsyMatrix-class 对称密集逻辑矩阵
- R CHMfactor-class 稀疏 Cholesky 分解
- R dgCMatrix-class 压缩、稀疏、面向列的数值矩阵
- R Cholesky-methods Cholesky 分解方法
- R Subassign-methods “[<-”的方法 - 分配给“矩阵”的子集
注:本文由纯净天空筛选整理自R-devel大神的英文原创作品 Construct Sparse Design / Model Matrices。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。