最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Error when trying to predict using a cosinor.lm() model - Stack Overflow

programmeradmin2浏览0评论

I am trying to apply a model obtained from the cosinor::cosinor.lm() function to predict values of a dummy dataset by using the predict() function. However, when I run the predict() function, I receive an error message: Error in [.data.frame(newdata, , paste(attr(object$Terms, "variables")[1 + : undefined columns selected

It seems like it is a column naming issue, but I even tried renaming the "time" column in new_data to "time(time)" to match the column name in the "fit" model but this did not work. BTW the vitamind dataset comes with the cosinor package. Reprex below.

fit <- cosinor::cosinor.lm(Y ~ time(time), data=vitamind, period=24)

new_data <- data.frame(
    time = seq(0, 24, length.out =200)
  )

predictions <- predict(fit, newdata = new_data, interval = "confidence")

I am trying to apply a model obtained from the cosinor::cosinor.lm() function to predict values of a dummy dataset by using the predict() function. However, when I run the predict() function, I receive an error message: Error in [.data.frame(newdata, , paste(attr(object$Terms, "variables")[1 + : undefined columns selected

It seems like it is a column naming issue, but I even tried renaming the "time" column in new_data to "time(time)" to match the column name in the "fit" model but this did not work. BTW the vitamind dataset comes with the cosinor package. Reprex below.

fit <- cosinor::cosinor.lm(Y ~ time(time), data=vitamind, period=24)

new_data <- data.frame(
    time = seq(0, 24, length.out =200)
  )

predictions <- predict(fit, newdata = new_data, interval = "confidence")

Share Improve this question asked Feb 7 at 0:40 BrennanBrennan 1 New contributor Brennan is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 0

cosinor:::predict.cosinor.lm is a rather short function:

function (object, newdata, ...) 
{
    if (missing(newdata) || is.null(newdata)) {
        Y <- object$fit$model[, paste(attr(object$Terms, "variables")[1 + 
            attr(object$Terms, "response")])]
        Y.hat <- fitted(object$fit)
    }
    else {
        Y <- newdata[, paste(attr(object$Terms, "variables")[1 + 
            attr(object$Terms, "response")])]
        Y.hat <- predict(object$fit, newdata = newdata)
    }
    mu.hat <- object$fit$coefficients[1]
    return(Y - Y.hat + mu.hat)
}

So it errors when running:

Y <- newdata[, paste(attr(object$Terms, "variables")[1 + attr(object$Terms, "response")])]

It is clearly looking for the Y variable, not the predictors (time). We can verify this, since

paste(attr(object$Terms, "variables")[1 + attr(object$Terms, "response")])

just evaluates to "Y".

Two more observations:

  • Including Y in newdata just leads to another error.
  • There are no examples in ?cosinor:::predict.cosinor.lm that use newdata.

So I'd be inclined to believe the prediction function simply doesn't work with newdata, and I would report as a bug to the author.

As a separate observation, ... is not used or passed on on predict.lm, so passing parameters like interval = "confidence" will not do anything.

发布评论

评论列表(0)

  1. 暂无评论