[R] Need some help
Thomas Lumley
tlumley at u.washington.edu
Wed Oct 17 00:03:41 CEST 2007
On Tue, 16 Oct 2007, azzza 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.
>
I think some earlier posters have misunderstood what you are trying to do: they thought you were interested in runs of heads of tails, but it seems to me that you are interested in whether the sample proportion is above or below 1/2.
Code to compute the number of sign changes looks like
x<-rbinom(1000,1,0.5)
proportion <- cumsum(x)/(1:1000)
above <- proportion > 1/2
nchanges<-length(rle(above)$values)
You might also want to plot the cumulative sum
plot(1:1000, cumsum(x))
abline(h=0)
Looking at the plot you should be able to see why the number of changes is relatively small: to get a sign change you need the curve to go in the right direction (50:50) but you also need to be close enough to zero.
-thomas
> 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.
>
Thomas Lumley Assoc. Professor, Biostatistics
tlumley at u.washington.edu University of Washington, Seattle
More information about the R-help
mailing list