Search code examples
rweibull

functional call issue in R


I am trying to calculate the cost rate with a Weibull lifetime distribution in R with the following code:


CR_age <- function(T,lambda,k,cpm,ccm)
{
  cum <- function(T,lambda,k)
  { 
    return(1 - exp((-T/lambda)^k))
  }
  survival <- function(T,lambda,k)
  {
    return(exp((-T/lambda)^k))
  }
  mean_cost <- ccm * cum(T,lambda,k) + cpm * survival(T,lambda,k)
  cycle_length <- Vectorize(survival, "T")
  mean_cycle_length <- integrate(cycle_length, 0, T)
  costRate <- mean_cost / mean_cycle_length$value

  return(costRate)
}

as I call the function

 CR_age(2,0.1,1,3,1)

I receive a warning from R saying that :

Error in (function (T, lambda, k)  : 
  argument "lambda" is missing, with no default 

May I ask what would be the reason in this case? Is that I did not pass the argument lambda in CR_age to the functions cum and survival?

Thanks in advance


Solution

  • You need to give all the parameters of you function survival in the Vectorize and integrate functions

    CR_age <- function(Te,lambda,k,cpm,ccm)
    {
      cum <- function(Te,lambda,k)
      { 
        return(1 - exp((-Te/lambda)^k))
      }
      survival <- function(Te,lambda,k)
      {
        return(exp((-Te/lambda)^k))
      }
      mean_cost <- ccm * cum(Te,lambda,k) + cpm * survival(Te,lambda,k)
      cycle_length <- Vectorize(survival, vectorize.args =c("Te", "lambda", "k"))
      mean_cycle_length <- integrate(cycle_length, 0, Te, lambda, k)
      costRate <- mean_cost / mean_cycle_length$value
    
      return(costRate)
    }
    
    CR_age(2,0.1,1,3,1)
    

    [1] 10