Skip to contents

Create a Freesurfer qdec from a model formula, utilising R's robust model formula syntax. The function also scales continuous variables, and can keep the original data in the output.

Usage

make_fs_qdec(data, formula, path = NULL, keep = FALSE)

Arguments

data

data.frame, list or environment containing the data. Neither matrix nor an array will be accepted.

formula

a model formula or terms object. For simple qdec file, with binary columns for all levels of a factor, make sure to include a formula with -1 to remove the intercept.

path

an option file path to write qdec to csv

keep

logical or vector of column names, to keep the original data in the output. Default is FALSE.

Value

data.frame with model matrix and scaled continuous variables.

Details

The formula should in all likelihood also include the -1 to remove the intercept, as this will provide a matrix where all levels of factor variables have their own binary column. This is necessary to follow the steps from the Freesurfer documentation.

It is highly recommended to have the base-id's for Freesurfer in their own column, and request to have this remain in the qdec by using the id-column's name in the keep argument.

Examples

cars <- mtcars
cars$cyl <- as.factor(cars$cyl)
cars$gear <- as.factor(cars$gear)

make_fs_qdec(cars, mpg ~ cyl + hp)
#>                     (Intercept) cyl6 cyl8        mpgz         hpz
#> Mazda RX4                     1    1    0  0.15088482 -0.53509284
#> Mazda RX4 Wag                 1    1    0  0.15088482 -0.53509284
#> Datsun 710                    1    0    0  0.44954345 -0.78304046
#> Hornet 4 Drive                1    1    0  0.21725341 -0.53509284
#> Hornet Sportabout             1    0    1 -0.23073453  0.41294217
#> Valiant                       1    1    0 -0.33028740 -0.60801861
#> Duster 360                    1    0    1 -0.96078893  1.43390296
#> Merc 240D                     1    0    0  0.71501778 -1.23518023
#> Merc 230                      1    0    0  0.44954345 -0.75387015
#> Merc 280                      1    1    0 -0.14777380 -0.34548584
#> Merc 280C                     1    1    0 -0.38006384 -0.34548584
#> Merc 450SE                    1    0    1 -0.61235388  0.48586794
#> Merc 450SL                    1    0    1 -0.46302456  0.48586794
#> Merc 450SLC                   1    0    1 -0.81145962  0.48586794
#> Cadillac Fleetwood            1    0    1 -1.60788262  0.85049680
#> Lincoln Continental           1    0    1 -1.60788262  0.99634834
#> Chrysler Imperial             1    0    1 -0.89442035  1.21512565
#> Fiat 128                      1    0    0  2.04238943 -1.17683962
#> Honda Civic                   1    0    0  1.71054652 -1.38103178
#> Toyota Corolla                1    0    0  2.29127162 -1.19142477
#> Toyota Corona                 1    0    0  0.23384555 -0.72469984
#> Dodge Challenger              1    0    1 -0.76168319  0.04831332
#> AMC Javelin                   1    0    1 -0.81145962  0.04831332
#> Camaro Z28                    1    0    1 -1.12671039  1.43390296
#> Pontiac Firebird              1    0    1 -0.14777380  0.41294217
#> Fiat X1-9                     1    0    0  1.19619000 -1.17683962
#> Porsche 914-2                 1    0    0  0.98049211 -0.81221077
#> Lotus Europa                  1    0    0  1.71054652 -0.49133738
#> Ford Pantera L                1    0    1 -0.71190675  1.71102089
#> Ferrari Dino                  1    1    0 -0.06481307  0.41294217
#> Maserati Bora                 1    0    1 -0.84464392  2.74656682
#> Volvo 142E                    1    0    0  0.21725341 -0.54967799

