I'm getting some odd coefficients when I apply
lm to dates that have been processed and rounded using the
lubridate package. MWE:
library(ggplot2) library(lubridate) library(dplyr) lakers$month <- ymd(lakers$date) %>% round_date(unit = 'month') items_by_month <- lakers %>% group_by(month) %>% summarize(count = n()) %>% mutate(count = count / 1000) ggplot(data = items_by_month, aes(x = month, y = count)) + geom_line() + stat_smooth(method = 'lm', data = items_by_month) model <- lm(data = items_by_month, count ~ month) summary(model) time <- max(items_by_month$month) - min(items_by_month$month) coef(model)['month'] * as.numeric(time)
summary(model) the coefficient on
month is on the order of 10^-7, which is about 5 orders of magnitude too small: the plot shows an increase of about 2.5 between the first and last dates, but the last line shows an increase of about 2.5 * 10^-5.
Note that I've divided the
count column by 10^3, in order to get values that are easier to read (and closer to my actual use case). But that shouldn't effect either the plot or
lm. Also, I know there are more sophisticated techniques than linear regression for analyzing time series data; but I'm just looking at gross trends over time, not factor out seasonal patterns, etc.