[R] How to do a stochastic model in R

Carl Witthoft carl at witthoft.com
Tue Nov 23 22:31:42 CET 2010


Seems to me you're going about it in a bit of a difficult way.  Assuming 
you know you want to run 20 samples (x = 1 to 20), just generate sets of 
20 random variables for each of your inputs.

for example,

ss <-rnorm(20,mean=.3, sd=.08)

Then inside the loop,   replace "ss"  with "ss[x]"

By the way,  I found it a bit confusing that you used 'x' as an index. 
For historical reasons, and because 'x' is the most common name for a 
variable in all of math,  people usually use 'i,', 'j,' 'k,' or names 
beginning with those letters for indices.

Carl


<quote>
I would like to get some help from you.
Here I attach you a model, that I would like to be stochastic so I would 
need each time the value of x changed, the values of parameters (ss, 
emrg, gf, spp, sr) did too according to a normal distribution, with mean 
its value and standard deviation given by the parameter name preceded by 
SDV (In the case of parameter ss, mean is 0.3 and Standad deviation 
0.08). I explain myself, the model take the mean value of these 
parameter (ss, emrg, gf, spp, sr) and I would like for example, when x 
changed to the value 2, the parameters values changed randomly according 
to their normal distribution (given the mean and standard deviation 
values) and got another mean value. And when x was 3 again, and every 
time until 20. Does anyone, know how to do it?

ss <- 0.3
emrg <- 0.35
gf <- 0.22
spp <- 100
sr <- 0.52

SDVss <- 0.08
SDVemrg <- 0.05
SDVgf <- 0.04
SDVspp <- 22
SDVsr <- 0.1

data.frame <- matrix(0,20,7)
data.frame
data.frame[,1] <- 1:20
data.frame
for(x in 1:20){
                 if(x==1){
                                 data.frame[1,2] <- 50
                                 data.frame[1,3] <- 18
                                 data.frame[1,4] <- 3
                                 data.frame[1,5] <- 300
                                 data.frame[1,6] <- NA
                                 data.frame[1,7] <- NA


                                 }else{


                                 data.frame[x,2] <- 
(data.frame[(x-1),2]-data.frame[(x-1),3]+data.frame[(x-1),5]*sr)*ss
                                 data.frame[x,3] <- data.frame[x,2]*emrg
                                 data.frame[x,4] <- data.frame[x,3]*gf
                                 data.frame[x,5] <- data.frame[x,4]*spp
                                 data.frame[x,6] <- data.frame[x,2]- 
data.frame[(x-1),2]
                                 data.frame[x,7] <- data.frame[x,2]/ 
data.frame[(x-1),2]


                                 }

                 }


end
data.frame



More information about the R-help mailing list