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.
1 Answer
Reset to default 0cosinor:::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 usenewdata
.
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.