创建一个函数来计算R中的auc

时间:2013-02-16 04:09:19

标签: r distribution

我对R非常陌生,我有一项工作,我正在努力,我遇到了很多麻烦。我已经定义了离散概率分布:

s   P(s)
0   1/9
1   4/9
2   1/9
3   0/9
4   1/9
5   0/9
6   0/9
7   1/9
8   0/9
9   1/9

现在我必须处理这个问题:

与R中可用的其他发行版一致,创建一个 概率分布的支持函数族:

f  =  dsidp(d)      # pmf - the height of the curve/bar for digit d
p  =  psidp(d)      # cdf - the probability of a value being d or less
d  =  qsidp(p)      # icdf - the digit corresponding to the given 
                    # cumulative probability p
d[]  =  rsidp(n)    # generate n random digits based on your probability distribution.

如果有人可以帮助我开始编写这些功能,我们将不胜感激!

1 个答案:

答案 0 :(得分:1)

首先,阅读数据:

dat <- read.table(text = "s   P(s)
0   1/9
1   4/9
2   1/9
3   0/9
4   1/9
5   0/9
6   0/9
7   1/9
8   0/9
9   1/9", header = TRUE, stringsAsFactors = FALSE)

names(dat) <- c("s", "P")

将分数(表示为字符串)转换为数值:

dat$P <- sapply(strsplit(dat$P, "/"), function(x) as.numeric(x[1]) / as.numeric(x[2]))

功能:

# pmf - the height of the curve/bar for digit d
dsidp <- function(d) {
  with(dat, P[s == d])
}

# cdf - the probability of a value being d or less
psidp <- function(d) {
  with(dat, cumsum(P)[s == d])
}    

# icdf - the digit corresponding to the given cumulative probability p
qsidp <- function(p)  {
  with(dat, s[sapply(cumsum(P), all.equal, p) == "TRUE"][1])
}   

请注意。由于某些概率为零,因此某些数字具有相同的累积概率。在这些情况下,函数qsidp返回最低位。

# generate n random digits based on your probability distribution.
rsidp <- function(n) {
  with(dat, sample(s, n, TRUE, P))
}