Erdogan CEVHER - 1 year ago 72
R Question

# How to assign strings instead of on-the-fly numbers as the value of the given specific dataframe?

(reproducible example given) The function

`causfinder::causalitycombinations`
below:

``````causalitycombinations <- function (nvars, ncausers, ndependents)
{
independents <- combn(nvars, ncausers)
swingnumber <- dim(combn(nvars - ncausers, ndependents))[[2]]
numberofallcombinations <- dim(combn(nvars, ncausers))[[2]] * swingnumber
dependents <- matrix(, nrow = dim(combn(nvars, ncausers))[[2]] * swingnumber, ncol = ndependents)
for (i in as.integer(1:dim(combn(nvars, ncausers))[[2]])) {
dependents[(swingnumber * (i - 1) + 1):(swingnumber * i), ] <- t(combn(setdiff(seq(1:nvars), independents[, i]), ndependents))
}
swingedindependents <- matrix(, nrow = dim(combn(nvars, ncausers))[[2]] * swingnumber, ncol = ncausers)
for (i in as.integer(1:dim(combn(nvars, ncausers))[[2]])) {
for (j in as.integer(1:swingnumber)) {
swingedindependents[(i - 1) * swingnumber + j, ] <- independents[, i]
}
}
independentsdependents <- cbind(swingedindependents, dependents)
others <- matrix(, nrow = dim(combn(nvars, ncausers))[[2]] * swingnumber, ncol = nvars - ncausers - ndependents)
for (i in as.integer(1:((dim(combn(nvars, ncausers))[[2]]) *
swingnumber))) {
others[i, ] <- setdiff(seq(1:nvars), independentsdependents[i, ])
}
causalitiestemplate <- cbind(independentsdependents, others)
causalitiestemplate
}
``````

lists all the multivariate causality combinations. For example, in a 4-variable system, conditioned on the other 2 variables of the system, they are (when variables are assigned to numbers 1,2,3,4 and this assignment is kept throughout the analysis):

``````causalitycombinations(4,1,1)

[,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    1    3    2    4
[3,]    1    4    2    3
[4,]    2    1    3    4
[5,]    2    3    1    4
[6,]    2    4    1    3 # to check whether 2nd var Grangercauses 4th var condioned on 1 and 3
[7,]    3    1    2    4
[8,]    3    2    1    4
[9,]    3    4    1    2
[10,]    4    1    2    3
[11,]    4    2    1    3
[12,]    4    3    1    2
``````

Now,

``````data.frame(from = causalitycombinations(4,1,1)[,1], to= causalitycombinations(4,1,1)[,2],
pval = c(0.5,0.6,0.1, #I just typed random p-vals here
0.4,0.8,0.2,
0.1,0.5,0.9,
0.0,0.0,0.1)
)
``````

produces:

``````   from to  pval
1     1  2 0.5
2     1  3 0.6
3     1  4 0.1
4     2  1 0.4
5     2  3 0.8
6     2  4 0.2
7     3  1 0.1
8     3  2 0.5
9     3  4 0.9
10    4  1 0.0
11    4  2 0.0
12    4  3 0.1
``````

1. In the above "from" and "to" columns' entries, I wanna print variables' names (say: "inf", "gdp", "exc", "stock") instead of their representative numbers (i.e., 1,2,3,4). How to achieve this?

2. Equivalently, how to list combinations with strings instead of numbers

We can update columns with matching names by position from string vector:

``````# update columns with matching name
df1\$from <- c("inf", "gdp", "exc", "stock")[df1\$from]
df1\$to <- c("inf", "gdp", "exc", "stock")[df1\$to]

# result
df1
#     from    to pval
# 1    inf   gdp  0.5
# 2    inf   exc  0.6
# 3    inf stock  0.1
# 4    gdp   inf  0.4
# 5    gdp   exc  0.8
# 6    gdp stock  0.2
# 7    exc   inf  0.1
# 8    exc   gdp  0.5
# 9    exc stock  0.9
# 10 stock   inf  0.0
# 11 stock   gdp  0.0
# 12 stock   exc  0.1

# input data
df1 <- read.table(text="   from to  pval
1     1  2 0.5
2     1  3 0.6
3     1  4 0.1
4     2  1 0.4
5     2  3 0.8
6     2  4 0.2
7     3  1 0.1
8     3  2 0.5
9     3  4 0.9
10    4  1 0.0
11    4  2 0.0
12    4  3 0.1", header = TRUE)
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download