Skip to content

Commit 8675c09

Browse files
author
maechler
committed
poly(<factor>, ..) should error (unless it is.ordered(.))
git-svn-id: https://svn.r-project.org/R/trunk@88970 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 519293f commit 8675c09

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

doc/NEWS.Rd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,10 @@
336336

337337
\item Default column labels are now correctly aligned when printing a
338338
matrix with right alignment (\PR{18943}, thanks to \I{Mikael Jagan}).
339+
340+
\item \code{poly(fct, n)} signals an error again, for a
341+
non-\code{ordered} factor \code{fct}, thanks to \I{Roland Fuss}'
342+
notice on R-devel.
339343
}
340344
}
341345
}

src/library/stats/R/contr.poly.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# File src/library/stats/R/contr.poly.R
22
# Part of the R package, https://www.R-project.org
33
#
4-
# Copyright (C) 1995-2022 The R Core Team
4+
# Copyright (C) 1995-2025 The R Core Team
55
#
66
# This program is free software; you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License as published by
@@ -79,7 +79,8 @@ poly <- function(x, ..., degree = 1, coefs = NULL, raw = FALSE, simple = FALSE)
7979
}
8080
if(degree < 1)
8181
stop("'degree' must be at least 1")
82-
if(is.object(x) && mode(x) == "numeric") x <- as.numeric(x) # for POSIXct, Date,..
82+
if(is.object(x) && mode(x) == "numeric" && !(is.factor(x) && !is.ordered(x)))
83+
x <- as.numeric(x) # for POSIXct, Date, ordered, but *not* (ordinary) factor
8384
if(raw) {
8485
Z <- outer(x, 1L:degree, `^`)
8586
colnames(Z) <- 1L:degree

src/library/stats/man/poly.Rd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ polym (\dots, degree = 1, coefs = NULL, raw = FALSE)
2222
evaluate raw polynomials.
2323
}
2424
\arguments{
25-
\item{x, newdata}{a numeric vector or an object with \code{\link{mode}}
26-
\code{"numeric"} (such as a \code{\link{Date}}) at which to evaluate the
25+
\item{x, newdata}{a numeric vector or an object such as a
26+
\code{\link{Date}} with \code{\link{mode}} \code{"numeric"}, including
27+
\code{\link{ordered}} but not plain \code{\link{factor}}s at which to evaluate the
2728
polynomial. \code{x} can also be a matrix. Missing values are not
2829
allowed in \code{x}.}
2930
\item{degree}{the degree of the polynomial. Must be less than the

tests/reg-tests-1e.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,6 +2294,20 @@ stopifnot(all.equal(list(l = -E, u = E, n = 2L), pp, tolerance = 1e-12))
22942294
## n = 1112538 (Lnx 64b) in R <= 4.5.1 ^^^^^^
22952295

22962296

2297+
## poly(<factor>, .)
2298+
hf <- c(-1, 1.2, -1.5, -0.75, -2)
2299+
x <- c("c", "b", "a", "b", "a")
2300+
xf <- as.factor(x)
2301+
xo <- as.ordered(x)
2302+
tools::assertError(verbose=TRUE, lm(hf ~ poly(x, 2)))
2303+
tools::assertError(verbose=TRUE, lm(hf ~ poly(xf, 2)))
2304+
coef(om <- lm(hf ~ poly(xo, 2)))
2305+
stopifnot(
2306+
all.equal(coef(om), tolerance = 5e-5,
2307+
c("(Intercept)" = -0.81, "poly(xo, 2)1" = 1.01, "poly(xo, 2)2" = -1.7105)))
2308+
## poly(xf, .) gave no error in R 4.1.1--4.5.x
2309+
2310+
22972311

22982312
## keep at end
22992313
rbind(last = proc.time() - .pt,

0 commit comments

Comments
 (0)