将日期用作exp()中的参数吗?

问题描述 投票:0回答:1

最终要对某些数据进行指数拟合,但是Date对象的数据类型会出错。如何将日期向量转换为exp()函数可以读取的格式?在很大程度上,我正在尝试使this example为我工作,但出现错误:

Error in Math.Date(date) : exp not defined for "Date" objects

我曾尝试使用as.numeric()进行转换,但出现错误

 Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  NA/NaN/Inf in 'x'

这里是MWE:

weight <- c(1, 10, 1000)
date <- as.Date(c('2010-11-1','2010-3-25','2010-1-14'))
df <- data.frame(date, weight)

linear.model <-lm(weight ~ date, df)
log.model <-lm(log(weight) ~ date, df)
exp.model <-lm(weight ~ exp(date), df)
r
1个回答
0
投票

我认为,即使使用数字,问题仍然是日期转换为从1970-01-01开始的天数。将一个大的数字放入exp将返回无穷大。您可以先将日期转换为较新的日期(例如,数据中的最小日期)。

© www.soinside.com 2019 - 2024. All rights reserved.