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) } , 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) } -- View this message in context: http://www.nabble.com/Newbie-that-don%27t-understand-R-code-tp25009034p25009034.html Sent from the R help mailing list archive at Nabble.com.
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){ > ................. > phi.inverse <- switch(name, > clayton = function(t, theta) > //where is t comming from, what is phi inverse > { > (1 + t)^(-1/theta) > } > , > 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) > } >First of all: R comments start with # and not // 1. t comes from the argument of the function object returned when <name> has the value clayton clayton = function(t, theta) ####where is t comming from, what is phi inverse { (1 + t)^(-1/theta) } 2. In phi.inverse <- switch(....) the value returned by the switch expression is assigned to phi.inverse. The switch expression appears to return a function object depending on the value of the <name> argument to rAC. 3. the code at the end of the rAC function contains two errors (spaces): log value should be logvalue T RUE should be TRUE After corrections it appears to work. Berend -- View this message in context: http://www.nabble.com/Newbie-that-don%27t-understand-R-code-tp25009034p25011959.html Sent from the R help mailing list archive at Nabble.com.
Hi, Comments inline and at end: 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 Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact