Hi
I tried to pass a contrast matrix as string variable, such as s=c("ab=a-b","ac=a-c") to makeContrasts(s), but it did not work. When I manually typed in ("ab=a-b","ac=a-c"), it works. Could some experts provide any suggestions?
Thanks.
John
Hi
I tried to pass a contrast matrix as string variable, such as s=c("ab=a-b","ac=a-c") to makeContrasts(s), but it did not work. When I manually typed in ("ab=a-b","ac=a-c"), it works. Could some experts provide any suggestions?
Thanks.
John
The makeContrasts
function accepts a variable number of arguments. When you type in "ab=a-b"
and "ac=a-c"
separately, each string is treated as a separate argument. A separate contrast is then constructed for each string, which is probably your desired behaviour. However, the vector s
is passed into the function as a single argument. This means that only one contrast is constructed, corresponding to the first string in s
.
To get back to our desired behaviour, what we need to do is to pass the contents of s
as separate arguments into makeContrasts
. This is done with some sleight of hand:
do.call(makeContrasts, c(s, list(levels=levels)))
The somewhat convoluted c(...)
term of the above expression just makes a list where the first entry is the first string of s
, the second entry is the second string, and the last entry is the levels
argument (which is missing from your original post; this argument is necessary for makeContrasts
, so I presume you've set it to something sensible). The do.call
function then passes each entry of the list as a separate argument into makeContrasts
, which gets us what we want.
Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
If you have your contrasts in a character vector, you can also do the same thing as
makeContrasts(contrasts=s, levels=levels)
.Ah, that's much better. Obviously, it seems that I like to overcomplicate things.
yeah! This is what I wanted. How cute!