multiple arguments to function in map

df <- tibble::tibble(
f = c("rbinom", "rpois", "rnorm"),
params = list(
list(list(n = 5,size=2,prob=0.5), list(n = 10,size=5,prob=0.5)),
list(n = 5, lambda = 10),
list(n = 10, mean = -3, sd = 10)
)
)
df
invoke_map(df$f, df$params)

The above is my working example. What i want is to pass multiple arguments only to rbinom function. but this throws an error.. any help would be much appreciated

I think the problem is in your df shape.
This works:

library(tidyverse)

df <- tibble::tibble(
  f = c("rbinom","rbinom", "rpois", "rnorm"),
  params = list(
    list(n = 5,size=2,prob=0.5),
    list(n = 10,size=5,prob=0.5),
    list(n = 5, lambda = 10),
    list(n = 10, mean = -3, sd = 10)
  )
)
df
invoke_map(df$f, df$params)
1 Like

Thanks a lot. I had thought this way. but wanted to minimise typing..

one more thing, can we get the names also for each? i mean the distribution names also in the ouput?

invoke_map(df$f, df$params) %>% set_names(df$f)
1 Like

Thats wonderfull... but on similar lines this doesn't work

<!-- language-all: lang-r -->


    packages<-c("tidyverse","ARDL","stargazer","data.table","readxl","dint","fable","zoo",
                "tseries","urca")
    sapply(packages,library,character.only=T)
    #> To cite ARDL in publications use:
    #>   
    #> Kleanthis Natsiopoulos and Nickolaos Tzeremes (2020). ARDL: ARDL, ECM and Bounds-Test for Cointegration. R package version 0.1.0. University of Thessaly, Department of Economics. https://github.com/Natsiopoulos/ARDL.
    #> 
    #> Please cite as:
    #>  Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
    #>  R package version 5.2.2. https://CRAN.R-project.org/package=stargazer
    #> 
    #> Attaching package: 'data.table'
    #> The following objects are masked from 'package:dplyr':
    #> 
    #>     between, first, last
    #> The following object is masked from 'package:purrr':
    #> 
    #>     transpose
    #> Loading required package: fabletools
    #> 
    #> Attaching package: 'zoo'
    #> The following objects are masked from 'package:base':
    #> 
    #>     as.Date, as.Date.numeric
    #> Registered S3 method overwritten by 'quantmod':
    #>   method            from
    #>   as.zoo.data.frame zoo
    #> $tidyverse
    #>  [1] "forcats"   "stringr"   "dplyr"     "purrr"     "readr"     "tidyr"    
    #>  [7] "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics"  "grDevices"
    #> [13] "utils"     "datasets"  "methods"   "base"     
    #> 
    #> $ARDL
    #>  [1] "ARDL"      "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
    #>  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
    #> [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
    #> 
    #> $stargazer
    #>  [1] "stargazer" "ARDL"      "forcats"   "stringr"   "dplyr"     "purrr"    
    #>  [7] "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"    
    #> [13] "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base"     
    #> 
    #> $data.table
    #>  [1] "data.table" "stargazer"  "ARDL"       "forcats"    "stringr"   
    #>  [6] "dplyr"      "purrr"      "readr"      "tidyr"      "tibble"    
    #> [11] "ggplot2"    "tidyverse"  "stats"      "graphics"   "grDevices" 
    #> [16] "utils"      "datasets"   "methods"    "base"      
    #> 
    #> $readxl
    #>  [1] "readxl"     "data.table" "stargazer"  "ARDL"       "forcats"   
    #>  [6] "stringr"    "dplyr"      "purrr"      "readr"      "tidyr"     
    #> [11] "tibble"     "ggplot2"    "tidyverse"  "stats"      "graphics"  
    #> [16] "grDevices"  "utils"      "datasets"   "methods"    "base"      
    #> 
    #> $dint
    #>  [1] "dint"       "readxl"     "data.table" "stargazer"  "ARDL"      
    #>  [6] "forcats"    "stringr"    "dplyr"      "purrr"      "readr"     
    #> [11] "tidyr"      "tibble"     "ggplot2"    "tidyverse"  "stats"     
    #> [16] "graphics"   "grDevices"  "utils"      "datasets"   "methods"   
    #> [21] "base"      
    #> 
    #> $fable
    #>  [1] "fable"      "fabletools" "dint"       "readxl"     "data.table"
    #>  [6] "stargazer"  "ARDL"       "forcats"    "stringr"    "dplyr"     
    #> [11] "purrr"      "readr"      "tidyr"      "tibble"     "ggplot2"   
    #> [16] "tidyverse"  "stats"      "graphics"   "grDevices"  "utils"     
    #> [21] "datasets"   "methods"    "base"      
    #> 
    #> $zoo
    #>  [1] "zoo"        "fable"      "fabletools" "dint"       "readxl"    
    #>  [6] "data.table" "stargazer"  "ARDL"       "forcats"    "stringr"   
    #> [11] "dplyr"      "purrr"      "readr"      "tidyr"      "tibble"    
    #> [16] "ggplot2"    "tidyverse"  "stats"      "graphics"   "grDevices" 
    #> [21] "utils"      "datasets"   "methods"    "base"      
    #> 
    #> $tseries
    #>  [1] "tseries"    "zoo"        "fable"      "fabletools" "dint"      
    #>  [6] "readxl"     "data.table" "stargazer"  "ARDL"       "forcats"   
    #> [11] "stringr"    "dplyr"      "purrr"      "readr"      "tidyr"     
    #> [16] "tibble"     "ggplot2"    "tidyverse"  "stats"      "graphics"  
    #> [21] "grDevices"  "utils"      "datasets"   "methods"    "base"      
    #> 
    #> $urca
    #>  [1] "urca"       "tseries"    "zoo"        "fable"      "fabletools"
    #>  [6] "dint"       "readxl"     "data.table" "stargazer"  "ARDL"      
    #> [11] "forcats"    "stringr"    "dplyr"      "purrr"      "readr"     
    #> [16] "tidyr"      "tibble"     "ggplot2"    "tidyverse"  "stats"     
    #> [21] "graphics"   "grDevices"  "utils"      "datasets"   "methods"   
    #> [26] "base"
    data.table::data.table(
      diff_ln_CONS = c(0.00237844629410211,
                       0.00502011796238699,0.00521423828112666,0.00396556205407395,
                       -0.000355909666671872,0.00729047396775528),
       diff_ln_INC = c(0.00498960353265465,
                       0.00683295933524053,0.00264198002337168,0.00513780756387416,
                       0.0034247231416793,0.000307124646656609),
       diff_ln_INV = c(-0.00107337503873972,
                       0.00633568939107332,0.00708907876911269,0.0177891127645107,
                       -0.00817818531435588,0.00459563441501265)
    )
    #>     diff_ln_CONS  diff_ln_INC  diff_ln_INV
    #> 1:  0.0023784463 0.0049896035 -0.001073375
    #> 2:  0.0050201180 0.0068329593  0.006335689
    #> 3:  0.0052142383 0.0026419800  0.007089079
    #> 4:  0.0039655621 0.0051378076  0.017789113
    #> 5: -0.0003559097 0.0034247231 -0.008178185
    #> 6:  0.0072904740 0.0003071246  0.004595634

    #Unit root tests

    work_table<-tibble(
            test=c("ur.df","ur.ers"),
            parms=list(
                    list(x=data_diff,type="none",selectlags="AIC"),
                    list(x=data_diff,type="DF-GLS",model="constant")
            )
    )
    #> Error in eval_tidy(xs[[j]], mask): object 'data_diff' not found

    invoke_map(work_table$test,work_table$parms)
    #> Error in as_invoke_function(.f): object 'work_table' not found

