带有新数据的 R 包余弦预测给出了错误消息



我将余弦模型与包中的数据"vitamind"拟合,然后我尝试使用我的新数据基于cosinor.fit进行预测。尽管我在 newdata 中使用了与"vitamind"中相同的变量名称,但在运行时收到错误消息predict(cosinor.fit, newdata = mydata)

cosinor.fit <- cosinor.lm(Y ~ time(time) + X + amp.acro(X), data = vitamind)
summary(cosinor.fit)
predict(cosinor.fit) # worked fine
predict(cosinor.fit, newdata = mydata) # did not work, error message as below 
# Error in eval(predvars, data, env) : object 'rrr' not found

我测试使用自己的数据作为新数据,它给出了相同的错误消息

predict(cosinor.fit, newdata = vitamind) # did not work either
# Error in eval(predvars, data, env) : object 'rrr' not found

谁能给我一些提示?非常感谢!

您需要在新数据中转换时间变量:

newdat <- vitamind
period  <- 12 #the default in cosinor.lm
newdat$rrr <- cos(2 * pi * newdat[, "time"]/period)
newdat$sss <- sin(2 * pi * newdat[, "time"]/period)
predict(fit, newdata = newdat) #works
#test if result is the same (within numerical precision)
all(abs(predict(fit) - predict(fit, newdata = newdat)) < 1e-10)
#[1] TRUE

可以说,预测方法应该为您执行此操作。您可以联系软件包维护者提出改进建议。

最新更新