系列相关系数计算 [英] Series of Correlation coefficient calculation

查看:119
本文介绍了系列相关系数计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分析R中的默认数据集(mtcars数据集)。我有兴趣根据以下规则创建相关系数列。仅 mpg和 wt之间的前三个观察值(即第1,2,3行)的相关系数,然后离开第一行,再次计算下三个观察值(即第2,3行, 4)在mpg和wt之间,然后离开前两行,再次计算mpg和wt之间的下三个观测值(即第3、4、5行)之间的相关系数,依此类推直至结束。例如

I want to analyse the default data set in R (mtcars data set). I am interested in creating column of correlation coefficients according to the below rule. Correlation coefficient of only first three observations ((i.e., row 1,2,3)) between "mpg" and "wt", then leaving the first row, calculate again correlation coefficient between next three observations (i.e., row 2,3,4) between mpg and wt then leaving the first two rows, calculate again correlation coefficient between next three observations (i.e., row 3,4,5) between mpg and wt and so on till end. For example

cor(mtcars$mpg[c(1,2,3)],mtcars$wt[c(1,2,3)])
cor(mtcars$mpg[c(2,3,4)],mtcars$wt[c(2,3,4)])
cor(mtcars$mpg[c(3,4,5)],mtcars$wt[c(3,4,5)]);

,依此类推。
谁能帮忙使用循环等方法自动执行此R代码。

and so on. Can anyone help to how to automate this R code using loop etc.

示例,看看我如何需要输出,我已经在excel中完成了,但是我需要在R中完成。

Example, see how i need output, i have done it in excel but i need to do it in R.

推荐答案

cor(mtcars $ mpg [c(1,2,3)],mtcars $ wt [c(1,2,3) ])为-0.8884586;但是,问题中输出图像的相关性列中的第一个值不是那个值,因此相对于所需内容的描述,所示图像中存在一些错误。我们将假定描述正确,而示例输出则不正确。

The value of cor(mtcars$mpg[c(1,2,3)],mtcars$wt[c(1,2,3)]) is -0.8884586; however, the first value in the Correlation column of the output image in the question is not that so there is some error in the image shown relative to the description of what is wanted. We will assume that the description is correct and the sample output is not.

尝试滚动应用, rollapply 。它将函数 cor2 应用于宽度为3的滚动窗口。 align = left 表示它使用当前行以及接下来的2行,以便NA值显示在问题图像中的末尾。 fill = NA 导致它为最后2个元素生成NA值,因为这些元素不再有3个元素。

Try a rolling apply, rollapply. It applies the function cor2 to a rolling window of width 3. align = "left" means it uses the current row and the next 2 rows so that the NA values appear at the end as in the image in the question. fill = NA causes it to generate NA values for the last 2 elements since there are not 3 more elements for those.

library(zoo)

mtcars2 <- mtcars[c("mpg", "wt")]
cor2 <- function(x) cor(x[, 1], x[, 2])
transform(mtcars2, cor = rollapply(mtcars2, 3, cor2, by.column = FALSE,  
   align = "left", fill = NA))

给出:

                     mpg    wt         cor
Mazda RX4           21.0 2.620 -0.88845855
Mazda RX4 Wag       21.0 2.875 -0.82589964
Datsun 710          22.8 2.320 -0.87097656
Hornet 4 Drive      21.4 3.215 -0.99520846
Hornet Sportabout   18.7 3.440 -0.99985063
Valiant             18.1 3.460 -0.99534538
Duster 360          14.3 3.570 -0.97267882
Merc 240D           24.4 3.190 -0.90784130
Merc 230            22.8 3.150 -0.96247218
Merc 280            19.2 3.440 -0.86602540
Merc 280C           17.8 3.440 -0.99308187
Merc 450SE          16.4 4.070 -0.05428913
Merc 450SL          17.3 3.730 -0.96311366
Merc 450SLC         15.2 3.780 -0.99534934
Cadillac Fleetwood  10.4 5.250  0.05301502
Lincoln Continental 10.4 5.424 -0.98658763
Chrysler Imperial   14.7 5.345 -0.96899291
Fiat 128            32.4 2.200  0.44730718
Honda Civic         30.4 1.615 -0.86317499
Toyota Corolla      33.9 1.835 -0.94182141
Toyota Corona       21.5 2.465 -0.99341821
Dodge Challenger    15.5 3.520 -0.94720046
AMC Javelin         15.2 3.435  0.21168794
Camaro Z28          13.3 3.840 -0.90670560
Pontiac Firebird    19.2 3.845 -0.99864434
Fiat X1-9           27.3 1.935 -0.99939736
Porsche 914-2       26.0 2.140 -0.99630829
Lotus Europa        30.4 1.513 -0.99962223
Ford Pantera L      15.8 3.170 -0.93453339
Ferrari Dino        19.7 2.770 -0.96372018
Maserati Bora       15.0 3.570          NA
Volvo 142E          21.4 2.780          NA

也可以看到此SO帖子,除了在data.table中有类似内容ext:与data.table的滚动关联

Also see this SO post which is similar except in a data.table context: Rolling correlation with data.table

这篇关于系列相关系数计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