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


R indMatrix-class 索引矩陣

R語言 indMatrix-class 位於 Matrix 包(package)。

說明

indMatrix 類是行和列索引矩陣的類,存儲為從 1 開始的整數索引向量。行(列)索引矩陣是以標準單位向量為行(列)的矩陣。當將觀測值映射到離散的協變量值集時,此類矩陣非常有用。

將左側矩陣乘以行索引矩陣相當於對其行進行索引,即對行“with replacement”進行采樣。類似地,將右側矩陣乘以列索引矩陣相當於對其列進行索引。事實上,此類產品在 Matrix 中作為索引操作實現;請參閱下麵的“詳細信息”。

行和列為標準單位向量的矩陣稱為置換矩陣。這種特殊情況由 pMatrix 類指定,它是 indMatrix 的直接子類。

細節

索引矩陣的轉置是與 perm 相同但與 margin 相反的索引矩陣。因此,行索引矩陣的轉置是列索引矩陣,反之亦然。

行索引矩陣 R 與其本身的叉積是一個對角矩陣,其對角線條目是 R 每列中的條目數。

給定一個帶有 permp 的行索引矩陣 R 、一個帶有 permq 的列索引矩陣 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] 的向量。

DimDimnames

從虛擬超類 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,並用 FALSETRUE 代替 0 和 1。

t

signature(x = "indMatrix") :轉置,它是與 perm 相同但與 margin 相反的 indMatrix

rowSumsrowMeanscolSumscolMeans

signature(x = "indMatrix"):行和列的總和及平均值。

rbind2cbind2

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.

也可以看看

置換矩陣的子類pMatrix,索引矩陣的特殊情況;非零模式矩陣的虛擬類nMatrix及其子類。

相關用法


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