Home > front end >  Plot curve with multiple parameters
Plot curve with multiple parameters

Time:03-09

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?

CodePudding user response:

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

CodePudding user response:

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

  • Related