0

I have this code for my paper and it still could not find the function even when I've properly re-installed the necessary packages.

expoUtility <- function(x, alpha, param_beta, W){
  (1-exp(-alpha*(W + x)^(1-param_beta)))/alpha
}

Round.Probability.Table <- cbind(Round.Probability.Table,c(1:10))
Round.Probability.Table <- Round.Probability.Table[,1:3]
names(Round.Probability.Table) <- c("Round","CasesAtEnd","Probability")

for (i in 1:9) {
  Round.Probability.Table$Probability[i] <- 1/choose(Round.Probability.Table$CasesAtEnd[i],Round.Probability.Table$CasesAtEnd[i+1])
}

LL.expoUtility <- function (parameters) {

  alpha <- parameters[1]
  param_beta <- parameters[2]
  W <- parameters[3]
  sigma <- parameters[4]

  LL <- foreach(i=1:nrow(data), .combine = "c") %dopar% {

    sv <- expoUtility(data$Bank.Offer[i], alpha = alpha, param_beta = param_beta, W = W)

    cv <- combn(Set.Cases.Remaining[[i]], Round.Probability.Table[1+match(data$Round[i], Round.Probability.Table$Round),2]) %>%
      apply(2,mean) %>%
      sapply(FUN = predictedBankOffer, b = data$Bank.Offer.Percent[i], r = data$Round[i]) %>%
      sapply(FUN = expoUtility, alpha = alpha, param_beta = param_beta, W = W)*Round.Probability.Table$Probability[data$Round[i]]
    cv <- sum(cv)

    delta <- combn(Set.Cases.Remaining[[i]], Round.Probability.Table[1+match(data$Round[i], Round.Probability.Table$Round), 2]) %>%
      apply(2,mean) %>%
      sapply(FUN = predictedBankOffer, b = data$Bank.Offer.Percent[i], r = data$Round[i]) %>%
      sapply(FUN = expoUtility, alpha = alpha, param_beta = param_beta, W = W) - cv 
    delta <- delta^2 %>%
      sum() %>%
      sqrt()

    if (data$Answer[i] == 0) {
      z <- (cv-sv)/(delta*sigma)
    } else {
      z <- (sv-cv)/(delta*sigma)
    }
  }
  LL <- sapply(LL, FUN = pnorm, mean = 0, sd = 1, log.p = TRUE)
  return(LL)
}

LL.logUtility <- function (parameters) {

  sigma <- parameters[1]

  LL <- foreach(i=1:nrow(data), .combine = "c") %dopar% {

    sv <- log(data$Bank.Offer[i])

    cv <- combn(Set.Cases.Remaining[[i]], Round.Probability.Table[1+match(data$Round[i], Round.Probability.Table$Round),2]) %>%
      apply(2,mean) %>%
      sapply(FUN = predictedBankOffer, b = data$Bank.Offer.Percent[i], r = data$Round[i]) %>%
      sapply(FUN = log)*Round.Probability.Table$Probability[data$Round[i]]
    cv <- sum(cv)

    delta <- combn(Set.Cases.Remaining[[i]], Round.Probability.Table[1+match(data$Round[i], Round.Probability.Table$Round), 2]) %>%
      apply(2,mean) %>%
      sapply(FUN = predictedBankOffer, b = data$Bank.Offer.Percent[i], r = data$Round[i]) %>%
      sapply(FUN = log) - cv 
    delta <- delta^2 %>%
      sum() %>%
      sqrt()

    if (data$Answer[i] == 0) {
      z <- (cv-sv)/(delta*sigma)
    } else {
      z <- (sv-cv)/(delta*sigma)
    }
  }
  LL <- sapply(LL, FUN = pnorm, mean = 0, sd = 1, log.p = TRUE)
  return(LL)
}

ptm.1 <- proc.time()
mle.1 <- maxLik(logLik = LL.expoUtility, start = c(0.1233,0.958,82370,0.1625), method = "NM", tol = 1e-20, iterlim = 3)

    Error in { : task 1 failed - "could not find function "expoUtility""
    Called from: e$fun(obj, substitute(ex), parent.frame(), e$data)

I am guessing that the problem lies on the foreach and doParallel package. I am also using a Windows OS and I obtain the code from a MAC OS. Will this affect the coding?

Roland
  • 127,288
  • 10
  • 191
  • 288
Paolo
  • 1
  • 3
  • Load the packages on the workers. See `help("foreach")` and the `.packages` parameter. – Roland Jul 27 '18 at 06:18
  • 1
    Possible duplicate of [Function not found in R doParallel 'foreach' - Error in { : task 1 failed - "could not find function "raster""](https://stackoverflow.com/questions/20704235/function-not-found-in-r-doparallel-foreach-error-in-task-1-failed-cou) – F. Privé Jul 27 '18 at 07:45

1 Answers1

0

I think the problem lies on the foreach, there is a parameter .packages that loads the package for every worker. From the help of foreach:

.packages: character vector of packages that the tasks depend on. If ex requires a R package to be loaded, this option can be used to load that package on each of the workers.

So, I think you need to declare the package that you are using on those functions in the foreach, like this:

LL <- foreach(i=1:nrow(data), .packages=c("name of the package you are using"),
              .combine = "c") %dopar% {
"Rest of your code"
 }
Random Cotija
  • 613
  • 1
  • 10
  • 26