r-如何在矩阵中插入概率



什么是一个可以自动填充矩阵a的好程序?

我们有colvector:

col=c(1,1,2,3,4,5,10,7,7,3,1,5,3,7,6,3,4,2,1,1,2,2,6,4,8,8,9,1,3,2)
col
[1]  1  1  2  3  4  5 10  7  7  3  1  5  3  7  6  3  4  2  1  1  2  2  6  4  8
[26]  8  9  1  3  2

我们有矩阵:

A=rbind(c(0:10),c(1,rep(0,10)),c(2,rep(0,10)),c(3,rep(0,10)),c(4,rep(0,10)),c(5,rep(0,10)),c(6,rep(0,10)),c(7,rep(0,10)),c(8,rep(0,10)),c(9,rep(0,10)),c(10,rep(0,10)))
A
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
 [1,]    0    1    2    3    4    5    6    7    8     9    10
 [2,]    1    0    0    0    0    0    0    0    0     0     0
 [3,]    2    0    0    0    0    0    0    0    0     0     0
 [4,]    3    0    0    0    0    0    0    0    0     0     0
 [5,]    4    0    0    0    0    0    0    0    0     0     0
 [6,]    5    0    0    0    0    0    0    0    0     0     0
 [7,]    6    0    0    0    0    0    0    0    0     0     0
 [8,]    7    0    0    0    0    0    0    0    0     0     0
 [9,]    8    0    0    0    0    0    0    0    0     0     0
[10,]    9    0    0    0    0    0    0    0    0     0     0
[11,]   10    0    0    0    0    0    0    0    0     0     0

矩阵A的第一列表示列向量中的先前值

矩阵A的第一行表示col矢量中的以下值在矩阵A中,我们希望替换0并存储条件概率。

通过查看col向量,我查看了所有包含1作为前一个数的实例,如1,1,2、1,5、1,1,21,3。

我提出了以下条件概率:

假设前面的数字在col向量中为1,那么下面的数字为1的概率等于:2/6。

假设前面的数字是1,下面的数字是2的概率等于:2/6。

假设前面的数字是1,下面的数字是3的概率等于:1/6。

假设前面的数字是1,下面的数字是5的概率等于:1/5。

我们使用这些值来填充矩阵A的第一行。然后我们获得了A的新版本。

A=rbind(c(0:10),c(1,2/6,2/6,1/6,0,1/6,0,0,0,0,0),c(2,rep(0,10)),c(3,rep(0,10)),c(4,rep(0,10)),c(5,rep(0,10)),c(6,rep(0,10)),c(7,rep(0,10)),c(8,rep(0,10)),c(9,rep(0,10)),c(10,rep(0,10)))
> A
      [,1]      [,2]      [,3]      [,4] [,5]      [,6] [,7] [,8] [,9] [,10] [,11]
 [1,]    0 1.0000000 2.0000000 3.0000000    4 5.0000000    6    7    8     9    10
 [2,]    1 0.3333333 0.3333333 0.1666667    0 0.1666667    0    0    0     0     0
 [3,]    2 0.0000000 0.0000000 0.0000000    0 0.0000000    0    0    0     0     0
 [4,]    3 0.0000000 0.0000000 0.0000000    0 0.0000000    0    0    0     0     0
 [5,]    4 0.0000000 0.0000000 0.0000000    0 0.0000000    0    0    0     0     0
 [6,]    5 0.0000000 0.0000000 0.0000000    0 0.0000000    0    0    0     0     0
 [7,]    6 0.0000000 0.0000000 0.0000000    0 0.0000000    0    0    0     0     0
 [8,]    7 0.0000000 0.0000000 0.0000000    0 0.0000000    0    0    0     0     0
 [9,]    8 0.0000000 0.0000000 0.0000000    0 0.0000000    0    0    0     0     0
[10,]    9 0.0000000 0.0000000 0.0000000    0 0.0000000    0    0    0     0     0
[11,]   10 0.0000000 0.0000000 0.0000000    0 0.0000000    0    0    0     0     0

我们想填第二排,第三排一直填到10号。

我手动完成了,但什么是一个好的程序可以自动填写矩阵a?

不确定这是否是使用aggregatedcast:的最有效方法

# Get data.
col=c(1,1,2,3,4,5,10,7,7,3,1,5,3,7,6,3,4,2,1,1,2,2,6,4,8,8,9,1,3,2)
# Make shifted vector and make a data frame.
index <- 1:length(col) - 1
index <- tail(index, length(col) - 1)
col.shift <-  c(col[index + 1], NA)
df <- data.frame(list("value" = col, "next.value" = col.shift))
# Count number of values per combination.
df$count <- 1
# Count number of value appearences..
df.agg.row <- aggregate(count ~ value, df, FUN = sum)
# Pivot the data.
library(reshape2)
res <- dcast(df, value ~ next.value, fun.aggregate = length)
# Get probability of number (row) being followed by number (col).
res2 <- res[, 2:11] / df.agg.row$count 

感谢大家的投入。我找到了自己问题的答案。如果有人感兴趣,那么你可以查看我的解决方案。

A=rbind(c(0:10),c(1,rep(0,10)),c(2,rep(0,10)),c(3,rep(0,10)),c(4,rep(0,10)),c(5,rep(0,10)),c(6,rep(0,10)),c(7,rep(0,10)),c(8,rep(0,10)),c(9,rep(0,10)),c(10,rep(0,10)))
    col=c(1,1,2,3,4,5,10,7,7,3,1,5,3,7,6,3,4,2,1,1,2,2,6,4,8,8,9,1,3,2)
    j=length(col)
    A
    while (j>1){
    if (col[j]==col[j-1]){A[match(col[j],A[,1]),match(col[j-1],A[1,])]=A[match(col[j],A[,1]),match(col[j-1],A[1,])]+1}else{
    if (col[j]!=col[j-1]){A[match(col[j],A[,1]),match(col[j-1],A[1,])]=A[match(col[j],A[,1]),match(col[j-1],A[1,])]+1} }
    j=j-1
    }
    A
    A=t(A)
    A=A[-1,-1]
    for (i in 1:nrow(A)){
    A[i,]=A[i,]/sum(A[1,])
    }
    A

最新更新