<sup>Created on 2020-08-14 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)</sup>

did you mean to assign the data.table::data.table( above to the name data_diff ?

No. data_diff is the dataframe upon which I want to conduct those tests on.. and I want the results of those tests for all the variables in a tibble format

you posted an error that clearly shows data_diff does not exist in your environment (or did not at the time that you had run your code). You should review that.

packages<-c("tidyverse","ARDL","stargazer","data.table","readxl","dint","fable","zoo",
            "tseries","urca")
sapply(packages,library,character.only=T)
#> To cite ARDL in publications use:
#>   
#> Kleanthis Natsiopoulos and Nickolaos Tzeremes (2020). ARDL: ARDL, ECM and Bounds-Test for Cointegration. R package version 0.1.0. University of Thessaly, Department of Economics. https://github.com/Natsiopoulos/ARDL.
#> 
#> Please cite as:
#>  Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
#>  R package version 5.2.2. https://CRAN.R-project.org/package=stargazer
#> 
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last
#> The following object is masked from 'package:purrr':
#> 
#>     transpose
#> Loading required package: fabletools
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
#> $tidyverse
#>  [1] "forcats"   "stringr"   "dplyr"     "purrr"     "readr"     "tidyr"    
#>  [7] "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics"  "grDevices"
#> [13] "utils"     "datasets"  "methods"   "base"     
#> 
#> $ARDL
#>  [1] "ARDL"      "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
#>  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
#> [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
#> 
#> $stargazer
#>  [1] "stargazer" "ARDL"      "forcats"   "stringr"   "dplyr"     "purrr"    
#>  [7] "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"    
#> [13] "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base"     
#> 
#> $data.table
#>  [1] "data.table" "stargazer"  "ARDL"       "forcats"    "stringr"   
#>  [6] "dplyr"      "purrr"      "readr"      "tidyr"      "tibble"    
#> [11] "ggplot2"    "tidyverse"  "stats"      "graphics"   "grDevices" 
#> [16] "utils"      "datasets"   "methods"    "base"      
#> 
#> $readxl
#>  [1] "readxl"     "data.table" "stargazer"  "ARDL"       "forcats"   
#>  [6] "stringr"    "dplyr"      "purrr"      "readr"      "tidyr"     
#> [11] "tibble"     "ggplot2"    "tidyverse"  "stats"      "graphics"  
#> [16] "grDevices"  "utils"      "datasets"   "methods"    "base"      
#> 
#> $dint
#>  [1] "dint"       "readxl"     "data.table" "stargazer"  "ARDL"      
#>  [6] "forcats"    "stringr"    "dplyr"      "purrr"      "readr"     
#> [11] "tidyr"      "tibble"     "ggplot2"    "tidyverse"  "stats"     
#> [16] "graphics"   "grDevices"  "utils"      "datasets"   "methods"   
#> [21] "base"      
#> 
#> $fable
#>  [1] "fable"      "fabletools" "dint"       "readxl"     "data.table"
#>  [6] "stargazer"  "ARDL"       "forcats"    "stringr"    "dplyr"     
#> [11] "purrr"      "readr"      "tidyr"      "tibble"     "ggplot2"   
#> [16] "tidyverse"  "stats"      "graphics"   "grDevices"  "utils"     
#> [21] "datasets"   "methods"    "base"      
#> 
#> $zoo
#>  [1] "zoo"        "fable"      "fabletools" "dint"       "readxl"    
#>  [6] "data.table" "stargazer"  "ARDL"       "forcats"    "stringr"   
#> [11] "dplyr"      "purrr"      "readr"      "tidyr"      "tibble"    
#> [16] "ggplot2"    "tidyverse"  "stats"      "graphics"   "grDevices" 
#> [21] "utils"      "datasets"   "methods"    "base"      
#> 
#> $tseries
#>  [1] "tseries"    "zoo"        "fable"      "fabletools" "dint"      
#>  [6] "readxl"     "data.table" "stargazer"  "ARDL"       "forcats"   
#> [11] "stringr"    "dplyr"      "purrr"      "readr"      "tidyr"     
#> [16] "tibble"     "ggplot2"    "tidyverse"  "stats"      "graphics"  
#> [21] "grDevices"  "utils"      "datasets"   "methods"    "base"      
#> 
#> $urca
#>  [1] "urca"       "tseries"    "zoo"        "fable"      "fabletools"
#>  [6] "dint"       "readxl"     "data.table" "stargazer"  "ARDL"      
#> [11] "forcats"    "stringr"    "dplyr"      "purrr"      "readr"     
#> [16] "tidyr"      "tibble"     "ggplot2"    "tidyverse"  "stats"     
#> [21] "graphics"   "grDevices"  "utils"      "datasets"   "methods"   
#> [26] "base"

data_diff<-tibble::tribble(
             ~diff_ln_CONS..diff_ln_INC..diff_ln_INV,
        "1:  0.0023784463 0.0049896035 -0.001073375",
        "2:  0.0050201180 0.0068329593  0.006335689",
        "3:  0.0052142383 0.0026419800  0.007089079",
        "4:  0.0039655621 0.0051378076  0.017789113",
        "5: -0.0003559097 0.0034247231 -0.008178185",
        "6:  0.0072904740 0.0003071246  0.004595634"
        )

work_table<-tibble(
        test=c("ur.df","ur.ers"),
        parms=list(
                list(x=data_diff,type="none",selectlags="AIC"),
                list(x=data_diff,type="DF-GLS",model="constant")
        )
)

invoke_map(work_table$test,work_table$parms)
#> Error in ur.df(x = structure(list(diff_ln_CONS..diff_ln_INC..diff_ln_INV = c("1:  0.0023784463 0.0049896035 -0.001073375", : unused argument (x = list(c("1:  0.0023784463 0.0049896035 -0.001073375", "2:  0.0050201180 0.0068329593  0.006335689", "3:  0.0052142383 0.0026419800  0.007089079", "4:  0.0039655621 0.0051378076  0.017789113", "5: -0.0003559097 0.0034247231 -0.008178185", "6:  0.0072904740 0.0003071246  0.004595634")))
Created on 2020-08-16 by the reprex package (v0.3.0)

I am sorry.. didnt quite notice it

I found there was a challenge in that the two ur functions take different required params, i.e. selectlags and model, so I found it convenient to make function wrappers to swallow dummy unused values for each alternate

packages<-c("tidyverse","urca")
sapply(packages,library,character.only=T)


data_diff<-tibble::tribble(
  ~diff_ln_CONS,~diff_ln_INC,~diff_ln_INV,
     0.0023784463, 0.0049896035,-0.001073375 ,
     0.0050201180, 0.0068329593, 0.006335689 ,
     0.0052142383, 0.0026419800, 0.007089079 ,
     0.0039655621, 0.0051378076, 0.017789113 ,
    -0.0003559097, 0.0034247231,-0.008178185 ,
     0.0072904740, 0.0003071246, 0.004595634 
) 
  
  ur.dfx <-function(y,type,selectlags,model){
    ur.df(y,type,selectlags,lags=1)
  }
  ur.ersx <-function(y,type,selectlags,model){
    ur.ers(y,type,model,lag.max = 1)
  }


  (y <- map(data_diff,~enframe(.,name=NULL)))
  (work_table0<-expand_grid(tibble(test=c("ur.dfx","ur.ersx"),
                                  type = c("none","DF-GLS"),
                                  selectlags = "AIC",
                                  model="constant"),y))
  (work_table <- mutate(rowwise(work_table0),
                       parms = list(list("type"=type,"selectlags"=selectlags,
                                         "model"=model,
                                         "y"=pull(y,value)))) %>% select(test,parms))
  
  (map_results <- invoke_map(work_table$test,work_table$parms))

Thats a nice solution.. however, we are not able to recognise which variable the test is for. I would have loved to have it printed too. i did something like this

packages<-c("tidyverse","urca","rlist")
sapply(packages,library,character.only=T)
#> $tidyverse
#>  [1] "forcats"   "stringr"   "dplyr"     "purrr"     "readr"     "tidyr"    
#>  [7] "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics"  "grDevices"
#> [13] "utils"     "datasets"  "methods"   "base"     
#> 
#> $urca
#>  [1] "urca"      "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
#>  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
#> [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
#> 
#> $rlist
#>  [1] "rlist"     "urca"      "forcats"   "stringr"   "dplyr"     "purrr"    
#>  [7] "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"    
#> [13] "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base"


data_diff<-tibble::tribble(
        ~diff_ln_CONS,~diff_ln_INC,~diff_ln_INV,
        0.0023784463, 0.0049896035,-0.001073375 ,
        0.0050201180, 0.0068329593, 0.006335689 ,
        0.0052142383, 0.0026419800, 0.007089079 ,
        0.0039655621, 0.0051378076, 0.017789113 ,
        -0.0003559097, 0.0034247231,-0.008178185 ,
        0.0072904740, 0.0003071246, 0.004595634 
)


MY_LIST=apply(data[,5:7],2,function(x){
        return(
                list(
                        ur.df(x, type = "drift",selectlags = c("BIC")),
                        ur.df(x, type = "trend",selectlags = c("BIC")),
                        ur.df(x, type = "none",selectlags = c("BIC")),
                        summary(ur.pp(x,type = "Z-tau",model = "constant")),
                        summary(ur.pp(x,type = "Z-tau",model = "trend"))
                )
        )
})
#> Error in data[, 5:7]: object of type 'closure' is not subsettable



rand<-unlist(MY_LIST)
#> Error in unlist(MY_LIST): object 'MY_LIST' not found


nb<-rand %>% map_df(.,~data.table("Test Statistic"=round(.x@teststat,3),model=.x@model,
                                  test=.x@test.name),.id="Variable")
#> Error in eval(lhs, parent, parent): object 'rand' not found
nb_crit<-rand %>% map_df(.,~data.table(crit_val=.x@cval,
                                       model=.x@model,
                                       test=.x@test.name),.id="Variable")
#> Error in eval(lhs, parent, parent): object 'rand' not found
Created on 2020-08-16 by the reprex package (v0.3.0)

the nb and nb_crit are the results i want. I coud'nt make it more appealing though. The issue is in nb_crit, it is not known for which statistic is the critical value. I

Thanks and regards

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.