【问题标题】:correlation between two data frames in RR中两个数据帧之间的相关性
【发布时间】:2014-08-15 15:42:24
【问题描述】:

我有一个数据框,其中包含时间段 Oct. 2000Dec. 2001(15 个月)的销售额值。我也有与上述相同时间段的利润值,我想在R 中找到这 15 个月的这两个数据框之间的月度相关性。我的数据框sales 是:

 Month       sales
Oct. 2000   24.1                                        
Nov. 2000   23.3    
Dec. 2000   43.9    
Jan. 2001   53.8    
Feb. 2001   74.9    
Mar. 2001   25  
Apr. 2001   48.5    
May. 2001   18  
Jun. 2001   68.1    
Jul. 2001   78  
Aug. 2001   48.8    
Sep. 2001   48.9    
Oct. 2001   34.3    
Nov. 2001   54.1    
Dec. 2001   29.3

我的第二个数据框profit是:

 period     profit
Oct 2000    14.1                                        
Nov 2000    3.3 
Dec 2000    13.9    
Jan 2001    23.8    
Feb 2001    44.9    
Mar 2001    15  
Apr 2001    58.5    
May 2001    18  
Jun 2001    58.1    
Jul 2001    38  
Aug 2001    28.8    
Sep 2001    18.9    
Oct 2001    24.3    
Nov 2001    24.1    
Dec 2001    19.3

现在我知道,在最初的两个月中,我无法获得相关性,因为没有足够的值,但从 Dec 2000 开始,我想通过考虑前几个月的值来计算相关性。因此,对于Dec. 200,我将考虑Oct. 2000Nov. 2000Dec. 2000 的值,这将给我3 个销售值和3 个利润值。同样对于Jan. 2001,我将考虑Oct. 2000Nov. 2000Dec. 2000Jan. 2001 的值,因此具有4 个销售价值和4 个利润价值。 因此每个月我都会考虑上个月的值来计算相关性,我的输出应该是这样的:

Month        Correlation
Oct. 2000    NA or Empty
Nov. 2000    NA or Empty
Dec. 2000       x
Jan. 2001       y
    .           .
    .           .
Dec. 2001       a

我知道在R 中有一个函数cor(sales, profit),但我怎样才能找出我的场景的相关性?

【问题讨论】:

  • 所以基本上你想循环cor(sales[1:i], profit[1:i])以增加i
  • @Spacedman 是否会通过考虑前几个月的值来为我提供每年每个月的相关性?很抱歉问了一个不重要的问题,但我是新手,因此没有太多知识。
  • @user2966197 请用清晰的统计术语解释您的问题。您是否可能想要计算互相关(参见?ccf)?
  • @Roland 我想计算每月销售额和利润之间的相关性,例如Oct 2000 Nov. 2000 Dec 2001 uptill Dec 2001。前两个月(Oct 2000 &`2000 年 11 月) I cannot get a correlation as there is only 1 or 2 values on each side of sales and profit. But for 2000 年 12 月`以后我可以得到相关性,因为我将考虑上个月的值也因此为Dec 2000 给出 3 个值。所以每个月我会考虑之前所有月份的值。

标签: r dataframe correlation


【解决方案1】:

制作一些样本数据:

> sales = c(1,4,3,2,3,4,5,6,7,6,7,5)
> profit = c(4,3,2,3,4,5,6,7,7,7,6,5)
> data = data.frame(sales=sales,profit=profit)
> head(data)
  sales profit
1     1      4
2     4      3
3     3      2
4     2      3
5     3      4
6     4      5

这是牛肉:

> data$runcor = c(NA,NA, 
    sapply(3:nrow(data), 
       function(i){
          cor(data$sales[1:i],data$profit[1:i])
        }))
> data
   sales profit      runcor
1      1      4          NA
2      4      3          NA
3      3      2 -0.65465367
4      2      3 -0.63245553
5      3      4 -0.41931393
6      4      5  0.08155909
7      5      6  0.47368421
8      6      7  0.69388867
9      7      7  0.78317543
10     6      7  0.81256816
11     7      6  0.80386072
12     5      5  0.80155885

所以现在data$runcor[3] 是前 3 个销售额和利润数字的相关性。

请注意,我将此runcor 称为“运行相关性”,就像“运行总和”一样,它是迄今为止所有元素的总和。这是迄今为止所有对的相关性。

【讨论】:

    【解决方案2】:

    另一种可能性是:(如果 dat1dat2 是初始数据集)

    更新

    dat1$Month <- gsub("\\.", "", dat1$Month)
    datN <- merge(dat1, dat2, sort=FALSE, by.x="Month", by.y="period")
    
    indx <- sequence(3:nrow(datN)) #create index to replicate the rows
    indx1 <- cumsum(c(TRUE,diff(indx) <0)) #create another index to group the rows
    
    #calculate the correlation grouped by `indx1` 
     datN$runcor <- setNames(c(NA, NA,by(datN[indx,-1], 
           list(indx1), FUN=function(x) cor(x$sales, x$profit) )), NULL)
    
    datN
    #      Month sales profit    runcor
    #1  Oct 2000  24.1   14.1        NA
    #2  Nov 2000  23.3    3.3        NA
    #3  Dec 2000  43.9   13.9 0.5155911
    #4  Jan 2001  53.8   23.8 0.8148546
    #5  Feb 2001  74.9   44.9 0.9345166
    #6  Mar 2001  25.0   15.0 0.9119941
    #7  Apr 2001  48.5   58.5 0.7056301
    #8  May 2001  18.0   18.0 0.6879528
    #9  Jun 2001  68.1   58.1 0.7647177
    #10 Jul 2001  78.0   38.0 0.7357748
    #11 Aug 2001  48.8   28.8 0.7351366
    #12 Sep 2001  48.9   18.9 0.7190413
    #13 Oct 2001  34.3   24.3 0.7175138
    #14 Nov 2001  54.1   24.1 0.7041889
    #15 Dec 2001  29.3   19.3 0.7094334
    

    【讨论】:

    • 抱歉,我错误地以错误的格式显示了数据格式。在第一帧中,我的月份是 Oct. 2000 格式,第二帧是“2000 年 10 月”(之后没有 .月)。我实现了你的方式,但它通过提供 225 个条目而不是 15 个条目多次复制/加入每个条目
    • 同样在第一个数据集中月份列被称为month,但在第二个数据帧中它被称为period。我再次为我原来的帖子中的错误道歉。我现在已经修改了帖子。
    猜你喜欢
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 2014-10-03
    • 2014-08-03
    相关资源
    最近更新 更多