如何绘制多个用户随时间推移对带宽消耗预测的偏差?

机器算法验证 数据可视化
2022-04-04 18:38:11

俗话说,一张图值一千字。我有一些数据,我需要将它们放入正确的图表表示中。

工作场景是这样的:

一些用户必须预测他们在 13 周内将消耗多少互联网带宽。用户可以组成群组以购买更便宜的互联网带宽。如果他们消费的比他们说的多,他们将不得不支付更多的钱。想法是尽可能接近所预测的。

所以,我的数据是这样的:

user1 = {-0.075, -0.09, 0.32, -0.242, -0.368, -0.401, -0.73, -0.367, -0.294, -0.043, 1.296, 0.075, -0.373}  
user2 = {-0.009, -0.013, -0.01, -0.008, -0.008, -0.01, -0.005, -0.02, 0.287, 0.345, -0.104, -0.324, 0.144}   
user3 = {-0.197, -0.271, -0.153, -0.621, -0.549, -0.09, 1.745, 0.436, -0.271, 0.093, 0.085, 0.211, 0.331}  
user4 = {-0.005, -0.005, -0.006, -0.006, -0.006, -0.005, -0.006, -0.086, -0.171, -0.15, -0.175, -0.067, 0.078}  
user5 = {-0.223, -0.048, -0.129, 0.14, -0.535, -0.29, 0.51, 0.801, 0.521, 0.482, -0.105, 5.082, 5.516}  
group = {-0.509, -0.427, 0.022, -0.737, -1.466, -0.796, 1.514, 0.764, 0.072, 0.727, 0.997, 4.977, 5.696} 

负值表示用户消耗的比他预测的少。正值意味着用户消费的比他说的多。组数组中的值是预测值与每个用户消费值之间的差值之和。

问题:

您有什么想法,关于如何以易于阅读的方式在图表中表示这些数据?

例如,您可以注意到,与 user2 和 user5 相比,user2 和 user4 的预测和消费之间的差异非常小。

包含先前数据的 CVS 文件在此处

2个回答

您可能希望将其绘制为与预测值的累积偏差。这是否有意义取决于计费/分析期是什么:如果团队必须在每个季度保持低于某个限制,这将使他们能够看到他们是否正在实现该目标。但是,如果他们的帐户余额每周都重置为零,那么这种图表的用处就会降低。

或者,超出曲线可能有用。这会按大小对每个用户的每周偏差进行排序。它允许评估每个用户有多少时间高于或低于他们的目标。在下面的图表中,您可以看到,尽管除了user5超过 50% 的时间之外,所有用户都低于他们的限制,但整个组几乎有 60% 的时间超过了他们的限制。

对于显示数据的完全不同的方式,瀑布图可能会让您感兴趣。它显示了每周值的细分,但有五个变量,它已经变得非常混乱:


这是图表的 R 代码。

累积偏差

    library(ggplot2)

    data <- cumsum(
        data.frame(
            user1 = c(-0.075, -0.09, 0.32, -0.242, -0.368, -0.401, -0.73, -0.367, -0.294, -0.043, 1.296, 0.075, -0.373),  
            user2 = c(-0.009, -0.013, -0.01, -0.008, -0.008, -0.01, -0.005, -0.02, 0.287, 0.345, -0.104, -0.324, 0.144),   
            user3 = c(-0.197, -0.271, -0.153, -0.621, -0.549, -0.09, 1.745, 0.436, -0.271, 0.093, 0.085, 0.211, 0.331),
            user4 = c(-0.005, -0.005, -0.006, -0.006, -0.006, -0.005, -0.006, -0.086, -0.171, -0.15, -0.175, -0.067, 0.078),  
            user5 = c(-0.223, -0.048, -0.129, 0.14, -0.535, -0.29, 0.51, 0.801, 0.521, 0.482, -0.105, 5.082, 5.516),
            group = c(-0.509, -0.427, 0.022, -0.737, -1.466, -0.796, 1.514, 0.764, 0.072, 0.727, 0.997, 4.977, 5.696)
        )
    )

    data$week=c(1:13)

    molten <- melt(data,id.vars="week")

    p <- ggplot(molten, aes(x=week, y=value, colour=variable)) + 
            geom_line(aes(group=variable)) + 
            scale_colour_hue(h=c(100,250)) +
            geom_line(aes(y=molten$value[molten$variable=="group"]), colour="orange", size=1.5) +
            theme_bw() + opts(legend.position = "none") +
            geom_text(data=molten[molten$week==13,], aes(label=variable), colour="black", hjust=-0.2, size=4) +
            xlim(0,13.9) + xlab("Week") + ylab("Cumulated Deviation")
    print(p)

超越曲线

library(ggplot2)

