如何应用 splom() 函数以创建多个相关成对图? [英] How to apply splom() function in order to create multiple correlation pairwise plots?

查看:55
本文介绍了如何应用 splom() 函数以创建多个相关成对图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经问过和 在这里,但由于我的编程技能低,我无法应用它.

I have already asked similar question on how to create the following figure: I was suggested to use splom() function but I do not know how to apply it on my data. I saw examples of splom() function which can be found here and here, but due to my low programming skills I am not able to apply it.

我有 24 个时间序列,属于 4 个独立组(4 个 Pirwise 相关图).4 组:

I have 24 time series, belonging to 4 independent groups (4 Pirwise correlation plots). 4 Groups:

1) 频率 = 1 分钟., 所属时间序列:AAPL_1m, MSFT_1m, INTC_1m, FB_1m, MU_1m, IBM_1m.2) 频率 = 2 分钟., 所属时间序列:AAPL_2m, MSFT_2m, INTC_2m, FB_2m, MU_2m, IBM_2m.3) 频率 = 5 分钟., 所属时间序列:AAPL_5m, MSFT_5m, INTC_5m, FB_5m, MU_5m, IBM_5m.4) 频率 = 10 分钟., 所属时间序列:AAPL_10m, MSFT_10m, INTC_10m, FB_10m, MU_10m, IBM_10m.

1) Frequency = 1 Min. , with belonging time series: AAPL_1m, MSFT_1m, INTC_1m, FB_1m, MU_1m, IBM_1m. 2) Frequency = 2 Min. , with belonging time series: AAPL_2m, MSFT_2m, INTC_2m, FB_2m, MU_2m, IBM_2m. 3) Frequency = 5 Min. , with belonging time series: AAPL_5m, MSFT_5m, INTC_5m, FB_5m, MU_5m, IBM_5m. 4) Frequency = 10 Min. , with belonging time series: AAPL_10m, MSFT_10m, INTC_10m, FB_10m, MU_10m, IBM_10m.

每个成对图应显示每个组中时间序列之间的相关性.为了创建每个单独的成对图,我使用了以下函数:

Each pairwise plot should show correlation between time series in each group. For creation of each individual pairwise plot I used following functions:

pairs(cbind(AAPL_1m, MSFT_1m, INTC_1m, FB_1m, MU_1m, IBM_1m),main="Frequency=1 Min.",font.labels = 2, col="blue",pch=16, cex=0.8, cex.axis=1.5,las=1)
pairs(cbind(AAPL_2m, MSFT_2m, INTC_2m, FB_2m, MU_2m, IBM_2m),main="Frequency = 2 Min.",font.labels = 2, col="blue",pch=16, cex=0.8, cex.axis=1.5,las=1)
pairs(cbind(AAPL_5m, MSFT_5m, INTC_5m, FB_5m, MU_5m, IBM_5m),main="Frequency = 5 Min.",font.labels = 2, col="blue",pch=16, cex=0.8, cex.axis=1.5,las=1)
pairs(cbind(AAPL_10m, MSFT_10m, INTC_10m, FB_10m, MU_10m, IBM_10m),main="Frequency = 10 Min.",font.labels = 2, col="blue",pch=16, cex=0.8, cex.axis=1.5,las=1)

如果有人可以建议如何应用 splom() 函数来创建提到/显示的图形,我们将不胜感激.

If anyone could suggest how to apply splom() function in order to create mentioned/shown figure it will be greatly appreciated.

此外,如果有另一个更合适的函数可以将单个成对图 (pairs()) 集成到一个图形中,我很想应用它.

Also if there is another more suitable function which can integrate for individual pairwise plots (pairs()) in one single figure, I am eager to apply it.

推荐答案

有些演示数据本来是不错的,但让我们先生成一些演示数据,这里只针对三个变量:

Some demodata would have been nice to have, but let's generate some first, just for three variables here:

AAPL_1m<-rnorm(1000)
MSFT_1m<-rnorm(1000) 
INTC_1m<-rnorm(1000)
AAPL_2m<-rnorm(1000)
MSFT_2m<-rnorm(1000) 
INTC_2m<-rnorm(1000) 

为了使 splom() 工作,您需要生成一个分组变量.这是来自 1m 组的 1000 个观察值,以及来自 2m 组的另外 1000 个观察值.所以分组变量只是一个简单的向量,其中包含 1000 个 1m 值,然后是 1000 个 2m 值:

In order for the splom() to work you would need to generate a grouping variable. Here are 1000 observation from the 1m group, and another 1000 observation from the 2m group. So the grouping variable would be just a simple vector of 1000 1m value and after them 1000 2m values:

group<-c(rep("1m", 1000), rep("2m", 1000))

在您的情况下,分组变量可能生成如下:

In your case the grouping variable might be generated as follows:

group<-c(rep("1m", length(AAPL_1m)), rep("2m", length(AAPL_2m)))

获得分组变量后,您可能希望将所有内容绑定到单个数据帧中,如下所示:

After you have the grouping variable, you might want to bind everything into a sinle dataframe as follows:

dat<-data.frame(AAPL=c(AAPL_1m, AAPL_2m), MSFT=c(MSFT_1m, MSFT_2m), INTC=c(INTC_1m, INTC_2m), group=group)

一旦你有一个单一的数据框,分组变量给出了观察组,你可以绘制散点图矩阵:

Once you have a single data frame with the grouping variable giving the groups of observations, you can plot the scatterplot matrices:

library(lattice)
# Three first columns of the data plotted conditional on the grouping
splom(~dat[,1:3]|group)

结果图应该大致如下:

这需要推广到您的四批数据,但它应该是直截了当的(只需为四批生成分组,并将所有四个单独的批次绑定在一起).函数 splom() 也有更多参数可供您使用,例如,使情节更漂亮.

This would need to be generalized to your four batches of data, but it should be straighforward (just generate grouping for four batches, and bind all four separate batches of together). Function splom() also has many more arguments that you can use for, e.g., making the plot prettier.

这篇关于如何应用 splom() 函数以创建多个相关成对图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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