# Remove the intercept, necessary to follow
# steps from Freesurfer docs
make_fs_qdec(cars, mpg ~ -1 + cyl + hp)
#>                     cyl4 cyl6 cyl8        mpgz         hpz
#> Mazda RX4              0    1    0  0.15088482 -0.53509284
#> Mazda RX4 Wag          0    1    0  0.15088482 -0.53509284
#> Datsun 710             1    0    0  0.44954345 -0.78304046
#> Hornet 4 Drive         0    1    0  0.21725341 -0.53509284
#> Hornet Sportabout      0    0    1 -0.23073453  0.41294217
#> Valiant                0    1    0 -0.33028740 -0.60801861
#> Duster 360             0    0    1 -0.96078893  1.43390296
#> Merc 240D              1    0    0  0.71501778 -1.23518023
#> Merc 230               1    0    0  0.44954345 -0.75387015
#> Merc 280               0    1    0 -0.14777380 -0.34548584
#> Merc 280C              0    1    0 -0.38006384 -0.34548584
#> Merc 450SE             0    0    1 -0.61235388  0.48586794
#> Merc 450SL             0    0    1 -0.46302456  0.48586794
#> Merc 450SLC            0    0    1 -0.81145962  0.48586794
#> Cadillac Fleetwood     0    0    1 -1.60788262  0.85049680
#> Lincoln Continental    0    0    1 -1.60788262  0.99634834
#> Chrysler Imperial      0    0    1 -0.89442035  1.21512565
#> Fiat 128               1    0    0  2.04238943 -1.17683962
#> Honda Civic            1    0    0  1.71054652 -1.38103178
#> Toyota Corolla         1    0    0  2.29127162 -1.19142477
#> Toyota Corona          1    0    0  0.23384555 -0.72469984
#> Dodge Challenger       0    0    1 -0.76168319  0.04831332
#> AMC Javelin            0    0    1 -0.81145962  0.04831332
#> Camaro Z28             0    0    1 -1.12671039  1.43390296
#> Pontiac Firebird       0    0    1 -0.14777380  0.41294217
#> Fiat X1-9              1    0    0  1.19619000 -1.17683962
#> Porsche 914-2          1    0    0  0.98049211 -0.81221077
#> Lotus Europa           1    0    0  1.71054652 -0.49133738
#> Ford Pantera L         0    0    1 -0.71190675  1.71102089
#> Ferrari Dino           0    1    0 -0.06481307  0.41294217
#> Maserati Bora          0    0    1 -0.84464392  2.74656682
#> Volvo 142E             1    0    0  0.21725341 -0.54967799
make_fs_qdec(cars, mpg ~ -1 + cyl + hp + gear)
#>                     cyl4 cyl6 cyl8 gear4 gear5        mpgz         hpz
#> Mazda RX4              0    1    0     1     0  0.15088482 -0.53509284
#> Mazda RX4 Wag          0    1    0     1     0  0.15088482 -0.53509284
#> Datsun 710             1    0    0     1     0  0.44954345 -0.78304046
#> Hornet 4 Drive         0    1    0     0     0  0.21725341 -0.53509284
#> Hornet Sportabout      0    0    1     0     0 -0.23073453  0.41294217
#> Valiant                0    1    0     0     0 -0.33028740 -0.60801861
#> Duster 360             0    0    1     0     0 -0.96078893  1.43390296
#> Merc 240D              1    0    0     1     0  0.71501778 -1.23518023
#> Merc 230               1    0    0     1     0  0.44954345 -0.75387015
#> Merc 280               0    1    0     1     0 -0.14777380 -0.34548584
#> Merc 280C              0    1    0     1     0 -0.38006384 -0.34548584
#> Merc 450SE             0    0    1     0     0 -0.61235388  0.48586794
#> Merc 450SL             0    0    1     0     0 -0.46302456  0.48586794
#> Merc 450SLC            0    0    1     0     0 -0.81145962  0.48586794
#> Cadillac Fleetwood     0    0    1     0     0 -1.60788262  0.85049680
#> Lincoln Continental    0    0    1     0     0 -1.60788262  0.99634834
#> Chrysler Imperial      0    0    1     0     0 -0.89442035  1.21512565
#> Fiat 128               1    0    0     1     0  2.04238943 -1.17683962
#> Honda Civic            1    0    0     1     0  1.71054652 -1.38103178
#> Toyota Corolla         1    0    0     1     0  2.29127162 -1.19142477
#> Toyota Corona          1    0    0     0     0  0.23384555 -0.72469984
#> Dodge Challenger       0    0    1     0     0 -0.76168319  0.04831332
#> AMC Javelin            0    0    1     0     0 -0.81145962  0.04831332
#> Camaro Z28             0    0    1     0     0 -1.12671039  1.43390296
#> Pontiac Firebird       0    0    1     0     0 -0.14777380  0.41294217
#> Fiat X1-9              1    0    0     1     0  1.19619000 -1.17683962
#> Porsche 914-2          1    0    0     0     1  0.98049211 -0.81221077
#> Lotus Europa           1    0    0     0     1  1.71054652 -0.49133738
#> Ford Pantera L         0    0    1     0     1 -0.71190675  1.71102089
#> Ferrari Dino           0    1    0     0     1 -0.06481307  0.41294217
#> Maserati Bora          0    0    1     0     1 -0.84464392  2.74656682
#> Volvo 142E             1    0    0     1     0  0.21725341 -0.54967799

