3

这是我打算做的(对于相当多的变量和数据集):

mygroupdf <- data.frame (varname = c("A", "B", "c1", "D2",
    "E", "F", "g1"), group = c(1, 1, 1, 2,3,3,4))

> mygroupdf
      varname group
  1       A     1
  2       B     1
  3      c1     1
  4      D2     2
  5       E     3
  6       F     3
  7      g1     4

此数据框仅包含用于变量分组的信息:

group 1 = A, B, c1
group 2 = D2
group 3 = E, F
group 4 = g1

第二个数据集 - 包含实际数据

set.seed(1234)
dataf <- data.frame (yvar = rnorm (10, 10,3), 
    A = sample(c(1,0), 10, T), B = sample(c(1,0), 10, T), 
    c1 = sample (c(1,0), 10, T), D2 = sample (c(1,0), 10, T), 
    E= sample (c(1,0), 10, T),F = sample (c(1,0), T), 
    g1 = sample (c(1,0), 10, T))

# manual workout:
xtemp <- dataf$A* dataf$B * dataf$c1 # all from group 1
# I error in previous version it is * not + 
# (is product of all members of a group i.e. 
 xtemp <- dataf$D2 (- group 2)
 xtemp <- dataf$E * dataf$F (- group 3)
 xtemp <- dataf$G (- group 4)

然后将产品与 Yvar 相关:

x <- cor(dataf$yvar, xtemp)

我想将它包装到一个函数中,以便我可以将它应用到我的数据集中的 1000 组变量中。

   corrfun <- function (x, V1, V2, V3) {
           xtemp <- V1 * V2  + V3
           x <- cor(dataf$yvar, xtemp)
           return (x)
          }

由于不同的组有不同的变量,我不确定如何构建这样的函数并应用于整个数据集。请帮忙 !

编辑:过程:

在此处输入图像描述

4

4 回答 4

3

我赌一把……

corrfun <- function (group.no, x=dataf, x.lookup=mygroupdf) {
  xtemp <- apply(x[x.lookup$varname[x.lookup$group == group.no]], 1, prod)

  out <- cor(x$yvar, xtemp)

  return (out)
}

>     corrfun(1)
[1] 0.35593
> corrfun(2)
[1] 0.4181311
> 
于 2012-08-02T22:01:52.173 回答
0

并使用我当前最喜欢的库创建另一个答案:

library(plyr)
ddply(mygroupdf, .(group), summarise,
      cor=cor(dataf$yvar, apply(dataf[as.character(varname)],1,prod)))

这将产生以下结果:

  group        cor
1     1  0.3559300
2     2  0.4181311
3     3         NA
4     4 -0.1015003
Warning message:
In cor(dataf$yvar, apply(dataf[as.character(varname)], 1, prod)) :
  the standard deviation is zero
于 2012-08-02T22:46:59.523 回答
0

另一个答案..

cbind(
  group = unique(mygroupdf$group),
  corr = 
    do.call(
      c,
      lapply(
        unique(mygroupdf$group),
        function(x) {
          varnames <- unique(mygroupdf[mygroupdf$group == x, 'varname'])
          products <- apply(as.matrix(dataf[, colnames(dataf) %in% varnames]), 1, prod)
          cor(products, dataf$yvar)
        }
      )
    )
)

这使

     group       corr
[1,]     1  0.3559300
[2,]     2  0.4181311
[3,]     3         NA
[4,]     4 -0.1015003
于 2012-08-02T22:14:50.827 回答
0
sapply(unique(mygroupdf$group), function(x) {
  a <- as.character(mygroupdf$varname[mygroupdf$group == x])
  cor(dataf$yvar, apply(dataf[a],1,prod))
})
  1. unique:标识唯一的组号
  2. sapply:对他们每个人应用功能
  3. a <- ...: leta是对应的变量名
  4. dataf[a]:从数据框中选择适当的列
  5. apply(…prod):计算每一行的乘积
  6. cor:相关
  7. sapply:将结果组合成一个简单的向量
于 2012-08-02T22:20:49.437 回答