[R] Optimization in R similar to MS Excel Solver
Berend Hasselman
bhh at xs4all.nl
Fri Oct 26 19:10:53 CEST 2012
On 26-10-2012, at 12:50, Richard James wrote:
> Dear Berend and Thomas,
>
> thank you for suggesting the lsei function. I found that the tlsce {BCE}
> function also works very well:
>
> library("BCE")
> tlsce(A=bmat,B=target)
>
> The limSolve package has an 'xsample' function for generating uncertainty
> values via Monte-Carlo simulation, however it only works when specifying the
> standard deviation on the target data (B). In my situation I have standard
> deviations for the source areas (A) only. Therefore I need to generate the
> uncertainty values manually.
>
> I've created a matrix of 1000 randonly distributed numbers for each of the
> source area (A) properties:
>
> TSCa<-matrix(rnorm(1000, mean=0.03, sd=0.005),ncol=1)
> TSMg<-matrix(rnorm(1000, mean=0.0073, sd=0.002),ncol=1)
> CBCa<-matrix(rnorm(1000, mean=0.6, sd=0.1),ncol=1)
> CBMg<-matrix(rnorm(1000, mean=0.0102, sd=0.005),ncol=1)
> RCa<-matrix(rnorm(1000, mean=0.2, sd=0.05),ncol=1)
> RMg<-matrix(rnorm(1000, mean=0.0141, sd=0.005),ncol=1)
> DCa<-matrix(rnorm(1000, mean=0.35, sd=0.1),ncol=1)
> DMg<-matrix(rnorm(1000, mean=0.012, sd=0.004),ncol=1)
>
> DistAll<-cbind(TSCa,TSMg,CBCa,CBMg,RCa,RMg,DCa,DMg)
> colnames(DistAll)<-c("TopSoilCa","TopSoilMg","ChannelBankCa","ChannelBankMg","RoadCa","RoadMg","DrainCa","DrainMg")
>
> I now want to run the lsei model again:
>
> lsei(A=bmat,B=target,E=matrix(rep(1,4),ncol=4),F=1,G=matrix(rep(1,4),ncol=4),H=0,
> fulloutput=TRUE))
>
> but with A=bmat replaced by the appropriate random values in DistAll. I
> assume I could then use the function "replicate" to then run the model 1000
> times to generate uncertainty values, e.g.
>
> replicate(n=1000,lsei(A=bmat,B=target,E=matrix(rep(1,4),ncol=4),F=1,G=matrix(rep(1,4),ncol=4),H=0,
> fulloutput=TRUE))
>
> Would anyone be able to help me write a function to replace bmat with new
> values from DistAll each time the lsei model is run?
I'm not sure if you can do that with replicate.
However, the first matter to be resolved is: how is bmat related to Distall? Which rows/columns should go into a single instance of bmat?
In addition you will need to store the output of lsei somewhere.
So it would be seem to be a good idea to make a function that runs a single instance of lsei given the input parameters Distall, .... so that it can generate a bmat, run lsei and return the output in a form usable for further analysis.
When that is available you can start thinking about your simulation.
Berend
More information about the R-help
mailing list