# Keep the original data also in the output
make_fs_qdec(cars, mpg ~ -1 + cyl + hp, keep = TRUE)
#>                     cyl4 cyl6 cyl8        mpgz         hpz  mpg cyl  hp
#> Mazda RX4              0    1    0  0.15088482 -0.53509284 21.0   6 110
#> Mazda RX4 Wag          0    1    0  0.15088482 -0.53509284 21.0   6 110
#> Datsun 710             1    0    0  0.44954345 -0.78304046 22.8   4  93
#> Hornet 4 Drive         0    1    0  0.21725341 -0.53509284 21.4   6 110
#> Hornet Sportabout      0    0    1 -0.23073453  0.41294217 18.7   8 175
#> Valiant                0    1    0 -0.33028740 -0.60801861 18.1   6 105
#> Duster 360             0    0    1 -0.96078893  1.43390296 14.3   8 245
#> Merc 240D              1    0    0  0.71501778 -1.23518023 24.4   4  62
#> Merc 230               1    0    0  0.44954345 -0.75387015 22.8   4  95
#> Merc 280               0    1    0 -0.14777380 -0.34548584 19.2   6 123
#> Merc 280C              0    1    0 -0.38006384 -0.34548584 17.8   6 123
#> Merc 450SE             0    0    1 -0.61235388  0.48586794 16.4   8 180
#> Merc 450SL             0    0    1 -0.46302456  0.48586794 17.3   8 180
#> Merc 450SLC            0    0    1 -0.81145962  0.48586794 15.2   8 180
#> Cadillac Fleetwood     0    0    1 -1.60788262  0.85049680 10.4   8 205
#> Lincoln Continental    0    0    1 -1.60788262  0.99634834 10.4   8 215
#> Chrysler Imperial      0    0    1 -0.89442035  1.21512565 14.7   8 230
#> Fiat 128               1    0    0  2.04238943 -1.17683962 32.4   4  66
#> Honda Civic            1    0    0  1.71054652 -1.38103178 30.4   4  52
#> Toyota Corolla         1    0    0  2.29127162 -1.19142477 33.9   4  65
#> Toyota Corona          1    0    0  0.23384555 -0.72469984 21.5   4  97
#> Dodge Challenger       0    0    1 -0.76168319  0.04831332 15.5   8 150
#> AMC Javelin            0    0    1 -0.81145962  0.04831332 15.2   8 150
#> Camaro Z28             0    0    1 -1.12671039  1.43390296 13.3   8 245
#> Pontiac Firebird       0    0    1 -0.14777380  0.41294217 19.2   8 175
#> Fiat X1-9              1    0    0  1.19619000 -1.17683962 27.3   4  66
#> Porsche 914-2          1    0    0  0.98049211 -0.81221077 26.0   4  91
#> Lotus Europa           1    0    0  1.71054652 -0.49133738 30.4   4 113
#> Ford Pantera L         0    0    1 -0.71190675  1.71102089 15.8   8 264
#> Ferrari Dino           0    1    0 -0.06481307  0.41294217 19.7   6 175
#> Maserati Bora          0    0    1 -0.84464392  2.74656682 15.0   8 335
#> Volvo 142E             1    0    0  0.21725341 -0.54967799 21.4   4 109

