1

In the following example, I'm using a wrapper function to fit a gls object. I can successfully return the model prediction for the original data, but not when using newdata, which returns the error "Error in eval(expr, envir, enclos) : 'nthcdr' needs a list to CDR down":

# library -----------------------------------------------------------------
library(nlme)


# wrapper function --------------------------------------------------------
test.gls <- function(data, ...){
  fit <- gls(data=data, ...)
  return(fit)
}


# make data ---------------------------------------------------------------
set.seed(1)
n <- 20
x <- sort(rlnorm(n, meanlog = 0.25, sdlog = 1.5))
b <- 100
cv <- 0.5
y <- x*b * rlnorm(n, 0, cv)
dat <- data.frame(x,y)


# fit model ---------------------------------------------------------------
fit <- test.gls(data=dat, model=y~x-1, weights=varExp())
class(fit)
# [1] "gls"
plot(y~x, dat)
lines(dat$x, predict(fit)) # works


# prediction --------------------------------------------------------------
newdat <- data.frame(x=sort(rlnorm(n, meanlog = 0.25, sdlog = 1.5)))
pred <- predict(fit)
newpred <- predict(fit, newdata = newdat)
# Error in eval(expr, envir, enclos) : 'nthcdr' needs a list to CDR down
Marc in the box
  • 11,769
  • 4
  • 47
  • 97
  • Strange... outside of your wrapper function it works fine: `fit <- gls(y ~ x - 1, data = dat, weights = varExp()); predict(fit, newdat)` – Raad Mar 08 '16 at 09:36
  • yes, I noticed that as well. That specific error seems to have something to do with "Syntactically valid names". Maybe that helps to locate the issue. – Marc in the box Mar 08 '16 at 09:38

1 Answers1

3

The following answer seems to provide one solution using do.call: https://stackoverflow.com/a/7668846/1199289

Example:

# library -----------------------------------------------------------------
library(nlme)


# wrapper function --------------------------------------------------------
test.gls <- function(data, ...){
  fit <- gls(data=data, ...)
  return(fit)
}
test.gls2 <- function(argList=NULL){
  fit <- do.call("gls", args = argList)
  return(fit)
}


# make data ---------------------------------------------------------------
set.seed(1)
n <- 20
x <- sort(rlnorm(n, meanlog = 0.25, sdlog = 1.5))
b <- 100
cv <- 0.5
y <- x*b * rlnorm(n, 0, cv)
dat <- data.frame(x,y)


# fit model ---------------------------------------------------------------
fit <- test.gls(data=dat, model=y~x-1, weights=varExp())
fit2 <- test.gls2(argList=list(data=dat, model=y~x-1, weights=varExp()))
class(fit)
# [1] "gls"
plot(y~x, dat)
lines(dat$x, predict(fit)) # works


# prediction --------------------------------------------------------------
newdat <- data.frame(x=sort(rlnorm(n, meanlog = 0.25, sdlog = 1.5)))
pred <- predict(fit)
newpred <- predict(fit, newdata = newdat)
# Error in eval(expr, envir, enclos) : 'nthcdr' needs a list to CDR down
newpred2 <- predict(fit2, newdata = newdat) # works
Community
  • 1
  • 1
Marc in the box
  • 11,769
  • 4
  • 47
  • 97
  • 1
    I took a look at the code and the problem stems from the method `formula.gls` which is one line `eval(x$call$model)` now if you look at `fit$call ` it returns `gls(model = ..1, ` which causes `formula(object)` to fail and return the error. – Raad Mar 08 '16 at 09:58