build.x: build.x

Description Usage Arguments Details Value Author(s) Examples

View source: R/buildMatrix.r

Description

Build the x matrix for a glmnet model

Usage

1

Arguments

formula

A formula

data

A data.frame

contrasts

Logical indicating whether a factor's base level is removed. Can be either one single value applied to every factor or a value for each factor. Values will be recycled if necessary.

sparse

Logical indicating if result should be sparse.

Details

Given a formula and a data.frame build the predictor matrix

Value

A matrix of the predictor variables specified in the formula

Author(s)

Jared P. Lander

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
require(ggplot2)
head(mpg)
head(build.x(hwy ~ class + cyl + year, data=mpg))

testFrame <- data.frame(First=sample(1:10, 20, replace=TRUE), 
Second=sample(1:20, 20, replace=TRUE), 
Third=sample(1:10, 20, replace=TRUE), 
Fourth=factor(rep(c("Alice","Bob","Charlie","David"), 5)), 
Fifth=ordered(rep(c("Edward","Frank","Georgia","Hank","Isaac"), 4)), 
Sixth=factor(rep(c("a", "b"), 10)), stringsAsFactors=F)
head(build.x(First ~ Second + Fourth + Sixth, testFrame, 
contrasts=c("Fourth"=TRUE, "Fifth"=FALSE, "Sixth"=TRUE)))
head(build.x(First ~ Second + Fourth + Fifth + Sixth, testFrame, 
contrasts=c(Fourth=TRUE, Fifth=FALSE, Sixth=TRUE)))
head(build.x(First ~ Second + Fourth + Fifth + Sixth, testFrame, contrasts=TRUE))
head(build.x(First ~ Second + Fourth + Fifth + Sixth, testFrame, 
contrasts=FALSE))
head(build.x(First ~ Second + Fourth + Fifth + Sixth - 1, testFrame, 
contrasts=TRUE))
build.x(First ~ Second + Fourth + Fifth + Sixth - 1, testFrame, 
contrasts=TRUE, sparse=TRUE)
head(build.x(First ~ Second + Fourth + Fifth + Fourth*Sixth, testFrame, contrasts=TRUE))
head(build.x(First ~ Second + Fourth + Fifth + Third*Sixth, testFrame, contrasts=TRUE))
#' head(build.x(First ~ Second + Fourth + Fifth + Fourth*Sixth, testFrame, contrasts=FALSE))
head(build.x(First ~ Second + Fourth + Fifth + Third*Sixth, testFrame, contrasts=FALSE))
build.x(First ~ Second + Fourth + Fifth + Third*Sixth, testFrame, contrasts=FALSE, sparse=TRUE)

## if contrasts is a list then you can specify just certain factors

Example output

Loading required package: ggplot2
# A tibble: 6 x 11
  manufacturer model displ  year   cyl      trans   drv   cty   hwy    fl
         <chr> <chr> <dbl> <int> <int>      <chr> <chr> <int> <int> <chr>
1         audi    a4   1.8  1999     4   auto(l5)     f    18    29     p
2         audi    a4   1.8  1999     4 manual(m5)     f    21    29     p
3         audi    a4   2.0  2008     4 manual(m6)     f    20    31     p
4         audi    a4   2.0  2008     4   auto(av)     f    21    30     p
5         audi    a4   2.8  1999     6   auto(l5)     f    16    26     p
6         audi    a4   2.8  1999     6 manual(m5)     f    18    26     p
# ... with 1 more variables: class <chr>
  (Intercept) classcompact classmidsize classminivan classpickup
1           1            1            0            0           0
2           1            1            0            0           0
3           1            1            0            0           0
4           1            1            0            0           0
5           1            1            0            0           0
6           1            1            0            0           0
  classsubcompact classsuv cyl year
1               0        0   4 1999
2               0        0   4 1999
3               0        0   4 2008
4               0        0   4 2008
5               0        0   6 1999
6               0        0   6 1999
  (Intercept) Second FourthBob FourthCharlie FourthDavid Sixthb
1           1      4         0             0           0      0
2           1     19         1             0           0      1
3           1      8         0             1           0      0
4           1      4         0             0           1      1
5           1      2         0             0           0      0
6           1      4         1             0           0      1
Warning message:
In model.matrix.default(formula, data = data, contrasts.arg = contrArgs) :
  variable 'Fifth' is absent, its contrast will be ignored
  (Intercept) Second FourthBob FourthCharlie FourthDavid FifthEdward FifthFrank
1           1      4         0             0           0           1          0
2           1     19         1             0           0           0          1
3           1      8         0             1           0           0          0
4           1      4         0             0           1           0          0
5           1      2         0             0           0           0          0
6           1      4         1             0           0           1          0
  FifthGeorgia FifthHank FifthIsaac Sixthb
