Get and Set Contrast Matrix Column Names
contr_colnames.RdSet and view the column names of contrasts associated with a factor.
Details
contr_colnames() returns the current column names of the contrasts for a factor.
contr_colnames()<- sets the column names of the contrasts for a factor.
cntr_pfx()<- prefixes the current column names of the contrasts for a factor with the character or string
provided. This can be useful when factor levels are elided with the factor name as, for instance, in the printed
output of summary.glm.
If contrasts are not set for x, both contr_colnames()<- and cntr_pfx()<- set the contrast attribute
using the default function from options("contrasts") before modifying the column names.
See also
contrast, contrasts and factor.
Other contrast-names:
helm_names()
Examples
(d <- data.frame(
f = gl(5, 5, labels = LETTERS[1:5]),
dv = sample(c(0,1), 25, replace = TRUE)
))
#> f dv
#> 1 A 1
#> 2 A 0
#> 3 A 0
#> 4 A 1
#> 5 A 1
#> 6 B 0
#> 7 B 1
#> 8 B 0
#> 9 B 1
#> 10 B 1
#> 11 C 1
#> 12 C 1
#> 13 C 1
#> 14 C 0
#> 15 C 0
#> 16 D 1
#> 17 D 1
#> 18 D 1
#> 19 D 0
#> 20 D 1
#> 21 E 1
#> 22 E 1
#> 23 E 0
#> 24 E 1
#> 25 E 0
contrasts(d$f) <- contr.helmert
contrasts(d$f)
#> [,1] [,2] [,3] [,4]
#> A -1 -1 -1 -1
#> B 1 -1 -1 -1
#> C 0 2 -1 -1
#> D 0 0 3 -1
#> E 0 0 0 4
contr_colnames(d$f)
#> NULL
glm(dv ~ f, family = binomial, data = d) |> summary()
#>
#> Call:
#> glm(formula = dv ~ f, family = binomial, data = d)
#>
#> Coefficients:
#> Estimate Std. Error z value Pr(>|z|)
#> (Intercept) 6.016e-01 4.282e-01 1.405 0.160
#> f1 -2.931e-16 6.455e-01 0.000 1.000
#> f2 -1.672e-16 3.727e-01 0.000 1.000
#> f3 2.452e-01 3.090e-01 0.794 0.427
#> f4 -4.904e-02 2.067e-01 -0.237 0.812
#>
#> (Dispersion parameter for binomial family taken to be 1)
#>
#> Null deviance: 32.671 on 24 degrees of freedom
#> Residual deviance: 31.924 on 20 degrees of freedom
#> AIC: 41.924
#>
#> Number of Fisher Scoring iterations: 4
#>
contr_colnames(d$f) <- c("A v. B", "AB v. C", "ABC v. D", "ABCD v. E")
contr_colnames(d$f)
#> [1] "A v. B" "AB v. C" "ABC v. D" "ABCD v. E"
contrasts(d$f)
#> A v. B AB v. C ABC v. D ABCD v. E
#> A -1 -1 -1 -1
#> B 1 -1 -1 -1
#> C 0 2 -1 -1
#> D 0 0 3 -1
#> E 0 0 0 4
glm(dv ~ f, family = binomial, data = d) |> summary()
#>
#> Call:
#> glm(formula = dv ~ f, family = binomial, data = d)
#>
#> Coefficients:
#> Estimate Std. Error z value Pr(>|z|)
#> (Intercept) 6.016e-01 4.282e-01 1.405 0.160
#> fA v. B -2.931e-16 6.455e-01 0.000 1.000
#> fAB v. C -1.672e-16 3.727e-01 0.000 1.000
#> fABC v. D 2.452e-01 3.090e-01 0.794 0.427
#> fABCD v. E -4.904e-02 2.067e-01 -0.237 0.812
#>
#> (Dispersion parameter for binomial family taken to be 1)
#>
#> Null deviance: 32.671 on 24 degrees of freedom
#> Residual deviance: 31.924 on 20 degrees of freedom
#> AIC: 41.924
#>
#> Number of Fisher Scoring iterations: 4
#>
contr_colpfx(d$f) <- ": "
contr_colnames(d$f)
#> [1] ": A v. B" ": AB v. C" ": ABC v. D" ": ABCD v. E"
glm(dv ~ f, family = binomial, data = d) |> summary()
#>
#> Call:
#> glm(formula = dv ~ f, family = binomial, data = d)
#>
#> Coefficients:
#> Estimate Std. Error z value Pr(>|z|)
#> (Intercept) 6.016e-01 4.282e-01 1.405 0.160
#> f: A v. B -2.931e-16 6.455e-01 0.000 1.000
#> f: AB v. C -1.672e-16 3.727e-01 0.000 1.000
#> f: ABC v. D 2.452e-01 3.090e-01 0.794 0.427
#> f: ABCD v. E -4.904e-02 2.067e-01 -0.237 0.812
#>
#> (Dispersion parameter for binomial family taken to be 1)
#>
#> Null deviance: 32.671 on 24 degrees of freedom
#> Residual deviance: 31.924 on 20 degrees of freedom
#> AIC: 41.924
#>
#> Number of Fisher Scoring iterations: 4
#>
rm(d)