【问题标题】:how to use a base class in dplyr如何在 dplyr 中使用基类
【发布时间】:2020-12-21 22:53:18
【问题描述】:

我正在尝试使用 RQuantLib 包来使用函数 EuropeanImpliedVolatility 计算数据框中期权的隐含波动率。该函数返回一个基类并得到错误Expecting a single string value: [type=integer; extent=1]。

在 dplyr 中使用基类的正确方法是什么?

library(tidyverse)
library(RQuantLib)

    book <- data.frame(
      type=c("call","call","call","put","put","put"),
      strike=c(27800,27600,27400,24800,24600,24400),
      last=c(240,280,320,310,270,230),
      underlying=rep(26425,6),
      dividendYield=rep(0,6),
      riskFreeRate=rep(0.015,6),
      maturity=rep(39/365,6)
    ) %>% 
      rowwise() %>% 
      mutate(
        impvol= EuropeanOptionImpliedVolatility(type, value=last, underlying,strike,dividendYield,riskFreeRate,maturity,volatility=0.4)
      )

【问题讨论】:

    标签: r tidyverse base-class


    【解决方案1】:

    有一些属性会导致问题。我们可以用as.numeric 包裹起来

    library(dplyr)
    library(RQuantLib)
    book %>%
       rowwise %>%
        mutate(impvol = as.numeric(EuropeanOptionImpliedVolatility(type, 
            value=last, underlying, strike,
                    dividendYield,riskFreeRate,maturity,volatility = 0.4)))
    

    -输出

    # A tibble: 6 x 8
    # Rowwise: 
    #  type  strike  last underlying dividendYield riskFreeRate maturity impvol
    #  <chr>  <dbl> <dbl>      <dbl>         <dbl>        <dbl>    <dbl>  <dbl>
    #1 call   27800   240      26425             0        0.015    0.107  0.204
    #2 call   27600   280      26425             0        0.015    0.107  0.202
    #3 call   27400   320      26425             0        0.015    0.107  0.197
    #4 put    24800   310      26425             0        0.015    0.107  0.275
    #5 put    24600   270      26425             0        0.015    0.107  0.277
    #6 put    24400   230      26425             0        0.015    0.107  0.278
    

    如果我们想保留这些属性,也可以用list 包装

    book %>% 
       rowwise %>% 
       mutate(impvol = list(EuropeanOptionImpliedVolatility(type, 
        value=last, underlying,strike,dividendYield,
                riskFreeRate,maturity,volatility=0.4))) 
    

    -输出

    # A tibble: 6 x 8
    # Rowwise: 
    #  type  strike  last underlying dividendYield riskFreeRate maturity impvol        
    #  <chr>  <dbl> <dbl>      <dbl>         <dbl>        <dbl>    <dbl> <list>        
    #1 call   27800   240      26425             0        0.015    0.107 <ErpnOpIV [1]>
    #2 call   27600   280      26425             0        0.015    0.107 <ErpnOpIV [1]>
    #3 call   27400   320      26425             0        0.015    0.107 <ErpnOpIV [1]>
    #4 put    24800   310      26425             0        0.015    0.107 <ErpnOpIV [1]>
    #5 put    24600   270      26425             0        0.015    0.107 <ErpnOpIV [1]>
    #6 put    24400   230      26425             0        0.015    0.107 <ErpnOpIV [1]>
     
    

    或者pmap的另一个选项

    library(purrr)
    book %>%
        mutate(impvol = pmap_dbl(., ~ EuropeanOptionImpliedVolatility(..1, value = ..3,
               ..4, ..2, ..5, ..6, ..7, volatility = 0.4)))
    #  type strike last underlying dividendYield riskFreeRate  maturity    impvol
    #1 call  27800  240      26425             0        0.015 0.1068493 0.2044270
    #2 call  27600  280      26425             0        0.015 0.1068493 0.2019167
    #3 call  27400  320      26425             0        0.015 0.1068493 0.1973822
    #4  put  24800  310      26425             0        0.015 0.1068493 0.2745614
    #5  put  24600  270      26425             0        0.015 0.1068493 0.2773029
    #6  put  24400  230      26425             0        0.015 0.1068493 0.2780447
    

    数据

    book <- data.frame(
          type=c("call","call","call","put","put","put"),
          strike=c(27800,27600,27400,24800,24600,24400),
          last=c(240,280,320,310,270,230),
          underlying=rep(26425,6),
          dividendYield=rep(0,6),
          riskFreeRate=rep(0.015,6),
          maturity=rep(39/365,6)
        ) 
    

    【讨论】:

    • @Zeus 你能显示dplyr的包版本吗?这两种方法对我来说都很好
    • 我认为你是对的 - 我正在使用 dplyr 1.0.2
    • @Zeus 我也在使用packageVersion('dplyr')# [1] ‘1.0.2’ 可能你有一些包屏蔽功能问题?您可以尝试仅加载 dplyr 和 RQuantLib 的新会话
    • 我在新会话中尝试了代码,但得到了同样的错误
    • 感谢您的帮助 - 所有方法都有效,问题是具有其他功能的过时 R 代码
    猜你喜欢
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    相关资源
    最近更新 更多