Hi, I am trying to apply *sapply* to functions within functions within functions to do a Monte Carlo experiment: take Y sample of size X based on cell probabilities (using Brian Ripley's suggestion for drawing from contingency tables), for each calculate a vector of statistics, calculate bootstrap std errs with R replications and their means and SD. Store results in a matrix for further use. The problem is how to pass 3 arguments returned as c() by a fucntion to another function that uses them using: sapply(rep(c(), R), anotherfucntion). This does not seem to work, possibly because I am passing a c() argument where 3 separate arguemnts are needed. What is a good way to do this? I am trying to avoid *for* loops. Anupam. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
The problem is that c() is of length zero and repeating such a vector R times will only give you a vector of length zero, e.g. length(rep(c(), R)) == 0. I think you have observed this already. The only way to store a repetition of empty vectors or NULL values is by using the data type list, e.g. args <- rep(list(c()), R) Then sapply(args, FUN=anotherfunction) will work, but I think you are better of with lapply(args, FUN=anotherfunction) I am not sure if you wrote the function that returns c() or not. If not, you can always convert a vector to a list and then make sure it has the right length, i.e. R, by doing: if (length(vec) == 0) args <- rep(list(vec), length.out=R) else args <- rep(as.list(vec), length.out=R) Warning, only using as.list() won't work if length(vec) == 0, but if you know that it is not a list that is returned, which I guess you know, all you have to do is: args <- rep(list(vec), length.out=R) Finally, if R is not way too big I actually think a for loop would be equally fast if that is your concern. Someone correct me if I am wrong, but for loops are not penalized that much in R as for instance in Matlab. Cheers Henrik Bengtsson> -----Original Message----- > From: owner-r-help at stat.math.ethz.ch > [mailto:owner-r-help at stat.math.ethz.ch] On Behalf Of > TyagiAnupam at aol.com > Sent: den 13 november 2002 13:02 > To: r-help at stat.math.ethz.ch > Subject: [R] using list to pass argument to function > > > Hi, > > I am trying to apply *sapply* to functions within functions > within functions > to do a Monte Carlo experiment: take Y sample of size X based on cell > probabilities (using Brian Ripley's suggestion for drawing > from contingency > tables), for each calculate a vector of statistics, calculate > bootstrap std > errs with R replications and their means and SD. Store > results in a matrix > for further use. The problem is how to pass 3 arguments > returned as c() by a > fucntion to another function that uses them using: > sapply(rep(c(), R), > anotherfucntion). This does not seem to work, possibly > because I am passing a > c() argument where 3 separate arguemnts are needed. What is a > good way to do > this? I am trying to avoid *for* loops. > > Anupam. > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. > -.-.-.-.-.-.-.-.- > r-help mailing list -- Read > http://www.ci.tuwien.ac.at/~hornik/R/R-> FAQ.html > Send "info", > "help", or "[un]subscribe" > (in the > "body", not the subject !) To: > r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. > _._._._._._._._._ > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
A brief follow up on an earlier posting. Thanks for the earlier suggestion. How does it matter if I pass a list or a vector of arguments to a function that is written to take individual arguments: f3args <- function(a1, a2, a3) { definition} f1 <- function(b1, b2) { calculations n <- samplesize b<- someestimate c<- anotherestimate d <- c(n, b, c) h <- sapply(rep(d, R), f3args) } e <- c(m, n) k <- sapply(rep(e, R), f1) Is there a problem with this. Can one pass a vector of arguments to a fucntion that is written for individual arguments. What would be a good way for doing this? --Anupam. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Thanks for the clarifications. All the symbols below refer to scalars, unless they are assigned to a non-scalar explicitly. I am trying to do the following. Would like to avoid using for-loops where possible, so that program can also be used in S efficiently. 1. Create an CxC contingency table (matrix) with certain cell frequencies: Is there are better way of assigning "population" probabilities of cells to contingency table and sampling from it? 2. Use the contingency table sampling function suggested by Ripley to create a sample ("population sample"), of size n. 3. Compute row and col frequencies and estimates of marginal probabilities: below f1 computes c,d for 2x2 (for a row and a col, the other can be calcualted by 1-c, 1-d inside a fucntion that uses them: f3args). 4. Compute K statistics based on marginal probabilities: f3args does this. 5. use R bootstrap samples of size n based on marginal probabilities to compute standard errors for the statistics estimates. 6. for each population sample use R samples based on *population cell probabilities* and compute same std errs (SD in this case) ("MC-SD"). 7. Do this for M samples from population k <- below attempts that (sorry for overloading on R, that should be different, say S): that is, repeat steps 2--6, M times (M is the value of S variable). 8. Store the MxK statistics from popoulation samples, their std. errs, and corresponding MC-SD's in a matrix for use,((Mx(3xK)) matrix. A question: I think the point is to differentiate between a vector of arguments and a list of arguments, because a vector of arguments is treated as value of one argument. Right? Then, how does one pass a list as a value for one argument, without converting it into a vector? Another Question: what is a good way to update R from disk (not online--can't get the wireless drivers to work on SuSE 7.1. So, have to use windows for internet.)? ---Anupam. ******Brian Ripley wrote*******> A brief follow up on an earlier posting. Thanks for the earlier suggestion. > > How does it matter if I pass a list or a vector of > arguments to a function that is written to take individual arguments:Yes, it does.> f3args <- function(a1, a2, a3) { definition} > > f1 <- function(b1, b2) { > calculations > n <- samplesize > b<- someestimate > c<- anotherestimate > d <- c(n, b, c) > h <- sapply(rep(d, R), f3args) > } > > e <- c(m, n) > > k <- sapply(rep(e, R), f1) > > Is there a problem with this.It seems to be incomplete, so it's hard to tell what you intended. For example, R is undefined. But you are not passing a second argument to f1. You do that via an extra argument to sapply.> Can one pass a vector of arguments to a > fucntion that is written for individual arguments. What would be a good way > for doing this??do.call, as in do.call("f1", as.list(e)) -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ ******************** Pleasures, like flowers, may wither and decay, And yet the root perennial may be. --H.W.Longfellow, ``Memories" ******************* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._