我想绘制随时间变化的转换概率。
The dimension of the ["Activities"][1] matrix is ncol=144 and nrows=16533; act1_1...ac1_144 are time-steps, and time is represented in 10 minutes intervals (e.g. act1_1 = 4.10am; act1_2=4.20am..). Time start from 4am (act1_1) and ends at act1_144(4am).The columns are filled in with different activities, such 2=sleep, 48=watching Tv, 5=eating, etc.
使用此功能,我成功进行了calculate the transition probabilities between个活动(活动矩阵)。
我该怎么做?
谢谢
这是我想要的情节
答案 0 :(得分:0)
要绘制过渡概率随时间的变化情况,(从定义上来说)您需要从一个动作a
到另一个动作b
的过渡的多个观察结果。
一旦有了多个交易概率矩阵,绘制它们随时间的变化实际上非常简单。在下面的示例中,大多数代码实际上都专门用于(重新)创建数据和不同的转移概率矩阵。
library(ggplot2)
num_samples <- 1000
num_actions <- 20
# 1. generate activities dataframe (dropped ID column):
activ <- data.frame(v1 = sample(1:num_actions, num_samples, replace = TRUE),
v2 = sample(1:num_actions, num_samples, replace = TRUE),
v3 = sample(1:num_actions, num_samples, replace = TRUE),
v4 = sample(1:num_actions, num_samples, replace = TRUE),
v5 = sample(1:num_actions, num_samples, replace = TRUE),
v6 = sample(1:num_actions, num_samples, replace = TRUE),
v7 = sample(1:num_actions, num_samples, replace = TRUE))
num_transp <- ncol(activ) - 1
# 2. calculate transition probabilities for each time step:
l_transp <- vector("list", num_transp)
for (t in 1:num_transp){
transp <- matrix(0, nrow = num_actions, ncol = num_actions)
data = activ[, t:(t+1)]
for (action in 1:num_actions){
rows_to_keep <- data[,1] == action
counts <- table(data[rows_to_keep,])
probs <- as.data.frame(counts/sum(rows_to_keep))
follow_actions <- as.integer(as.character(probs[,2]))
transp[action, follow_actions] <- probs$Freq
}
l_transp[[t]] <- transp
}
# 3. get development of transition probability from action 2 to action 3 over time:
to_plot <- vector("numeric", num_transp)
for (i in 1:num_transp){
to_plot[i] <- l_transp[[i]][2, 3]
}
# plot development of transition probability from action 2 to action 3 over time:
ggplot(data.frame(x=1:num_transp, y=to_plot), aes(x=x, y=y)) +
geom_line() +
xlab("time") +
ylab("transition probability")
的信息
看起来很像一个场景,随着时间的推移,随机地,独立地选择20个不同的动作,人们会期待什么。