Skip to content

Instantly share code, notes, and snippets.

@rolandkofler
Last active August 29, 2015 14:00
Show Gist options
  • Save rolandkofler/d13a0384c33b3d30d381 to your computer and use it in GitHub Desktop.
Save rolandkofler/d13a0384c33b3d30d381 to your computer and use it in GitHub Desktop.
Regression of weekly average bitcoin prices since 2010
a=read.csv('http://www.quandl.com/api/v1/datasets/BAVERAGE/USD.csv?&trim_start=2010-07-17&trim_end=2018-05-02&collapse=weekly&sort_order=desc', colClasses=c('Date'='Date'))
names(a)[2] <- "Weighted.Price"
fit=lm(formula=Weighted.Price~Date, data=a)
#abline(fit$coefficients)
summary(fit)
library(ggplot2)
library(scales)
m = lm(log10(Weighted.Price) ~ Date, a);
eq <- substitute(italic(y) == 10^(a + b %.% italic(x))*","~~italic(r)^2~"="~r2,
list(a = format(coef(m)[1], digits = 2),
b = format(coef(m)[2], digits = 2, decimal.mark=".", big.mark=",", , small.interval=3),
r2 = format(summary(m)$r.squared, digits = 3)))
regressionText=as.character(as.expression(eq));
ggplot(a, aes(x = Date, y = log10(Weighted.Price))) + geom_line() +
scale_y_continuous(labels = math_format(10^.x)) +
theme_bw()+ theme(panel.grid.minor = element_blank())+
annotation_logticks()+
stat_smooth(method="lm")+
labs(list(title = "Exponential Growth of average Bitcoin Price", x = "date", y = "USD average price"))+
annotate("text", x = as.Date(250, a[nrow(a),1]), y = 2.11, label = regressionText, parse = TRUE)
@rolandkofler
Copy link
Author

creates charts like this: Imgur

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment