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? 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 Thank you very much in advanced, Judit Barroso [[alternative HTML version deleted]]
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
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.framePlease, 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
Apparently Analagous Threads
- vector("integer", length) : vector size specified is too large
- assign()ing within apply
- Degrees of Freedom Not Allocated to Residuals in Reduced Model
- newbie questions - looping through hierarchial datafille
- Can I index a dataframe with a reference from/to a second dataframe?