繁体   English   中英

R中两个数据帧之间的相关性

[英]correlation between two data frames in R

我有一个数据框,其中包含Oct. 2000 Dec. 2001 Oct. 2000Dec. 2001 Oct. 2000 Dec. 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 2000Dec 2000开始,我想通过考虑前几个月的值来计算相关性。 因此,对于Dec. 200我将考虑Oct. 2000Nov. 2000 Oct. 2000 Nov. 2000Dec. 2000 Nov. 2000 Dec. 2000值,这将给我3个销售值和3个利润值。 同样,对于Jan. 2001我将考虑Oct. 2000Nov. 2000 Oct. 2000 Nov. 2000 Dec. 2000Jan. 2001 Dec. 2000 Jan. 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)但是如何找到与我的情况相关的函数呢?

制作一些样本数据:

> 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称为“运行相关性”,就像“运行总和”一样,它是到目前为止所有元素的总和。 这是到目前为止所有对的相关性。

另一个可能性是:(如果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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM