better loop for simulation

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

better loop for simulation

Naresh Gurbuxani
I want to calculate a function many times over.  My solution below works, but does not seem very elegant.  

# my function to run many times over
stud.score <- function(n.questions, mult.choice = 2) {
        prob.success <- 1 / mult.choice
        answers <- (runif(n.questions) < prob.success)
        return(sum(answers))
}

# my method to run above function 1000 times and store results
count.df <- data.frame(n.count = rep(10, 1000))
scores.df <- apply(count.df, 1, function(x) return(stud.score(x)))

Creating a data frame just to repeat the the count seems wasteful.  How can I generate scores.df without count.df?

Thanks,
Naresh
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: better loop for simulation

jholtman
use replicate:

> stud.score <- function(n.questions, mult.choice = 2) {
+          prob.success <- 1 / mult.choice
+          answers <- (runif(n.questions) < prob.success)
+          return(sum(answers))
+  }
>
> # create 1000 results
> result <- replicate(1000, stud.score(10))
>
> # look at histogram
> stem(result)
  The decimal point is at the |
   0 | 0
   1 | 0000000000000
   2 | 000000000000000000000000000000000000000000000000000000000
   3 |
00000000000000000000000000000000000000000000000000000000000000000000+26
   4 |
00000000000000000000000000000000000000000000000000000000000000000000+116
   5 |
00000000000000000000000000000000000000000000000000000000000000000000+153
   6 |
00000000000000000000000000000000000000000000000000000000000000000000+142
   7 |
00000000000000000000000000000000000000000000000000000000000000000000+35
   8 | 00000000000000000000000000000000000000000000
   9 | 000000000000
  10 | 0


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.

On Sat, Jun 18, 2016 at 6:12 PM, Naresh Gurbuxani <
[hidden email]> wrote:

> I want to calculate a function many times over.  My solution below works,
> but does not seem very elegant.
>
> # my function to run many times over
> stud.score <- function(n.questions, mult.choice = 2) {
>         prob.success <- 1 / mult.choice
>         answers <- (runif(n.questions) < prob.success)
>         return(sum(answers))
> }
>
> # my method to run above function 1000 times and store results
> count.df <- data.frame(n.count = rep(10, 1000))
> scores.df <- apply(count.df, 1, function(x) return(stud.score(x)))
>
> Creating a data frame just to repeat the the count seems wasteful.  How
> can I generate scores.df without count.df?
>
> Thanks,
> Naresh
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: better loop for simulation

Duncan Murdoch-2
In reply to this post by Naresh Gurbuxani
On 18/06/2016 6:12 PM, Naresh Gurbuxani wrote:

> I want to calculate a function many times over.  My solution below works, but does not seem very elegant.
>
> # my function to run many times over
> stud.score <- function(n.questions, mult.choice = 2) {
> prob.success <- 1 / mult.choice
> answers <- (runif(n.questions) < prob.success)
> return(sum(answers))
> }
>
> # my method to run above function 1000 times and store results
> count.df <- data.frame(n.count = rep(10, 1000))
> scores.df <- apply(count.df, 1, function(x) return(stud.score(x)))
>
> Creating a data frame just to repeat the the count seems wasteful.  How can I generate scores.df without count.df?
>
> Thanks,

You don't need a data frame or a loop at all.  You're simulating
binomial values, and R has rbinom() to do that in a vectorized way.

Duncan

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: better loop for simulation

Dan D
In reply to this post by Naresh Gurbuxani
try:

n.questions <- 10  # or however many you want
mult.choice <- 2
scores <- rbinom(1000, size = n.questions, prob = 1/mult.choice)


On Sat, Jun 18, 2016 at 3:12 PM, Naresh Gurbuxani <
[hidden email]> wrote:

> I want to calculate a function many times over.  My solution below works,
> but does not seem very elegant.
>
> # my function to run many times over
> stud.score <- function(n.questions, mult.choice = 2) {
>         prob.success <- 1 / mult.choice
>         answers <- (runif(n.questions) < prob.success)
>         return(sum(answers))
> }
>
> # my method to run above function 1000 times and store results
> count.df <- data.frame(n.count = rep(10, 1000))
> scores.df <- apply(count.df, 1, function(x) return(stud.score(x)))
>
> Creating a data frame just to repeat the the count seems wasteful.  How
> can I generate scores.df without count.df?
>
> Thanks,
> Naresh
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>



--
Dan Dalthorp, PhD
USGS Forest and Rangeland Ecosystem Science Center
Forest Sciences Lab, Rm 189
3200 SW Jefferson Way
Corvallis, OR 97331
ph: 541-750-0953
[hidden email]

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: better loop for simulation

Naresh Gurbuxani
Daniel, Duncan, and Jim,


Many thanks for your prompt responses.  My example function is indeed binomial, for which a built function already exists.  But my goal is to find a general solution which would work for other functions as well.

replicate() works well for me.

Naresh
________________________________
From: Dalthorp, Daniel <[hidden email]>
Sent: Saturday, June 18, 2016 7:31 PM
To: Naresh Gurbuxani
Cc: [hidden email]
Subject: Re: [R] better loop for simulation

try:

n.questions <- 10  # or however many you want
mult.choice <- 2
scores <- rbinom(1000, size = n.questions, prob = 1/mult.choice)


On Sat, Jun 18, 2016 at 3:12 PM, Naresh Gurbuxani <[hidden email]<mailto:[hidden email]>> wrote:
I want to calculate a function many times over.  My solution below works, but does not seem very elegant.

# my function to run many times over
stud.score <- function(n.questions, mult.choice = 2) {
        prob.success <- 1 / mult.choice
        answers <- (runif(n.questions) < prob.success)
        return(sum(answers))
}

# my method to run above function 1000 times and store results
count.df <- data.frame(n.count = rep(10, 1000))
scores.df <- apply(count.df, 1, function(x) return(stud.score(x)))

Creating a data frame just to repeat the the count seems wasteful.  How can I generate scores.df without count.df?

Thanks,
Naresh
______________________________________________
[hidden email]<mailto:[hidden email]> mailing list -- To UNSUBSCRIBE and more, see
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.



--
Dan Dalthorp, PhD
USGS Forest and Rangeland Ecosystem Science Center
Forest Sciences Lab, Rm 189
3200 SW Jefferson Way
Corvallis, OR 97331
ph: 541-750-0953
[hidden email]<mailto:[hidden email]>


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.