[R] Need some help

jim holtman jholtman at gmail.com
Tue Oct 16 20:06:47 CEST 2007


If what you are asking for is to see how many times it crosses the
axis when 'accumulating' the values of the top (+1, -1), then the
following will do it - this is for 1000 and shows there are 32
crossings of the axis.

> x <- sample(c(-1,1), 1000, TRUE)
> plot(cumsum(x), type='l')
> # now if you are looking at when it crosses the axis
> cumValue <- cumsum(x)
> signChanges <- ifelse(cumValue >= 0, 1, -1)
> # number of times it crosses the axis
> sum(diff(signChanges) != 0)
[1] 32


On 10/16/07, azzza <azza.khogaliali at utoronto.ca> wrote:
>
> Thats what is frustrating me....I've done a lot of reading, but there doesnt
> seem to be much info on sign changes..............I  dont udnerstand why the
> poportion of sign changes is much less than 50%, despite the proportion of
> heads being equal to that of tails.
>
>
> Marc Schwartz wrote:
> >
> > 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.
> >> >
> >> >
> >>
> >
> > ______________________________________________
> > 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.
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Need-some-help-tf4624513.html#a13238567
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list