Can I run R parallel computing along side JuliaCall? - r

I have an R function code that runs with JuliaCall. However, due to the computation time, I would like to parallel compute my code. My code below produces an error that the Distributions and PoissonRandom packages in Julia do not exist even though the package has already been installed. My question: is it possible to run the parallel package, JuliaCall, and Julia packages?
two_part_sim_RTLs <- function(station_data){
julia_setup(JULIA_HOME = "C:/Users/Kenneth Kin Pomeyie/AppData/Local/Programs/Julia-1.6.1/bin/")
julia_library("Distributions")
julia_library("DataFrames")
julia_library("PoissonRandom")
lambda = station_data$lambda
location = station_data$location
scale = station_data$scale
shape = station_data$shape
julia_assign("lambda", lambda)
julia_assign("location", location)
julia_assign("scale", scale)
julia_assign("shape", shape)
julia_eval("x = rand(Truncated(Poisson(lambda), 0.0, 10), 50000000)")
julia_eval("load = Array{Float64}(undef, 50000000)")
julia_eval("
for i in 1:50000000
load[i] = max(rand(GeneralizedPareto(location, scale, shape)), x[i])
end
")
load = julia_eval("load")
}

Related

Is there an R function to parallelize nlminb()?

The function BEKK11 from the library MTS uses nlminb(start = par, objective = mlikeG, RTN = RTN, include.mean = include.mean, lower = c1, upper = c2) to optimize and thereby fit the BEKK model. Unfortunately, R uses only one core to optimize the function. This takes ages. Is there any way I can get R to use more cores on nlminb()? I have used the library doparallel before but this library only applies to looping routines and not to optimization functions.
Thank you very much in advance!

slice_plot() could not find function error

I'm following the book "Computer-age Calculus with R" but cannot run the function slice_plot( which is the first one used for graphing functions). The library mosaic, mosaicCalc and mosaicModel are installed. I don't know what I'm missing.
this is the code with the libraries required by the book:
library(mosaic)
library(mosaicCalc)
library(mosaicModel)
library(akima)
drug_remaining <- function(dose, duration, time_constant){
dose * exp(-duration / time_constant)
}
slice_plot(
drug_remaining(dose = 100, time_constant = 4, duration = t) ~ t,
domain(t = 0:20))
I've found the function slice_plot() and countour_plot() are still in development in a beta version of the mosaicCalc package, to use them we need to install the beta version running this code:
remotes::install_github("ProjectMOSAIC/mosaicCalc", ref="beta")
https://github.com/ProjectMOSAIC/mosaicCalc/issues/4

Temporary objects in parallel computing in R

I have a pretty long R code which needs to be iterated several hundred times. I am using a 32 core and 32 GB RAM cloud service to do the job. To make the code run faster, I want to use parallel computing using foreach() command. I have set the codes working with no errors. However, I need to make sure if I am getting proper results. To illustrate my point I have set a simplified mock code:
foreach (i = 1:100) %dopar% {
age <- seq(from=20,to=79, by=1)
d <- as.data.frame(age)
d$gender <- rbinom(nrow(d),size = 1,prob = 0.5)
d$prob <- cut(d$age, breaks = c(20,30,40,50,60,70,80), include.lowest = T,right = F,labels = c(.001,.01,.1,.25,.3,.1))
d$prob <- as.numeric(as.character(d$prob))
d$event <- rbinom(nrow(d),size = 1,prob = d$prob)
save(d,file = paste("d_",i,".rda", sep = ""))
table(d$gender,d$event)
}
I am wondering if temporary objects, like “d” in this example, is independent for each cluster when running this code. If there is only one object “d” in the memory which is shared by different clusters, what is the solution for an independent object.
For reference, I am using the code proposed by this page (https://github.com/tobigithub/R-parallel) to make clusters.
Thanks in advance for your reply.

How to get Rglpk_solve_LP to skip to the next line of code when it is taking too long to complete the calculation?

I am running simulations in R using R Studio. I need to get an optimal solution using Rglpk library. I call repeatedly the function Rglpk_solve_LP to solve the problem. At times it works fine but at times it runs forever. I want to be able to skip to next line when Rglpk_solve_LP can't converge.
I have attempted to change some of the settings in the Rglpk_solve_LP function such as control = list(lp_time_limit = time_limit ) but it still gets stuck
R code
lp = Rglpk_solve_LP(obj = obj, mat = mat, dir = dir,
rhs = rhs, max = max, types = rep("B",length(obj)),
control = list(lp_time_limit = 5 ))
print(lp)
lp$solution = lp$solution
lp$objval = lp$optimum
return (lp)
I expect a binary vector indicating whether a particular item was included in the solution. However, in some instances it does not finish the calculation and carries on forever.

glmmPQL crashes on inclusion of corSpatial object

Link to data (1170 obs, 9 variables, .Rd file)
Simply read it in using readRDS(file).
I´m trying to setup a GLMM using the glmmPQL function from the MASS package including a random effects part and accounting for spatial autocorrelation. However, R (Version: 3.3.1) crashes upon execution.
library(nlme)
# setup model formula
fo <- hail ~ prec_nov_apr + t_min_nov_apr + srad_nov_apr + age
# setup corSpatial object
correl = corSpatial(value = c(10000, 0.1), form = ~ry + rx, nugget = TRUE,
fixed = FALSE, type = "exponential")
correl = Initialize(correl, data = d)
# fit model
fit5 <- glmmPQL(fo, random = ~1 | date, data = d,
correl = correl, family = binomial)
What I tried so far:
reduce number of observation
play with corSpatial parameters (range and nugget)
reduce number of fixed predictors
execute code on Windows, Linux (Debian) and Mac R installations
While I get no error message on my local pc (RStudio just crashes), running the script on a server returns the following error message:
R: malloc.c:3540: _int_malloc: Assertion (fwd->size & 0x4) == 0' failed. Aborted
I'd use the INLA package to model this, as it allows to use spatially correlated random effects. The required code is a bit too long to place here. Therefore I've place it in a document on http://rpubs.com/INBOstats/spde

Resources