Model Matrix

Students cheer on the Redhawks during a sporting event at Miami University.


The model matrix function takes an object like a linear model and creates a design matrix out of it. The design matrix contains dummy variables for different variables. An example of this is shown below:

cars <- mtcars
carslm <- lm(mpg ~ disp, cars)
model.matrix(carslm)
     
                         (Intercept)  disp
Mazda RX4                     1       160.0
Mazda RX4 Wag                 1       160.0
Datsun 710                    1       108.0
Hornet 4 Drive                1       258.0
Hornet Sportabout             1       360.0
Valiant                       1       225.0
Duster 360                    1       360.0
Merc 240D                     1       146.7
Merc 230                      1       140.8
Merc 280                      1       167.6
Merc 280C                     1       167.6
Merc 450SE                    1       275.8
Merc 450SL                    1       275.8
Merc 450SLC                   1       275.8
Cadillac Fleetwood            1       472.0
Lincoln Continental           1       460.0
Chrysler Imperial             1       440.0
Fiat 128                      1         78.7
Honda Civic                   1         75.7
Toyota Corolla                1         71.1
Toyota Corona                 1       120.1
Dodge Challenger              1       318.0
AMC Javelin                   1       304.0
Camaro Z28                    1       350.0
Pontiac Firebird              1       400.0
Fiat X1-9                     1       79.0
Porsche 914-2                 1       120.3
Lotus Europa                  1        95.1
Ford Pantera L                1      351.0
Ferrari Dino                  1      145.0
Maserati Bora                 1      301.0
Volvo 142E                    1      121.0
attr(,"assign")
[1] 0 1