2

I am very new to R, and this is my first time of encountering the eval() function. So I am trying to use the med and boot.med function from the following package: mma. I am using it to conduct mediation analysis. med and boot.med take in models such as linear models, and dataframes that specify mediators and predictors and then estimate the mediation effect of each mediator.

The author of the package gives the flexible option of specifying one's own custom.function. From the source code of med, it can be seen that the custom.function is passed to the eval(). So I tried insert the gbmt function as the custom function. However, R kept giving me error message: Error during wrapup: Number of trees to be used in prediction must be provided. I have been searching online for days and tried many ways of specifying the number of trees parameter n.trees, but nothing works (I believe others have raised similar issues: post 1, post 2).

The following codes are part of the source code of the med function:

cf1 = gsub("responseY", "y[,j]", custom.function[j])
cf1 = gsub("dataset123", "x2", cf1)
cf1 = gsub("weights123", "w", cf1)
full.model[[j]] <- eval(parse(text = cf1))

One custom function example the author gives in the package documentation is as follows:

temp1<-med(data=data.bin,n=2,custom.function = 'glm(responseY~.,data=dataset123,family="quasibinomial",
           weights=weights123)')

Here the glm is the custom function. This example code works and you can replicate it easily (if you have mma installed and loaded). However when I am trying to use the gbmt function on a survival object, I got errors and here is what my code looks like:

temp1 <- med(data = data.surv,n=2,type = "link",
                    custom.function = 'gbmt(responseY ~.,
                                       data = dataset123, 
                                       distribution = dist,
                                       train_params = start_stop, 
                                       cv_folds=10, 
                                       keep_gbm_data = TRUE, 
                                       )') 

Anyone has any idea how the argument about number of trees n.trees can be added somewhere in the above code?

Many thanks in advance!

Update: in order to replicate the example code, please install mma and try the following:

library("mma")

data("weight_behavior") ##binary x #binary y

x=weight_behavior[,c(2,4:14)] 
pred=weight_behavior[,3] 
y=weight_behavior[,15]

data.bin<-data.org(x,y,pred=pred,contmed=c(7:9,11:12),binmed=c(6,10), binref=c(1,1),catmed=5,catref=1,predref="M",alpha=0.4,alpha2=0.4) 

temp1<-med(data=data.bin,n=2) #or use self-defined final function

temp1<-med(data=data.bin,n=2, custom.function = 'glm(responseY~.,data=dataset123,family="quasibinomial",
weights=weights123)')

I changed the custom.function to gbmt and used a survival object as responseY and the error occurs. When I use the gbmt function on my data outside the med function, there is no error.

sky sky
  • 21
  • 2
  • 1
    Is `gbmt` defined in the scope that calls the `med` function? Are all `dist` and `start_stop`? It would help to have a complete, minimal example to reproduce the issue. (Apart from that, well done on analysing the issue and posting relevant info.) – Konrad Rudolph May 19 '20 at 15:28
  • 1
    Actually I don’t think you *can* use custom objects inside the `custom.function`: `med` does not support this due to the way this function is implemented (which is badly, incidentally). – Konrad Rudolph May 19 '20 at 15:33
  • @Konrad Rudolphv: Thank you so much for your prompt reply! `gbmt` is not defined in the scope that calls the `med` function. It is a function from another R package [gbm3](https://www.rdocumentation.org/packages/gbm3/versions/2.2). However, as you can see from the example code given by the `mma` package author, the `glm` function is also external and undefined in the `med` function, but the example code works... – sky sky May 19 '20 at 15:45
  • 2
    `glm` is a function from the ‘stats’ package, which is always attached. Make sure you’ve loaded and attached the `gbm3` package. That said, note my second comment: I don’t think `med` supports using other objects inside the `custom.function`. In fact, just by my reading of the code you’ve posted, even other *packages* won’t be found. Note also that the package has recently been removed from CRAN for failing to fix errors. – Konrad Rudolph May 19 '20 at 15:51
  • @Konrad Rudolph: This is very helpful! I think all users of the `mma` package should be notified by your information. Hope the `custom.function` will be further developed in the future. – sky sky May 19 '20 at 16:02

0 Answers0