R, how to write a method which will make operations with class parameters by months

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

R, how to write a method which will make operations with class parameters by months

Gectar
How to perform all those operations in my code for each month and report an output by dates?

Here is my code: 1) I've created a class, and here is it:

    balancesheet <- setClass("balancesheet",

              slots = c(
                report_date = "numeric",
                account_id = "numeric",
                q = "numeric",
                currency = "numeric",
                maturity_date = "numeric",
                assetLiability = "numeric",
                lcr_weight = "numeric",
                nsfr_weight = "numeric"
              ),
              prototype=list(
                report_date = 0,
                account_id = 100000,
                q = 1,
                currency = 810,
                maturity_date = 1,
                assetLiability = 1,
                lcr_weight = 0,
                nsfr_weight = 0
              )

            )
2) Then I set a method for operations for object parameters as following:

    setGeneric(name="LCR",
       def=function(balancesheet.list)
       {
         standardGeneric("LCR")
       }
    setMethod(f="LCR",
      signature="ANY",
      definition=function(balancesheet.list)
      {
        outflows <- 0
        inflows <- 0
        available1 <- 0
        required1 <- 0
        available2 <- 0
        required2 <- 0
        n <- length(balancesheet.list)
        for (i in 1:n)
        {
          object <- balancesheet.list[[i]]
          if ((object@maturity_date <= object@report_date + 30) && (object@assetLiability = -1))
               {
            outflows <- outflows + object@q * object@lcr_weight

          if ((object@maturity_date <= object@report_date + 30) && (object@assetLiability = 1))
              {
            inflows <- inflows + object@q * object@lcr_weight

          if ((object@maturity_date <= object@report_date + 180)) && (object@assetLiability = -1))
              {
            available1 <- available1 + object@q * object@nsfr_weight

          if ((object@maturity_date <= object@report_date + 180)) && (object@assetLiability = 1))
              {
            required1 <- required1 + object@q * object@nsfr_weight

          if ((object@maturity_date <= object@report_date + 360)) && (object@assetLiability = -1))
              {
            available2 <- available2 + object@q * object@nsfr_weight

          if ((object@maturity_date <= object@report_date + 360)) && (object@assetLiability = 1))
              {
              required2 <- required2 + object@q * object@nsfr_weight

Part of my balance.sheet object looks like:

[[565]] An object of class "balancesheet" Slot "report_date": [1] 17167

Slot "account_id": [1] 91315

Slot "q": [1] 1234474908

Slot "currency": [1] 810

Slot "maturity_date": [1] 17532

Slot "assetLiability": [1] -1

Slot "lcr_weight": [1] 1

Slot "nsfr_weight": [1] NA

[[566]] An object of class "balancesheet" Slot "report_date": [1] 17167

Slot "account_id": [1] 91316

Slot "q": [1] 307607222

Slot "currency": [1] 810

Slot "maturity_date": [1] 18262

Slot "assetLiability": [1] -1

Slot "lcr_weight": [1] 1

Slot "nsfr_weight": [1] NA

[[567]] An object of class "balancesheet" Slot "report_date": [1] 17167

Slot "account_id": [1] 91317

Slot "q": [1] 1902804701

Slot "currency": [1] 810

Slot "maturity_date": [1] 20817

Slot "assetLiability": [1] -1

Slot "lcr_weight": [1] 1

Slot "nsfr_weight": [1] NA

And so I need to make all this operations for each month and I need the output reported for the begining of each month I have in data. What else should I add to make the question as clear as possible? Thank you all very much!