Skip to contents

In particular, the resulting dataframe will always contain:

  • column called id containing observation id values;

  • column called weight containing weight values;

  • a dataframe-column called x containing all columns that are going to be used in segmentation models, or in other words, features. You can think of it as of a two-level header in a summary table. More details to follow in examples.

Usage

pack_model_data(data, id_col, weight_col = NULL, x_cols = dplyr::everything())

Arguments

data

a dataframe, with vector-columns only

id_col

a tidyselect expression to select id column This means that you can provide quoted name, unquoted name or column position number. The column must be either character, factor or numeric. A check is performed to ensure it does not contain any duplicated values.

weight_col

a tidyselect expression to select a column with weights. By default it is set to NULL and in this case all-1 weights will be used. The column must be numeric, mustn't contain negative or missing values.

x_cols

a tidyselect expression to select features. By default is set to everything() which means that all columns that were not marked as either id or weight column will be selected. Features can be either plain numeric or haven_labelled numeric (coming from haven::read_spss). It will be checked that segmentation columns contain no missing values.

Examples

seg_data <- pack_model_data(
  example_seg_df,
  id_col = response_id,
  weight_col = weight,
  x_cols = starts_with("m2_")
)
#> ✔ Converted 54 <factor> columns to <labelled numeric>: m2_1_1, m2_1_2, m2_1_3, m2_1_4, m2_1_5, m2_1_6, m2_1_7, m2_2_8, m2_2_9, m2_2_10, m2_2_11, m2_2_12, m2_2_13, m2_2_14, m2_2_16, m2_3_17, m2_3_18, m2_3_19, …, m2_6_56, and m2_6_57.
#> 
#> ── Summary of mapping for 54 <labelled> columns ────────────────────────────────
#> 
#> [54 columns] All columns:
#>   1: Strongly disagree
#>   2: Somewhat disagree
#>   3: Somewhat agree
#>   4: Strongly agree
#> 

# 3 columns: id, weight, x
names(seg_data)
#> [1] "id"     "weight" "x"     

# extracts all features at once
seg_data$x
#> # A tibble: 1,512 × 54
#>    m2_1_1        m2_1_2  m2_1_3  m2_1_4  m2_1_5  m2_1_6  m2_1_7  m2_2_8  m2_2_9 
#>    <dbl+lbl>     <dbl+l> <dbl+l> <dbl+l> <dbl+l> <dbl+l> <dbl+l> <dbl+l> <dbl+l>
#>  1 2 [Somewhat … 3 [Som… 2 [Som… 3 [Som… 2 [Som… 3 [Som… 3 [Som… 3 [Som… 3 [Som…
#>  2 4 [Strongly … 4 [Str… 4 [Str… 4 [Str… 4 [Str… 4 [Str… 4 [Str… 4 [Str… 4 [Str…
#>  3 3 [Somewhat … 3 [Som… 2 [Som… 3 [Som… 2 [Som… 3 [Som… 3 [Som… 3 [Som… 2 [Som…
#>  4 4 [Strongly … 4 [Str… 3 [Som… 4 [Str… 3 [Som… 4 [Str… 2 [Som… 2 [Som… 3 [Som…
#>  5 2 [Somewhat … 3 [Som… 3 [Som… 3 [Som… 3 [Som… 3 [Som… 2 [Som… 2 [Som… 3 [Som…
#>  6 3 [Somewhat … 4 [Str… 1 [Str… 4 [Str… 1 [Str… 3 [Som… 3 [Som… 1 [Str… 4 [Str…
#>  7 1 [Strongly … 3 [Som… 3 [Som… 3 [Som… 1 [Str… 3 [Som… 3 [Som… 4 [Str… 4 [Str…
#>  8 3 [Somewhat … 3 [Som… 2 [Som… 3 [Som… 2 [Som… 4 [Str… 3 [Som… 4 [Str… 4 [Str…
#>  9 3 [Somewhat … 3 [Som… 2 [Som… 3 [Som… 2 [Som… 2 [Som… 2 [Som… 3 [Som… 2 [Som…
#> 10 4 [Strongly … 3 [Som… 4 [Str… 3 [Som… 4 [Str… 4 [Str… 4 [Str… 4 [Str… 4 [Str…
#> # ℹ 1,502 more rows
#> # ℹ 45 more variables: m2_2_10 <dbl+lbl>, m2_2_11 <dbl+lbl>, m2_2_12 <dbl+lbl>,
#> #   m2_2_13 <dbl+lbl>, m2_2_14 <dbl+lbl>, m2_2_16 <dbl+lbl>, m2_3_17 <dbl+lbl>,
#> #   m2_3_18 <dbl+lbl>, m2_3_19 <dbl+lbl>, m2_3_21 <dbl+lbl>, m2_3_22 <dbl+lbl>,
#> #   m2_3_23 <dbl+lbl>, m2_4_24 <dbl+lbl>, m2_4_25 <dbl+lbl>, m2_4_26 <dbl+lbl>,
#> #   m2_4_27 <dbl+lbl>, m2_4_28 <dbl+lbl>, m2_4_29 <dbl+lbl>, m2_4_30 <dbl+lbl>,
#> #   m2_4_31 <dbl+lbl>, m2_4_32 <dbl+lbl>, m2_4_33 <dbl+lbl>, …

