0

So I am finding V values of a Wilcoxon sum ranked test from 10 uniformly distributed RV's. I can do this once like this

uniform=runif(10,min=0,max=1)
diff <- c(uniform - .5) 
 diff <- diff[ diff!=0 ] 
 diff.rank <- rank(abs(diff)) 
 diff.rank.sign <- diff.rank * sign(diff) 
 ranks.pos <- sum(diff.rank.sign[diff.rank.sign > 0])

but now I need to repeat this process 10,000 times and make a histogram out of it. How can I do this?

Nich
  • 45
  • 1
  • 6

1 Answers1

1

Try setting a dataframe to save the results and use a loop like this. The plot will be designed with ggplot2. Here the code:

library(dplyr)
library(ggplot2)
#Dataframe for storing results
N <- 10000
df <- data.frame(Var=rep(NA,N))
#Loop
for(i in 1:N)
{
  #Code
  uniform=runif(10,min=0,max=1)
  diff <- c(uniform - .5) 
  diff <- diff[ diff!=0 ] 
  diff.rank <- rank(abs(diff)) 
  diff.rank.sign <- diff.rank * sign(diff) 
  ranks.pos <- sum(diff.rank.sign[diff.rank.sign > 0])
  #Save
  df$Var[i] <- ranks.pos
}
#Plot
ggplot(df,aes(x=Var))+
  geom_histogram(fill='cyan3',color='black')

The output for the plot will be:

enter image description here

And the results for each N iteration will be in df.

Duck
  • 39,058
  • 13
  • 42
  • 84
  • Awesome looks like it worked for me too! Now I just need to find the rejection region at 5% sig lvl,, thanks! – Nich Oct 14 '20 at 19:39
  • @Nich Great! If you are interested in helping please check this https://stackoverflow.com/help/someone-answers – Duck Oct 14 '20 at 20:20