Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Using date as argument in exp()?

$
0
0

Working to eventually put an exponential fit on some data, but running into errors with the datatype of a Date object. How can I convert a vector of dates into a format that can be read by the exp() function? Largely, I am trying to make this example work for me, but get error:

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

I have tried converting using as.numeric(), but get error

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

Converting the date object to a smaller number (ie days from first date, weeks from first date) solved the issue. Below is updated code to make the example work:

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

df$date_days <- df$date - min(df$date)
df$date_days_numeric <- as.numeric(df$date_days)


linear.model <-lm(weight ~ date_days_numeric, df)
log.model <-lm(log(weight) ~ date_days_numeric, df)
exp.model <-lm(weight ~ exp(date_days_numeric), df)

log.model.df <- data.frame(x = df$date_days_numeric,
                           y = exp(fitted(log.model)))

ggplot(df, aes(x=date_days_numeric, y=weight)) + 
  geom_point() +
  geom_smooth(method="lm", aes(color="Exp Model"), formula= (y ~ exp(x)), se=FALSE, linetype = 1) +
  geom_line(data = log.model.df, aes(x,y, color = "Log Model"), size = 1, linetype = 2) + 
  guides(color = guide_legend("Model Type"))

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>