Skip to contents

Will remove unselected variables from the results. To remove the intercept, use tidy_remove_intercept().

Usage

tidy_select_variables(x, include = everything(), model = tidy_get_model(x))

Arguments

x

(data.frame)
A tidy tibble as produced by tidy_*() functions.

include

(tidy-select)
Variables to include. Default is everything(). See also all_continuous(), all_categorical(), all_dichotomous() and all_interaction().

model

(a model object, e.g. glm)
The corresponding model, if not attached to x.

Value

The x tibble limited to the included variables (and eventually the intercept), sorted according to the include parameter.

Details

If the variable column is not yet available in x, tidy_identify_variables() will be automatically applied.

Examples

df <- Titanic |>
  dplyr::as_tibble() |>
  dplyr::mutate(Survived = factor(Survived))
res <-
  glm(Survived ~ Class + Age * Sex, data = df, weights = df$n, family = binomial) |>
  tidy_and_attach() |>
  tidy_identify_variables()

res
#> # A tibble: 7 × 11
#>   term      variable var_class var_type var_nlevels estimate std.error statistic
#>   <chr>     <chr>    <chr>     <chr>          <int>    <dbl>     <dbl>     <dbl>
#> 1 (Interce… (Interc… NA        interce…          NA    2.18      0.176    12.4  
#> 2 Class2nd  Class    character categor…           4   -1.03      0.200    -5.17 
#> 3 Class3rd  Class    character categor…           4   -1.81      0.176   -10.3  
#> 4 ClassCrew Class    character categor…           4   -0.803     0.160    -5.03 
#> 5 AgeChild  Age      character dichoto…           2   -0.110     0.335    -0.328
#> 6 SexMale   Sex      character dichoto…           2   -2.62      0.151   -17.3  
#> 7 AgeChild… Age:Sex  NA        interac…          NA    1.90      0.433     4.39 
#> # ℹ 3 more variables: p.value <dbl>, conf.low <dbl>, conf.high <dbl>
res |> tidy_select_variables()
#> # A tibble: 7 × 11
#>   term      variable var_class var_type var_nlevels estimate std.error statistic
#>   <chr>     <chr>    <chr>     <chr>          <int>    <dbl>     <dbl>     <dbl>
#> 1 (Interce… (Interc… NA        interce…          NA    2.18      0.176    12.4  
#> 2 Class2nd  Class    character categor…           4   -1.03      0.200    -5.17 
#> 3 Class3rd  Class    character categor…           4   -1.81      0.176   -10.3  
#> 4 ClassCrew Class    character categor…           4   -0.803     0.160    -5.03 
#> 5 AgeChild  Age      character dichoto…           2   -0.110     0.335    -0.328
#> 6 SexMale   Sex      character dichoto…           2   -2.62      0.151   -17.3  
#> 7 AgeChild… Age:Sex  NA        interac…          NA    1.90      0.433     4.39 
#> # ℹ 3 more variables: p.value <dbl>, conf.low <dbl>, conf.high <dbl>
res |> tidy_select_variables(include = "Class")
#> # A tibble: 4 × 11
#>   term      variable var_class var_type var_nlevels estimate std.error statistic
#>   <chr>     <chr>    <chr>     <chr>          <int>    <dbl>     <dbl>     <dbl>
#> 1 (Interce… (Interc… NA        interce…          NA    2.18      0.176     12.4 
#> 2 Class2nd  Class    character categor…           4   -1.03      0.200     -5.17
#> 3 Class3rd  Class    character categor…           4   -1.81      0.176    -10.3 
#> 4 ClassCrew Class    character categor…           4   -0.803     0.160     -5.03
#> # ℹ 3 more variables: p.value <dbl>, conf.low <dbl>, conf.high <dbl>
res |> tidy_select_variables(include = -c("Age", "Sex"))
#> # A tibble: 5 × 11
#>   term      variable var_class var_type var_nlevels estimate std.error statistic
#>   <chr>     <chr>    <chr>     <chr>          <int>    <dbl>     <dbl>     <dbl>
#> 1 (Interce… (Interc… NA        interce…          NA    2.18      0.176     12.4 
#> 2 Class2nd  Class    character categor…           4   -1.03      0.200     -5.17
#> 3 Class3rd  Class    character categor…           4   -1.81      0.176    -10.3 
#> 4 ClassCrew Class    character categor…           4   -0.803     0.160     -5.03
#> 5 AgeChild… Age:Sex  NA        interac…          NA    1.90      0.433      4.39
#> # ℹ 3 more variables: p.value <dbl>, conf.low <dbl>, conf.high <dbl>
res |> tidy_select_variables(include = starts_with("A"))
#> # A tibble: 3 × 11
#>   term      variable var_class var_type var_nlevels estimate std.error statistic
#>   <chr>     <chr>    <chr>     <chr>          <int>    <dbl>     <dbl>     <dbl>
#> 1 (Interce… (Interc… NA        interce…          NA    2.18      0.176    12.4  
#> 2 AgeChild  Age      character dichoto…           2   -0.110     0.335    -0.328
#> 3 AgeChild… Age:Sex  NA        interac…          NA    1.90      0.433     4.39 
#> # ℹ 3 more variables: p.value <dbl>, conf.low <dbl>, conf.high <dbl>
res |> tidy_select_variables(include = all_categorical())
#> # A tibble: 6 × 11
#>   term      variable var_class var_type var_nlevels estimate std.error statistic
#>   <chr>     <chr>    <chr>     <chr>          <int>    <dbl>     <dbl>     <dbl>
#> 1 (Interce… (Interc… NA        interce…          NA    2.18      0.176    12.4  
#> 2 Class2nd  Class    character categor…           4   -1.03      0.200    -5.17 
#> 3 Class3rd  Class    character categor…           4   -1.81      0.176   -10.3  
#> 4 ClassCrew Class    character categor…           4   -0.803     0.160    -5.03 
#> 5 AgeChild  Age      character dichoto…           2   -0.110     0.335    -0.328
#> 6 SexMale   Sex      character dichoto…           2   -2.62      0.151   -17.3  
#> # ℹ 3 more variables: p.value <dbl>, conf.low <dbl>, conf.high <dbl>
res |> tidy_select_variables(include = all_dichotomous())
#> # A tibble: 3 × 11
#>   term      variable var_class var_type var_nlevels estimate std.error statistic
#>   <chr>     <chr>    <chr>     <chr>          <int>    <dbl>     <dbl>     <dbl>
#> 1 (Interce… (Interc… NA        interce…          NA    2.18      0.176    12.4  
#> 2 AgeChild  Age      character dichoto…           2   -0.110     0.335    -0.328
#> 3 SexMale   Sex      character dichoto…           2   -2.62      0.151   -17.3  
#> # ℹ 3 more variables: p.value <dbl>, conf.low <dbl>, conf.high <dbl>
res |> tidy_select_variables(include = all_interaction())
#> # A tibble: 2 × 11
#>   term      variable var_class var_type var_nlevels estimate std.error statistic
#>   <chr>     <chr>    <chr>     <chr>          <int>    <dbl>     <dbl>     <dbl>
#> 1 (Interce… (Interc… NA        interce…          NA     2.18     0.176     12.4 
#> 2 AgeChild… Age:Sex  NA        interac…          NA     1.90     0.433      4.39
#> # ℹ 3 more variables: p.value <dbl>, conf.low <dbl>, conf.high <dbl>
res |> tidy_select_variables(
  include = c("Age", all_categorical(dichotomous = FALSE), all_interaction())
)
#> # A tibble: 6 × 11
#>   term      variable var_class var_type var_nlevels estimate std.error statistic
#>   <chr>     <chr>    <chr>     <chr>          <int>    <dbl>     <dbl>     <dbl>
#> 1 (Interce… (Interc… NA        interce…          NA    2.18      0.176    12.4  
#> 2 AgeChild  Age      character dichoto…           2   -0.110     0.335    -0.328
#> 3 Class2nd  Class    character categor…           4   -1.03      0.200    -5.17 
#> 4 Class3rd  Class    character categor…           4   -1.81      0.176   -10.3  
#> 5 ClassCrew Class    character categor…           4   -0.803     0.160    -5.03 
#> 6 AgeChild… Age:Sex  NA        interac…          NA    1.90      0.433     4.39 
#> # ℹ 3 more variables: p.value <dbl>, conf.low <dbl>, conf.high <dbl>