0

我编写了代码来绘制 A/B 测试变化的密度数据。我想通过对每条曲线下方的区域进行着色(填充稍微透明)来改善视觉效果。我目前正在使用 matplot,但了解 ggplot 可能是更好的选择。

有任何想法吗?谢谢。

# Setup data frame - these are results from an A/B experiment
conv_data = data.frame(
            VarNames = c("Variation 1", "Variation 2", "Variation 3") # Set variation names
            ,NumSuccess = c(1,90,899) # Set number of successes / conversions
            ,NumTrials = c(10,100,1070) # Set number of trials
            )
conv_data$NumFailures = conv_data$NumTrials - conv_data$NumSuccess # Set number of failures [no conversions]
num_var = NROW(conv_data) # Set total number of variations
plot_col = rainbow(num_var) # Set plot colors

get_density_data <- function(n_var, s, f) {
    x = seq(0,1,length.out=100) # 0.01,0.02,0.03...1
    dens_data = matrix(data = NA, nrow=length(x), ncol=(n_var+1))
    dens_data[,1] = x

    # set density data
    for(j in 1:n_var) {
        # +1 to s[], f[] to ensure uniform prior
        dens_data[,j+1] = dbeta(x, s[j]+1, f[j]+1)
    }
    return(dens_data)
}

density_data = get_density_data(num_var, conv_data$NumSuccess, conv_data$NumFailures)

matplot(density_data[,1]*100, density_data[,-1], type = "l", lty = 1, col = plot_col, ylab = "Probability Density", xlab = "Conversion Rate %", yaxt = "n")
legend("topleft", col=plot_col, legend = conv_data$VarNames, lwd = 1)

这将产生以下图: 在此处输入图像描述

4

2 回答 2

1
# Setup data frame - these are results from an A/B experiment
conv_data = data.frame(
  VarNames = c("Variation 1", "Variation 2", "Variation 3") # Set variation names
  ,NumSuccess = c(1,90,899) # Set number of successes / conversions
  ,NumTrials = c(10,100,1070) # Set number of trials
)
conv_data$NumFailures = conv_data$NumTrials - conv_data$NumSuccess # Set number of failures [no conversions]
num_var = NROW(conv_data) # Set total number of variations
plot_col = rainbow(num_var) # Set plot colors

get_density_data <- function(n_var, s, f) {
  x = seq(0,1,length.out=100) # 0.01,0.02,0.03...1
  dens_data = matrix(data = NA, nrow=length(x), ncol=(n_var+1))
  dens_data[,1] = x

  # set density data
  for(j in 1:n_var) {
    # +1 to s[], f[] to ensure uniform prior
    dens_data[,j+1] = dbeta(x, s[j]+1, f[j]+1)
  }
  return(dens_data)
}

density_data = get_density_data(num_var, conv_data$NumSuccess, conv_data$NumFailures)

matplot(density_data[,1]*100, density_data[,-1], type = "l",
        lty = 1, col = plot_col, ylab = "Probability Density",
        xlab = "Conversion Rate %", yaxt = "n")
legend("topleft", col=plot_col, legend = conv_data$VarNames, lwd = 1)

## and add this part
for (ii in seq_along(plot_col))
  polygon(c(density_data[, 1] * 100, rev(density_data[, 1] * 100)),
          c(density_data[, ii + 1], rep(0, nrow(density_data))),
          col = adjustcolor(plot_col[ii], alpha.f = .25))

在此处输入图像描述

于 2015-04-07T20:00:48.327 回答
0

能够回答自己的问题:

df = as.data.frame(t(conversion_data))
dfs = stack(df)
ggplot(dfs, aes(x=values)) + geom_density(aes(group=ind, colour=ind, fill=ind), alpha=0.3)
于 2015-04-07T19:58:58.553 回答