File size: 490 Bytes
8570062
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

pmf <- function(a, b, pmf) {
  # To convert pmf to a function
  pmf <- gsub(" ", "", pmf)
  pmf <- substr(pmf, start=6, stop=nchar(pmf))
  funcion <- function(x) eval(parse(text=pmf))
  
  # To obtain probs and cumulative
  x_vals <- a:b
  n <- length(x_vals)
  probs <- numeric(n)
  for (i in 1:n) 
    probs[i] <- funcion(x_vals[i])
  suma <- sum(probs)
  cumul_probs <- cumsum(probs)
  
  list(x_vals=x_vals, 
       cumul_probs=cumul_probs,
       probs=probs, 
       suma=suma)
}