# [R] Need some help

Marc Schwartz marc_schwartz at comcast.net
Tue Oct 16 18:16:54 CEST 2007

```You might want to do some more research on "Bernoulli Trials" and "Fair
Coins", which would provide some enlightenment on why you would not
"expect" 500 sign changes in 1,000 tosses and why you should, if the
coin is "fair", expect to *approach* a 50/50 distribution of heads and
tails in a large number of tosses, but not actually observe it.

HTH,

Marc Schwartz

On Tue, 2007-10-16 at 09:05 -0700, azzza wrote:
>
> You are right, I was a bit too vague. I am trying to simulate 1000 coin
> Tosses. Then to write a code for the number of heads/Tails.....naturally, we
> would expect the proportion of heads to be 50% so the number of heads would
> be around 500. Secondly, I'm trying to look at the number of sign changes in
> 1000 tosses. The example in the book shows that the number of sign changes
> is WAY less than 50%. so I was  trying to look for a code that shows the
> number of sign changes, which someone pointed out would be to use
> rle(x).....my concern however is that when the rle(x) is used, the
> proportion of sign changes is about 50% (around 500), which would be logical
> to expect, however, real life experiments have shown that the number of sign
> changes is actually much less than 50%. The other thing I was trying to do
> is to plot a line graph of number of sign changes versus the number of
> tosses. Yes, the number of tosses should begin at zero, and at zero coin
> toss, the sign change is zero. The range of the number of sign changes in
> the Y-axis should include negative values of Y. So, for toss number zero,
> the sign change is 0, for the first toss, the sign change may be +3 for
> instance, for the 50th toss, the sign change may be -5 for instance. The
> plot shown in the book shows that for most of the tosses, the sign changes
> were negative, and this would explain how the overall number of sign changes
> is very low.
>
> so basically, i was looking for a code to show the number of sign changes,
> and a plot of sign changes versus toss number (from toss #0 to toss number
> 1000). The Y axis (number of sign changes, should include negative values of
> Y)
>
> thanks
>
>
>
> Daniel Nordlund wrote:
> >
> >> -----Original Message-----
> >> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> >> On Behalf
> >> Of azzza
> >> Sent: Monday, October 15, 2007 6:06 PM
> >> To: r-help at r-project.org
> >> Subject: Re: [R] Need some help
> >>
> >>
> >>
> >> Thanks Jholtman.
> >> However, the plot didnt come out the way I envisone dit to be. On the Y
> >> axis, i should have sign changes in 1000 tosses, the range being from
> >> negative to postitive, and a straight horizontal line across  y=0. The
> >> X-axis should have the toss number, range 0-1000
> >>
> >
> > You would probably get more prompt and useful help if you would provide a
> > small, self- contained example of what you wanted (maybe small enough that
> > you could work it by hand).  Also, if you provided some code using the
> > help that you have already received, that would show that you are trying
> > to solve the problem yourself and others could help with the specific R
> > program issues that you are having.  For example, it is not clear to me if
> > you want to count the first run of tosses as 0 or 1 sign change.  I will
> > assume it is zero.  So with that assumption, does this get you what you
> > want?
> >
> > n <- 1000
> > x <- sample(c(0,1), n, TRUE)
> > y <- rle(x)
> >
> > z <- rep(1:length(y\$lengths),y\$lengths)
> > plot(1:n,z-1)
> >
> > Hope this is helpful,
> >
> > Dan
> >
> > Daniel Nordlund
> > Bothell, WA USA
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
> >
>

```