[R] How to do a stochastic model in R

David Winsemius dwinsemius at comcast.net
Tue Nov 23 22:48:24 CET 2010


On Nov 23, 2010, at 3:26 PM, Barroso, Judit wrote:

> Dear all of you,
>
> 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?

You probably need to read:

?replicate   # and/or ...
?rnorm

And rethink your question. (there is no random draw or sampling being  
done in your code.)

>
> 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

Please, please, don't call objects by that name. It's a function name  
and a very commonly used one at that. And it's even doubly confusing  
because data.frame is not a data.frame. Call it dmtx or dfm,  
anything,  just don't call it Sue.


dmtx<- matrix(0,20,7)
dmtx[,1] <- 1:20
dmtx[1, 2:7 ] <- c(50,18,3,300,NA,NA)
dmtx[-1, 2] <- dmtx[(x-1),2]-dmtx[(x-1),3] + dmtx[(x-1),5]*rnorm(1,sr,  
SDVsr)*rnorm(1, ss, SDVss)

> 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]

There is also a diff function you might want to get familiar with.

>                                data.frame[x,7] <- data.frame[x,2]/  
> data.frame[(x-1),2]
>                                }
>                }
> end
> data.frame
>
> Thank you very much in advanced,
> Judit Barroso
>
> 	[[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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list