I was writing a function using the logistf::logistf and logistf::forward function. I will give here a minimum working example using sex2 from the logistf package.
data(sex2)
fwSel <- function(datamod) {
fitnull <- logistf(case ~ 1, data = datamod, pl = FALSE)
fw <- forward(fitnull)
return(fw)
}
fwSel(sex2)
I get the following output:
Step 0 : starting model
Error in is.data.frame(data) : object 'datamod' not found`.
Has anybody an explanation for that?
This is a typical error which you can get in R. It has been asked again and unfortunately it happens according to how different functions work in different environments and how functions try to find data according to the use of
parent.envorparent.frame. It might be one of the two problems:Try to use
force(datamod)before yourlogistffunction because your datamod is not currently evaluated in your custom function. This might not work if the following problem exists:parent.frame()or a call toparent_env(), this would cause a problem because of the different ways that R looks in different environments to find the data. The only way to solve this is to initiate datamod in the global environment i.e.:This will definitely work because the global environment will be searched anyway.
I find this link as an excellent way of finding out how the
parent.envis different toparent.frameand how using those two inside functions can cause problems like the one you are facing.I made a new example based on the functions in the link that demonstrates your problem exactly:
As you can see above using
getwith the calling environment works whereas usinggetwith the parent environment fails and produces an error. This is what (probably) happens in your functions too.