Search code examples
rloopsmode

How to form loop inside loop that reflects jumpings in the pattern to create a certain pattern that is given below?


When I work on pairwise conditional and partial Granger causality, I decided to express the effect receivings of each of the variables in a given system via a simple function. Here, "pairwise" is in the sense of advanced G-causality theory; namely, the number of independent (causer) variables and the number of dependent variables are BOTH 1 when they are conditioned on third set of variables (possibly with more than 1 variables). Note that in advanced GC theory, counfounding effects are taken into account, and those effects are subtracted from the main effect, hence, a clear effect in one direction is found, i.e., isolated from the effects of confounding variables; hence, that is true causality.

Now, to reflect the effect receiving matrix (5x6), first, the effects matrix is considered:

V1 V2 V3 V4 V5 V6 (Variables)
1  6  11 16 21 26
2  7  12 17 22 27
3  8  13 18 23 28
4  9  14 19 24 29
5  10 15 20 25 30

Each of the cells from 1 to 30 holds a p value (I did not enter exact p values themselves; rather entered location index for each p value). It is read like this:

(The effect of a variable on itself is disregarded: every variable is the cause of itself!)

The GC from V1 to V2 conditional on the others (V3,V4,V5,V6) is 1. Hence, V2 is receiving an effect of 1.

The GC from V1 to V3 conditional on the others (V2,V4,V5,V6) is 2. Hence, V3 is receiving an effect of 2.

The GC from V1 to V4 conditional on the others (V2,V3,V5,V6) is 3. Hence, V4 is receiving an effect of 3. ...
The GC from V5 to V4 conditional on the others (V1,V2,V3,V6) is 24. Hence, V4 is receiving an effect of 24. ... The GC from V2 to V1 conditional on the others (V3,V4,V5,V6) is 6. Hence, V1 is receiving an effect of 6.

This way, the value of effect receivings are derived. I want to embed these effect receiving values to a 6x5 matrix (here, 6 is for variables):

Effect Receiving Matrix (6x5):

Vars
V1 6  11 16 21 26
V2 1  12 17 22 27
V3 2   7 18 23 28
V4 3   8 13 24 29
V5 4   9 14 19 30
V6 5  10 15 20 25

I want to create a matrix with above entries: i.e., a[1,1]=6 a[1,2]=11 a[2,3]=17...

What I tried: I tried loop inside loop, thought mode operations can be helpful. Anyway, I did not solve. What I observed:
1. There is a pattern: increase 5*k
2. This pattern is damaged at jumps. For example, for V1, there is a jump at i=1; for V2, there is a jump at i=2; for V6, there is no jump since up to that jumping point, V6's all effect receivings are recorded.

Any help will be greatly appreciated.


Solution

  • Let us dispense with the high-powered jargon like Granger Causality, and just call it effect.

    Isn't it more expressive in terms of following expression?

    vars <- 1:6
    id <- c()
    for (i in 1:6){
      id <- c(id, vars[-i])
    }
    df1 <- stack(df)
    df1$id <- id
    library(tidyr)
    spread(df1, ind, values)
    #  id  1  2  3  4  5  6
    #1  1 NA  6 11 16 21 26
    #2  2  1 NA 12 17 22 27
    #3  3  2  7 NA 18 23 28
    #4  4  3  8 13 NA 24 29
    #5  5  4  9 14 19 NA 30
    #6  6  5 10 15 20 25 NA
    spread(df1, id, values)
    #  ind  1  2  3  4  5  6
    #1   1 NA  1  2  3  4  5
    #2   2  6 NA  7  8  9 10
    #3   3 11 12 NA 13 14 15
    #4   4 16 17 18 NA 19 20
    #5   5 21 22 23 24 NA 25
    #6   6 26 27 28 29 30 NA
    

    It easily reads, for instance, the effect of 1 to 2 is 1, 2 to 1 is 6, 5 to 6 is 25 etc.

    Input used

    df <- structure(list(`1` = 1:5, `2` = 6:10, `3` = 11:15, `4` = 16:20, 
        `5` = 21:25, `6` = 26:30), .Names = c("1", "2", "3", "4", 
    "5", "6"), class = "data.frame", row.names = c(NA, -5L))