如何制作按日期计算和报告的方法

时间:2017-06-01 21:08:27

标签: r class loops date methods

我有一个课程,按以下方式组织:

balancesheet <- setClass("balancesheet",

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

            )

然后我用我的数据填充这个类,它看起来像(它的一些部分):

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

Slot "account_id":
[1] 20202

Slot "q":
[1] 163592774

Slot "currency":
[1] 810

Slot "maturity_date":
[1] 17227

Slot "assetLiability":
[1] -1

Slot "lcr_weight":
[1] 0

Slot "nsfr_weight":
[1] 1

[[144]]

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

Slot "account_id":
[1] 31403

Slot "q":
[1] 0

Slot "currency":
[1] 810

Slot "maturity_date":
[1] 17256

Slot "assetLiability":
[1] -1

Slot "lcr_weight":
[1] 0.5

Slot "nsfr_weight":
[1] 0.5

[[146]]
An object of class "balancesheet"
Slot "report_date":
[1] 17226

Slot "account_id":
[1] 40903

Slot "q":
[1] 490

Slot "currency":
[1] 810

Slot "maturity_date":
[1] 17227

Slot "assetLiability":
[1] -1

Slot "lcr_weight":
[1] 0.4

Slot "nsfr_weight":
[1] 0  



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

Slot "account_id":
[1] 91412

Slot "q":
[1] 74442089

Slot "currency":
[1] 810

Slot "maturity_date":
[1] 17197

Slot "assetLiability":
[1] 1

Slot "lcr_weight":
[1] 0.1

Slot "nsfr_weight":
[1] 0


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

Slot "account_id":
[1] 91414

Slot "q":
[1] 38306207912

Slot "currency":
[1] 810

Slot "maturity_date":
[1] 17347

Slot "assetLiability":
[1] 1

Slot "lcr_weight":
[1] 0.1

Slot "nsfr_weight":
[1] 0


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

Slot "account_id":
[1] 91416

Slot "q":
[1] 5847202

Slot "currency":
[1] 810

Slot "maturity_date":
[1] 17532

Slot "assetLiability":
[1] 1

Slot "lcr_weight":
[1] 0.1

Slot "nsfr_weight":
[1] 0

然后我写了一个方法来转换数据框中的这个列表:

setGeneric(name="getBalanceSheet",
           def=function(balancesheet.list)
           {
             standardGeneric("getBalanceSheet")
           }
)
setMethod(f="getBalanceSheet",
          signature="ANY",
          definition=function(balancesheet.list)
          {
            balancesheet.df <- vector()
            n <- length(balancesheet.list)
            for(i in 1:n) 
           {
              object <- balancesheet.list[[i]] 
              row <- c(object@report_date,object@maturity_date,object@account_id,object@q,object@assetLiability,object@lcr_weight,object@nsfr_weight)
              balancesheet.df <- rbind(balancesheet.df,row)
            }
            return(balancesheet.df)
          }
        )

这个数据框看起来像:

     [,1]  [,2]    [,3]         [,4]       [,5]  [,6] [,7]
row 17226 17227 20202.0 1.635928e+08         -1  0.00  1.0
row 17226 17227 20208.0 1.943708e+08         -1  0.00  1.0
row 17226 17227 20209.0 5.638990e+07         -1  0.00  1.0
row 17226 17227 30233.0 2.314207e+08          1  0.40  1.0
row 17226 17256 31903.0 0.000000e+00          1  0.50  0.5
row 17226 17256 32002.0 1.600000e+06          1  0.50  0.5
row 17226 17256 32003.0 2.000000e+06          1  0.50  0.5    

其中1为report_date,2为maturity_date,3为account_id,4为q,5为assetLiability_flag,6为lcr_coeff },7是nsfr_coeff

然后我为这个类设置了一个方法如下:

setGeneric(name="LiquidityRisk",
           def=function(balancesheet.list)
           {
             standardGeneric("LiquidityRisk")
           }
)
setMethod(f="LiquidityRisk",
          signature="ANY",
          definition=function(balancesheet.list)
          {
            outflows <- 0
            inflows <- 0
            available1 <- 0
            required1 <- 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 + 360 && object@assetLiability == -1) 
                  {
                    available1 <- available1 + object@q * object@nsfr_weight
                  }
              if (object@maturity_date <= object@report_date + 360 && object@assetLiability == 1) 
                  {
                      required1 <- required1 + object@q * object@nsfr_weight
                  }
            }
            cat(outflows, " ", inflows," ",available1," ",required1, " 
")
            ratios <- list()
            output <- as.data.frame(matrix(0,1,3))
            colnames(output) <- c("outflows","inflows","netcashflows")
            output[1] <- outflows
            output[2] <- inflows
            output[3] <- max(outflows-inflows,0.25*outflows)

            output.nsfr <- as.data.frame(matrix(0,1,2))
            colnames(output.nsfr) <- c("available","required")
            output.nsfr[1] <- available1
            output.nsfr[2] <- required1

            ratios[["lcr"]] <- output
            ratios[["nsfr"]] <- output.nsfr

            #ratios[["lcr"]] <- round(output/10^9,1)
            #ratios[["nsfr"]] <- round(output.nsfr/10^9,1)

            return(ratios)

                     }
)

它有效!

但是,我只检查了balancesheet.list的一个report_date(2017-03-01,数字格式为17226):我为此report_date过滤了它并使用了我的方法。如果我有10个这样的report_dates,我会以同样的方式为每个人做手。但是,我有很多report_dates,即每个月从2003年到2017年(以及每个月的相应数据)。我怎样才能在lcr的某个单独的txt或Excel文件中报告nsfrreport_date系数?

我想到了一些与循环有关的report_date的唯一值,但没有取得成功。

1 个答案:

答案 0 :(得分:0)

如果您的所有数据都保存在上面显示的17226相同的7列数据框中,您可以split(df, df[,1])按日期将df分成列表(您将拥有超过150个这些列表)我推测)然后你可以用lapplysapply将split df作为第一个参数并将LiquidityRisk作为FUN参数?

ManyMonths <- lapply(split(df, df[,1]), LiquidityRisk)

这会将所有结果打印为单个对象,如果您选择,可以将其保存到文件中: Export a list into a CSV or TXT file in R