Dear R users, I am running simulations (1000), and in my simulation I am looking at specific sums. For example, if the sum is >=4 then count this, if say <3, then don't count, if the sum=3, then generate a random number from uniform distribution, if this number is say less than 0.5, then count this sum, if greater than 0.5, then don't count. I am having trouble with introducing this uniform number and decide whether to count 3 or not. Any help or hint will be greatly appreciated. Thank you very much in advance [[alternative HTML version deleted]]
jim holtman
2011-Nov-25 15:18 UTC
[R] counting values with some conditions in a simulation
How are you computing the sum? Does FAQ 7.31 apply? Showing at least a sample of your code would help. On Friday, November 25, 2011, Sl K <s.karmv@gmail.com> wrote:> Dear R users, > > I am running simulations (1000), and in my simulation I am looking at > specific sums. For example, if the sum is >=4 then count this, if say <3, > then don't count, if the sum=3, then generate a random number from uniform > distribution, if this number is say less than 0.5, then count this sum, if > greater than 0.5, then don't count. I am having trouble with introducing > this uniform number and decide whether to count 3 or not. Any help orhint> will be greatly appreciated. Thank you very much in advance > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. [[alternative HTML version deleted]]
Jeff Newmiller
2011-Nov-25 15:19 UTC
[R] counting values with some conditions in a simulation
You need to read the posting guide. Provide a reproducible code sample, simplified, with self-contained data. You might find the "ave" function useful if you are working with vectorized simulations. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Sl K <s.karmv at gmail.com> wrote:>Dear R users, > >I am running simulations (1000), and in my simulation I am looking at >specific sums. For example, if the sum is >=4 then count this, if say ><3, >then don't count, if the sum=3, then generate a random number from >uniform >distribution, if this number is say less than 0.5, then count this sum, >if >greater than 0.5, then don't count. I am having trouble with >introducing >this uniform number and decide whether to count 3 or not. Any help or >hint >will be greatly appreciated. Thank you very much in advance > > [[alternative HTML version deleted]] > >______________________________________________ >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.
Jeff Newmiller
2011-Nov-25 18:56 UTC
[R] counting values with some conditions in a simulation
A) you need to reply-all to keep the discussion on the mailing list. B) you need to post in plain text. C) this has the arbitrary smell of homework. This is not a homework help line. D) You are overwriting your accumulation variable "sumt" after each test. Since you are not handling this calculation in a vectorized manner, I suggest you use the "if ... else ... else" syntax to accomplish this. See the help for "if". --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Sl K <s.karmv at gmail.com> wrote:>Sorry, I forgot to include my code. Here is what I am trying to do. > > > >rep=10 > >results<-numeric(rep) > >x<- data.frame(matrix(runif(10*15),15)) > >y<- data.frame(matrix(runif(10*15),15)) > >for (i in c(1:rep)){ > >st<-data.frame(y=c(x[,i],y[,i]),samp=factor(c(rep("X",15),rep("Y",15)))) > >stt<-st[order(st[,1]),] > >dt<-stt[1:30,] > >r<-as.vector( dt$samp) > > tt<-rle(r)$lengths[rle(r)$values == "X"] > >sumt<-sum(tt[1:3]) > >sumt[sumt <=3] <- 0 > > sumt[sumt>3]<- 1 > >sums<-as.numeric(sumt) > >results[i] <- sums > >} > >xx<-as.vector(results) > >sum(xx) > > >This was the original code I had, before I was just counting how many >will >give me a sum more than 3. Now, I want to show that if sumt<3 then 0, >if >sum>3 then 1, if sum=3, then generate a random number from uniform >distribution, if this number is say less than 0.5 then it's 1, if >greater >than 0.5, then it's 0. > >Thank you very much for your help. > > >On Fri, Nov 25, 2011 at 10:19 AM, Jeff Newmiller ><jdnewmil at dcn.davis.ca.us>wrote: > >> You need to read the posting guide. Provide a reproducible code >sample, >> simplified, with self-contained data. >> You might find the "ave" function useful if you are working with >> vectorized simulations. >> >--------------------------------------------------------------------------- >> Jeff Newmiller The ..... ..... Go >Live... >> DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live >> Go... >> Live: OO#.. Dead: OO#.. >Playing >> Research Engineer (Solar/Batteries O.O#. #.O#. with >> /Software/Embedded Controllers) .OO#. .OO#. >rocks...1k >> >--------------------------------------------------------------------------- >> Sent from my phone. Please excuse my brevity. >> >> Sl K <s.karmv at gmail.com> wrote: >> >> >Dear R users, >> > >> >I am running simulations (1000), and in my simulation I am looking >at >> >specific sums. For example, if the sum is >=4 then count this, if >say >> ><3, >> >then don't count, if the sum=3, then generate a random number from >> >uniform >> >distribution, if this number is say less than 0.5, then count this >sum, >> >if >> >greater than 0.5, then don't count. I am having trouble with >> >introducing >> >this uniform number and decide whether to count 3 or not. Any help >or >> >hint >> >will be greatly appreciated. Thank you very much in advance >> > >> > [[alternative HTML version deleted]] >> > >> >______________________________________________ >> >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. >> >>