Skip to contents

Create a data frame with just the variables in the formula in a lm object. This is comparable to model.matrix or model.frame except that factors, polynomials, transformations, etc. are not expanded.

Usage

model.data(lmobj, lhs = FALSE)

Arguments

lmobj

An object returned by lm or one of its relatives.

lhs

Boolean indicator of whether or not to include the variable(s) on the left-hand side of the model formula.

Details

This is an easy-to-use substitute for get_all_vars. The formula, data, and subset arguments, if present in lmobj's call, affect the result appropriately.

Value

A data frame containing each of the variables referenced in the model formula.

References

Lenth RV (2009) ``Response-Surface Methods in R, Using rsm'', Journal of Statistical Software, 32(7), 1--17. doi:10.18637/jss.v032.i07

Author

Russell V. Lenth

Examples

library(rsm)
trees.lm <- lm(log(Volume) ~ poly(log(Girth),3), data = trees, subset = 1:20)
model.frame(trees.lm)
#>    log(Volume) poly(log(Girth), 3).1 poly(log(Girth), 3).2
#> 1     2.332144         -0.3446832763          0.4567447866
#> 2     2.332144         -0.3169363383          0.3641302873
#> 3     2.322388         -0.2989710263          0.3081915142
#> 4     2.797281         -0.1609473887         -0.0160161673
#> 5     2.933857         -0.1462024861         -0.0396070722
#> 6     2.980619         -0.1389330694         -0.0504530758
#> 7     2.747271         -0.1245940129         -0.0703279088
#> 8     2.901422         -0.1245940129         -0.0703279088
#> 9     3.117950         -0.1175219586         -0.0793878367
#> 10    2.990720         -0.1105133318         -0.0878826704
#> 11    3.186353         -0.1035670047         -0.0958267381
#> 12    3.044522         -0.0966818795         -0.1032339139
#> 13    3.063391         -0.0966818795         -0.1032339139
#> 14    3.058707         -0.0763831627         -0.1223663929
#> 15    2.949688         -0.0565983932         -0.1371263302
#> 16    3.100092         -0.0000829308         -0.1581467511
#> 17    3.520461         -0.0000829308         -0.1581467511
#> 18    3.310543          0.0237801953         -0.1576184585
#> 19    3.246491          0.0469361608         -0.1517677963
#> 20    3.214868          0.0526195070         -0.1495282372
#>    poly(log(Girth), 3).3
#> 1          -0.3406927197
#> 2          -0.1758692323
#> 3          -0.0884195074
#> 4           0.1775782804
#> 5           0.1731860686
#> 6           0.1693326943
#> 7           0.1587464400
#> 8           0.1587464400
#> 9           0.1521753308
#> 10          0.1448537501
#> 11          0.1368533675
#> 12          0.1282424881
#> 13          0.1282424881
#> 14          0.0993828259
#> 15          0.0671973637
#> 16         -0.0363417204
#> 17         -0.0363417204
#> 18         -0.0803976631
#> 19         -0.1205100002
#> 20         -0.1297411118
model.data(trees.lm)
#>    Girth
#> 1    8.3
#> 2    8.6
#> 3    8.8
#> 4   10.5
#> 5   10.7
#> 6   10.8
#> 7   11.0
#> 8   11.0
#> 9   11.1
#> 10  11.2
#> 11  11.3
#> 12  11.4
#> 13  11.4
#> 14  11.7
#> 15  12.0
#> 16  12.9
#> 17  12.9
#> 18  13.3
#> 19  13.7
#> 20  13.8