3

I want to make a plot using curve for multiple parameters.

For example, say I have the following distribution function for the binomial distribution:

enter image description here

I can plot curve for a probability mass function likeso:

curve(factorial(10)/(factorial(5)*factorial(5))*y^5*(1-y)^5, from=0, to = 1)

Because we want 0 < y < 1, however this won't work for multiple variables as from = 0, to = 1 will only apply for a single variable.

So - how can I get curve to work for something like:

curve(factorial(10)/(factorial(10-x)*factorial(x))*y^x*(1-y)^{10-x}, from=0, to = 1)

But also to indicate the the distribution function for x is less than or equal to 5, so from = 0, to = 5?

2 Answers 2

1

I guess you can use dbinom directly

curve(dbinom(5, 10, y), xname = "y")

enter image description here

or if you need to vary x, you can try

sapply(0:10, function(k) curve(dbinom(k, 10, y), xname = "y", add = TRUE, ylim = c(0, 1)))

enter image description here

1
  • 1
    pbinom works better in my case as it's specifically for the distribution function. Thanks for showing me that it works! because there are many available distributions for this on R that I can implement alongside this one. Commented Mar 9, 2022 at 10:22
1

You could loop over a sequence from 0 to 5.

curve(factorial(10)/(factorial(5)*factorial(5))*x^5*(1-x)^5, from=0, to=1, 
      ylim=c(0, 1), type='n')
invisible(lapply(seq.int(.005, 5, .005), \(y) 
       curve(factorial(10)/(factorial(10 - x)*factorial(x))*y^x*(1 - y)^{10 - x}, 
             add=TRUE))
)

The invisible avoids cluttering of the console.

enter image description here

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.