Leveraging R’s power of making models, we can create a QDEC file from a model formula and a data frame. The function relies on the correct setup of data (categoricals as factors etc), which you would normally do in R.
First, we prepare a data.frame. We will use the mtcars
data to imagine what we want to do. We will make sure to convert the
cyl
and gear
columns to factors, to prepare
ourselves for how to deal with categorical variables in the qdec
file.
cars <- mtcars
cars$cyl <- factor(cars$cyl)
cars$gear <- factor(cars$gear)
cars
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
#> Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
#> Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
#> Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
#> Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
#> Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
#> Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
#> Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
#> [ reached 'max' / getOption("max.print") -- omitted 23 rows ]
To create a qdec object, we use the make_fs_qdec
function, with a formula and the data. The formula decides what
variables the qdec file will contain, and will binarise the levels of
factors, and scale continuous variables so they have a mean of 0 and a
standard deviation of 1. Make sure to include -1
to remove
intercept, which would make sure that factors have all levels
represented in the qdec file.
make_fs_qdec(cars, mpg ~ -1 + cyl + hp)
#> cyl4 cyl6 cyl8 mpgz hpz
#> Mazda RX4 0 1 0 0.1508848 -0.5350928
#> Mazda RX4 Wag 0 1 0 0.1508848 -0.5350928
#> Datsun 710 1 0 0 0.4495434 -0.7830405
#> Hornet 4 Drive 0 1 0 0.2172534 -0.5350928
#> Hornet Sportabout 0 0 1 -0.2307345 0.4129422
#> Valiant 0 1 0 -0.3302874 -0.6080186
#> Duster 360 0 0 1 -0.9607889 1.4339030
#> Merc 240D 1 0 0 0.7150178 -1.2351802
#> Merc 230 1 0 0 0.4495434 -0.7538702
#> Merc 280 0 1 0 -0.1477738 -0.3454858
#> Merc 280C 0 1 0 -0.3800638 -0.3454858
#> Merc 450SE 0 0 1 -0.6123539 0.4858679
#> Merc 450SL 0 0 1 -0.4630246 0.4858679
#> Merc 450SLC 0 0 1 -0.8114596 0.4858679
#> Cadillac Fleetwood 0 0 1 -1.6078826 0.8504968
#> Lincoln Continental 0 0 1 -1.6078826 0.9963483
#> Chrysler Imperial 0 0 1 -0.8944204 1.2151256
#> Fiat 128 1 0 0 2.0423894 -1.1768396
#> Honda Civic 1 0 0 1.7105465 -1.3810318
#> Toyota Corolla 1 0 0 2.2912716 -1.1914248
#> [ reached 'max' / getOption("max.print") -- omitted 12 rows ]
If you do not include -1
, the model will not expose all
levels of factors, but capture the first levels of each factor in the
Intercept
which will consist of only the value
1
.
make_fs_qdec(cars, mpg ~ cyl + hp)
#> (Intercept) cyl6 cyl8 mpgz hpz
#> Mazda RX4 1 1 0 0.1508848 -0.5350928
#> Mazda RX4 Wag 1 1 0 0.1508848 -0.5350928
#> Datsun 710 1 0 0 0.4495434 -0.7830405
#> Hornet 4 Drive 1 1 0 0.2172534 -0.5350928
#> Hornet Sportabout 1 0 1 -0.2307345 0.4129422
#> Valiant 1 1 0 -0.3302874 -0.6080186
#> Duster 360 1 0 1 -0.9607889 1.4339030
#> Merc 240D 1 0 0 0.7150178 -1.2351802
#> Merc 230 1 0 0 0.4495434 -0.7538702
#> Merc 280 1 1 0 -0.1477738 -0.3454858
#> Merc 280C 1 1 0 -0.3800638 -0.3454858
#> Merc 450SE 1 0 1 -0.6123539 0.4858679
#> Merc 450SL 1 0 1 -0.4630246 0.4858679
#> Merc 450SLC 1 0 1 -0.8114596 0.4858679
#> Cadillac Fleetwood 1 0 1 -1.6078826 0.8504968
#> Lincoln Continental 1 0 1 -1.6078826 0.9963483
#> Chrysler Imperial 1 0 1 -0.8944204 1.2151256
#> Fiat 128 1 0 0 2.0423894 -1.1768396
#> Honda Civic 1 0 0 1.7105465 -1.3810318
#> Toyota Corolla 1 0 0 2.2912716 -1.1914248
#> [ reached 'max' / getOption("max.print") -- omitted 12 rows ]
-1
makes sure you get a qdec as expected from the Freesurfer
documentation
You can keep the original data in the qdec file by using the
keep
argument. This is useful if you want to keep the
original data for reference, or if you want to use the qdec file for
other purposes than just running a model. By default, keep
is FALSE
, and by switching to TRUE
you keep
the original data of the columns in your model.
make_fs_qdec(cars, mpg ~ -1 + cyl + hp, keep = TRUE)
#> cyl4 cyl6 cyl8 mpgz hpz mpg cyl hp
#> Mazda RX4 0 1 0 0.1508848 -0.5350928 21.0 6 110
#> Mazda RX4 Wag 0 1 0 0.1508848 -0.5350928 21.0 6 110
#> Datsun 710 1 0 0 0.4495434 -0.7830405 22.8 4 93
#> Hornet 4 Drive 0 1 0 0.2172534 -0.5350928 21.4 6 110
#> Hornet Sportabout 0 0 1 -0.2307345 0.4129422 18.7 8 175
#> Valiant 0 1 0 -0.3302874 -0.6080186 18.1 6 105
#> Duster 360 0 0 1 -0.9607889 1.4339030 14.3 8 245
#> Merc 240D 1 0 0 0.7150178 -1.2351802 24.4 4 62
#> Merc 230 1 0 0 0.4495434 -0.7538702 22.8 4 95
#> Merc 280 0 1 0 -0.1477738 -0.3454858 19.2 6 123
#> Merc 280C 0 1 0 -0.3800638 -0.3454858 17.8 6 123
#> Merc 450SE 0 0 1 -0.6123539 0.4858679 16.4 8 180
#> [ reached 'max' / getOption("max.print") -- omitted 20 rows ]
It is recommended that you also have the base-id’s of the data in
their own column, which you can also request to have in the qdec by
using the id-column’s name in the keep
argument.
cars$type <- row.names(cars)
make_fs_qdec(cars,
mpg ~ -1 + cyl + hp + gear,
keep = "type")
#> cyl4 cyl6 cyl8 gear4 gear5 mpgz hpz
#> Mazda RX4 0 1 0 1 0 0.1508848 -0.5350928
#> Mazda RX4 Wag 0 1 0 1 0 0.1508848 -0.5350928
#> Datsun 710 1 0 0 1 0 0.4495434 -0.7830405
#> Hornet 4 Drive 0 1 0 0 0 0.2172534 -0.5350928
#> Hornet Sportabout 0 0 1 0 0 -0.2307345 0.4129422
#> Valiant 0 1 0 0 0 -0.3302874 -0.6080186
#> Duster 360 0 0 1 0 0 -0.9607889 1.4339030
#> Merc 240D 1 0 0 1 0 0.7150178 -1.2351802
#> Merc 230 1 0 0 1 0 0.4495434 -0.7538702
#> Merc 280 0 1 0 1 0 -0.1477738 -0.3454858
#> Merc 280C 0 1 0 1 0 -0.3800638 -0.3454858
#> Merc 450SE 0 0 1 0 0 -0.6123539 0.4858679
#> type
#> Mazda RX4 Mazda RX4
#> Mazda RX4 Wag Mazda RX4 Wag
#> Datsun 710 Datsun 710
#> Hornet 4 Drive Hornet 4 Drive
#> Hornet Sportabout Hornet Sportabout
#> Valiant Valiant
#> Duster 360 Duster 360
#> Merc 240D Merc 240D
#> Merc 230 Merc 230
#> Merc 280 Merc 280
#> Merc 280C Merc 280C
#> Merc 450SE Merc 450SE
#> [ reached 'max' / getOption("max.print") -- omitted 20 rows ]
This can also be a character vector, and let’s you specify which columns to keep.
cars$type <- row.names(cars)
make_fs_qdec(
cars,
mpg ~ -1 + cyl + hp + gear,
keep = c("type", "mpg", "cyl", "hp")
)
#> cyl4 cyl6 cyl8 gear4 gear5 mpgz hpz
#> Mazda RX4 0 1 0 1 0 0.1508848 -0.5350928
#> Mazda RX4 Wag 0 1 0 1 0 0.1508848 -0.5350928
#> Datsun 710 1 0 0 1 0 0.4495434 -0.7830405
#> Hornet 4 Drive 0 1 0 0 0 0.2172534 -0.5350928
#> Hornet Sportabout 0 0 1 0 0 -0.2307345 0.4129422
#> Valiant 0 1 0 0 0 -0.3302874 -0.6080186
#> Duster 360 0 0 1 0 0 -0.9607889 1.4339030
#> Merc 240D 1 0 0 1 0 0.7150178 -1.2351802
#> Merc 230 1 0 0 1 0 0.4495434 -0.7538702
#> type mpg cyl hp
#> Mazda RX4 Mazda RX4 21.0 6 110
#> Mazda RX4 Wag Mazda RX4 Wag 21.0 6 110
#> Datsun 710 Datsun 710 22.8 4 93
#> Hornet 4 Drive Hornet 4 Drive 21.4 6 110
#> Hornet Sportabout Hornet Sportabout 18.7 8 175
#> Valiant Valiant 18.1 6 105
#> Duster 360 Duster 360 14.3 8 245
#> Merc 240D Merc 240D 24.4 4 62
#> Merc 230 Merc 230 22.8 4 95
#> [ reached 'max' / getOption("max.print") -- omitted 23 rows ]
# Keeping all columns
make_fs_qdec(
cars,
mpg ~ -1 + cyl + hp,
keep = names(cars))
#> cyl4 cyl6 cyl8 mpgz hpz mpg cyl disp hp drat
#> Mazda RX4 0 1 0 0.1508848 -0.5350928 21.0 6 160 110 3.90
#> Mazda RX4 Wag 0 1 0 0.1508848 -0.5350928 21.0 6 160 110 3.90
#> Datsun 710 1 0 0 0.4495434 -0.7830405 22.8 4 108 93 3.85
#> Hornet 4 Drive 0 1 0 0.2172534 -0.5350928 21.4 6 258 110 3.08
#> Hornet Sportabout 0 0 1 -0.2307345 0.4129422 18.7 8 360 175 3.15
#> wt qsec vs am gear carb type
#> Mazda RX4 2.620 16.46 0 1 4 4 Mazda RX4
#> Mazda RX4 Wag 2.875 17.02 0 1 4 4 Mazda RX4 Wag
#> Datsun 710 2.320 18.61 1 1 4 1 Datsun 710
#> Hornet 4 Drive 3.215 19.44 1 0 3 1 Hornet 4 Drive
#> Hornet Sportabout 3.440 17.02 0 0 3 2 Hornet Sportabout
#> [ reached 'max' / getOption("max.print") -- omitted 27 rows ]