
R version 2.15.0 (2012-03-30)
Copyright (C) 2012 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-pc-mingw32/x64 (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.

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.

> 
> ###################################################
> ### code chunk: Ch05init
> ###################################################
> options(digits=5, show.signif.stars = FALSE)
> date()
[1] "Wed Mar 06 17:39:41 2013"
> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-pc-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=C                          
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     
> 
> 
> ###################################################
> ### code chunk: R5.1
> ###################################################
> y ~ x1                       # Univariate linear regression 
y ~ x1
> formula(y ~ x1)              # ... equivalent specification
y ~ x1
> y ~ 1 + x1                   # Explicit indication for intercept  
y ~ 1 + x1
> y ~ 0 + x1                   # No intercept using term 0
y ~ 0 + x1
> y ~ f1 + x1                  # ANCOVA with main effects only 
y ~ f1 + x1
> y ~ f1 + x1 + f1:x1          # Main effects and ...
y ~ f1 + x1 + f1:x1
>                              # ... factor by numeric interaction   
> y ~ f1 + f2 + f1:f2          # Main effects and ...
y ~ f1 + f2 + f1:f2
>                              # ... f1 by f2 two way interaction 
> y ~ f1 + f1:f3               # f3 nested within f1  
y ~ f1 + f1:f3
> y ~ x1 + f1 + f2 +           # Main effects and ... 
+        x1:f1+ x1:f2 + f1:f2  # ... two-way interactions 
y ~ x1 + f1 + f2 + x1:f1 + x1:f2 + f1:f2
> 
> 
> ###################################################
> ### code chunk: R5.2
> ###################################################
> y ~ f1*f2               # ANOVA with two-way interaction
y ~ f1 * f2
> y ~ f1 + f3 %in% f1     # f3 nested within f1 
y ~ f1 + f3 %in% f1
> y ~ f1/f3               # ... equivalent specification
y ~ f1/f3
> y ~ (x1+f1+f2)^2        # Up to 2nd order interactions
y ~ (x1 + f1 + f2)^2
> y ~ x1 - 1              # Intercept omitted 
y ~ x1 - 1
> 
> 
> ###################################################
> ### code chunk: R5.3a
> ###################################################
> y ~ sqrt(x1) + x2             # Square root transformation of x1
y ~ sqrt(x1) + x2
> y ~ ordered(x1, breaks)+      # Ordered factor created and ...
+             poly(x1,2)        # ...second degree polynomial added
y ~ ordered(x1, breaks) + poly(x1, 2)
> y  ~ poly(x1,x2,2)            # Bivariate quadratic surface ...
y ~ poly(x1, x2, 2)
>                               # ... for x1 and x2
> log(y) ~ bs(x1, df=3)         # log transform for y modeled ... 
log(y) ~ bs(x1, df = 3)
>                               # ... by using B-spline for x1
> y ~ f1*bs(x1, df=3) -1        # Factor by spline interaction ... 
y ~ f1 * bs(x1, df = 3) - 1
>                               # ... with intercept omitted
> 
> ###################################################
> ### code chunk: R5.3b
> ###################################################
> form2   <- y ~ I(x1 + 100/x2) # I() function 
> update(form2, . ~ . + x3)     # x3 predictor added to form2	
y ~ I(x1 + 100/x2) + x3
> update(form2, . ~ . -1)       # Intercept omitted from form2
y ~ I(x1 + 100/x2) - 1
> 
> 
> ###################################################
> ### code chunk: R5.4a
> ###################################################
> formA <- y ~ f1*f2            # Formula A                   
> termsA <- terms(formA)        # Object of class terms
> names(attributes(termsA))     # Names of attributes
[1] "variables"    "factors"      "term.labels"  "order"        "intercept"   
[6] "response"     "class"        ".Environment"
> labels(termsA)                # Terms; interaction after main effect
[1] "f1"    "f2"    "f1:f2"
> attr(termsA,"order")          # Interaction order for each term
[1] 1 1 2
> attr(termsA,"intercept")      # Intercept present?
[1] 1
> attr(termsA,"variables")      # Variable names
list(y, f1, f2)
> 
> 
> ###################################################
> ### code chunk: R5.4b
> ###################################################
> formB <- update(formA, . ~ . - f1:f2 -1)          # Formula B
> termsB <- terms(formB)
> labels(termsB)                # Terms of formula B
[1] "f1" "f2"
> attr(termsB,"intercept")      # Intercept omitted
[1] 0
> 
> 
> ###################################################
> ### code chunk: R5.5a
> ###################################################
> data(armd.wide, package = "nlmeU")
> form1 <- formula(               # *** Formula  ***
+   visual52 ~                    # Dependent variable 
+   sqrt(line0) +                 # Continuous explanatory variable
+   factor(lesion) +              # Factor with 4 levels
+   treat.f*log(visual24) +       # Crossing of two variables
+   poly(visual0,2))              # Polynomial of 2nd degree
> 
> ###################################################
> ### code chunk: R5.5b
> ###################################################
> armd.mf1 <-                     # *** Model frame ***
+    model.frame(form1,           # Formula
+       data = armd.wide,         # Data frame
+       subset = !(subject %in% c(1,2)), # Exclude two subjects   
+       na.action = na.exclude,   # Dealing with missing data
+       SubjectId = subject)      # Identifier of data records
> class(armd.mf1)           
[1] "data.frame"
> dim(armd.wide)                  # Data frame dimensions
[1] 240  10
> dim(armd.mf1)                   # Model frame dimensions
[1] 189   7
> names(armd.mf1)                 # Components of the model frame
[1] "visual52"         "sqrt(line0)"      "factor(lesion)"   "treat.f"         
[5] "log(visual24)"    "poly(visual0, 2)" "(SubjectId)"     
> head(armd.mf1, n = 4)           # First four records
  visual52 sqrt(line0) factor(lesion) treat.f log(visual24) poly(visual0, 2).1
4       68      3.6056              2 Placebo        4.1589          0.0523462
6       42      3.4641              3  Active        3.9703          0.0175815
7       65      3.6056              1 Placebo        4.2767          0.0393095
8       37      2.8284              3 Placebo        3.6109         -0.0693302
  poly(visual0, 2).2 (SubjectId)
4         -0.0054435           4
6         -0.0460843           6
7         -0.0243944           7
8         -0.0091566           8
> 
> 
> ###################################################
> ### code chunk: R5.6
> ###################################################
> terms.mf1 <- attr(armd.mf1,"terms")       # Terms attribute
> class(terms.mf1)
[1] "terms"   "formula"
> names(attributes(terms.mf1))              # Names of attributes
 [1] "variables"    "factors"      "term.labels"  "order"        "intercept"   
 [6] "response"     "class"        ".Environment" "predvars"     "dataClasses" 
> attr(terms.mf1,"dataClasses")             # dataClasses attribute
        visual52      sqrt(line0)   factor(lesion)          treat.f 
       "numeric"        "numeric"         "factor"         "factor" 
   log(visual24) poly(visual0, 2)      (SubjectId) 
       "numeric"      "nmatrix.2"         "factor" 
> attr(terms.mf1,"predvars")                # predvars attribute
list(visual52, sqrt(line0), factor(lesion), treat.f, log(visual24), 
    poly(visual0, 2, coefs = list(alpha = c(54.9541666666667, 
    50.5097520799239), norm2 = c(1, 240, 52954.4958333333, 16341393.4347853
    ))))
> labels(terms.mf1)                         # Component names
[1] "sqrt(line0)"           "factor(lesion)"        "treat.f"              
[4] "log(visual24)"         "poly(visual0, 2)"      "treat.f:log(visual24)"
> 
> 
> ###################################################
> ### code chunk: R5.7
> ###################################################
> Xmtx <-  model.matrix(form1, armd.mf1)         # Design matrix
> dim(Xmtx)                                      # No rows and cols
[1] 189  10
> (nms <- colnames(Xmtx))                        # Col names
 [1] "(Intercept)"                 "sqrt(line0)"                
 [3] "factor(lesion)2"             "factor(lesion)3"            
 [5] "factor(lesion)4"             "treat.fActive"              
 [7] "log(visual24)"               "poly(visual0, 2)1"          
 [9] "poly(visual0, 2)2"           "treat.fActive:log(visual24)"
> colnames(Xmtx) <- abbreviate(nms)
> print(head(Xmtx, n = 6), digits = 4)           # First 6 rows
   (In)  s(0) f()2 f()3 f()4 tr.A  l(24  p(0,2)1   p(0,2)2  t.A:
4     1 3.606    1    0    0    0 4.159  0.05235 -0.005443 0.000
6     1 3.464    0    1    0    1 3.970  0.01758 -0.046084 3.970
7     1 3.606    0    0    0    0 4.277  0.03931 -0.024394 0.000
8     1 2.828    0    1    0    0 3.611 -0.06933 -0.009157 0.000
9     1 3.464    1    0    0    1 3.989  0.01758 -0.046084 3.989
12    1 3.000    0    0    0    1 3.296 -0.03891 -0.044592 3.296
> names(attributes(Xmtx))                        # Attribute names
[1] "dim"       "dimnames"  "assign"    "contrasts"
> attr(Xmtx,"assign")                            # Cols to terms map
 [1] 0 1 2 2 2 3 4 5 5 6
> attr(Xmtx,"contrasts")                         # Contrasts attribute
$`factor(lesion)`
[1] "contr.treatment"

$treat.f
[1] "contr.treatment"

> 
> 
> ###################################################
> ### code chunk: R5.8
> ###################################################
> contr.treatment(3)           # Default base level = 1 
  2 3
1 0 0
2 1 0
3 0 1
> contr.treatment(3, base = 3) # base level = 3. Same as contr.SAS(3)
  1 2
1 1 0
2 0 1
3 0 0
> contr.sum(3)                 # Sum to zero
  [,1] [,2]
1    1    0
2    0    1
3   -1   -1
> contr.helmert(3)             # Helmert contrasts 
  [,1] [,2]
1   -1   -1
2    1   -1
3    0    2
> contr.poly(3, scores=c(1,5,7)) # Polynomial contrasts
           .L       .Q
[1,] -0.77152  0.26726
[2,]  0.15430 -0.80178
[3,]  0.61721  0.53452
> 
> 
> ###################################################
> ### code chunk: R5.9a
> ###################################################
> options()$contrasts                    # Default contrasts
        unordered           ordered 
"contr.treatment"      "contr.poly" 
> lesion.f <- factor(armd.wide$lesion)   # Factor created
> str(lesion.f)                          # Structure
 Factor w/ 4 levels "1","2","3","4": 3 1 4 2 1 3 1 3 2 1 ...
> names(attributes(lesion.f))            # Names of factor attributes
[1] "levels" "class" 
> levels(lesion.f)                       # Levels extracted
[1] "1" "2" "3" "4"
> contrasts(lesion.f)                    # Contrasts extracted
  2 3 4
1 0 0 0
2 1 0 0
3 0 1 0
4 0 0 1
> 
> ###################################################
> ### code chunk: R5.9b
> ###################################################
> lesion2.f <- C(lesion.f, contr.sum(4)) # New contrasts using C()
> names(attributes(lesion2.f))           # Names of factor attributes
[1] "levels"    "class"     "contrasts"
> contrasts(lesion2.f)                   # Contrasts extracted
  [,1] [,2] [,3]
1    1    0    0
2    0    1    0
3    0    0    1
4   -1   -1   -1
> 
> ###################################################
> ### code chunk: R5.9c
> ###################################################
> 
> lesion2a.f <- lesion.f                # lesion2a.f created with the use of... 
> contrasts(lesion2a.f) <- contr.sum(4) # ... "contrasts <- " syntax
> 
> 
> proc.time()
   user  system elapsed 
   0.31    0.01    0.31 
