0
votes

I would like to make to plot this following function:

where

Let r in (0,1) and \mu in (0,1) but in my case \mu=0.5. My code is following and I have the problem to make a plot of this function.

n <- 10
r <- seq(from=0.0, to=1, by = 0.0001)
h.star <- r*log(r/0.5)+(1-r)*log((1-r)/(1-0.5))
F.mu <- function(r) {
  if (r > 0 & r < 0.5)
     F.mu <- exp(-n*h.star)
   else (r > 0.5 & r < 1) 
    F.mu <- 1

}
plot(r, F.mu)

Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ

1
In the calling enviroment F.mu is a function, so test it with str(F.mu) just before plot(...) - jogo
try plot(x.bar, F.mu(r)), assuming x.bar is a vector of the same length of F.mu(r). - LAP
@LAP sorry I fixed it but it is not problem. - Melina
Unfortunately length of F.mu says it is 1 - Melina
@Melina Your calculation in the function is not vectorised. Eventually you can use ifelse() - jogo

1 Answers

3
votes

I suppose you want this:

n <- 10
r <- seq(from=0.0, to=1, by = 0.0001)
F.mu <- function(r, mu) {
  H.star <- r*log(r/mu) + (1-r)*log((1-r)/(1-mu))
  ifelse(r >= mu, 1, exp(-n*H.star))
}
plot(r, F.mu(r, mu=0.5))

You can reduce the needed calculations in the function:

H.star <- ifelse(r>=mu, 0, r*log(r/mu) + (1-r)*log((1-r)/(1-mu)))