1            0         0          0      0
2            0         0          0      1
3            1         0          0      0
4            0         1          0      1
5            0         0          1      0
6            0         0          0      1
  (Intercept) Second FourthBob FourthCharlie FourthDavid       Fifth.L
1           1      4         0             0           0 -6.324555e-01
2           1     19         1             0           0 -3.162278e-01
3           1      8         0             1           0 -3.510833e-17
4           1      4         0             0           1  3.162278e-01
5           1      2         0             0           0  6.324555e-01
6           1      4         1             0           0 -6.324555e-01
     Fifth.Q       Fifth.C    Fifth^4 Sixthb
1  0.5345225 -3.162278e-01  0.1195229      0
2 -0.2672612  6.324555e-01 -0.4780914      1
3 -0.5345225  1.755417e-16  0.7171372      0
4 -0.2672612 -6.324555e-01 -0.4780914      1
5  0.5345225  3.162278e-01  0.1195229      0
6  0.5345225 -3.162278e-01  0.1195229      1
  (Intercept) Second FourthAlice FourthBob FourthCharlie FourthDavid
1           1      4           1         0             0           0
2           1     19           0         1             0           0
3           1      8           0         0             1           0
4           1      4           0         0             0           1
5           1      2           1         0             0           0
6           1      4           0         1             0           0
  FifthEdward FifthFrank FifthGeorgia FifthHank FifthIsaac Sixtha Sixthb
1           1          0            0         0          0      1      0
2           0          1            0         0          0      0      1
3           0          0            1         0          0      1      0
4           0          0            0         1          0      0      1
5           0          0            0         0          1      1      0
6           1          0            0         0          0      0      1
  Second FourthAlice FourthBob FourthCharlie FourthDavid       Fifth.L
1      4           1         0             0           0 -6.324555e-01
2     19           0         1             0           0 -3.162278e-01
3      8           0         0             1           0 -3.510833e-17
4      4           0         0             0           1  3.162278e-01
5      2           1         0             0           0  6.324555e-01
6      4           0         1             0           0 -6.324555e-01
     Fifth.Q       Fifth.C    Fifth^4 Sixthb
1  0.5345225 -3.162278e-01  0.1195229      0
2 -0.2672612  6.324555e-01 -0.4780914      1
3 -0.5345225  1.755417e-16  0.7171372      0
4 -0.2672612 -6.324555e-01 -0.4780914      1
5  0.5345225  3.162278e-01  0.1195229      0
6  0.5345225 -3.162278e-01  0.1195229      1
20 x 10 sparse Matrix of class "dgCMatrix"
   [[ suppressing 10 column names 'Second', 'FourthAlice', 'FourthBob' ... ]]
                                                                 
1   4 1 . . . -6.324555e-01  0.5345225 -3.162278e-01  0.1195229 .
2  19 . 1 . . -3.162278e-01 -0.2672612  6.324555e-01 -0.4780914 1
3   8 . . 1 . -3.510833e-17 -0.5345225  1.755417e-16  0.7171372 .
4   4 . . . 1  3.162278e-01 -0.2672612 -6.324555e-01 -0.4780914 1
5   2 1 . . .  6.324555e-01  0.5345225  3.162278e-01  0.1195229 .
6   4 . 1 . . -6.324555e-01  0.5345225 -3.162278e-01  0.1195229 1
7  19 . . 1 . -3.162278e-01 -0.2672612  6.324555e-01 -0.4780914 .
8  18 . . . 1 -3.510833e-17 -0.5345225  1.755417e-16  0.7171372 1
9  14 1 . . .  3.162278e-01 -0.2672612 -6.324555e-01 -0.4780914 .
10  4 . 1 . .  6.324555e-01  0.5345225  3.162278e-01  0.1195229 1
11 13 . . 1 . -6.324555e-01  0.5345225 -3.162278e-01  0.1195229 .
12 16 . . . 1 -3.162278e-01 -0.2672612  6.324555e-01 -0.4780914 1
13  6 1 . . . -3.510833e-17 -0.5345225  1.755417e-16  0.7171372 .
14  5 . 1 . .  3.162278e-01 -0.2672612 -6.324555e-01 -0.4780914 1
15  3 . . 1 .  6.324555e-01  0.5345225  3.162278e-01  0.1195229 .
16 17 . . . 1 -6.324555e-01  0.5345225 -3.162278e-01  0.1195229 1
17  2 1 . . . -3.162278e-01 -0.2672612  6.324555e-01 -0.4780914 .
18 15 . 1 . . -3.510833e-17 -0.5345225  1.755417e-16  0.7171372 1
19 16 . . 1 .  3.162278e-01 -0.2672612 -6.324555e-01 -0.4780914 .
20 12 . . . 1  6.324555e-01  0.5345225  3.162278e-01  0.1195229 1
  (Intercept) Second FourthBob FourthCharlie FourthDavid       Fifth.L
