0

I want to study the distribution (histogram of the values of S greater than 0 for n=10000 replicated 10000 times. However, I'm not getting the correct output, how can I go about obtaining the histogram? Here's what I have:

rwlength=function(nsims,n){
t=numeric(nsims)
  for( i in (1:nsims))
{
  t[i]=aboveaxis(n)
}
return(t)
}
hist(rwlength(10000,10000))

Note:

aboveaxis = function(n) {
if (n<=0){ return(cat("n must be greater than 0"))}
else
step = c(1, -1)
S = c(0, cumsum(sample(step,n, prob=c(.5, .5), replace=T)))
above=sum(S[S > 0])
return(above)
}

aboveaxis is a function that returns the sum of the S values greater than 0

Thanks!

user3347124
  • 153
  • 1
  • 2
  • 7
  • Perhaps you can be more specific with exactly what aboveaxis is supposed to do? Are you just counting how many of the n are equal to 1? – user1357015 Mar 11 '14 at 04:58
  • aboveaxis is a function that returns the sum of the S values greater than 0 – user3347124 Mar 11 '14 at 04:59
  • I'm not sure why you're studying "S" but as coded, it works fine. When I plug in 1000 =nsims,n, it returns a histogram pronto. Are you concerned that it takes to long? – user1357015 Mar 11 '14 at 05:01
  • Ok. I just expected a histogram with more of a U-shape or a symmetric shape... – user3347124 Mar 11 '14 at 05:06
  • Why do you think the output is not correct? – jlhoward Mar 11 '14 at 05:06
  • @user3347124, output seems reasonable to me..most of the time, S is close to 0 since the E[X_1 + X_2 .... X_n] where X_i is 0 with prob .5 and 1 with prob .5 = 0. Since you're only taking the top half, you have the right half. If you were to plot a similar function "belowaxis" you would get a symmetric graph. – user1357015 Mar 11 '14 at 05:09

0 Answers0