Question: makeContrasts from limma overides "i" as a varible
0
4 weeks ago by
olof.rundquist0 wrote:

# makeContrasts from limma overides "i" as a varible

This is not really a question. Instead this is a coding practice detail. Using makeContrasts inside a for loop is risky since it globally overwrites the variable i. This leads to unexpected behavior, see example. This is of course easily solved by simply using another variable. Still this is not an expected behavior and āiā is a very popular iteration variable so having a function silently replace it is not good practice. I would suggest to eventually update the function so it no longer overwrites "i" since this can cause some nasty surprises.

Best Olof Rundquist

# Example (Time course experiment with six time points and two conditions):

colnames(eR_contrasts)


[1] "Group11" "Group12" "Group21" "Group22" "Group31" "Group32" [7] "Group41" "Group42" "Group51" "Group52" "Group61" "Group62"

comparison_vector = c(paste("(Group", seq(2,time_points), "_2-Group", seq(2,time_points), "_1)-(Group1_2-Group1_1)", sep=""))
makeContrasts(comparison_vector[3], levels=colnames(eR_contrasts))


Outputs:

          Contrasts

Levels     (Group4_2-Group4_1)-(Group1_2-Group1_1)
Group1_1                                       1
Group1_2                                      -1
Group2_1                                       0
Group2_2                                       0
Group3_1                                       0
Group3_2                                       0
Group4_1                                      -1
Group4_2                                       1
Group5_1                                       0
Group5_2                                       0
Group6_1                                       0
Group6_2                                       0


Set i to a number: i=3 makeContrasts(comparisonvector[i], levels=colnames(eRcontrasts))

Outputs:

          Contrasts

Levels     <NA>
Group1_1   NA
Group1_2   NA
Group2_1   NA
Group2_2   NA
Group3_1   NA
Group3_2   NA
Group4_1   NA
Group4_2   NA
Group5_1   NA
Group5_2   NA
Group6_1   NA
Group6_2   NA


Change to calling it z instead. z=3 makeContrasts(comparisonvector[z], levels=colnames(eRcontrasts))

Outputs:

          Contrasts

Levels     (Group4_2-Group4_1)-(Group1_2-Group1_1)
Group1_1                                       1
Group1_2                                      -1
Group2_1                                       0
Group2_2                                       0
Group3_1                                       0
Group3_2                                       0
Group4_1                                      -1
Group4_2                                       1
Group5_1                                       0
Group5_2                                       0
Group6_1                                       0
Group6_2                                       0


# This is an example of the final code when it works like intended:

comparison_vector <- c(paste("(Group", seq(2,time_points), "_2-Group", seq(2,time_points), "_1)-(Group1_2-Group1_1)", sep=""))
cont.matrix <- makeContrasts(comparison_vector[1], levels=colnames(eR_contrasts))
for (z in seq(2, length(comparison_vector))){
cont.matrix <- cbind(cont.matrix, makeContrasts(comparison_vector[z], levels=colnames(eR_contrasts)))
}

limma • 54 views
modified 4 weeks ago by James W. MacDonald51k • written 4 weeks ago by olof.rundquist0
Answer: makeContrasts from limma overides "i" as a varible
0
4 weeks ago by
United States
James W. MacDonald51k wrote:

Why are you iterating like that? If you need to use a for loop (probably unlikely, but...), you can do that to make the contrasts first, and then pass to makeContrasts.

> design <- model.matrix(~0 + gl(5,3))
> colnames(design) <- LETTERS[1:5]
> cont <- paste(LETTERS[2:5], "A", sep = "-")
> cont
[1] "B-A" "C-A" "D-A" "E-A"
> makeContrasts(contrasts = cont, levels = design)
Contrasts
Levels B-A C-A D-A E-A
A  -1  -1  -1  -1
B   1   0   0   0
C   0   1   0   0
D   0   0   1   0
E   0   0   0   1


Ok maybe the for loop is unnecessary. Although it seems like makeContrast does not correctly handel my version of the vector since if I run like above I only get the last column in my contrast matrix with the first comparison as the level. This is why I decided to use the for loop in the first place. In case it helps I am attaching the full design matrix at the end. Mostly I created this ticket since I consider it to be bad practice to have anything silently overwrite variabels.This might be harder to avoid than I realize though. Thanks for the support.

 > makeContrasts(contrast=comparison_vector, levels=eR_contrasts)
Contrasts
Levels     (Group2_2-Group2_1)-(Group1_2-Group1_1)
Group1_1                                       1
Group1_2                                      -1
Group2_1                                       0
Group2_2                                       0
Group3_1                                       0
Group3_2                                       0
Group4_1                                       0
Group4_2                                       0
Group5_1                                       0
Group5_2                                       0
Group6_1                                      -1
Group6_2                                       1

Group1_1 Group1_2 Group2_1 Group2_2 Group3_1 Group3_2 Group4_1
np_R2.0m.RNA         1        0        0        0        0        0        0
np_R6.0m.RNA         1        0        0        0        0        0        0
np_R7.0m.RNA         1        0        0        0        0        0        0
np_R1.30m.RNA        0        0        1        0        0        0        0
np_R2.30m.RNA        0        0        1        0        0        0        0
np_R6.30m.RNA        0        0        1        0        0        0        0
Group4_2 Group5_1 Group5_2 Group6_1 Group6_2
np_R2.0m.RNA         0        0        0        0        0
np_R6.0m.RNA         0        0        0        0        0
np_R7.0m.RNA         0        0        0        0        0
np_R1.30m.RNA        0        0        0        0        0
np_R2.30m.RNA        0        0        0        0        0
np_R6.30m.RNA        0        0        0        0        0


Thinking about it it is probably more informative to include the design matrix and the model function:

> eRdesign
Time Condition
np_R2.0m.RNA     1         1
np_R6.0m.RNA     1         1
np_R7.0m.RNA     1         1
np_R1.30m.RNA    2         1
np_R2.30m.RNA    2         1
np_R6.30m.RNA    2         1
np_R1.1h.RNA     3         1
np_R2.1h.RNA     3         1
np_R6.1h.RNA     3         1
np_R2.2h.RNA     4         1
np_R4.2h.RNA     4         1
np_R7.2h.RNA     4         1
np_R1.6h.RNA     5         1
np_R2.6h.RNA     5         1
np_R4.6h.RNA     5         1
np_R1.24h.RNA    6         1
np_R2.24h.RNA    6         1
np_R4.24h.RNA    6         1
prog_K0p         1         2
prog_L0p         1         2
prog_M0p         1         2
prog_K0.5p       2         2
prog_L0.5p       2         2
prog_M0.5p       2         2
prog_K1p         3         2
prog_L1p         3         2
prog_M1p         3         2
prog_K2p         4         2
prog_L2p         4         2
prog_M2p         4         2
prog_K6p         5         2
prog_L6p         5         2
prog_M6p         5         2
prog_K24p        6         2
prog_L24p        6         2
prog_M24p        6         2


eR_contrasts <- model.matrix(~0 + Group, data=eRdesign)

> cn <- paste(rep(paste0("Group_", 1:6), each = 2), rep(1:2, 6), sep = "_")
> cont <- list(c(4:1), c(6:5,2:1), c(8:7,2:1), c(10,9,2,1))
> cont2 <- sapply(cont, function(x) paste(paste(cn[x], c("-", "-", "+", "")), collapse = " "))
> cont2
[1] "Group_2_2 - Group_2_1 - Group_1_2 + Group_1_1 "
[2] "Group_3_2 - Group_3_1 - Group_1_2 + Group_1_1 "
[3] "Group_4_2 - Group_4_1 - Group_1_2 + Group_1_1 "
[4] "Group_5_2 - Group_5_1 - Group_1_2 + Group_1_1 "
> cont3 <- sapply(cont, function(x) paste(c("(", paste(cn[x], c("-", ") - (", "-", "")), ")"), collapse = " "))
> cont3
[1] "( Group_2_2 - Group_2_1 ) - ( Group_1_2 - Group_1_1  )"
[2] "( Group_3_2 - Group_3_1 ) - ( Group_1_2 - Group_1_1  )"
[3] "( Group_4_2 - Group_4_1 ) - ( Group_1_2 - Group_1_1  )"
[4] "( Group_5_2 - Group_5_1 ) - ( Group_1_2 - Group_1_1  )"
> contmat1 <- makeContrasts(contrasts = cont2, levels = cn)
> contmat1
Contrasts
Levels      Group_2_2 - Group_2_1 - Group_1_2 + Group_1_1
Group_1_1                                              1
Group_1_2                                             -1
Group_2_1                                             -1
Group_2_2                                              1
Group_3_1                                              0
Group_3_2                                              0
Group_4_1                                              0
Group_4_2                                              0
Group_5_1                                              0
Group_5_2                                              0
Group_6_1                                              0
Group_6_2                                              0
Contrasts
Levels      Group_3_2 - Group_3_1 - Group_1_2 + Group_1_1
Group_1_1                                              1
Group_1_2                                             -1
Group_2_1                                              0
Group_2_2                                              0
Group_3_1                                             -1
Group_3_2                                              1
Group_4_1                                              0
Group_4_2                                              0
Group_5_1                                              0
Group_5_2                                              0
Group_6_1                                              0
Group_6_2                                              0
Contrasts
Levels      Group_4_2 - Group_4_1 - Group_1_2 + Group_1_1
Group_1_1                                              1
Group_1_2                                             -1
Group_2_1                                              0
Group_2_2                                              0
Group_3_1                                              0
Group_3_2                                              0
Group_4_1                                             -1
Group_4_2                                              1
Group_5_1                                              0
Group_5_2                                              0
Group_6_1                                              0
Group_6_2                                              0
Contrasts
Levels      Group_5_2 - Group_5_1 - Group_1_2 + Group_1_1
Group_1_1                                              1
Group_1_2                                             -1
Group_2_1                                              0
Group_2_2                                              0
Group_3_1                                              0
Group_3_2                                              0
Group_4_1                                              0
Group_4_2                                              0
Group_5_1                                             -1
Group_5_2                                              1
Group_6_1                                              0
Group_6_2                                              0
> contmat2 <- makeContrasts(contrasts = cont3, levels = cn)
> colnames(contmat2)
[1] "( Group_2_2 - Group_2_1 ) - ( Group_1_2 - Group_1_1  )"
[2] "( Group_3_2 - Group_3_1 ) - ( Group_1_2 - Group_1_1  )"
[3] "( Group_4_2 - Group_4_1 ) - ( Group_1_2 - Group_1_1  )"
[4] "( Group_5_2 - Group_5_1 ) - ( Group_1_2 - Group_1_1  )"
> all.equal(contmat1, contmat2, check.attributes = FALSE)
[1] TRUE


Thanks for the solution. So in summary I needed to added some extra spaces everywhere and then it worked like above. That's fine I guess, normal R syntax, but why do I need two spaces before the last parantheses. It does not work without it.

Thanks You can close this ticket now Best Olof Rundquist

ok I misidentified the problem. The spaces are not needed I just needed to specify that it was a contrast vector and everything worked fine. I had tried this before asking the question in the first place but I guess I must have misspelled it. makeContrast does not seem to throw and error if you do and simply defaults to the normal method of assuming that it is an expression. No much that can be done about that I guess.

Thanks for the help.