1           1      4         0             0           0 -6.324555e-01
2           1     19         1             0           0 -3.162278e-01
3           1      8         0             1           0 -3.510833e-17
4           1      4         0             0           1  3.162278e-01
5           1      2         0             0           0  6.324555e-01
6           1      4         1             0           0 -6.324555e-01
     Fifth.Q       Fifth.C    Fifth^4 Sixthb FourthBob:Sixthb
1  0.5345225 -3.162278e-01  0.1195229      0                0
2 -0.2672612  6.324555e-01 -0.4780914      1                1
3 -0.5345225  1.755417e-16  0.7171372      0                0
4 -0.2672612 -6.324555e-01 -0.4780914      1                0
5  0.5345225  3.162278e-01  0.1195229      0                0
6  0.5345225 -3.162278e-01  0.1195229      1                1
  FourthCharlie:Sixthb FourthDavid:Sixthb
1                    0                  0
2                    0                  0
3                    0                  0
4                    0                  1
5                    0                  0
6                    0                  0
  (Intercept) Second FourthBob FourthCharlie FourthDavid       Fifth.L
1           1      4         0             0           0 -6.324555e-01
2           1     19         1             0           0 -3.162278e-01
3           1      8         0             1           0 -3.510833e-17
4           1      4         0             0           1  3.162278e-01
5           1      2         0             0           0  6.324555e-01
6           1      4         1             0           0 -6.324555e-01
     Fifth.Q       Fifth.C    Fifth^4 Third Sixthb Third:Sixthb
1  0.5345225 -3.162278e-01  0.1195229     9      0            0
2 -0.2672612  6.324555e-01 -0.4780914     1      1            1
3 -0.5345225  1.755417e-16  0.7171372    10      0            0
4 -0.2672612 -6.324555e-01 -0.4780914     3      1            3
5  0.5345225  3.162278e-01  0.1195229     9      0            0
6  0.5345225 -3.162278e-01  0.1195229     2      1            2
  (Intercept) Second FourthAlice FourthBob FourthCharlie FourthDavid
1           1      4           1         0             0           0
2           1     19           0         1             0           0
3           1      8           0         0             1           0
4           1      4           0         0             0           1
5           1      2           1         0             0           0
6           1      4           0         1             0           0
  FifthEdward FifthFrank FifthGeorgia FifthHank FifthIsaac Third Sixtha Sixthb
1           1          0            0         0          0     9      1      0
2           0          1            0         0          0     1      0      1
3           0          0            1         0          0    10      1      0
4           0          0            0         1          0     3      0      1
5           0          0            0         0          1     9      1      0
6           1          0            0         0          0     2      0      1
  Third:Sixtha Third:Sixthb
1            9            0
2            0            1
3           10            0
4            0            3
5            9            0
6            0            2
20 x 16 sparse Matrix of class "dgCMatrix"
   [[ suppressing 16 column names '(Intercept)', 'Second', 'FourthAlice' ... ]]
                                     
1  1  4 1 . . . 1 . . . .  9 1 .  9 .
2  1 19 . 1 . . . 1 . . .  1 . 1  . 1
3  1  8 . . 1 . . . 1 . . 10 1 . 10 .
4  1  4 . . . 1 . . . 1 .  3 . 1  . 3
5  1  2 1 . . . . . . . 1  9 1 .  9 .
6  1  4 . 1 . . 1 . . . .  2 . 1  . 2
7  1 19 . . 1 . . 1 . . .  7 1 .  7 .
8  1 18 . . . 1 . . 1 . .  3 . 1  . 3
9  1 14 1 . . . . . . 1 .  8 1 .  8 .
10 1  4 . 1 . . . . . . 1  4 . 1  . 4
11 1 13 . . 1 . 1 . . . .  2 1 .  2 .
12 1 16 . . . 1 . 1 . . .  9 . 1  . 9
13 1  6 1 . . . . . 1 . .  9 1 .  9 .
14 1  5 . 1 . . . . . 1 .  4 . 1  . 4
15 1  3 . . 1 . . . . . 1  1 1 .  1 .
16 1 17 . . . 1 1 . . . .  3 . 1  . 3
17 1  2 1 . . . . 1 . . .  7 1 .  7 .
18 1 15 . 1 . . . . 1 . .  3 . 1  . 3
19 1 16 . . 1 . . . . 1 .  2 1 .  2 .
20 1 12 . . . 1 . . . . 1  2 . 1  . 2

useful documentation built on June 10, 2018, 1:04 a.m.