if (FALSE) {
## Not run in checks: excluded to keep MPI examples stable and check times short.
## Example 1: Bivariate Mixed Data
## The following example is adapted for interactive parallel execution
## in R. Here we spawn 1 slave so that there will be two compute nodes
## (master and slave). Kindly see the batch examples in the demos
## directory (npRmpi/demos) and study them carefully. Also kindly see
## the more extensive examples in the np package itself. See the npRmpi
## vignette for further details on running parallel np programs via
## vignette("npRmpi",package="npRmpi").
## Start npRmpi for interactive execution. If slaves are already running and
## `options(npRmpi.reuse.slaves=TRUE)` (default on some systems), this will
## reuse the existing pool instead of respawning. To change the number of
## slaves, call `npRmpi.stop(force=TRUE)` then restart.
npRmpi.start(nslaves=1)
mpi.bcast.cmd(library(MASS),caller.execute=TRUE)
set.seed(42)
## Simulate correlated Gaussian data (rho(x,y)=0.99)
n <- 1000
n.eval <- 100
rho <- 0.99
mu <- c(0,0)
Sigma <- matrix(c(1,rho,rho,1),2,2)
mydat <- mvrnorm(n=n, mu, Sigma)
mydat <- data.frame(x=mydat[,1],
y=ordered(as.integer(cut(mydat[,2],
quantile(mydat[,2],seq(0,1,by=.1)),
include.lowest=TRUE))-1))
q.min <- 0.0
q.max <- 1.0
grid.seq <- seq(q.min,q.max,length=n.eval)
grid.dat <- cbind(grid.seq,grid.seq)
mpi.bcast.Robj2slave(mydat)
mpi.bcast.Robj2slave(grid.dat)
## Estimate the copula (bw object obtained from npudistbw())
mpi.bcast.cmd(bw.cdf <- npudistbw(~x+y,data=mydat),
caller.execute=TRUE)
mpi.bcast.cmd(copula <- npcopula(bws=bw.cdf,data=mydat,u=grid.dat),
caller.execute=TRUE)
## Plot the copula
contour(grid.seq,grid.seq,matrix(copula$copula,n.eval,n.eval),
xlab="u1",
ylab="u2",
main="Copula Contour")
persp(grid.seq,grid.seq,matrix(copula$copula,n.eval,n.eval),
ticktype="detailed",
xlab="u1",
ylab="u2",
zlab="Copula",zlim=c(0,1))
## Plot the empirical copula
mpi.bcast.cmd(copula.emp <- npcopula(bws=bw.cdf,data=mydat),
caller.execute=TRUE)
plot(copula.emp$u1,copula.emp$u2,xlab="u1",ylab="u2",cex=.25,main="Empirical Copula")
## Estimate the copula density (bw object obtained from npudensbw())
mpi.bcast.cmd(bw.pdf <- npudensbw(~x+y,data=mydat),
caller.execute=TRUE)
mpi.bcast.cmd(copula <- npcopula(bws=bw.pdf,data=mydat,u=grid.dat),
caller.execute=TRUE)
## Plot the copula density
persp(grid.seq,grid.seq,matrix(copula$copula,n.eval,n.eval),
ticktype="detailed",
xlab="u1",
ylab="u2",
zlab="Copula Density")
## For the interactive run only we close the slaves perhaps to proceed
## with other examples and so forth. This is redundant in batch mode.
## Note: on some systems (notably macOS+MPICH), repeatedly spawning and
## tearing down slaves in the same R session can lead to hangs/crashes.
## npRmpi may therefore keep slave daemons alive by default and
## `npRmpi.stop()` performs a "soft close". Use `force=TRUE` to
## actually shut down the slaves.
##
## You can disable reuse via `options(npRmpi.reuse.slaves=FALSE)` or by
## setting the environment variable `NP_RMPI_NO_REUSE_SLAVES=1` before
## loading the package.
npRmpi.stop() ## soft close (may keep slaves alive)
## npRmpi.stop(force=TRUE) ## hard close
## Note that in order to exit npRmpi properly avoid quit(), and instead
## use mpi.quit() as follows.
## mpi.bcast.cmd(mpi.quit(),
## caller.execute=TRUE)
## Example 2: Bivariate Continuous Data
## The following example is adapted for interactive parallel execution
## in R. Here we spawn 1 slave so that there will be two compute nodes
## (master and slave). Kindly see the batch examples in the demos
## directory (npRmpi/demos) and study them carefully. Also kindly see
## the more extensive examples in the np package itself. See the npRmpi
## vignette for further details on running parallel np programs via
## vignette("npRmpi",package="npRmpi").
## Start npRmpi for interactive execution. If slaves are already running and
## `options(npRmpi.reuse.slaves=TRUE)` (default on some systems), this will
## reuse the existing pool instead of respawning. To change the number of
## slaves, call `npRmpi.stop(force=TRUE)` then restart.
npRmpi.start(nslaves=1)
mpi.bcast.cmd(library(MASS),caller.execute=TRUE)
set.seed(42)
## Simulate correlated Gaussian data (rho(x,y)=0.99)
n <- 1000
n.eval <- 100
rho <- 0.99
mu <- c(0,0)
Sigma <- matrix(c(1,rho,rho,1),2,2)
mydat <- mvrnorm(n=n, mu, Sigma)
mydat <- data.frame(x=mydat[,1],y=mydat[,2])
q.min <- 0.0
q.max <- 1.0
grid.seq <- seq(q.min,q.max,length=n.eval)
grid.dat <- cbind(grid.seq,grid.seq)
mpi.bcast.Robj2slave(mydat)
mpi.bcast.Robj2slave(grid.dat)
## Estimate the copula (bw object obtained from npudistbw())
mpi.bcast.cmd(bw.cdf <- npudistbw(~x+y,data=mydat),
caller.execute=TRUE)
mpi.bcast.cmd(copula <- npcopula(bws=bw.cdf,data=mydat,u=grid.dat),
caller.execute=TRUE)
## Plot the copula
contour(grid.seq,grid.seq,matrix(copula$copula,n.eval,n.eval),
xlab="u1",
ylab="u2",
main="Copula Contour")
persp(grid.seq,grid.seq,matrix(copula$copula,n.eval,n.eval),
ticktype="detailed",
xlab="u1",
ylab="u2",
zlab="Copula",
zlim=c(0,1))
## Plot the empirical copula
mpi.bcast.cmd(copula.emp <- npcopula(bws=bw.cdf,data=mydat),
caller.execute=TRUE)
plot(copula.emp$u1,copula.emp$u2,xlab="u1",ylab="u2",cex=.25,main="Empirical Copula")
## Estimate the copula density (bw object obtained from npudensbw())
mpi.bcast.cmd(bw.pdf <- npudensbw(~x+y,data=mydat),
caller.execute=TRUE)
mpi.bcast.cmd(copula <- npcopula(bws=bw.pdf,data=mydat,u=grid.dat),
caller.execute=TRUE)
## Plot the copula density
persp(grid.seq,grid.seq,matrix(copula$copula,n.eval,n.eval),ticktype="detailed",xlab="u1",
ylab="u2",zlab="Copula Density")
## For the interactive run only we close the slaves perhaps to proceed
## with other examples and so forth. This is redundant in batch mode.
## Note: on some systems (notably macOS+MPICH), repeatedly spawning and
## tearing down slaves in the same R session can lead to hangs/crashes.
## npRmpi may therefore keep slave daemons alive by default and
## `npRmpi.stop()` performs a "soft close". Use `force=TRUE` to
## actually shut down the slaves.
##
## You can disable reuse via `options(npRmpi.reuse.slaves=FALSE)` or by
## setting the environment variable `NP_RMPI_NO_REUSE_SLAVES=1` before
## loading the package.
npRmpi.stop() ## soft close (may keep slaves alive)
## npRmpi.stop(force=TRUE) ## hard close
## Note that in order to exit npRmpi properly avoid quit(), and instead
## use mpi.quit() as follows.
## mpi.bcast.cmd(mpi.quit(),
## caller.execute=TRUE)
}
Run the code above in your browser using DataLab