Hi Kevin,
Regarding your first question, try this:
library(combinat)
all.pairs <- combn2(5:40)
marker1 <- as.matrix(names(qtl)[all.pairs[, 1]])
marker1 <- as.matrix(names(qtl)[all.pairs[, 2]])
myfun <- function(idx) {
summary(aov(qtl$CPP ~ qtl[,idx[1]] *
qtl[,idx[2]]))[[1]]$"Pr(>F)"[3])
}
p.interaction <- as.matrix(apply(all.pairs, 1, myfun)
HTH,
Adrian
On Monday 17 July 2006 05:18, Kevin J Emerson wrote:> Hello R-users!
>
> I have a style question. I know that for loops are somewhat frowned upon
> in R, and I was trying to figure out a nice way to do something without
> using loops, but figured that i could get it done quickly using them. I am
> now looking to see what kind of tricks I can use to make this code a bit
> more aesthetically appealing to other R users (and learn something about R
> along the way...).
>
> Here's the problem. I have a data.frame with 4 columns of dependent
> variables and then ~35 columns of predictor variables (factors) [for those
> interested, it is a qtl problem, where the predictors are genotypes at DNA
> markers and the dependent variable is a biological trait]. I want to go
> through all pairwise combinations of predictor variables and perform an
> anova with two predictors and their interaction on a given dependent
> variable. I then want to store the p.value of the interaction term, along
> with the predictor variable information. So I want to end up with a
> dataframe at the end with the two variable names and the interaction p
> value in each row, for all pairwise combinations of predictors. I used the
> following code:
>
> # qtl is the original data.frame, and my dependent var in this case is
> # qtl$CPP.
>
> marker1 <- NULL
> marker2 <- NULL
> p.interaction <- NULL
> for ( i in 5:40) { # cols 5 - 41 are the predictor factors
> for (j in (i+1):41) {
> marker1 <- rbind(marker1,names(qtl)[i])
> marker2 <- rbind(marker2,names(qtl)[j])
> tmp2 <- summary(aov(tmp$CPP ~ tmp[,i] * tmp[,j]))[[1]]
> p.interaction <- rbind(p.interaction, tmp2$"Pr(>F)"[3])
> }
> }
>
> I have two questions:
> (1) is there a nicer way to do this without having to invoke for loops?
> (2) my other dependent variables are categorical in nature. I need
> basically the same information - I am looking for information regarding the
> interaction of predictors on a categorical variable. Any ideas on what
> tests to use? (I am new to analysis of all-categorical data).
>
> Thanks in advance!
> Kevin
>
> --------------------------------------
> --------------------------------------
> Kevin Emerson
> Center for Ecology and Evolutionary Biology
> 1210 University of Oregon
> Eugene, OR 97403
> USA
> kemerson at uoregon.edu
--
Adrian DUSA
Romanian Social Data Archive
1, Schitu Magureanu Bd
050025 Bucharest sector 5
Romania
Tel./Fax: +40 21 3126618 \
+40 21 3120210 / int.101