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)))
}
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.
Thinking about it it is probably more informative to include the design matrix and the model function:
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.