0

我对 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.

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

4

1 回答 1

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))
}
于 2013-02-16T05:36:51.890 回答