Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

find_formula fails when applied to lme inside lapply #658

Closed
iago-pssjd opened this issue Oct 5, 2022 · 10 comments
Closed

find_formula fails when applied to lme inside lapply #658

iago-pssjd opened this issue Oct 5, 2022 · 10 comments
Labels
Won't fix 🚫 This will not be worked on

Comments

@iago-pssjd
Copy link

I come here from vincentarelbundock/modelsummary#559.
I adapt the reprex

library(insight)
library(nlme)
models <-lapply(c("", " + Sex"), \(.x) lme(as.formula(paste0("distance  ~ age", .x)), random = ~ 1, data = Orthodont))
lapply(models, find_formula)
Error in paste0("distance  ~ age", .x) : object '.x' not found

I would say that this seems to be a duplicate of issue #309, but next line works

find_formula(lme(as.formula(paste0("distance  ~ age", "")), random = ~ 1, data = Orthodont))
$conditional
distance ~ age
<environment: 0x55c02e144f78>

$random
~1
<environment: 0x55c02e144f78>

attr(,"class")
[1] "insight_formula" "list"         

Thanks!

@iago-pssjd
Copy link
Author

iago-pssjd commented Oct 5, 2022

Maybe solvable changing eval(x$call$fixed) in

insight/R/find_formula.R

Lines 1198 to 1200 in 6fdc601

find_formula.lme <- function(x, verbose = TRUE, ...) {
fm <- eval(x$call$fixed)
fmr <- eval(x$call$random)

with formula(x$terms)?

@strengejacke
Copy link
Member

ok, but can we deal with random effects then?

library(insight)
library(nlme)
models <-lapply(c("", " + Sex"), \(.x) lme(as.formula(paste0("distance  ~ age", .x)), random = ~ 1, data = Orthodont))
lapply(models, function(i) formula(i$terms))
#> [[1]]
#> distance ~ age
#> <environment: 0x0000021eb3dc7010>
#> 
#> [[2]]
#> distance ~ age + Sex
#> <environment: 0x0000021eb68257f0>

find_formula(models[[]])
#> $conditional
#> list()
#> <environment: 0x0000021eb70a1520>
#> 
#> attr(,"class")
#> [1] "insight_formula" "list"


m2 <- lme(distance ~ age, random = ~ 1, data = Orthodont)
find_formula(m2)
#> $conditional
#> distance ~ age
#> <environment: 0x0000021eb745e998>
#> 
#> $random
#> ~1
#> <environment: 0x0000021eb745e998>
#> 
#> attr(,"class")
#> [1] "insight_formula" "list"

Created on 2022-10-06 with reprex v2.0.2

@iago-pssjd
Copy link
Author

@strengejacke I don't see your point. I only modify the fixed effects part of find_formula.lme

@vincentarelbundock
Copy link
Contributor

@strengejacke I don't see your point. I only modify the fixed effects part of find_formula.lme

One question is: if the random part is something like random=~x1+x2, will your change properly classify those as "random", or will it stuff the "x1" and "x2" variables in the "fixed" part by mistake?

@strengejacke
Copy link
Member

Ah ok, didn't test with the PR.

@iago-pssjd
Copy link
Author

@vincentarelbundock
Let's see with the example:

library(insight)
library(nlme)
models <-lapply(c("", " + Sex"), \(.x) lme(as.formula(paste0("distance  ~ age", .x)), random = ~ Sex, data = Orthodont))
find_formula(models[[1]])
$conditional
distance ~ age
<environment: 0x55a163099e58>

$random
~Sex
<environment: 0x55a163aea038>

attr(,"class")
[1] "insight_formula" "list"      

In this case, it seems to work. I may try some more complex example.

@vincentarelbundock
Copy link
Contributor

Great!

But I bet the code breaks if the lapply modifies the random component instead of the fixed.

Is that a big deal?

@iago-pssjd
Copy link
Author

iago-pssjd commented Oct 6, 2022

@vincentarelbundock

if the lapply modifies the random component

I am not sure what do you mean, but maybe you refer if I use the as.formula x paste combination in the random component. Indeed, this part is not solved with this PR, it behaves like until now, the only information about random formula contained in lme object is that in call, which is the used by find_formula.lme, so the issue would persist for the random part.

@iago-pssjd
Copy link
Author

I close the issue, since th PR to solve the fixed effects part was merged and I do not see there is any chance to solve the random effects part as described in previous comments above.

@strengejacke strengejacke added the Won't fix 🚫 This will not be worked on label Oct 10, 2022
@strengejacke
Copy link
Member

thanks, we can always open a new issue if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Won't fix 🚫 This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants