Bug 17474 - polym crashes for 1D input
Summary: polym crashes for 1D input
Status: CLOSED FIXED
Alias: None
Product: R
Classification: Unclassified
Component: Analyses (show other bugs)
Version: R 3.5.0
Hardware: All All
: P5 normal
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2018-09-18 15:39 UTC by Alexandre Courtiol
Modified: 2018-09-18 16:13 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alexandre Courtiol 2018-09-18 15:39:40 UTC
Dear R-core team,

We noticed a simple-to-fix bug in stats::polym() causing troubles in lm, lme4...

Example:

```
mod <- lm(Petal.Length ~ poly(cbind(Petal.Width, Sepal.Length), 1), data = iris)
predict(mod, newdata = data.frame(Petal.Width = 1, Sepal.Length = 1))
```

crashes with the error ouput:

"Error in `colnames<-`(`*tmp*`, value = apply(z, 1L, function(x) paste(x,  : 
  attempt to set 'colnames' on an object with less than two dimensions", but

```
mod <- lm(Petal.Length ~ poly(cbind(Petal.Width, Sepal.Length), 1), data = iris)
predict(mod, newdata = data.frame(Petal.Width = 1, Sepal.Length = 1:2))
```

works fine (notice the change from length of 1 to length 2 for Sepal.Length).

For a RME directly involving the culprit, you may try:

```
test <- list(x = 1, z = 1)
coefs <- list(list(alpha = 1, norm2 = c(1, 1, 1)), list(alpha = 1, norm2 = c(1, 1, 1)))
polym(test, coefs = coefs)
```

The issue is that polym tries to define column names on a vector.

Here is one possible fix for the definition of polym:

```
function (..., degree = 1, coefs = NULL, raw = FALSE) 
{
    [SNIP]
    if (is.null(coefs)) {
        [SNIP]
    }
    else {
        [SNIP]
        if (nd > 1) 
            for (i in 2:nd) res <- res * cbind(1, poly(newdata[[i]], 
                degree = degree, coefs = coefs[[i]], simple = TRUE))[, 
                1L + z[, i]]
        res <- matrix(res, nrow = 1L)  ### FIX ###
        colnames(res) <- apply(z, 1L, function(x) paste(x, collapse = "."))
        res
    }
}
```

Best,

Alexandre Courtiol and Colin Vullioud
Comment 1 Martin Maechler 2018-09-18 16:13:57 UTC
Thank you Alexandre & Cortiol!

Yes, this is indeed a bug... an infamously missing    ', drop=FALSE'

One remark: we consider "crash" to be a wrong use of word here:

In our language, if R signals a regular error and continues to work fine afterwards, there's no crash.

A typical crash is a segmentation fault (after which you must start R anew).

I will commit the fix (and a regression test) to R-devel shortly,
with the plan to port to "R 3.5.1 patched" within a day or two.

Thank you in any case for your contribution to make R better!
Martin