diff --git a/R/contrs.R b/R/contrs.R index 4de5a3582..bea9999a3 100644 --- a/R/contrs.R +++ b/R/contrs.R @@ -34,52 +34,52 @@ #' #' @examplesIf !identical(Sys.getenv("IN_PKGDOWN"), "true") #' \donttest{ -#' data("mtcars") +#' data("mtcars") #' -#' mtcars <- data_modify(mtcars, cyl = factor(cyl)) +#' mtcars <- data_modify(mtcars, cyl = factor(cyl)) #' -#' c.treatment <- cbind(Intercept = 1, contrasts(mtcars$cyl)) -#' solve(c.treatment) -#' #> 4 6 8 -#' #> Intercept 1 0 0 # mean of the 1st level -#' #> 6 -1 1 0 # 2nd level - 1st level -#' #> 8 -1 0 1 # 3rd level - 1st level +#' c.treatment <- cbind(Intercept = 1, contrasts(mtcars$cyl)) +#' solve(c.treatment) +#' #> 4 6 8 +#' #> Intercept 1 0 0 # mean of the 1st level +#' #> 6 -1 1 0 # 2nd level - 1st level +#' #> 8 -1 0 1 # 3rd level - 1st level #' -#' contrasts(mtcars$cyl) <- contr.sum -#' c.sum <- cbind(Intercept = 1, contrasts(mtcars$cyl)) -#' solve(c.sum) -#' #> 4 6 8 -#' #> Intercept 0.333 0.333 0.333 # overall mean -#' #> 0.667 -0.333 -0.333 # deviation of 1st from overall mean -#' #> -0.333 0.667 -0.333 # deviation of 2nd from overall mean +#' contrasts(mtcars$cyl) <- contr.sum +#' c.sum <- cbind(Intercept = 1, contrasts(mtcars$cyl)) +#' solve(c.sum) +#' #> 4 6 8 +#' #> Intercept 0.333 0.333 0.333 # overall mean +#' #> 0.667 -0.333 -0.333 # deviation of 1st from overall mean +#' #> -0.333 0.667 -0.333 # deviation of 2nd from overall mean #' #' -#' contrasts(mtcars$cyl) <- contr.deviation -#' c.deviation <- cbind(Intercept = 1, contrasts(mtcars$cyl)) -#' solve(c.deviation) -#' #> 4 6 8 -#' #> Intercept 0.333 0.333 0.333 # overall mean -#' #> 6 -1.000 1.000 0.000 # 2nd level - 1st level -#' #> 8 -1.000 0.000 1.000 # 3rd level - 1st level +#' contrasts(mtcars$cyl) <- contr.deviation +#' c.deviation <- cbind(Intercept = 1, contrasts(mtcars$cyl)) +#' solve(c.deviation) +#' #> 4 6 8 +#' #> Intercept 0.333 0.333 0.333 # overall mean +#' #> 6 -1.000 1.000 0.000 # 2nd level - 1st level +#' #> 8 -1.000 0.000 1.000 # 3rd level - 1st level #' -#' ## With Interactions ----------------------------------------- -#' mtcars <- data_modify(mtcars, am = C(am, contr = contr.deviation)) -#' mtcars <- data_arrange(mtcars, select = c("cyl", "am")) +#' ## With Interactions ----------------------------------------- +#' mtcars <- data_modify(mtcars, am = C(am, contr = contr.deviation)) +#' mtcars <- data_arrange(mtcars, select = c("cyl", "am")) #' -#' mm <- unique(model.matrix(~ cyl * am, data = mtcars)) -#' rownames(mm) <- c( -#' "cyl4.am0", "cyl4.am1", "cyl6.am0", -#' "cyl6.am1", "cyl8.am0", "cyl8.am1" -#' ) +#' mm <- unique(model.matrix(~ cyl * am, data = mtcars)) +#' rownames(mm) <- c( +#' "cyl4.am0", "cyl4.am1", "cyl6.am0", +#' "cyl6.am1", "cyl8.am0", "cyl8.am1" +#' ) #' -#' solve(mm) -#' #> cyl4.am0 cyl4.am1 cyl6.am0 cyl6.am1 cyl8.am0 cyl8.am1 -#' #> (Intercept) 0.167 0.167 0.167 0.167 0.167 0.167 # overall mean -#' #> cyl6 -0.500 -0.500 0.500 0.500 0.000 0.000 # cyl MAIN eff: 2nd - 1st -#' #> cyl8 -0.500 -0.500 0.000 0.000 0.500 0.500 # cyl MAIN eff: 2nd - 1st -#' #> am1 -0.333 0.333 -0.333 0.333 -0.333 0.333 # am MAIN eff -#' #> cyl6:am1 1.000 -1.000 -1.000 1.000 0.000 0.000 -#' #> cyl8:am1 1.000 -1.000 0.000 0.000 -1.000 1.000 +#' solve(mm) +#' #> cyl4.am0 cyl4.am1 cyl6.am0 cyl6.am1 cyl8.am0 cyl8.am1 +#' #> (Intercept) 0.167 0.167 0.167 0.167 0.167 0.167 # overall mean +#' #> cyl6 -0.500 -0.500 0.500 0.500 0.000 0.000 # cyl MAIN eff: 2nd - 1st +#' #> cyl8 -0.500 -0.500 0.000 0.000 0.500 0.500 # cyl MAIN eff: 2nd - 1st +#' #> am1 -0.333 0.333 -0.333 0.333 -0.333 0.333 # am MAIN eff +#' #> cyl6:am1 1.000 -1.000 -1.000 1.000 0.000 0.000 +#' #> cyl8:am1 1.000 -1.000 0.000 0.000 -1.000 1.000 #' } #' #' @export diff --git a/man/contr.deviation.Rd b/man/contr.deviation.Rd index 07ca22814..d9d378429 100644 --- a/man/contr.deviation.Rd +++ b/man/contr.deviation.Rd @@ -51,52 +51,52 @@ the differences the \examples{ \dontshow{if (!identical(Sys.getenv("IN_PKGDOWN"), "true")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \donttest{ - data("mtcars") +data("mtcars") - mtcars <- data_modify(mtcars, cyl = factor(cyl)) +mtcars <- data_modify(mtcars, cyl = factor(cyl)) - c.treatment <- cbind(Intercept = 1, contrasts(mtcars$cyl)) - solve(c.treatment) - #> 4 6 8 - #> Intercept 1 0 0 # mean of the 1st level - #> 6 -1 1 0 # 2nd level - 1st level - #> 8 -1 0 1 # 3rd level - 1st level +c.treatment <- cbind(Intercept = 1, contrasts(mtcars$cyl)) +solve(c.treatment) +#> 4 6 8 +#> Intercept 1 0 0 # mean of the 1st level +#> 6 -1 1 0 # 2nd level - 1st level +#> 8 -1 0 1 # 3rd level - 1st level - contrasts(mtcars$cyl) <- contr.sum - c.sum <- cbind(Intercept = 1, contrasts(mtcars$cyl)) - solve(c.sum) - #> 4 6 8 - #> Intercept 0.333 0.333 0.333 # overall mean - #> 0.667 -0.333 -0.333 # deviation of 1st from overall mean - #> -0.333 0.667 -0.333 # deviation of 2nd from overall mean +contrasts(mtcars$cyl) <- contr.sum +c.sum <- cbind(Intercept = 1, contrasts(mtcars$cyl)) +solve(c.sum) +#> 4 6 8 +#> Intercept 0.333 0.333 0.333 # overall mean +#> 0.667 -0.333 -0.333 # deviation of 1st from overall mean +#> -0.333 0.667 -0.333 # deviation of 2nd from overall mean - contrasts(mtcars$cyl) <- contr.deviation - c.deviation <- cbind(Intercept = 1, contrasts(mtcars$cyl)) - solve(c.deviation) - #> 4 6 8 - #> Intercept 0.333 0.333 0.333 # overall mean - #> 6 -1.000 1.000 0.000 # 2nd level - 1st level - #> 8 -1.000 0.000 1.000 # 3rd level - 1st level +contrasts(mtcars$cyl) <- contr.deviation +c.deviation <- cbind(Intercept = 1, contrasts(mtcars$cyl)) +solve(c.deviation) +#> 4 6 8 +#> Intercept 0.333 0.333 0.333 # overall mean +#> 6 -1.000 1.000 0.000 # 2nd level - 1st level +#> 8 -1.000 0.000 1.000 # 3rd level - 1st level - ## With Interactions ----------------------------------------- - mtcars <- data_modify(mtcars, am = C(am, contr = contr.deviation)) - mtcars <- data_arrange(mtcars, select = c("cyl", "am")) +## With Interactions ----------------------------------------- +mtcars <- data_modify(mtcars, am = C(am, contr = contr.deviation)) +mtcars <- data_arrange(mtcars, select = c("cyl", "am")) - mm <- unique(model.matrix(~ cyl * am, data = mtcars)) - rownames(mm) <- c( - "cyl4.am0", "cyl4.am1", "cyl6.am0", - "cyl6.am1", "cyl8.am0", "cyl8.am1" - ) +mm <- unique(model.matrix(~ cyl * am, data = mtcars)) +rownames(mm) <- c( + "cyl4.am0", "cyl4.am1", "cyl6.am0", + "cyl6.am1", "cyl8.am0", "cyl8.am1" +) - solve(mm) - #> cyl4.am0 cyl4.am1 cyl6.am0 cyl6.am1 cyl8.am0 cyl8.am1 - #> (Intercept) 0.167 0.167 0.167 0.167 0.167 0.167 # overall mean - #> cyl6 -0.500 -0.500 0.500 0.500 0.000 0.000 # cyl MAIN eff: 2nd - 1st - #> cyl8 -0.500 -0.500 0.000 0.000 0.500 0.500 # cyl MAIN eff: 2nd - 1st - #> am1 -0.333 0.333 -0.333 0.333 -0.333 0.333 # am MAIN eff - #> cyl6:am1 1.000 -1.000 -1.000 1.000 0.000 0.000 - #> cyl8:am1 1.000 -1.000 0.000 0.000 -1.000 1.000 +solve(mm) +#> cyl4.am0 cyl4.am1 cyl6.am0 cyl6.am1 cyl8.am0 cyl8.am1 +#> (Intercept) 0.167 0.167 0.167 0.167 0.167 0.167 # overall mean +#> cyl6 -0.500 -0.500 0.500 0.500 0.000 0.000 # cyl MAIN eff: 2nd - 1st +#> cyl8 -0.500 -0.500 0.000 0.000 0.500 0.500 # cyl MAIN eff: 2nd - 1st +#> am1 -0.333 0.333 -0.333 0.333 -0.333 0.333 # am MAIN eff +#> cyl6:am1 1.000 -1.000 -1.000 1.000 0.000 0.000 +#> cyl8:am1 1.000 -1.000 0.000 0.000 -1.000 1.000 } \dontshow{\}) # examplesIf} }