indMatrix-class
位於 Matrix
包(package)。 說明
indMatrix
類是行和列索引矩陣的類,存儲為從 1 開始的整數索引向量。行(列)索引矩陣是以標準單位向量為行(列)的矩陣。當將觀測值映射到離散的協變量值集時,此類矩陣非常有用。
將左側矩陣乘以行索引矩陣相當於對其行進行索引,即對行“with replacement”進行采樣。類似地,將右側矩陣乘以列索引矩陣相當於對其列進行索引。事實上,此類產品在 Matrix
中作為索引操作實現;請參閱下麵的“詳細信息”。
行和列為標準單位向量的矩陣稱為置換矩陣。這種特殊情況由 pMatrix
類指定,它是 indMatrix
的直接子類。
細節
索引矩陣的轉置是與 perm
相同但與 margin
相反的索引矩陣。因此,行索引矩陣的轉置是列索引矩陣,反之亦然。
行索引矩陣 R
與其本身的叉積是一個對角矩陣,其對角線條目是 R
每列中的條目數。
給定一個帶有 perm
槽 p
的行索引矩陣 R
、一個帶有 perm
槽 q
的列索引矩陣 C
和一個具有一致尺寸的矩陣 M
,我們有
= | R %*% M | = | M[p, ] |
|
= | M %*% C | = | M[, q] |
|
= | crossprod(C, M) | = | M[q, ] |
|
= | tcrossprod(M, R) | = | M[, p] |
|
= | crossprod(R) | = | Diagonal(x=tabulate(p, ncol(R))) |
|
= | tcrossprod(C) | = | Diagonal(x=tabulate(q, nrow(C)))
|
對索引矩陣進行的操作會相應地返回 indMatrix
。這些包括兩個列索引矩陣的乘積和(等效地)列索引矩陣的column-indexing(當未刪除維度時)。 indMatrix
上的大多數其他操作將它們視為稀疏非零模式矩陣(即從虛擬類 nsparseMatrix
繼承)。因此 indMatrix
的 vector-valued 子集(例如 diag
給出的子集)始終為 "logical"
類型。
類中的對象
可以使用 new("indMatrix", ...)
形式的調用顯式創建對象,但更常見的是通過使用 as(., "indMatrix")
形式的調用強製基於 1 的整數索引向量來創建對象;請參閱下麵的“方法”。
插槽
margin
-
一個整數,1 或 2,指定矩陣是行 (1) 還是列 (2) 索引。
perm
-
從 1 開始的整數索引向量,即長度為
Dim[margin]
且元素取自1:Dim[1+margin%%2]
的向量。 Dim
、Dimnames
-
從虛擬超類
Matrix
繼承。
擴展
直接類 "sparseMatrix"
和 "generalMatrix"
。
方法
%*%
-
signature(x = "indMatrix", y = "Matrix")
和showMethods("%*%", classes = "indMatrix")
列出的其他內容:在適當的情況下作為索引操作實現的矩陣乘積。 coerce
-
signature(from = "numeric", to = "indMatrix")
:支持從正整數向量構建典型的indMatrix
。假定行索引。 coerce
-
signature(from = "list", to = "indMatrix")
:支持行和列索引的indMatrix
構造,包括長度為0的索引向量和最大值小於被索引的行或列數的索引向量。 coerce
-
signature(from = "indMatrix", to = "matrix")
:強製轉換為 logical 類型的傳統matrix
,並用FALSE
和TRUE
代替 0 和 1。 t
-
signature(x = "indMatrix")
:轉置,它是與perm
相同但與margin
相反的indMatrix
。 rowSums
、rowMeans
、colSums
、colMeans
-
signature(x = "indMatrix")
:行和列的總和及平均值。 rbind2
、cbind2
-
signature(x = "indMatrix", y = "indMatrix")
:具有相同列數的兩個行索引矩陣的按行串聯以及具有相同行數的兩個列索引矩陣的按列串聯。 - 克羅內克
-
signature(X = "indMatrix", Y = "indMatrix")
:兩個行索引矩陣或兩個列索引矩陣的克羅內克積,給出與其“interaction”對應的行或列索引矩陣。
例子
p1 <- as(c(2,3,1), "pMatrix")
(sm1 <- as(rep(c(2,3,1), e=3), "indMatrix"))
stopifnot(all(sm1 == p1[rep(1:3, each=3),]))
## row-indexing of a <pMatrix> turns it into an <indMatrix>:
class(p1[rep(1:3, each=3),])
set.seed(12) # so we know '10' is in sample
## random index matrix for 30 observations and 10 unique values:
(s10 <- as(sample(10, 30, replace=TRUE),"indMatrix"))
## Sample rows of a numeric matrix :
(mm <- matrix(1:10, nrow=10, ncol=3))
s10 %*% mm
set.seed(27)
IM1 <- as(sample(1:20, 100, replace=TRUE), "indMatrix")
IM2 <- as(sample(1:18, 100, replace=TRUE), "indMatrix")
(c12 <- crossprod(IM1,IM2))
## same as cross-tabulation of the two index vectors:
stopifnot(all(c12 - unclass(table(IM1@perm, IM2@perm)) == 0))
# 3 observations, 4 implied values, first does not occur in sample:
as(2:4, "indMatrix")
# 3 observations, 5 values, first and last do not occur in sample:
as(list(2:4, 5), "indMatrix")
as(sm1, "nMatrix")
s10[1:7, 1:4] # gives an "ngTMatrix" (most economic!)
s10[1:4, ] # preserves "indMatrix"-class
I1 <- as(c(5:1,6:4,7:3), "indMatrix")
I2 <- as(7:1, "pMatrix")
(I12 <- rbind(I1, I2))
stopifnot(is(I12, "indMatrix"),
identical(I12, rbind(I1, I2)),
colSums(I12) == c(2L,2:4,4:2))
作者
Fabian Scheipl and Uni Muenchen, building on the existing class
pMatrix
after a nice hike's conversation with
Martin Maechler. Methods for crossprod(x, y)
and
kronecker(x, y)
with both arguments inheriting from
indMatrix
were made considerably faster thanks to a suggestion
by Boris Vaillant. Diverse tweaks by Martin Maechler and
Mikael Jagan, notably the latter's implementation of margin
,
prior to which the indMatrix
class was designated only for
row index matrices.
也可以看看
相關用法
- R index-class 虛擬類“index” - 矩陣索引的簡單類
- R invertPerm 排列向量的實用程序
- R isSymmetric-methods “Matrix”包中函數“isSymmetric”的方法
- R is.null.DN Dimnames dn 是否類似於 NULL?
- R is.na-methods “矩陣”對象的 is.na()、is.finite() 方法
- R image-methods “Matrix”包中的 image() 方法
- R isTriangular-methods 測試矩陣是三角形還是對角矩陣
- R dtrMatrix-class 三角形稠密數值矩陣
- R facmul-methods 乘以矩陣因式分解的因數
- R solve-methods 函數求解矩陣包中的方法
- R updown-methods 更新和降級稀疏 Cholesky 分解
- R bdiag 構建分塊對角矩陣
- R printSpMatrix 靈活格式化和打印稀疏矩陣
- R symmetricMatrix-class 包矩陣中對稱矩陣的虛擬類
- R all.equal-methods 函數 all.equal() 的矩陣封裝方法
- R boolmatmult-methods 布爾算術矩陣乘積:%&% 和方法
- R ltrMatrix-class 三角密集邏輯矩陣
- R Hilbert 生成希爾伯特矩陣
- R nearPD 最近正定矩陣
- R lsyMatrix-class 對稱密集邏輯矩陣
- R CHMfactor-class 稀疏 Cholesky 分解
- R symmpart-methods 矩陣的對稱部分和偏斜(對稱)部分
- R sparseMatrix 從非零項構建一般稀疏矩陣
- R dgCMatrix-class 壓縮、稀疏、麵向列的數值矩陣
- R Cholesky-methods Cholesky 分解方法
注:本文由純淨天空篩選整理自R-devel大神的英文原創作品 Index Matrices。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。