如何在循环中找到相关系数?

时间:2018-07-20 07:25:47

标签: r

我有一个像这样的数据集:

Account_tenure_years = c(982,983,984,985,986,987,988)
N=c(12328,18990,21255,27996,32014,15487,4347)
Y=c(76,64,61,76,94,55,11)
df_table_account_tenure_vs_PPC = data.frame(Account_tenure_years,N,Y)

The dataset looks like this:

Account_tenure_years   N     Y
982                  12328  76
983                  18990  64
984                  21255  61
985                  27996  76
986                  32014  94
987                  15487  55
988                   4347  11

我想做的是这样:

我想在correlation(例如Account_tenure_years)的任意两个之间找到982,983,并用correlation coefficientN找到Y columns即我想找到下表的correlation coefficient

  Account_tenure_years   N     Y
  982                  12328  76
  983                  18990  64

现在我要重复此8C2次,即28次。分别采用不同的行并找到correlation coefficient。 即在下一次迭代中,我会想要的:

 Account_tenure_years   N     Y
  983                  18990  64
  984                  21255  61 

找到它的correlation coefficient。现在,当我收到所有这28个correlation coefficients之后,我average找出来,并为整个数据集找到一个mean correlation coefficient

如何在R中执行此操作?

好吧,如果我找出列之间的相关系数,就可以弄清楚

Account_tenure_years column, N 

如果我试图找出列之间的相关系数

Account_tenure_years column, Y

如果在每种情况下我都找到负相关系数,我们可以从中推断出什么吗?

3 个答案:

答案 0 :(得分:1)

这不是为每种情况计算correlation coefficient的理想方法。应该针对整个数据集进行计算:

Account_tenure_years = c(982,983,984,985,986,987,988)
N=c(12328,18990,21255,27996,32014,15487,4347)
Y=c(76,64,61,76,94,55,11)
df = data.frame(Account_tenure_years,N,Y)

cor(df$Account_tenure_years,df$N)
cor(df$Account_tenure_years,df$Y)

输出如下所示:

> cor(df$Account_tenure_years,df$N)
[1] -0.1662244
> cor(df$Account_tenure_years,df$Y)
[1] -0.5332263

您可以推断出数据是负相关的。这意味着增加Account_tenure_years的值将减少NY的值,反之亦然。

请随时纠正我!

答案 1 :(得分:0)

执行此操作以转置数据应该更容易,而且最好的部分是您甚至不需要编写循环。

尝试一下:

dt <- data.table::fread("
Account_tenure_years   N     Y
982                  12328  76
983                  18990  64
984                  21255  61
985                  27996  76
986                  32014  94
987                  15487  55
988                   4347  11
")


dt.t <- as.data.frame(t(dt[, 2:3]))

colnames(dt.t) = dt$Account_tenure_years
# transpose
dt.t
#>     982   983   984   985   986   987  988
#> N 12328 18990 21255 27996 32014 15487 4347
#> Y    76    64    61    76    94    55   11

# calculate correlation matrix, read more help(cor)
cor(dt.t)
#>     982 983 984 985 986 987 988
#> 982   1   1   1   1   1   1   1
#> 983   1   1   1   1   1   1   1
#> 984   1   1   1   1   1   1   1
#> 985   1   1   1   1   1   1   1
#> 986   1   1   1   1   1   1   1
#> 987   1   1   1   1   1   1   1
#> 988   1   1   1   1   1   1   1

reprex package(v0.2.0.9000)于2018-07-20创建。

答案 2 :(得分:0)

我不明白您如何计算两个变量之间的相关系数,每个变量只有一个观察值。因此,我假设您的行数超过了此处提供的行数。

首先定义所有组合:

combinations <- combn(df_table_account_tenure_vs_PPC$Account_tenure_years, 2)

对于每种组合,您要提取相应的行并计算每个变量的相关系数:

coefficients <- apply(combinations, 2, function(x, df_table_account_tenure_vs_PPC){
    coef <- sapply(c("N", "Y"), function(v, x, df_table_account_tenure_vs_PPC){
        c <- cor(df_table_account_tenure_vs_PPC[df_table_account_tenure_vs_PPC == x[1], v], df_table_account_tenure_vs_PPC[df_table_account_tenure_vs_PPC == x[2], v])
        return(c)},
    x, df_table_account_tenure_vs_PPC)
    return(c(x, coef))},
df_table_account_tenure_vs_PPC)

然后,您可以将结果汇总到data.frame中:

df <- as.data.frame(t(coefficients))
colnames(df) <- c("Year1", "Year2", "N_cor", "Y_cor")

这应该有效。如果您有任何问题,请告诉我。 同样,如果您想要有意义的相关系数,请确保在每种情况下都具有多个观察值。