-2

I have this for loop that i need to repeat (like 1000 times), and count the number of times the p.value is higher the some value. I think I have the counting part under control, but I just can't figure out how to repeat the loop to get a output that i can use.

    leads <-list(lead_one, lead_two, lead_three, lead_four, lead_five, lead_six, lead_seven, lead_eight, lead_nine, lead_ten)

    for(i in 1:length(leads)){   
  (ks.test(sample(lead_en, 75), sample(leads[[i]], 75)))$p.value  
    }

Help?

Update (update #2: code should be reproducible now):

I’m sorry for not providing enough informations, I’m new to programming, and brand new to asking for help about programming. Let me try again. I want to run the ks.test on a vector of lead times (lead_en) vs. all the vectors of lead times in "leads". This I can do with the for loop posted above. Then I would like to run this for loop a number of times to count the times of a certain outcome.

I’ ve tried (without counter):

lead_en <- c(4, 5, 3, 4, 5, 6, 7, 3, 4, 2, 3)
lead_one <- c(3, 4, 5, 2, 2, 5, 7, 8, 3, 2, 6)
lead_two <- c(4, 2, 5, 6, 3, 1, 5, 7, 5)
lead_three <- c(4, 3, 5, 5, 6, 7, 9, 9, 3, 3, 3)
lead_four <- c(5, 3, 5, 6, 3, 4, 5, 6)
lead_five <- c(4, 5, 3, 5, 6)
lead_six <- c(2, 3, 6, 9, 9, 5, 5, 6, 7, 4, 3, 3)
lead_seven <- c(3, 4, 5, 5, 6, 3, 3)
lead_eight <- c(2, 3, 3, 3, 3, 4, 5)
lead_nine <- c(5, 6, 7, 3, 3, 4)
lead_ten <- c(5, 3, 4, 5, 6, 7, 7)
leads <-list(lead_one, lead_two, lead_three, lead_four, lead_five, lead_six, lead_seven, lead_eight, lead_nine, lead_ten)


d <- 100 # number of replications
res = matrix(rep(0, nrow=10, ncol=d))

for (k in 1:length(d)){
  for(i in 1:length(leads)){   
 res[i, k] <-   print(ks.test(sample(lead_en, 75), sample(leads[[i]], 75)))$p.value    
  }
}

Which does not work. I hope this clarifies what I’m trying to do.

  • What are `leads_one`, `leads_two`, three, etc.? – Rich Scriven May 03 '14 at 09:55
  • A list of vectors. leads_one etc. are vectors of lead time. I want t o run the ks.test on lead_en and all the vectors in leads. – user3598791 May 03 '14 at 09:56
  • Welcome to SO. Please [read this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Rich Scriven May 03 '14 at 09:58
  • Thanks for the additional info, but the example is not self-contained, i.e. we do not have your data. In addition, `which does not work` is a rather vague description of what is going wrong. Have a look a the link @RichardScriven posted for ways to provide us with your data. – Paul Hiemstra May 03 '14 at 13:32
  • I added some data, should be reproducible. – user3598791 May 03 '14 at 14:51

1 Answers1

0

Try:

 sapply(leads, function(x) ks.test(sample(lead_en, 75), sample(x, 75)))$p.value)

I think I cannot help more, because you haven't provided enough informations.

Davide Passaretti
  • 2,741
  • 1
  • 21
  • 32