[R] Draw values from multiple data sets as inputs to a Monte-Carlo function; then apply across entire matrix

Diann Prosser dprosser at usgs.gov
Mon Mar 12 16:52:20 CET 2012


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.



More information about the R-help mailing list