Closed Matteo21Q closed 1 year ago
The problem is that the insight::get_data()
function does not return a data frame with appropriate column names. I will close this because this is not a bug in marginaleffects
and it should be fixed upstream. Unfortunately, I don’t have time to make a pull request to fix this in insight
right now, but the code there is usually very easy to read, so you may want to give it a shot if you have time and interest.
library(mfp)
# Loading required package: survival
set.seed(20)
X <- rnorm(100)
Y <- rbinom(100, 1, 0.5 + 0.001 * X)
dd <- data.frame(X, Y)
testfun <- function(dd) {
dd2 <- dd
colnames(dd2)[1] <- "Z"
ff2 <- as.formula(Y ~ fp(Z, df = 4))
mfp.fit2 <- mfp(ff2, data = dd2, family = "binomial")
glm.fit2 <- mfp.fit2$fit
insight::get_data(glm.fit2)
}
testfun(dd) |> head()
# Warning: Could not recover model data from environment. Please make sure your
# data is available in your workspace.
# Trying to retrieve data from the model frame now.
# Y
# 1 0 4.062685....
# 2 1 2.314075....
# 3 1 4.685465....
# 4 0 1.567406....
# 5 1 2.453433....
# 6 1 3.469606....
Hi, I am using marginaleffects together with mfp. mfp fits models (eg, glm) applying polynomial transformations to covariates. It returns an object that contains the standard glm fit object, on which I can get marginal risk diff easily as follows:
but I need to do this within a function, and the data set on which to fit the model is defined within the function. When I use the same code just included in a function, it gives an error, suggesting to try and pass newdata explicitly:
But passing newdata explicitly doesn't seem to fix it:
My sessioninfo is as follows:
Thanks in advance for any help!