[英]How to insert probabilities into a matrix?
什么是可以自動化並填寫矩陣A的好的程序?
我們有col向量:
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的第一列代表col向量中的先前值
矩陣A的第一行表示col向量中的以下值。在矩陣A中,我們要替換0並存儲條件概率。
通過查看col向量,我可以查看所有涉及1的實例,例如1,1,2,1,5,1,1,2 1,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?
不確定這是否是最有效的方法,使用aggregate
和dcast
:
# 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.