0

Good afternoon !

I have the following function of the variable n :

P(n) = 1-(1-p)^n 
  • p is a parameter in the interval [0 , 1]

I'm wanting to plot this one dimentional function with several values of p , within the same plot :

p=seq(0 , 1 , 0.1 ) 

So i tried for p=0.1 and p=0.9 :

p=0.1

n <- seq(0, 100, 8) ;  y <- 1-(1-p)^n 

plot(n , y ,pch= 21,type = "o", col = "red", xlab = "n-values", ylab = "P(n) values ", main = "P(n)= 1-(1-p)^n ")

p=0.9

lines(n , 1-(1-p)^n , type="o", pch=16 , col="blue") 

legend(60 , 0.4, c("p=0.1","p=0.9"), cex=0.7, col=c("red", "blue"),pch=c(21, 16))

This gives :

enter image description here

The problem :

  • I'm searching an elegant way to plot this function for all values of p=seq(0,1,0.1) within the same plot . I'm wanting to delete the extrapolation effect ( I'm wanting to delete the pch symbols at all , the graph sould contains the curves with different colors and without the effect of extrapoling segments ).

  • I wish my question is clear.

    Thank you for help in advance !

2 Answers 2

2

You can use outer and matplot, more x values and a suitable color scheme:

n <- seq(0, 100, length.out=200)
p <- seq(0.1, 1, 0.1)
f <- function(n, p) 1-(1-p)^n
M <- outer(n, p, "f")
col <- hcl.colors(length(p), "Spectral")
matplot(n, M, type = "l", col = col, lty = 1)
legend("bottomright", legend = p, col = col, lty = 1)
Sign up to request clarification or add additional context in comments.

6 Comments

Error in hcl.colors(length(p), "Spectral") : could not find function "hcl.colors"
Which R version do you use? If it is an older one, try for example col <- topo.colors(length(p))
"R version 3.5.1 (2018-07-02)"
These are the codes of topo colors. What do you want to tell with this?
Nothing , i updated now my r version . Thank you for your precious help !
|
2

Another simply way is replacing

lines(n , 1-(1-p)^n , type="o", pch=16 , col="blue") 

with

p = seq(0,1,0.1)
invisible(lapply(p, function(i) {lines(n, 1-(1-i)^n, type="o", col=i*10)}))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.