Debian Codegraph @

R/contrasts.R

raw
1 dummy_labels <- function(x){
2 ux <- sort(unique(as.character(x)))
3 new("value.labels",ux,values=ux)
4 }
5
6
7 contr <- function(type,...){
8 call <- match.call()
9 contr.fun <- as.name(paste("contr",type,sep="."))
10 args <- list(n=quote(n),...,contrasts=quote(contrasts))
11 fun <- function(n,contrasts=TRUE) NULL
12 body(fun) <- as.call(c(contr.fun,args))
13 fun
14 }
15
16 setMethod("contrasts","ANY",function(x,contrasts=TRUE,...)stats::contrasts(x,contrasts=contrasts,...))
17
18 ## Copied from stats:contrasts and modified
19 ## Original copyright (C) 1995-2013 The R Core Team
20
21 setMethod("contrasts","item",function(x,contrasts=TRUE,...)
22 {
23 if(measurement(x) %nin% c("nominal","ordinal"))
24 warning("contrasts(x,...) called with non-categorical x")
25 if(!length(vl <- labels(x))) vl <- dummy_labels(x) # stop("cannot obtain contrasts for unlabelled item")
26 vl <- vl[is.valid2(vl@values,[email protected])]
27 nvl <- length(vl@values)
28 labs <- [email protected]
29 if (!contrasts)
30 return(structure(diag(nvl), dimnames = list(labs,
31 labs)))
32 ctr <- attr(x, "contrasts")
33 if (is.null(ctr)) {
34 ctrname <- getOption("contrasts")[[if (is.nominal(x)) 1 else 2]]
35 ctr <- get(ctrname, mode = "function", envir = parent.frame())(labs,
36 contrasts = contrasts)
37 }
38 else if (is.character(ctr)){
39 ctr <- get(ctr, mode = "function", envir = parent.frame())(labs,
40 contrasts = contrasts)
41 }
42 else if (is.function(ctr)){
43 ctr <- ctr(labs,contrasts = contrasts)
44 }
45 else if (is.matrix(ctr) && nrow(ctr) != nvl){
46 warning("contrast matrix has wrong rows, deleting it")
47 ctr <- NULL
48 }
49 ctr
50 })
51
52 ## Copied and mobified from file 'contrasts.R' in the R package 'stats'.
53 ## The original copyright notice says:
54 ## """
55 ## File src/library/stats/R/contrast.R
56 ## Part of the R package, https://www.R-project.org
57 ##
58 ## Copyright (C) 1995-2021 The R Core Team
59 ##
60 ## This program is free software; you can redistribute it and/or modify
61 ## it under the terms of the GNU General Public License as published by
62 ## the Free Software Foundation; either version 2 of the License, or
63 ## (at your option) any later version.
64 ## """
65 setMethod("contrasts<-","item",function(x,how.many,value){
66 if(measurement(x) %nin% c("nominal","ordinal"))
67 warning("contrasts(x,...) called with non-categorical x")
68 if(!length(vl <- labels(x))) vl <- dummy_labels(x) #stop("cannot obtain contrasts for unlabelled item")
69 vl <- vl[is.valid2(vl@values,[email protected])]
70 nvl <- length(vl@values)
71 labs <- [email protected]
72
73 if (nvl < 2)
74 stop("contrasts can be applied only to factors with 2 or more levels")
75 if (is.numeric(value)) {
76 value <- as.matrix(value)
77 if (nrow(value) != nvl)
78 stop("wrong number of contrast matrix rows")
79 n1 <- if (missing(how.many)) nvl - 1
80 else how.many
81 nc <- ncol(value)
82 rownames(value) <- labs
83 if (nc < n1) {
84 cm <- qr(cbind(1, value))
85 if (cm$rank != nc + 1)
86 stop("singular contrast matrix")
87 cm <- qr.qy(cm, diag(nvl))[, 2:nvl]
88 cm[, 1:nc] <- value
89 dimnames(cm) <- list(levels(x), NULL)
90 if (!is.null(nmcol <- dimnames(value)[[2]]))
91 dimnames(cm)[[2]] <- c(nmcol,
92 rep.int("", n1 - nc))
93 }
94 else cm <- value[, 1:n1, drop = FALSE]
95 }
96 else if (is.function(value))
97 cm <- value
98 else if (is.character(value))
99 cm <- value
100 else if (is.null(value))
101 cm <- NULL
102 else stop("numeric contrasts or contrast name expected")
103 attr(x, "contrasts") <- cm
104 x
105 })
106
107 ## Copied and mobified from file 'contrasts.R' in the R package 'stats'.
108 ## The original copyright notice says:
109 ## """
110 ## File src/library/stats/R/contrast.R
111 ## Part of the R package, https://www.R-project.org
112 ##
113 ## Copyright (C) 1995-2021 The R Core Team
114 ##
115 ## This program is free software; you can redistribute it and/or modify
116 ## it under the terms of the GNU General Public License as published by
117 ## the Free Software Foundation; either version 2 of the License, or
118 ## (at your option) any later version.
119 ## """
120 contr.treatment <- function (n, base = 1, contrasts = TRUE)
121 {
122 if (is.numeric(n) && length(n) == 1) {
123 if (n > 1)
124 levs <- 1:n
125 else stop("not enough degrees of freedom to define contrasts")
126 }
127 else {
128 levs <- n
129 n <- length(n)
130 }
131 contr <- array(0, c(n, n), list(levs, levs))
132 diag(contr) <- 1
133 if (contrasts) {
134 if (n < 2)
135 stop(gettextf("contrasts not defined for %d degrees of freedom",
136 n - 1), domain = NA)
137 if (is.character(base)){
138 base <- match(base,rownames(contr))
139 if(is.na(base)) stop("Undefined baseline category")
140 }
141 if (base < 1 | base > n)
142 stop("baseline group number out of range")
143 contr <- contr[, -base, drop = FALSE]
144 }
145 contr
146 }
147
148 ## Copied and mobified from file 'contrasts.R' in the R package 'stats'.
149 ## The original copyright notice says:
150 ## """
151 ## File src/library/stats/R/contrast.R
152 ## Part of the R package, https://www.R-project.org
153 ##
154 ## Copyright (C) 1995-2021 The R Core Team
155 ##
156 ## This program is free software; you can redistribute it and/or modify
157 ## it under the terms of the GNU General Public License as published by
158 ## the Free Software Foundation; either version 2 of the License, or
159 ## (at your option) any later version.
160 ## """
161 contr.sum <- function (n, base = NULL, contrasts = TRUE)
162 {
163 if (is.numeric(n) && length(n) == 1)
164 levs <- 1:n
165 else {
166 levs <- n
167 n <- length(n)
168 }
169 contr <- array(0, c(n, n), list(levs, levs))
170 diag(contr) <- 1
171 if (contrasts) {
172 if (n < 2)
173 stop(paste("Contrasts not defined for", n - 1, "degrees of freedom"))
174 if (is.null(base)) base <- n
175 if (is.character(base)){
176 base <- match(base,rownames(contr))
177 if(is.na(base)) stop("Undefined baseline category")
178 }
179 if (base < 1 | base > n)
180 stop("Baseline group number out of range")
181 contr <- contr[, -base, drop = FALSE]
182 contr[base,] <- -1
183 }
184 contr
185 }
186
187 ## Copied and mobified from file 'contr.sdif.R' in the R package 'stats'.
188 ## The original copyright notice says:
189 ## """
190 ## file MASS/R/contr.sdif.R
191 ## copyright (C) 1994-2015 W. N. Venables and B. D. Ripley
192 ##
193 ## This program is free software; you can redistribute it and/or modify
194 ## it under the terms of the GNU General Public License as published by
195 ## the Free Software Foundation; either version 2 or 3 of the License
196 ## (at your option).
197 ## """
198 contr.sdif <- function (n, contrasts = TRUE)
199 {
200 if (is.numeric(n) && length(n) == 1) {
201 if (n%%1 || n < 2)
202 stop("invalid number of levels")
203 lab <- as.character(seq(n))
204 }
205 else {
206 lab <- as.character(n)
207 n <- length(n)
208 if (n < 2)
209 stop("invalid number of levels")
210 }
211 if (contrasts) {
212 contr <- col(matrix(nrow = n, ncol = n - 1))
213 upper.tri <- !lower.tri(contr)
214 contr[upper.tri] <- contr[upper.tri] - n
215 structure(contr/n, dimnames = list(lab, paste(lab[-1],
216 lab[-n], sep = "/")))
217 }
218 else structure(diag(n), dimnames = list(lab, lab))
219 }