计算从X,Y点的向量生成的曲线的第五分位数

时间:2019-03-16 00:23:09

标签: r

我的曲线如下:

Curves

这些曲线是使用名为discreteRV的库生成的。

library(discreteRV)

placebo.rate <- 0.5
mmm.rate <- 0.3
mmm.power <- power.prop.test(p1 = placebo.rate, p2 = mmm.rate, power = 0.8, alternative = "one.sided")
n <- as.integer(ceiling(mmm.power$n))
patients <- seq(from = 0, to = n, by = 1)
placebo_distribution <- dbinom(patients, size = n, prob = placebo.rate)
mmm_distribution <- dbinom(patients, size = n, prob = mmm.rate)

get_pmf <- function(p1, p2) {
  X1 <- RV(patients,p1, fractions = F)
  X2 <- RV(patients,p2, fractions = F)
  pmf <- joint(X1, X2, fractions = F)
  return(pmf)
}
extract <- function(string) {
  ints <- unlist(strsplit(string,","))
  x1 <- as.integer(ints[1])
  x2 <- as.integer(ints[2])
  return(x1-x2)
}
diff_prob <- function(pmf) {
  diff <- unname(sapply(outcomes(pmf),FUN = extract)/n)
  probabilities <- unname(probs(pmf))
  df <- data.frame(diff,probabilities)
  df <- aggregate(. ~ diff, data = df, FUN = sum)
  return(df)
}
most_likely_rate <- function(x) {
  x[which(x$probabilities == max(x$probabilities)),]$diff
}

mmm_rate_diffs <- diff_prob(get_pmf(mmm_distribution,placebo_distribution))
placebo_rate_diffs <- diff_prob(get_pmf(placebo_distribution,placebo_distribution))
plot(mmm_rate_diffs$diff,mmm_rate_diffs$probabilities * 100, type = "l", lty = 2, xlab = "Rate difference", ylab = "# of trials per 100", main = paste("Trials with",n,"patients per treatment arm",sep = " "))
lines(placebo_rate_diffs$diff, placebo_rate_diffs$probabilities * 100, lty = 1, xaxs = "i")
abline(v = c(most_likely_rate(placebo_rate_diffs), most_likely_rate(mmm_rate_diffs)), lty = c(1,2))
legend("topleft", legend = c("Alternative hypothesis", "Null hypothesis"), lty = c(2,1))

基本上,我取了两个二项式离散随机变量,创建了联合概率质量函数,确定了任何给定速率差的概率,然后将它们作图以证明如果零假设为真或替代假设为真时这些速率差的分布在100项相同的试验中都是正确的。

现在我想说明原假设曲线上的5%百分位数。不幸的是,我不知道该怎么做。如果仅使用quantile(x = placebo_rate_diffs$diff, probs = 0.05,我将得到-0.377027。查看图表是不正确的。我想像使用pbinom()一样计算第5个百分位数,但我不知道如何使用本质上仅由xy向量创建的图形来做到这一点。

也许我可以将这两条曲线近似为二项式,因为它们似乎是二项式的,但是我仍然不确定如何做到这一点。

任何帮助将不胜感激。

0 个答案:

没有答案