parallelisation computing
I already show you how to do for
loop and nested for
loop. When you
want to do parallelisation computing, you simply replace the for
command by the foreach
command and %dopart%
.
Depending if you are on Windows, MacOS or Linux, you will need to do the parallelisation differently. I personally do it on Windows and Linux.
Within a foreach
loop, you will need to declare the loop variable
as you usually do in the for
command, but also to declare the
package that you are going to use within the foreach loop using the
command .packages
. You also need to set the .multicombine
to TRUE
is you want combine all the parallelism computing within a single
output and finally you can use the .final
argument to customise the
output (list of list, etc.).
Keep in mind that the parallelisation will be processing using the
number of core that you declare in the foreach
session and those
results will be sort in the same
First we create 100 random point patterns with 2
different marks "A" and "B" :
$`1` Marked planar point pattern: 70 points Multitype, with levels = A, B window: rectangle = [0, 1] x [0, 1] units $`2` Marked planar point pattern: 70 points Multitype, with levels = A, B window: rectangle = [0, 1] x [0, 1] units
Before going more further, you will need to install the following package in R: doMC (or doSNOW if you use Windows), spptest and marksummary package which are 2 greats packages when you do spatial point pattern (you can found them at https://github.com/myllym/GET for the spptest package and at https://github.com/myllym/marksummary for the mark summary package).
The following code will compute the "Lest" function overall 30
spatial pattern simulation (usually this is 2499 simulation) from the random data set. The "Lest" function will
basically see if the mark "A" from the spatial pattern is randomly
distributed regarding the mark "B" across the spatial pattern (this is
define in the simulate
argument in the envelope
function. Then, we
compute a rank envelope test (see here for more details). Thus, if the
result from the rank envelope test is significant and when we plot it
the observed data are above the envelope, then "A" and "B" have a
positive correlation relationship (i.e. there are attracted one to another), if this is below the envelope threshold,
then "A" and "B" have a negative correlation relationship (i.e. there
are segregate one to another).
Finally, we put the result of the rank envelope test into a list with also the random labelling residual data.
It will took some time to compute every 100 spatial patterns.
Then you should have this list of result:
$`1` $`1`$rank_env_NA Rank envelope test p-value of the test: 0.3225806 (ties method: midrank) p-interval : (0.03225806, 0.6129032) $`1`$random_labelling curve_set object containing : List of 4 $ r : num [1:513] 0 0.000488 0.000977 0.001465 0.001953 ... $ obs : num [1:513] 0 -0.000488 -0.000977 -0.001465 -0.001953 ... $ sim_m : num [1:513, 1:30] 0 -0.000488 -0.000977 -0.001465 -0.001953 ... ..- attr(*, "dimnames")=List of 2 .. ..$ : chr [1:513] "1" "2" "3" "4" ... .. ..$ : chr [1:30] "sim1" "sim2" "sim3" "sim4" ... $ is_residual: logi TRUE - attr(*, "class")= chr "curve_set" $`2` $`2`$rank_env_NA Rank envelope test p-value of the test: 0.8709677 (ties method: midrank) p-interval : (0.8064516, 0.9354839) $`2`$random_labelling curve_set object containing : List of 4 $ r : num [1:513] 0 0.000488 0.000977 0.001465 0.001953 ... $ obs : num [1:513] 0 -0.000488 -0.000977 -0.001465 -0.001953 ... $ sim_m : num [1:513, 1:30] 0 -0.000488 -0.000977 -0.001465 -0.001953 ... ..- attr(*, "dimnames")=List of 2 .. ..$ : chr [1:513] "1" "2" "3" "4" ... .. ..$ : chr [1:30] "sim1" "sim2" "sim3" "sim4" ... $ is_residual: logi TRUE - attr(*, "class")= chr "curve_set"
You can now plot the results from the rank envelope test: