Dear all, Even though one of R users answered my question, I cannot understand, so I re-ask this question. I am trying to use "do.call", but I don't think I totally understand this function. Here is an simple example. --------------------------------------------> B <- matrix(c(.5,.1,.2,.3),2,2) > B[,1] [,2] [1,] 0.5 0.2 [2,] 0.1 0.3> x <- c(.1,.2) > X <- cbind(1,x) > Xx [1,] 1 0.1 [2,] 1 0.2> > lt <- expand.grid(i=seq(1,2), y0=seq(0,2)) > lti y0 1 1 0 2 2 0 3 1 1 4 2 1 5 1 2 6 2 2> > fc <- function(y0,i) dpois(y0, exp(rowSums(t(X[i,])*B[,1]))) > > do.call(fc,lt)[1] 1.892179e-09 3.348160e-01 3.800543e-08 3.663470e-01 3.816797e-07 2.004237e-01 -------------------------------------------- Unfortunately, what I want to get is dpois(0, exp(rowSums(t(X[1,])*B[,1]))) = 0.1891356 dpois(0, exp(rowSums(t(X[2,])*B[,1]))) = 0.1859965 dpois(1, exp(rowSums(t(X[1,])*B[,1]))) = 0.3149658 dpois(1, exp(rowSums(t(X[2,])*B[,1]))) = 0.3128512 dpois(2, exp(rowSums(t(X[1,])*B[,1]))) = 0.2622549 dpois(2, exp(rowSums(t(X[2,])*B[,1]))) = 0.2631122 -------------------------------------------- Would you plz tell me why these two results are different?? and how do I get what I want to using "do.call" function?? Regards, Kathryn Lord -- View this message in context: http://r.789695.n4.nabble.com/on-do-call-function-tp3727262p3727262.html Sent from the R help mailing list archive at Nabble.com.
On Aug 8, 2011, at 17:16 , Kathie wrote:> Dear all, > > Even though one of R users answered my question, I cannot understand, so I > re-ask this question. > > I am trying to use "do.call", but I don't think I totally understand this > function.It has nothing to do with do.call, the problem is that your fc() function isn't vectorized. Specifically:> dpois(0, exp(rowSums(t(X[1,])*B[,1])))[1] 0.1891356> dpois(0, exp(rowSums(t(X[2,])*B[,1])))[1] 0.1859965> dpois(0, exp(rowSums(t(X[c(1,2),])*B[,c(1,1)])))x 0.06598804 0.35684473 which in turn boils down to> t(X[c(1,2),])*B[,c(1,1)][,1] [,2] 0.50 0.50 x 0.01 0.02> t(X[1,])*B[,1]x [1,] 0.5 0.01> t(X[2,])*B[,1]x [1,] 0.5 0.02 (You may be needing a drop=FALSE or two, but how am I to tell...)> > Here is an simple example. > > -------------------------------------------- > >> B <- matrix(c(.5,.1,.2,.3),2,2) >> B > [,1] [,2] > [1,] 0.5 0.2 > [2,] 0.1 0.3 >> x <- c(.1,.2) >> X <- cbind(1,x) >> X > x > [1,] 1 0.1 > [2,] 1 0.2 >> >> lt <- expand.grid(i=seq(1,2), y0=seq(0,2)) >> lt > i y0 > 1 1 0 > 2 2 0 > 3 1 1 > 4 2 1 > 5 1 2 > 6 2 2 >> >> fc <- function(y0,i) dpois(y0, exp(rowSums(t(X[i,])*B[,1]))) >> >> do.call(fc,lt) > [1] 1.892179e-09 3.348160e-01 3.800543e-08 3.663470e-01 3.816797e-07 > 2.004237e-01 > > -------------------------------------------- > > Unfortunately, what I want to get is > > dpois(0, exp(rowSums(t(X[1,])*B[,1]))) = 0.1891356 > dpois(0, exp(rowSums(t(X[2,])*B[,1]))) = 0.1859965 > dpois(1, exp(rowSums(t(X[1,])*B[,1]))) = 0.3149658 > dpois(1, exp(rowSums(t(X[2,])*B[,1]))) = 0.3128512 > dpois(2, exp(rowSums(t(X[1,])*B[,1]))) = 0.2622549 > dpois(2, exp(rowSums(t(X[2,])*B[,1]))) = 0.2631122 > > -------------------------------------------- > > Would you plz tell me why these two results are different?? and how do I get > what I want to using "do.call" function?? > > > Regards, > > Kathryn Lord > > -- > View this message in context: http://r.789695.n4.nabble.com/on-do-call-function-tp3727262p3727262.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com "D?den skal tape!" --- Nordahl Grieg
On Aug 8, 2011, at 11:16 AM, Kathie wrote:> Dear all, > > Even though one of R users answered my question, I cannot > understand, so I > re-ask this question. > > I am trying to use "do.call", but I don't think I totally understand > this > function.Why are you trying to use do.call? It operates on a sequence of lists which would be dataframe columns. while it appears you want to operate on a row-by-row basis. You probably want apply rather than do.call (as you were already told.). ?apply (You will need to modify the fc() function to take row arguments by either their position of by named index.) -- David.> > Here is an simple example. > > -------------------------------------------- > >> B <- matrix(c(.5,.1,.2,.3),2,2) >> B > [,1] [,2] > [1,] 0.5 0.2 > [2,] 0.1 0.3 >> x <- c(.1,.2) >> X <- cbind(1,x) >> X > x > [1,] 1 0.1 > [2,] 1 0.2 >> >> lt <- expand.grid(i=seq(1,2), y0=seq(0,2)) >> lt > i y0 > 1 1 0 > 2 2 0 > 3 1 1 > 4 2 1 > 5 1 2 > 6 2 2 >> >> fc <- function(y0,i) dpois(y0, exp(rowSums(t(X[i,])*B[,1]))) >> >> do.call(fc,lt) > [1] 1.892179e-09 3.348160e-01 3.800543e-08 3.663470e-01 3.816797e-07 > 2.004237e-01 > > -------------------------------------------- > > Unfortunately, what I want to get is > > dpois(0, exp(rowSums(t(X[1,])*B[,1]))) = 0.1891356 > dpois(0, exp(rowSums(t(X[2,])*B[,1]))) = 0.1859965 > dpois(1, exp(rowSums(t(X[1,])*B[,1]))) = 0.3149658 > dpois(1, exp(rowSums(t(X[2,])*B[,1]))) = 0.3128512 > dpois(2, exp(rowSums(t(X[1,])*B[,1]))) = 0.2622549 > dpois(2, exp(rowSums(t(X[2,])*B[,1]))) = 0.2631122 > > -------------------------------------------- > > Would you plz tell me why these two results are different?? and how > do I get > what I want to using "do.call" function??-- David Winsemius, MD West Hartford, CT
Hi, Your use of "do.call" is essentially equal to "dpois(lt$y0, exp(rowSums(t(X[lt$i,])*B[,1])))". You do not need to use "do.call", "sapply" or "apply" will do, e.g.,> sapply(1:nrow(lt), function(x) fc(lt[x,2],lt[x,1]))[1] 0.1891356 0.1859965 0.3149658 0.3128512 0.2622549 0.2631122 Wayne (Yanwei) Zhang Statistical Research>CNA-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Kathie Sent: Monday, August 08, 2011 10:16 AM To: r-help at r-project.org Subject: [R] on "do.call" function Dear all, Even though one of R users answered my question, I cannot understand, so I re-ask this question. I am trying to use "do.call", but I don't think I totally understand this function. Here is an simple example. --------------------------------------------> B <- matrix(c(.5,.1,.2,.3),2,2) > B[,1] [,2] [1,] 0.5 0.2 [2,] 0.1 0.3> x <- c(.1,.2) > X <- cbind(1,x) > Xx [1,] 1 0.1 [2,] 1 0.2> > lt <- expand.grid(i=seq(1,2), y0=seq(0,2)) > lti y0 1 1 0 2 2 0 3 1 1 4 2 1 5 1 2 6 2 2> > fc <- function(y0,i) dpois(y0, exp(rowSums(t(X[i,])*B[,1]))) > > do.call(fc,lt)[1] 1.892179e-09 3.348160e-01 3.800543e-08 3.663470e-01 3.816797e-07 2.004237e-01 -------------------------------------------- Unfortunately, what I want to get is dpois(0, exp(rowSums(t(X[1,])*B[,1]))) = 0.1891356 dpois(0, exp(rowSums(t(X[2,])*B[,1]))) = 0.1859965 dpois(1, exp(rowSums(t(X[1,])*B[,1]))) = 0.3149658 dpois(1, exp(rowSums(t(X[2,])*B[,1]))) = 0.3128512 dpois(2, exp(rowSums(t(X[1,])*B[,1]))) = 0.2622549 dpois(2, exp(rowSums(t(X[2,])*B[,1]))) = 0.2631122 -------------------------------------------- Would you plz tell me why these two results are different?? and how do I get what I want to using "do.call" function?? Regards, Kathryn Lord -- View this message in context: http://r.789695.n4.nabble.com/on-do-call-function-tp3727262p3727262.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. NOTICE: This e-mail message, including any attachments and appended messages, is for the sole use of the intended recipients and may contain confidential and legally privileged information. If you are not the intended recipient, any review, dissemination, distribution, copying, storage or other use of all or any portion of this message is strictly prohibited. If you received this message in error, please immediately notify the sender by reply e-mail and delete this message in its entirety.