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

David Winsemius dwinsemius at comcast.net
Fri Apr 12 18:56:15 CEST 2013

```On Apr 12, 2013, at 7:58 AM, Julio Sergio wrote:

> 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) - pnorm(zz))
>   }
>
>   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,

This could be completely tangential to your problem with vectorization
of arguments to 'curve'. Feel free to ignore. The second of your
functions looks to be doing the same as the R function 'scale'. I
would have expected it to be applied first and then to have an
'unscale' operation performed to restore (but I am not aware that
there is an inbuilt function with that feature):

umat <- sweep( mat, 2, attr(m2, 'scaled:scale'), '*')

umat <- sweep( umat, 2, attr(m2, 'scaled:center'), '+')

# Or perhaps this where 'sx' was the result of a 'scale' call:

scale(sx, -attr(sx, 'scaled:center'), 1/attr(sx,'scaled:scale') ), -
attr(sx, 'scaled:center')

--

David

>
>  -Sergio.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help