[R] A strange behaviour in the graphical function "curve"

Julio Sergio juliosergio at gmail.com
Fri Apr 12 16:58:18 CEST 2013


Berend Hasselman <bhh <at> xs4all.nl> writes:

> 
> Your function miBeta returns a scalar when the argument mu is a vector.
> Use Vectorize to vectorize it. Like this
> 
>   VmiBeta <- Vectorize(miBeta,vectorize.args=c("mu"))
>   VmiBeta(c(420,440))
> 
> and draw the curve with this
> 
>   curve(VmiBeta,xlim=c(370,430), xlab="mu", ylab="L(mu)")
> 
> Berend
> 

Taking into account what you have pointed out, I reprogrammed my function 
as follows, as an alternative solution to yours:

   zetas <- function(alpha) {z <- qnorm(alpha/2); c(z,-z)}

   # First transformation function
   Tzx <- function(z, sigma_p, mu_p) sigma_p*z + mu_p

   # Second transformation function
   Txz <- function(x, sigma_p, mu_p) (x - mu_p)/sigma_p

   BetaG <- function(mu, alpha, n, sigma, mu_0) {
     lasZ <- zetas(alpha) # Zs corresponding to alpha
     sigma_M <- sigma/sqrt(n) # sd of my distribution
     lasX <- Tzx(lasZ, sigma_M, mu_0) # Transformed Zs into Xs
     # Now I consider mu to be a vector composed of m's 
     NewZ <- lapply(mu, function(m) Txz(lasX, sigma_M, m))
     # NewZ is a list, the same length as mu, with 2D vectors
     # The result will be a vector, the same length as mu (and NewZ)
     sapply(NewZ, function(zz) pnorm(zz[2]) - pnorm(zz[1]))
   }

   miBeta <- function(mu) BetaG(mu, 0.05, 36, 48, 400)

   plot(miBeta,xlim=c(370,430), xlab="mu", ylab="L(mu)")

I hope this is useful to people following this discussion,

  -Sergio.



More information about the R-help mailing list