[R] random value changes in a vector
David Winsemius
dwinsemius at comcast.net
Wed Nov 19 17:41:12 CET 2008
Using rbinom might be more clear than my sample() solution. May I
speculate without actual testing that:
inv <- 2*rbinom(length(vec),1,0.5) - 1
might be more efficient than a 2-step rbinom / ifelse strategy?
--
David Winsemius
Heritage Labs
On Nov 18, 2008, at 5:21 PM, joris meys wrote:
> The function rbinom might be a solution.
>
> Try following simple program :
> vec <- c(-1,1,-1,1,1,-1,-1,1,1)
>
> inv <-rbinom(length(vec),1,0.5)
> inv <-ifelse(inv==0,-1,1)
>
> vec2 <- vec*inv #switches sign with p=0.5
>
> In this, inv is a random binomial vector, where the probability for
> being 1 is 0.5 in all positions. Changing the values of 0 in this
> vector to -1, gives you a vector you can multiply with the original
> one to change the sign with a probability of 0.5 for all positions
>
> Kind regards
> Joris
> On Tue, Nov 18, 2008 at 6:11 PM, Salas, Andria Kay
> <aks2515 at uncw.edu> wrote:
>> To clear up a question regarding my earlier posting regarding
>> random changes in a vector:
>>
>> Say you have a vector (1, -1, -1, 1, 1, -1). I want each value in
>> the vector to have a probability that it will change signs when
>> this vector is regenerated. For example, probability = 50%. When
>> the vector is regenerated, the first value in the vector (1) has a
>> 50% chance of switching to -1. If I regenerated this vector 10
>> times, 5 of the times it would switch to -1. Similarly, I need
>> each value in the vector to have this same probability of switching
>> signs when the vector is regenerated, and each value's chances of
>> doing so is independent of the other values. So the second value
>> (-1) also has a 50% chance of switching to 1, and whether or not it
>> does so is independent of if the first value changes from 1 to -1
>> (also a 50% probability).
>>
>> I hope this clears up the confusion, and I would appreciate any
>> help anyone can provide!
>> ______________________________________________
>> 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.
More information about the R-help
mailing list