data <- data.frame(
        user1 = c(-0.075, -0.09, 0.32, -0.242, -0.368, -0.401, -0.73, -0.367, -0.294, -0.043, 1.296, 0.075, -0.373),  
        user2 = c(-0.009, -0.013, -0.01, -0.008, -0.008, -0.01, -0.005, -0.02, 0.287, 0.345, -0.104, -0.324, 0.144),   
        user3 = c(-0.197, -0.271, -0.153, -0.621, -0.549, -0.09, 1.745, 0.436, -0.271, 0.093, 0.085, 0.211, 0.331),
        user4 = c(-0.005, -0.005, -0.006, -0.006, -0.006, -0.005, -0.006, -0.086, -0.171, -0.15, -0.175, -0.067, 0.078),  
        user5 = c(-0.223, -0.048, -0.129, 0.14, -0.535, -0.29, 0.51, 0.801, 0.521, 0.482, -0.105, 5.082, 5.516),
        group = c(-0.509, -0.427, 0.022, -0.737, -1.466, -0.796, 1.514, 0.764, 0.072, 0.727, 0.997, 4.977, 5.696)
)

data_sorted <- data.frame(apply(data,2,sort,decreasing=T))
data_sorted$exceedance_prob=c(0:12)/12


molten <- melt(data_sorted,id.vars="exceedance_prob")

p <- ggplot(molten, aes(x=exceedance_prob, y=value, colour=variable)) + 
        geom_line(aes(group=variable)) + 
        scale_colour_hue(h=c(100,250)) +
        geom_line(aes(y=molten$value[molten$variable=="group"]), colour="orange", size=1.5) +
        theme_bw() + opts(legend.position = "none") +
        geom_text(data=molten[molten$exceedance_prob==0,], aes(label=variable), colour="black", hjust=1.2, size=4) +
        xlim(-0.1,1) + xlab("Exceedance Probability") + ylab("Deviation")
print(p)

超越曲线

library(ggplot2)

data <- data.frame(
                user1 = c(-0.075, -0.09, 0.32, -0.242, -0.368, -0.401, -0.73, -0.367, -0.294, -0.043, 1.296, 0.075, -0.373),  
                user2 = c(-0.009, -0.013, -0.01, -0.008, -0.008, -0.01, -0.005, -0.02, 0.287, 0.345, -0.104, -0.324, 0.144),   
                user3 = c(-0.197, -0.271, -0.153, -0.621, -0.549, -0.09, 1.745, 0.436, -0.271, 0.093, 0.085, 0.211, 0.331),
                user4 = c(-0.005, -0.005, -0.006, -0.006, -0.006, -0.005, -0.006, -0.086, -0.171, -0.15, -0.175, -0.067, 0.078),  
                user5 = c(-0.223, -0.048, -0.129, 0.14, -0.535, -0.29, 0.51, 0.801, 0.521, 0.482, -0.105, 5.082, 5.516),
                group = c(-0.509, -0.427, 0.022, -0.737, -1.466, -0.796, 1.514, 0.764, 0.072, 0.727, 0.997, 4.977, 5.696)
        )

originaldata <- data
data <- data[,1:5]

lower<-as.data.frame(t(apply(data,1,"cumsum")))

data$week=c(1:13)
lower$week=c(1:13)


molten <- melt(data,id.vars="week")
moltenlower <- melt(lower,id.vars="week")
molten$lower <- moltenlower$value

p <- ggplot(molten, aes(x=week, y=value, fill=variable)) + 
        geom_rect(aes(
            xmin=week+as.numeric(variable)/6-0.5,
            xmax=week+as.numeric(variable)/6-0.35,
            ymin=lower,
            ymax=lower-value,
            group=variable),
            colour="black") +
        scale_fill_brewer()+
        theme_bw() + 
        xlim(0.5,13.5) + xlab("Week") + ylab("Cumulated Deviation") + ylim(-2,2)

print(p)

对于这些特定数据,我会制作一个线图,如下所示。

线图

这是我使用的 R 代码:

dat <- data.frame(user1 = c(-0.075, -0.09, 0.32, -0.242, -0.368, -0.401, -0.73, -0.367, -0.294, -0.043, 1.296, 0.075, -0.373),
                  user2 = c(-0.009, -0.013, -0.01, -0.008, -0.008, -0.01, -0.005, -0.02, 0.287, 0.345, -0.104, -0.324, 0.144),
                  user3 = c(-0.197, -0.271, -0.153, -0.621, -0.549, -0.09, 1.745, 0.436, -0.271, 0.093, 0.085, 0.211, 0.331),
                  user4 = c(-0.005, -0.005, -0.006, -0.006, -0.006, -0.005, -0.006, -0.086, -0.171, -0.15, -0.175, -0.067, 0.078),
                  user5 = c(-0.223, -0.048, -0.129, 0.14, -0.535, -0.29, 0.51, 0.801, 0.521, 0.482, -0.105, 5.082, 5.516))

plot(dat[,1], xlab="Time", ylab="Outcome", ylim=range(dat),
     type="l", lwd=2, xlim=c(1, nrow(dat)+2), las=1, xaxt="n")
axis(side=1, at=seq(1, 13, by=2))
abline(h=0, lty=2, col="gray40")
col <- c("black", "blue", "red", "orange", "green")
for(i in 2:5)
  lines(dat[,i], col=col[i], lwd=2)
text(13.2, dat[13,]+c(0, 0.1, 0.1, -0.1, 0), paste("user", 1:5, sep=""), 
     col=col, adj=c(0, 0.5))