How to use the splom () function to create several paired correlation sections?

I already asked a similar question about how to create the following drawing: enter image description here I was offered to use the splom () function, but I don’t know how to apply it to my data. I saw examples of the splom () function, which can be found here and here , but due to my low programming skills, I cannot apply it.

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

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

Each pair chart should show a correlation between time series in each group. To create each separate pair plot, I used the 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) 

If anyone can suggest how to use the splom () function to create the indicated / shown shape, we will be very grateful.

Also, if there is another suitable function that can be integrated for individual paired graphs (pairs ()) in one figure, I really want to apply it.

0
source share
2 answers

Some demodates would be nice to have, but let them generate first, just for three variables:

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

To work splom() , you need to create a grouping variable. Here are 1000 observations from the 1st group and another 1000 observations from the 2nd group. Thus, the grouping variable will be just a simple vector 1000 1m and after them 1000 2m values:

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

In your case, a grouping variable can be generated as follows:

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

Once you have the grouping variable, you can bind everything to the sinle file frame 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 one data frame with a grouping variable that gives observation groups, you can build the scatter matrices:

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

The resulting graph should look something like this:

enter image description here

This should be generalized to your four batches of data, but it should be unrelated (just create a group for four batches and join all four separate batches together). The splom() function also has many more arguments that you can use to, for example, to create a more beautiful graph.

+3
source

JTT gave an exact explanation of how splom () should be applied for this problem. The following code is an extension of the JTT code that applies to the problem.

 group<-c(rep("Frequency = 1 Min.", length(AAPL_1m)), rep("Frequency = 2 Min.", length(AAPL_2m)), rep("Frequency = 5 Min.", length(AAPL_5m)), rep("Frequency = 10 Min.", length(AAPL_10m))) dat<-data.frame(AAPL=c(AAPL_1m, AAPL_2m, AAPL_5m, AAPL_10m), MSFT=c(MSFT_1m, MSFT_2m, MSFT_5m, MSFT_10m), INTC=c(INTC_1m, INTC_2m, INTC_5m, INTC_10m), FB=c(FB_1m, FB_2m, FB_5m, FB_10m), MU=c(MU_1m, MU_2m, MU_5m, MU_10m), IBM=c(IBM_1m, IBM_2m, IBM_5m, IBM_10m), group=group) splom(~dat[,1:6]|group) 

The result of the code is as follows: enter image description here

However, there should be some improvements regarding:

  • x and Y axis and labels should be set outside (as shown in the question task)
  • the order of pairwise charts should be changed (the upper left corner should be "Frequency = 1", the upper right corner should be "Frequency = 1" ...)
+1
source

Source: https://habr.com/ru/post/973157/


All Articles