R version 2.14.1 (2011-12-22) Copyright (C) 2011 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > str(Formaldehyde) 'data.frame': 6 obs. of 2 variables: $ carb : num 0.1 0.3 0.5 0.6 0.7 0.9 $ optden: num 0.086 0.269 0.446 0.538 0.626 0.782 > > str(mf <- model.frame(optden ~ 1 + carb, Formaldehyde)) 'data.frame': 6 obs. of 2 variables: $ optden: num 0.086 0.269 0.446 0.538 0.626 0.782 $ carb : num 0.1 0.3 0.5 0.6 0.7 0.9 - attr(*, "terms")=Classes 'terms', 'formula' length 3 optden ~ 1 + carb .. ..- attr(*, "variables")= language list(optden, carb) .. ..- attr(*, "factors")= int [1:2, 1] 0 1 .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : chr [1:2] "optden" "carb" .. .. .. ..$ : chr "carb" .. ..- attr(*, "term.labels")= chr "carb" .. ..- attr(*, "order")= int 1 .. ..- attr(*, "intercept")= int 1 .. ..- attr(*, "response")= int 1 .. ..- attr(*, ".Environment")= .. ..- attr(*, "predvars")= language list(optden, carb) .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric" .. .. ..- attr(*, "names")= chr [1:2] "optden" "carb" > str(form <- optden ~ 1 + carb) Class 'formula' length 3 optden ~ 1 + carb ..- attr(*, ".Environment")= > form optden ~ 1 + carb > str(mm <- model.matrix(form, mf)) num [1:6, 1:2] 1 1 1 1 1 1 0.1 0.3 0.5 0.6 ... - attr(*, "dimnames")=List of 2 ..$ : chr [1:6] "1" "2" "3" "4" ... ..$ : chr [1:2] "(Intercept)" "carb" - attr(*, "assign")= int [1:2] 0 1 > str(model.response(mf)) Named num [1:6] 0.086 0.269 0.446 0.538 0.626 0.782 - attr(*, "names")= chr [1:6] "1" "2" "3" "4" ... > > str(InsectSprays) 'data.frame': 72 obs. of 2 variables: $ count: num 10 7 20 14 14 12 10 23 17 20 ... $ spray: Factor w/ 6 levels "A","B","C","D",..: 1 1 1 1 1 1 1 1 1 1 ... > str(mf <- model.frame(count ~ spray, InsectSprays)) 'data.frame': 72 obs. of 2 variables: $ count: num 10 7 20 14 14 12 10 23 17 20 ... $ spray: Factor w/ 6 levels "A","B","C","D",..: 1 1 1 1 1 1 1 1 1 1 ... - attr(*, "terms")=Classes 'terms', 'formula' length 3 count ~ spray .. ..- attr(*, "variables")= language list(count, spray) .. ..- attr(*, "factors")= int [1:2, 1] 0 1 .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : chr [1:2] "count" "spray" .. .. .. ..$ : chr "spray" .. ..- attr(*, "term.labels")= chr "spray" .. ..- attr(*, "order")= int 1 .. ..- attr(*, "intercept")= int 1 .. ..- attr(*, "response")= int 1 .. ..- attr(*, ".Environment")= .. ..- attr(*, "predvars")= language list(count, spray) .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "factor" .. .. ..- attr(*, "names")= chr [1:2] "count" "spray" > str(mm <- model.matrix(terms(mf), mf)) # alternative specification num [1:72, 1:6] 1 1 1 1 1 1 1 1 1 1 ... - attr(*, "dimnames")=List of 2 ..$ : chr [1:72] "1" "2" "3" "4" ... ..$ : chr [1:6] "(Intercept)" "sprayB" "sprayC" "sprayD" ... - attr(*, "assign")= int [1:6] 0 1 1 1 1 1 - attr(*, "contrasts")=List of 1 ..$ spray: chr "contr.treatment" > head(mm) (Intercept) sprayB sprayC sprayD sprayE sprayF 1 1 0 0 0 0 0 2 1 0 0 0 0 0 3 1 0 0 0 0 0 4 1 0 0 0 0 0 5 1 0 0 0 0 0 6 1 0 0 0 0 0 > tail(mm) (Intercept) sprayB sprayC sprayD sprayE sprayF 67 1 0 0 0 0 1 68 1 0 0 0 0 1 69 1 0 0 0 0 1 70 1 0 0 0 0 1 71 1 0 0 0 0 1 72 1 0 0 0 0 1 > str(mm <- model.matrix(~ 0 + spray, mf)) # suppresses the intercept term num [1:72, 1:6] 1 1 1 1 1 1 1 1 1 1 ... - attr(*, "dimnames")=List of 2 ..$ : chr [1:72] "1" "2" "3" "4" ... ..$ : chr [1:6] "sprayA" "sprayB" "sprayC" "sprayD" ... - attr(*, "assign")= int [1:6] 1 1 1 1 1 1 - attr(*, "contrasts")=List of 1 ..$ spray: chr "contr.treatment" > head(mm) sprayA sprayB sprayC sprayD sprayE sprayF 1 1 0 0 0 0 0 2 1 0 0 0 0 0 3 1 0 0 0 0 0 4 1 0 0 0 0 0 5 1 0 0 0 0 0 6 1 0 0 0 0 0 > tail(mm) sprayA sprayB sprayC sprayD sprayE sprayF 67 0 0 0 0 0 1 68 0 0 0 0 0 1 69 0 0 0 0 0 1 70 0 0 0 0 0 1 71 0 0 0 0 0 1 72 0 0 0 0 0 1 > > proc.time() user system elapsed 0.420 0.364 0.292