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

clean_names: correctly extract several variables from mgcv::s() #573

Merged
merged 5 commits into from
Jun 7, 2022

Conversation

etiennebacher
Copy link
Member

Close #553

library(insight)
library(mgcv)
#> Loading required package: nlme
#> This is mgcv 1.8-40. For overview type 'help("mgcv-package")'.

set.seed(2) ## simulate some data... 
dat <- gamSim(1,n=400,dist="normal",scale=2)
#> Gu & Wahba 4 term additive model
b <- gam(y~x0+s(x1)+s(x2)+x3+s(x1,x2),data=dat)

find_smooth(b, flatten = TRUE) |> clean_names()
#> [1] "x1"     "x2"     "x1, x2"

Created on 2022-06-01 by the reprex package (v2.0.1)

@etiennebacher
Copy link
Member Author

Works with mgcv::s() but not with gam::s():

mgcv::s(Age)
#> $term
#> [1] "Age"
#> 
#> $bs.dim
#> [1] -1
#> 
#> $fixed
#> [1] FALSE
#> 
#> $dim
#> [1] 1
#> 
#> $p.order
#> [1] NA
#> 
#> $by
#> [1] "NA"
#> 
#> $label
#> [1] "s(Age)"
#> 
#> $xt
#> NULL
#> 
#> $id
#> NULL
#> 
#> $sp
#> NULL
#> 
#> attr(,"class")
#> [1] "tp.smooth.spec"
gam::s(Age)
#> Error in as.matrix(x): object 'Age' not found

Created on 2022-06-01 by the reprex package (v2.0.1)

Using eval(parse) is probably the wrong approach because of this namespace conflict

@etiennebacher
Copy link
Member Author

@IndrajeetPatil @strengejacke Unless you have changes to suggest, I think this is good to go:

library(insight)
library(mgcv)
#> Le chargement a nécessité le package : nlme
#> This is mgcv 1.8-40. For overview type 'help("mgcv-package")'.

dat <- gamSim(1,n=400,dist="normal",scale=2)
#> Gu & Wahba 4 term additive model
b <- gam(y~x0+s(x1)+s(x2)+x3+s(x1,x2),data=dat)

find_smooth(b, flatten = TRUE) |> clean_names()
#> [1] "x1"     "x2"     "x1, x2"

Created on 2022-06-07 by the reprex package (v2.0.1)

@IndrajeetPatil
Copy link
Member

LGTM, thanks!

@etiennebacher
Copy link
Member Author

actually, I noticed some improvements to make, don't merge now

@etiennebacher etiennebacher merged commit ab41b91 into easystats:master Jun 7, 2022
@etiennebacher etiennebacher deleted the fix-clean-names branch June 7, 2022 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Question - Is there a way to find variables for smooth components in mgcv::gam?
2 participants