user3643547
user3643547

Reputation: 3

How to repeat a loop that generates different outputs with each run?

pvals=c()
for (i in 1:ncol(my_geno)){
my_pheno=sample(my_pheno,replace=F)
pvals[i]= anova(lm(my_pheno~my_geno[,i]))[1,5]
print(i)
}
which.min(pvals)

Hi everyone. I'll go ahead and try to describe my code a bit, and sorry in advance that I don't have generic variables written in this code. The loop above generates pvalues based on the regression of my_pheno on my_geno and what I am trying to do is find a way to make it so the entire loop from <1:ncol(mygeno)> repeats a set number of times and then outputs the minimum pvals using for each run into a vector. I believe that <[1,5]> selects for the pvalues in the table that is created by anova.

Thank you in advance!

Upvotes: 0

Views: 81

Answers (1)

Roman Luštrik
Roman Luštrik

Reputation: 70653

Some optimization of your for loop could be had, but this should work for now. This code will repeat itself 100 times.

result <- replicate(100, {
  pvals=c()
  for (i in 1:ncol(my_geno)){
    my_pheno=sample(my_pheno,replace=F)
    pvals[i]= anova(lm(my_pheno~my_geno[,i]))[1,5]
    print(i)
  }
  which.min(pvals)
})

Upvotes: 1

Related Questions