Created
December 22, 2016 14:51
-
-
Save eduardszoecs/b0cae49b3ebb52db06d0f8fc977dd265 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get data ---------------------------------------------------------------- | |
library(Ecdat) | |
data("CRANpackages") | |
df <- rbind(CRANpackages, | |
data.frame(Version = c("3.2", "3.3") , | |
Date = as.Date(c("2016-04-27", "2016-12-22")), | |
Packages = c(8329, 9687), | |
Source = c("Andrie de Vries", "Eduard Szöcs") | |
) | |
) | |
# convert date to numeric | |
df$date_d <- as.numeric(df$Date) | |
# plot data --------------------------------------------------------------- | |
ggplot(df, aes(x = Date, y = Packages)) + | |
geom_point() | |
ggplot(df, aes(x = date_d, y = log(Packages))) + | |
geom_point() | |
# Model data -------------------------------------------------------------- | |
library(mgcv) | |
mod_gam <- gam(Packages ~ s(date_d), data = df, method = 'REML') | |
gam.check(mod_gam) | |
# predict data | |
pdat <- data.frame(date_d = seq(min(df$date_d), 17300, 1)) | |
pdat$Date <- as.Date(pdat$date_d, origin = '1970-01-01') | |
pdat$pred <- predict(mod_gam, newdata = pdat) | |
# check predictions for 10000 | |
pdat[pdat$pred > 9997 & pdat$pred < 10003, ] | |
ggplot() + | |
geom_point(data = df, aes(x = Date, y = Packages)) + | |
geom_line(data = pdat, aes(x = Date, y = pred)) + | |
geom_hline(aes(yintercept = 10000), linetype = 'dotted') + | |
geom_vline(aes(xintercept = 17248), linetype = 'dotted') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment