rparallel-processingdoparallelfitdistrplusrparallel# Error in parallel calculation on R function

I am mastering parallel programming in R simple examples tried to implement the function given in the code, it loads the processor, but the results of calculations are not. Could you please tell me where I made a mistake in the code?

```
library(foreach)
library(doSNOW)
power_Ks_Cauchy_par <- function(resp, alpha, distData, sample)
{
corNum<-detectCores()-1
cluster <- makeCluster(corNum, type = "SOCK", outfile="")
registerDoSNOW(cluster)
power <- c()
num <- c()
foreach(i=3:sample, .combine=rbind) %dopar%{
distPar <- fitdistrplus::fitdist(data = distData[1:i],"cauchy")
loc <- distPar$estimate[1]
sc <- distPar$estimate[2]
test<-mean(replicate(resp,(ks.test(rcauchy(i, loc, sc),"pnorm")$`p.value`<alpha)))
power<-c(power,test)
num <- c(num,i)
}
power <- data.frame(power, samples=num)
stopCluster(cluster)
return(power)
}
repl<-10000
alpha <- 0.05
samles <- length(baseDataMeh$Sigma02)
time1<-system.time({
resKStestCauchy_1 <- power_Ks_Cauchy_par(repl,alpha = alpha,
distData=baseDataMeh$Sigma02,sample=samles)
})
```

I tried several variants of paralleling, but on my architecture only implementation through these libraries gave a gain in calculation time on simple examples.

The code implemented through for in a function works, but it takes a very long time to calculate.

The function loads the processor, but there is no calculation result at the output.

Could you please tell me what I am doing wrong?

Solution

Consider directly assigning an object to `foreach`

call. If using `.combine`

, `foreach`

resembles an apply family function (i.e., lapply, sapply) to return an object (equal length to iterable) and not simply `for`

loop to iteratively update environment objects.

Below adjustment returns *test* in `foreach`

function and assigns to *power_vec* object. Also, `num`

capturing i-th number is not needed.

```
power_vec <- foreach(
i=3:sample, .combine=rbind
) %dopar% {
distPar <- fitdistrplus::fitdist(data = distData[1:i],"cauchy")
loc <- distPar$estimate[1]
sc <- distPar$estimate[2]
test <- mean(
replicate(
resp, (ks.test(rcauchy(i, loc, sc),"pnorm")$`p.value`<alpha)
)
)
}
power_df <- data.frame(power=power_vec, samples=3:sample)
```

- Installing R on Linux: configure: error: libcurl >= 7.28.0 library and headers are required with support for https
- How to do ensembles with time series using AICc?
- planes3d expands and draws the area based on the sphere's radius
- How to extract tag code itself using R, rvest
- How to Display or Print Contents of Environment in R
- How to use Windows user credentials for proxy authentication in R/RStudio
- R reticulate specifying python executable to use
- Replace multiple Instances of a variable name in an R function and save the modified function
- Standardizing address formatting in R
- How to fix "failed to load cairo DLL" in R?
- Using grepl to filter columns names in specific range of columns
- changing the legends in ggplot2 to have groups of similar labels
- How to keep only unique rows but ignore a column?
- convert string date to R Date FAST for all dates
- Add subgroup text to plotly pie chart
- R Shiny : adjust height of DT datatable when fillContainer=TRUE,
- Why do R external pointers' "unusual copying semantics" mean they should not be used stand-alone?
- How to extract somo character after a string with a number of word which can change in R
- What does `se` stand for in geom_smooth(..., se = FALSE)?
- How to find number of rows greater than any values in R
- Align text and reduce space between text and parentheses in plotly hover info box
- Remove outer box of geom_bar plot with broken y-axis
- How to use lag/lead in mutate with an initial value?
- Is it possible to have a Shiny ConditionalPanel whose condition is a global variable?
- counting elements in one list in another list
- How to vectorize nested loops in R?
- Replace NA values with an incrementing sequence starting from the previous non-NA value
- How can I calculate the number of uniques in a row within a species matrix?
- How to perform operations on pairs of rows, based on a "distinguishing" column's values
- Mutate variable based on previous observations