# [R] Newbie that don't understand R code

Steve Lianoglou mailinglist.honeypot at gmail.com
Tue Aug 18 17:02:18 CEST 2009

```Hi,

On Aug 17, 2009, at 11:36 AM, kfcnhl wrote:

> I got some R code that I don't understand.
>
> Question as comment in code
> //where is t comming from, what is phi inverse
>
> rAC <- function(name, n, d, theta){
> #generic function for Archimedean copula simulation
> illegalpar <- switch(name,
> clayton = (theta < 0),
> gumbel = (theta < 1),
> frank = (theta < 0),
> BB9 = ((theta[1] < 1) | (theta[2] < 0)),
> GIG = ((theta[2] < 0) | (theta[3] < 0) | ((theta[1]>0) &
> (theta[3]==0)) |
> ((theta[1]<0) & (theta[2]==0))))
> if(illegalpar)
> stop("Illegal parameter value")
> independence <- switch(name,
> clayton = (theta == 0),
> gumbel = (theta == 1),
> frank = (theta == 0),
> BB9 = (theta[1] == 1),
> GIG=FALSE)
> U <- runif(n * d)
> U <- matrix(U, nrow = n, ncol = d)
> if(independence)
> return(U)
> Y <- switch(name,
> clayton = rgamma(n, 1/theta),
> gumbel = rstable(n, 1/theta) * (cos(pi/(2 * theta)))^theta,
> frank = rFrankMix(n, theta),
> BB9 = rBB9Mix(n, theta),
> GIG = rGIG(n,theta[1],theta[2],theta[3]))
> Y <- matrix(Y, nrow = n, ncol = d)
> phi.inverse <- switch(name,
> clayton = function(t, theta)
> //where is t comming from, what is phi inverse
> {
> (1 + t)^(-1/theta)
> }

t isn't coming from anywhere, it's just a parameter to the function
definition here.

phi.inverse will be THE FUNCTION returned by the switch statement
here, depending on the value of ``name``.

> ,
> gumbel = function(t, theta)
> {
> exp( - t^(1/theta))
> }
> ,
> frank = function(t, theta)
> {
> (-1/theta) * log(1 - (1 - exp( - theta)) * exp( - t))
> }
> ,
> BB9 = function(t, theta)
> {
> exp( - (theta[2]^theta[1] + t)^(1/theta[1]) + theta[2])
> }
> ,
> GIG = function(t, theta)
> {
> lambda <- theta[1]
> chi <- theta[2]
> psi <- theta[3]
> if (chi==0)
> out <- (1+2*t/psi)^(-lambda)
> else if (psi==0)
> out <- 2^(lambda+1)*exp(besselM3(lambda,sqrt(2*chi*t),log
> value=TRUE)-lambda*log(2*chi*t)/2)/gamma(-lambda)
> else
> out <- exp(besselM3(lambda,sqrt(chi*(psi+2*t)),logvalue=T
> RUE)+lambda*log(chi*psi)/2-
> besselM3(lambda,sqrt(chi*psi),logvalue=TRUE)-lambda*log(chi*(psi
> +2*t))/2)
> out
> }
> )
> phi.inverse( - log(U)/Y, theta)

phi.inverse was defined as the function returned by the switch
statement. ``- log(U)/Y`` is passed in to the function's ``t`` argument.

Does that help?

-steve
--
Steve Lianoglou