# to extract one column
seg_data$x$m2_1_1
#> <labelled<double>[1512]>: Achieving personal success is most important to me
#>    [1] 2 4 3 4 2 3 1 3 3 4 3 4 4 2 4 4 2 3 4 3 4 3 3 1 1 1 3 4 3 3 3 3 4 4 4 4 3
#>   [38] 2 3 3 2 1 4 2 2 4 3 1 4 3 4 3 3 2 1 2 3 2 2 3 3 3 2 2 3 4 3 2 2 1 3 4 1 3
#>   [75] 4 1 3 2 3 4 2 3 1 3 3 2 1 1 1 3 2 3 4 2 1 2 2 3 3 3 2 3 3 3 3 2 2 3 4 3 3
#>  [112] 3 2 4 3 3 2 3 4 2 2 3 4 3 3 3 2 2 2 2 2 4 3 4 2 3 3 2 4 3 3 4 2 4 3 3 3 2
#>  [149] 2 3 3 4 3 4 3 3 3 4 4 3 4 4 3 4 2 3 1 3 2 4 2 3 2 2 3 3 4 2 4 3 4 2 1 2 2
#>  [186] 4 1 1 4 1 2 4 3 3 2 1 4 2 2 4 2 3 3 3 1 1 3 3 3 3 4 2 4 3 2 3 1 3 4 3 3 4
#>  [223] 3 4 3 2 3 3 3 4 3 4 2 3 3 4 2 3 1 3 3 2 1 4 4 4 4 3 4 3 4 4 3 3 3 1 3 2 3
#>  [260] 2 2 3 3 2 4 4 4 2 4 3 3 2 2 3 3 2 4 4 3 3 3 4 2 1 3 3 3 4 3 3 4 3 1 3 3 1
#>  [297] 3 3 2 4 3 4 2 3 3 1 2 2 2 3 3 3 2 3 3 2 3 1 3 4 3 3 2 2 3 4 3 4 3 3 4 3 4
#>  [334] 3 3 4 2 3 3 2 4 4 3 3 4 4 3 3 4 4 2 4 2 4 3 2 3 3 3 4 4 3 4 3 4 3 4 3 3 2
#>  [371] 3 4 4 4 4 3 3 3 2 4 4 4 4 3 1 4 3 3 3 3 3 3 3 3 4 3 4 3 3 3 2 3 4 3 2 3 2
#>  [408] 2 3 3 4 1 3 3 3 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 2 4 4 3 3 4 4 3 2 2 3 4 3 3 4 3 2
#> [1370] 4 3 3 4 3 3 2 3 3 3 4 1 2 2 3 4 3 3 2 4 3 4 3 3 2 3 2 3 4 2 3 4 3 3 3 2 4
#> [1407] 2 2 2 2 3 3 3 1 3 4 2 3 4 4 2 2 2 4 4 3 3 2 3 4 3 2 3 1 4 3 4 4 4 3 1 2 3
#> [1444] 4 4 3 3 4 3 2 3 4 2 2 3 3 4 3 3 3 3 1 4 3 2 4 3 1 3 2 2 2 2 2 3 4 2 2 2 3
#> [1481] 4 4 3 4 3 3 3 1 4 4 4 3 3 3 4 3 4 4 4 3 3 1 2 3 2 4 3 4 4 2 3 2
#> 
#> Labels:
#>  value             label
#>      1 Strongly disagree
#>      2 Somewhat disagree
#>      3    Somewhat agree
#>      4    Strongly agree

