Wednesday, 15 July 2015

Statistics : binomial distribution and simple graph in R -



Statistics : binomial distribution and simple graph in R -

i'm trying create simple graph binomial distribution in r.

so question "there 20 patients, , probability of operating on 4 patients successfully, (given probability = 0.8)."

what did is

x <- rbinom(4, size= 20, prob = 0.8) y <- pbinom(x, size = 20, prob = 0.8) plot(y, type="l")

but i'm not sure if right way of graphing it..

in general, question visualisation comes when question like:

what's probability of having "at least" 16 successes out of 20 operations given probability of success 0.8?

this can done using binomial formula is:

p(x=k) = choose(n, k) * .8^k * .2^(n-k) # equivalent dbinom(k, n, prob=0.8)

we need same k = 16..20 , sum these values probability of sucess in @ to the lowest degree 16 out of 20. done using dbinom as:

sum(dbinom(16:20, 20, prob=0.8)) # 0.6296483

note probability of @ to the lowest degree 4 successes @ success rate(0.8) 1. is, have @ to the lowest degree 4 successes. that's why chose relatively high amount of successes.

to plot (using ggplot2):

df <- data.frame(x=1:20, prob=dbinom(1:20, 20, prob=0.8)) require(ggplot2) ggplot(data=dd, aes(x=x,y=prob)) + geom_line() + geom_ribbon(data=subset(dd,x>=16 & x<=20),aes(ymax=prob),ymin=0, fill="red", colour = na, alpha = 0.5)

this gives like:

hope helps.

r

No comments:

Post a Comment