Is there available in R a generalized inner product or matrix product, similar to 'outer(x,y, fun)', where one can specifiy an arbitrary function in place of ordinary multiplication? Here's my application. I frequently analyze user questionnaires from our HR/training department. These have questions of the form "please rate your skill in task X", and other questions of the form "Have you taken course Y?" (or "How many years since you have taken course Y?") I look at rank correlation between the (suitably ordered) vectors of responses to a question in the first group and a question in the second group. (The two vectors have the same length, but I want to replace the standard inner product with a different operation; in this case, rank correlation) Repeat the process across all possible pairs of questions. Is there a way to accomplish this without nested 'for' statements? Hope this is clear - thanks! <>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<> George Heine, PhD Mathematical Analyst National IRM Center U.S. Bureau of Land Management voice (303) 236-0099 fax (303) 236-1974 cell (303) 905-5382 pager gheine at my2way.com <>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>t
> From: George_Heine at blm.gov > > Is there available in R a generalized inner product or matrix product, > similar to 'outer(x,y, fun)', where one can specifiy an > arbitrary function > in place of ordinary multiplication? > > Here's my application. I frequently analyze user > questionnaires from our > HR/training department. These have questions of the form > "please rate your skill in task X", > and other questions of the form > "Have you taken course Y?" (or "How many years since > you have taken > course Y?") > > I look at rank correlation between the (suitably ordered) vectors of > responses to a question in the first group and a question in > the second > group. (The two vectors have the same length, but I want to > replace the > standard inner product with a different operation; in this case, rank > correlation) Repeat the process across all possible pairs of > questions. > > Is there a way to accomplish this without nested 'for' statements?I don't see how you can generalized inner product to get to rank correlations. Rank correlations are not computed by simply replacing the multiplication in the inner product with something else, but they replace the data values with ranks, and then compute the usual correlations on the ranks. If you want to generate rank correlation matrix, use cor(..., method="spearman"). Andy> Hope this is clear - thanks! > <>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<> > George Heine, PhD > Mathematical Analyst > National IRM Center > U.S. Bureau of Land Management > voice (303) 236-0099 > fax (303) 236-1974 > cell (303) 905-5382 > pager gheine at my2way.com > <>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>t > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >
Say you have a function FUN of two vector arguments which operates "columnwise on arrays". E.g., if FUN(v,w) is to be the inner product, then instead of v%*%t(w) write something like colSums(v*w). Now you can do something like vectorOuterProd <- function(A,B,FUN) { da <- dim(A)[-1] na <- prod(da) db <- dim(B)[-1] nb <- prod(db) result <- matrix(nrow=na,ncol=nb) dim(B) <- c(dim(B)[1],nb) for (j in 1:nb) { result[,j] <- FUN(A,B[,j]) } dim(result) <- c(da,db) result } which loops over the columns of B rather than replicate A and B as in outer() to save space. Reid Huntsinger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of George_Heine at blm.gov Sent: Friday, April 29, 2005 2:24 PM To: r-help at stat.math.ethz.ch Subject: [R] generalized matrix product ? Is there available in R a generalized inner product or matrix product, similar to 'outer(x,y, fun)', where one can specifiy an arbitrary function in place of ordinary multiplication? Here's my application. I frequently analyze user questionnaires from our HR/training department. These have questions of the form "please rate your skill in task X", and other questions of the form "Have you taken course Y?" (or "How many years since you have taken course Y?") I look at rank correlation between the (suitably ordered) vectors of responses to a question in the first group and a question in the second group. (The two vectors have the same length, but I want to replace the standard inner product with a different operation; in this case, rank correlation) Repeat the process across all possible pairs of questions. Is there a way to accomplish this without nested 'for' statements? Hope this is clear - thanks! <>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<> George Heine, PhD Mathematical Analyst National IRM Center U.S. Bureau of Land Management voice (303) 236-0099 fax (303) 236-1974 cell (303) 905-5382 pager gheine at my2way.com <>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>t ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On 4/29/05, George_Heine at blm.gov <George_Heine at blm.gov> wrote:> > > Is there available in R a generalized inner product or matrix product, > similar to 'outer(x,y, fun)', where one can specifiy an arbitrary function > in place of ordinary multiplication? > > Here's my application. I frequently analyze user questionnaires from our > HR/training department. These have questions of the form > "please rate your skill in task X", > and other questions of the form > "Have you taken course Y?" (or "How many years since you have taken > course Y?") > > I look at rank correlation between the (suitably ordered) vectors of > responses to a question in the first group and a question in the second > group. (The two vectors have the same length, but I want to replace the > standard inner product with a different operation; in this case, rank > correlation) Repeat the process across all possible pairs of questions. > > Is there a way to accomplish this without nested 'for' statements?Try this: inner <- function(a,b,f, ...) { f <- match.fun(f) apply(b,2,function(x)apply(a,1,function(y)f(x,y, ...))) } data(iris); irish <- head(iris)[,-5] # test data res <- inner(t(irish), irish, cor, method = "spearman") # test res2 <- cor(irish, method = "spearman") # should give same result identical(res, res2) # TRUE