My goal is to calculate the slope of a curve.
I read that I can take the first derivation for this. Which in turn requires a formula that describes my data. So I am looking into curve fits rigth now, but can not find anything that describes how to fit curves properly with date objects in them.
My data are measurements taken at irregular time intervals, and is just incrementing at different rates, but lets ignore this for now, as it makes stuff even more complicated and I am looking for the basics. The data can be represented by the economics dataset from ggplot:
library(ggplot2)
ggplot(economics, aes(date, pce))+
geom_area()
Main question How to take the first derivation of a curve fit containing date objects? Can I just convert them to a numeric, and will this distort the result?
Fitting a linear model works, but for fit2, I will get an error.
fit1 <- lm(pce ~ date, data = economics)
fit2 <- lm(pce ~ poly(date, 2, raw = T), data = economics)
Error in Ops.Date(X, Y, ...) : ^ not defined for "Date" objects
Side question
Note that I am looking for things like weekly variations. So I need a fit that is very detailed.
geom_smooth can effortlessly fit a function to my data even if x-axis is still in date format, but the loess curve or lm that I have seen with this will not do the trick for me, as they hide the interesting aspects.
So maybe I am in the wrong place and should use element wise differentation? And if yes, how does it work?
Thank you for helping or giving me resources where I can find the solutions.


