Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGZILLA #17226] predict with gnls fails if not all levels are given #6401

Open
MichaelChirico opened this issue May 19, 2020 · 2 comments
Open

Comments

@MichaelChirico
Copy link
Owner

In the following example, predict only works when all levels of a predictor are given, but it should work when all levels or any subset is given by applying the original contrasts rather than re-calculating the contrasts for the new object. The issue appears to be similar to one posted on Stack Exchange which has a suggested patch: http://stats.stackexchange.com/questions/29513/error-in-getting-predictions-from-a-lme-object

library(nlme)
d.mod <- mtcars
d.mod$gear <- factor(d.mod$gear)

mymod <-
gnls(mp∼e.gear*disp,
data=d.mod,
params=e.gea∼gear-1,
start=rep(0.1, nlevels(d.mod$gear)))
summary(mymod)

# Fails "contrasts can be applied only to factors with 2 or more levels"
predict(mymod, newdata=data.frame(disp=100, gear=factor("3")))
# Fails "Error in p %*% beta[pmap[[nm]]] : non-conformable arguments"
predict(mymod, newdata=data.frame(disp=100, gear=factor(c("3", "4"))))
# Succeeds
predict(mymod, newdata=data.frame(disp=100, gear=factor(c("3", "4", "5"))))


METADATA

  • Bug author - Bill Denney
  • Creation time - 2017-02-21 22:52:22 UTC
  • Bugzilla link
  • Status - UNCONFIRMED
  • Alias - None
  • Component - Add-ons
  • Version - R-devel (trunk)
  • Hardware - All All
  • Importance - P5 normal
  • Assignee - R-core
  • URL -
  • Modification time - 2017-02-22 16:22 UTC

RELATED ISSUES

Blocks Bugzilla #17228

@MichaelChirico
Copy link
Owner Author

Changing the function getParsGnls to the following will fix the second error:

getParsGnls <- function (plist, pmap, beta, N) {
pars <- array(0, c(N, length(plist)), list(NULL, names(plist)))
for (nm in names(plist)) {
pars[, nm] <- if (is.logical(p <- plist[[nm]])) {
beta[pmap[[nm]]]
} else {
betatmp <- beta[pmap[[nm]]]
p.cols <- paste(nm, colnames(p), sep=".")
if (!all(mask.p <- p.cols %in% names(betatmp))) {
stop("Invalid level(s) for covariate ", nm, ": ", colnames(p)[!mask.p])
}
p %*% betatmp[colnames(p)]
}
}
pars
}

The above is not the right solution for anything but contr.treatment, but hopefully it provides an idea of how the solution could be generated.


METADATA

  • Comment author - Bill Denney
  • Timestamp - 2017-02-22 04:43:15 UTC

@MichaelChirico
Copy link
Owner Author

A better version of getParsGnls is in the related bug 17228.

Also, related fixes should also be applied to nlme (and probably other predict functions within the nlme library should also be examined:

library(nlme)
d.mod <- mtcars
d.mod$gear <- factor(d.mod$gear)
d.mod$fcyl <- factor(d.mod$cyl)

mymod.nlme <-
nlme(mp∼e.gear*disp,
data=d.mod,
fixed=e.gea∼gear-1,
random=e.gea∼1|fcyl,
start=rep(0.1, nlevels(d.mod$gear)))
summary(mymod.nlme)

# Fails "contrasts can be applied only to factors with 2 or more levels"
predict(mymod.nlme, newdata=data.frame(disp=100, gear=factor("3"), fcyl=factor("4")))
# Fails "Error in p %*% beta[pmap[[nm]]] : non-conformable arguments"
predict(mymod.nlme, newdata=data.frame(disp=100, gear=factor(c("3", "4"), fcyl=factor("4"))))
predict(mymod.nlme, newdata=data.frame(disp=100, gear=factor(c("3", "5"), fcyl=factor("4"))))
# Succeeds
predict(mymod.nlme,
newdata=data.frame(disp=100,
gear=factor(c("3", "4", "5")),
fcyl=factor(c("4", "6", "8"))))
# Fails but should provide NA
predict(mymod.nlme,
newdata=data.frame(disp=100,
gear=factor(c("6", "7", "8")),
fcyl=factor(c("4", "6", "8"))))
predict(mymod.nlme,
newdata=data.frame(disp=100,
gear=factor(c("3", "4", "5", "6", "7", "8")),
fcyl=factor(c("4", "6", "8", "4", "6", "8"))))
predict(mymod.nlme,
newdata=data.frame(disp=100,
gear=factor(c("3", "4", "5", "3", "4", "5")),
fcyl=factor(c("4", "6", "8", "4", "6", "8"))))


METADATA

  • Comment author - Bill Denney
  • Timestamp - 2017-02-22 16:22:31 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant