Reputation:
I am extremely new with R, I have an assignment that I'm working on that I am having a lot of trouble with. I have defined a discrete probability distribution:
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
Now I have to work on this question:
Consistent with other distributions available in R, create a family of support functions for your probability distributuon:
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.
If someone could help me get started on writing these functions, it would be greatly appreciated!
Upvotes: 0
Views: 456
Reputation: 81693
Firstly, read the data:
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")
Transform the fractions (represented as strings) to numeric values:
dat$P <- sapply(strsplit(dat$P, "/"), function(x) as.numeric(x[1]) / as.numeric(x[2]))
The functions:
# 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])
}
Note. Since some probabilities are zero, some digits have identical cumulative probabilities. In these cases the lowest digit is returned by function qsidp
.
# generate n random digits based on your probability distribution.
rsidp <- function(n) {
with(dat, sample(s, n, TRUE, P))
}
Upvotes: 1