R Error exporting cluster "object not found"

Can someone please help me understand why my program is producing this error?

As seen here, "pay.freq" is clearly part of the environment, so why can't it find it? The syntax is the same as for "ts" which it can find without problems

The large circle partially reflects the function of the word, the small circle partially covers the plot of the word.

Screenshot of error

cf.pro <- function(t=0,Tb=T,r=Y, k=1, PRFlag="P", freq="w",plot=0){ #Beregner exposure for alle tidspunkter med udgangspunkt 
  ts <- seq(0,30,1/52)
  pay.freq <- if(toupper(freq)=="W"){1}else #bestemmer hvor ofte der sker betalinger
    if(toupper(freq)=="Q"){13}else
      if(toupper(freq)=="H"){26}else
        if(toupper(freq)=="Y"){52}else print("Fejl i frequency input")

  library('parallel')
  cl <- makeCluster(7)
  clusterEvalQ(cl,source("C:/Users/Marcus/Documents/CBS/Speciale/Data/Global data.R"))
  clusterEvalQ(cl,source("C:/Users/Marcus/Documents/CBS/Speciale/Data/Swappriser.R"))
  clusterEvalQ(cl,source("C:/Users/Marcus/Documents/CBS/Speciale/Data/Interest simulation.R"))
  clusterEvalQ(cl,source("C:/Users/Marcus/Documents/CBS/Speciale/Data/Survival sim.R"))
  clusterEvalQ(cl,source("C:/Users/Marcus/Documents/CBS/Speciale/Data/Exposures.R"))
  clusterExport(cl,"ts")
  clusterExport(cl,"pay.freq")

 cf.pro <- parSapplyLB(cl,1:n, function(j){ #Beregner exposure serie n gange
    if (k==1) k=Swap(t=0,Ta=0,Tb=Tb,r=r[,j])
    sapply(ts,function(i){Exposure.cf(t=i,Tb=Tb,r=r[,j], k=k, PRFlag=PRFlag, pay.freq=pay.freq)}) #beregner exposure for alle tidspunkter
  })
  stopCluster(cl)

  if (plot==1) {
    tss <- seq(t, Tb, dt)
    matplot(tss, cf.pro[,1:n], type="l", lty=1, main="Exposure Profiles", ylab="Exposure") 
    lines(tss,rowMeans(cf.pro), lty=1, lwd=3)
  } 
  return(cf.pro)
}

CF.pro.w=cf.pro(t=0,Tb=T,r=r, PRFlag="P", freq="w", plot=1)

      

+3


source to share


1 answer


If you look at the clusterExport documentation, the call is as follows

clusterExport(cl, varlist, envir = .GlobalEnv)

      

As you can see, the default environment to look for the variable you are trying to export is .GlobalEnv.



You are exporting inside a function, and the scope of pay.freq is not global, but the local environment of the function. However, you did not specify a function environment for clusterExport, so clusterExport looks in GlobalEnv and does not find pay.freq.

I bet this is your problem and that pay.freq is showing up in your environment now because you probably went through your code for testing. I have cleared your environment and try to run the code again by specifying the function environment in clusterExport.

Let me know how it goes and we can possibly work through it if the problem is a little more subtle. This was my first thought to consider this issue.

+5


source







All Articles