# to 'unpack' features to have a normal flat structure
library(tidyr)
unpack(seg_data, x) 
#> # A tibble: 1,512 × 56
#>    id     weight m2_1_1  m2_1_2  m2_1_3  m2_1_4  m2_1_5  m2_1_6  m2_1_7  m2_2_8 
#>    <chr>   <dbl> <dbl+l> <dbl+l> <dbl+l> <dbl+l> <dbl+l> <dbl+l> <dbl+l> <dbl+l>
#>  1 R_yyy…  0.771 2 [Som… 3 [Som… 2 [Som… 3 [Som… 2 [Som… 3 [Som… 3 [Som… 3 [Som…
#>  2 R_3Jq…  0.879 4 [Str… 4 [Str… 4 [Str… 4 [Str… 4 [Str… 4 [Str… 4 [Str… 4 [Str…
#>  3 R_TsV…  1.08  3 [Som… 3 [Som… 2 [Som… 3 [Som… 2 [Som… 3 [Som… 3 [Som… 3 [Som…
#>  4 R_1Fn…  1.42  4 [Str… 4 [Str… 3 [Som… 4 [Str… 3 [Som… 4 [Str… 2 [Som… 2 [Som…
#>  5 R_ai3…  0.707 2 [Som… 3 [Som… 3 [Som… 3 [Som… 3 [Som… 3 [Som… 2 [Som… 2 [Som…
#>  6 R_2uw…  1.41  3 [Som… 4 [Str… 1 [Str… 4 [Str… 1 [Str… 3 [Som… 3 [Som… 1 [Str…
#>  7 R_1Pd…  1.46  1 [Str… 3 [Som… 3 [Som… 3 [Som… 1 [Str… 3 [Som… 3 [Som… 4 [Str…
#>  8 R_enW…  1.17  3 [Som… 3 [Som… 2 [Som… 3 [Som… 2 [Som… 4 [Str… 3 [Som… 4 [Str…
#>  9 R_1mC…  1.14  3 [Som… 3 [Som… 2 [Som… 3 [Som… 2 [Som… 2 [Som… 2 [Som… 3 [Som…
#> 10 R_2V4…  0.566 4 [Str… 3 [Som… 4 [Str… 3 [Som… 4 [Str… 4 [Str… 4 [Str… 4 [Str…
#> # ℹ 1,502 more rows
#> # ℹ 46 more variables: m2_2_9 <dbl+lbl>, m2_2_10 <dbl+lbl>, m2_2_11 <dbl+lbl>,
#> #   m2_2_12 <dbl+lbl>, m2_2_13 <dbl+lbl>, m2_2_14 <dbl+lbl>, m2_2_16 <dbl+lbl>,
#> #   m2_3_17 <dbl+lbl>, m2_3_18 <dbl+lbl>, m2_3_19 <dbl+lbl>, m2_3_21 <dbl+lbl>,
#> #   m2_3_22 <dbl+lbl>, m2_3_23 <dbl+lbl>, m2_4_24 <dbl+lbl>, m2_4_25 <dbl+lbl>,
#> #   m2_4_26 <dbl+lbl>, m2_4_27 <dbl+lbl>, m2_4_28 <dbl+lbl>, m2_4_29 <dbl+lbl>,
#> #   m2_4_30 <dbl+lbl>, m2_4_31 <dbl+lbl>, m2_4_32 <dbl+lbl>, …