Diann J Prosser
2012-Mar-10 19:13 UTC
[R] Draw values from multiple data sets as inputs to a Monte-Carlo function; then apply across entire matrix
Hi all, I am trying to implement a Monte-Carlo simulation for each cell in a spatial matrix (using mcd2 package) . I have figured out how to conduct the simulation using data from a single location (where I manually input distribution parameters into the R code), but am having trouble (a) adjusting the code to pull input variables from my various data sets and then (b) applying the entire process across each of the cells of the matrices. I have been doing a lot of reading about loops (a big no-no?), apply, and ddply, but can not quite figure it out. Here is the situation: Data: I have (for simplicity) 3 spatial raster data sets (each 4848 x 4053 cells) as ASCII files: -Poultry density (mean value in each cell) -Poultry density (standard deviation in each cell) -Wild bird density (single estimate in each cell) I read them into R using read.table. The data look correct: Pmn <- read.table("D:/Data/PoultryMeans.txt") Psd <- read.table("D:/Data/PoultryStDev.txt") Wde <- read.table("D:/Data/WildBirdDensity.txt") The Model: In the Monte-Carlo simulation, Poultry and Wild birds have different distributions (normal and triangle, respectively). Below are the 2 lines of code that use the mcstoc function to draw the samples. The values in bold are ones that I would like to draw from the data tables I read in above. For example, 3.5 would be cell (i,j) in the Poultry MEAN density table; 0.108 would be cell (i,j) in the Poultry STDEV table; and 47 the single estimate for cell (i,j) of the Wild bird density table. Poultry <- mcstoc(rnorm, type="U", 3.5, 0.108, rtrunc=TRUE, linf=0) Wild <- mcstoc(rtriang, type="U", min=0, mode=47, max=75) Risk <- Poultry * Wild #this is the risk function the MC is applied to Questions: 1) How can I edit the Poultry and Wild variables above to read the data values directly from the 3 input tables (i.e., replacing 3.5, 0.108, and 47 with some variable name for the data table and using a loop?? Or somehow use apply or ddply?) 2) Have the entire process be run for every cell in the 4848 x 4053 matrix? Thank you for any help you can provide to get me moving forward! Diann [[alternative HTML version deleted]]
Diann Prosser
2012-Mar-12 15:52 UTC
[R] Draw values from multiple data sets as inputs to a Monte-Carlo function; then apply across entire matrix
Dear all, I have developed a loop but receive an error (error 1 below) because the mcstoc function prohibits arrays. I tried creating an mcnode to represent the data frame, but received Error 2 below (array size not compatible with node dimension). I'm still at a loss on how to apply the Monte-Carlo simulation for each cell in a matrix. There is an mcapply function, but I did not have success applying it. Any insight would be much appreciated. Error 1:> stPte <- mcstoc(rnorm, type="U", Pmn, Psd, rtrunc=TRUE, linf=0)Error in function (argsd, typethismc) : Array prohibited in mcstoc as parameter. Use an mcnode instead> stWab <- mcstoc(rtriang, type="U", min=0, mode=Wab, max=75)Error in function (argsd, typethismc) : Array prohibited in mcstoc as parameter. Use an mcnode instead Error 2 (after setting up mcnode for each raster; see code below): Error in mcdata(Pmn, type = "0") : The array size is not compatible with the node dimension. Should be of dim: 1 1 1 Error in mcdata(Psd, type = "0") : The array size is not compatible with the node dimension. Should be of dim: 1 1 1 Error in mcdata(Wab, type = "0") : The array size is not compatible with the node dimension. Should be of dim: 1 1 1 ________________________________________________________________________ #Code - using 2x2 example matrices for each raster: #Construct reduced (2x2) matrices for testing code Pmn<- matrix(c(3.5, 2.1, 2.9, 3.2), nrow=2) Psd<- matrix(c(0.108, 0.117, 0.092, 0.125), nrow=2) Wab<- matrix(c(47, 33, 56, 44), nrow=2) m<- matrix(nrow=2, ncol=2) library(mc2d) #load package #create mcnodes for use in mcstoc (since array arguments not permitted); Error 2's occur after each of these 3 lines: dPmn<- mcdata(Pmn, type="0") dPsd<- mcdata(Psd, type="0") dWab<- mcdata(Wab, type="0") ndunc(1001) for (i in m) { stPte <- mcstoc(rnorm, type="U", dPmn, dPsd, rtrunc=TRUE, linf=0) stWab <- mcstoc(rtriang, type="U", min=0, mode=dWab, max=75) risk <- stPte * stWab } ____________________________________________________________________________ -- View this message in context: http://r.789695.n4.nabble.com/Draw-values-from-multiple-data-sets-as-inputs-to-a-Monte-Carlo-function-then-apply-across-entire-matx-tp4462874p4466426.html Sent from the R help mailing list archive at Nabble.com.