You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
a suggested code or documentation change, improvement to the code, or feature request
Background
It seems that the requirement to use stats::poly() rather than just poly() in a model formula has been a persistent issue (#79#133#175, and in the prediction package)
Reproducible error:
set.seed(99)
library(margins)
data<-data.frame(y= rnorm(n=100, mean=0, sd=1),
x= rnorm(n=100, mean=0, sd=1))
fit<- lm(formula=y~ poly(x, degree=2), data=data)
margins(fit)
#> Error in poly(x, degree = 2, coefs = list(alpha = c(-0.087688783657271, : could not find function "poly"fit2<- lm(formula=y~stats::poly(x, degree=2), data=data)
margins(fit2)
#> Average marginal effects#> lm(formula = y ~ stats::poly(x, degree = 2), data = data)#> x#> 0.04987
Proposed solution:
The solution that I would like to propose is to provide a reducemem option for the various methods of the margins() function.
I have debugged the error to be the result of this line of code where the ".Environment" attribute of model[["terms"]] is removed.
I propose that the reducemem option be used to allow the user to turn this feature off so the formula will work as expected (not only for the stats::poly() function, but any other user-defined functions they would like to have in the formula).
Example of working fix using assignInNamespace() function:
An additional thought I had was that rather than setting the ".Environment" attribute to NULL, you can just create a new environment that has the same parents as the global environment by doing:
This would prevent creating a massive memory footprint if the user has a large dataset loaded into R, but still allow use of any packages that were attached prior to running the margins() function.
Please specify whether your issue is about:
Background
It seems that the requirement to use
stats::poly()
rather than justpoly()
in a model formula has been a persistent issue (#79 #133 #175, and in the prediction package)Reproducible error:
Proposed solution:
The solution that I would like to propose is to provide a
reducemem
option for the various methods of themargins()
function.I have debugged the error to be the result of this line of code where the
".Environment"
attribute ofmodel[["terms"]]
is removed.I propose that the
reducemem
option be used to allow the user to turn this feature off so the formula will work as expected (not only for thestats::poly()
function, but any other user-defined functions they would like to have in the formula).Example of working fix using
assignInNamespace()
function:Alternative option
An additional thought I had was that rather than setting the
".Environment"
attribute toNULL
, you can just create a new environment that has the same parents as the global environment by doing:This would prevent creating a massive memory footprint if the user has a large dataset loaded into R, but still allow use of any packages that were attached prior to running the
margins()
function.Created on 2021-08-18 by the reprex package (v2.0.1)
The text was updated successfully, but these errors were encountered: