gpt4 book ai didi

r - 循环: selection of variable for correlation function in r

转载 作者:行者123 更新时间:2023-12-02 04:47:13 24 4
gpt4 key购买 nike

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

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)
}

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

编辑:过程:

enter image description here

最佳答案

我猜猜......

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
>

关于r - 循环: selection of variable for correlation function in r,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11785954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com