# Keep the original data of specific columns
# Use a character vector
make_fs_qdec(cars, mpg ~ -1 + cyl + hp, keep = c("mpg", "gear"))
#>                     cyl4 cyl6 cyl8        mpgz         hpz  mpg gear
#> Mazda RX4              0    1    0  0.15088482 -0.53509284 21.0    4
#> Mazda RX4 Wag          0    1    0  0.15088482 -0.53509284 21.0    4
#> Datsun 710             1    0    0  0.44954345 -0.78304046 22.8    4
#> Hornet 4 Drive         0    1    0  0.21725341 -0.53509284 21.4    3
#> Hornet Sportabout      0    0    1 -0.23073453  0.41294217 18.7    3
#> Valiant                0    1    0 -0.33028740 -0.60801861 18.1    3
#> Duster 360             0    0    1 -0.96078893  1.43390296 14.3    3
#> Merc 240D              1    0    0  0.71501778 -1.23518023 24.4    4
#> Merc 230               1    0    0  0.44954345 -0.75387015 22.8    4
#> Merc 280               0    1    0 -0.14777380 -0.34548584 19.2    4
#> Merc 280C              0    1    0 -0.38006384 -0.34548584 17.8    4
#> Merc 450SE             0    0    1 -0.61235388  0.48586794 16.4    3
#> Merc 450SL             0    0    1 -0.46302456  0.48586794 17.3    3
#> Merc 450SLC            0    0    1 -0.81145962  0.48586794 15.2    3
#> Cadillac Fleetwood     0    0    1 -1.60788262  0.85049680 10.4    3
#> Lincoln Continental    0    0    1 -1.60788262  0.99634834 10.4    3
#> Chrysler Imperial      0    0    1 -0.89442035  1.21512565 14.7    3
#> Fiat 128               1    0    0  2.04238943 -1.17683962 32.4    4
#> Honda Civic            1    0    0  1.71054652 -1.38103178 30.4    4
#> Toyota Corolla         1    0    0  2.29127162 -1.19142477 33.9    4
#> Toyota Corona          1    0    0  0.23384555 -0.72469984 21.5    3
#> Dodge Challenger       0    0    1 -0.76168319  0.04831332 15.5    3
#> AMC Javelin            0    0    1 -0.81145962  0.04831332 15.2    3
#> Camaro Z28             0    0    1 -1.12671039  1.43390296 13.3    3
#> Pontiac Firebird       0    0    1 -0.14777380  0.41294217 19.2    3
#> Fiat X1-9              1    0    0  1.19619000 -1.17683962 27.3    4
#> Porsche 914-2          1    0    0  0.98049211 -0.81221077 26.0    5
#> Lotus Europa           1    0    0  1.71054652 -0.49133738 30.4    5
#> Ford Pantera L         0    0    1 -0.71190675  1.71102089 15.8    5
#> Ferrari Dino           0    1    0 -0.06481307  0.41294217 19.7    5
#> Maserati Bora          0    0    1 -0.84464392  2.74656682 15.0    5
#> Volvo 142E             1    0    0  0.21725341 -0.54967799 21.4    4