library(tidyverse)
library(lubridate)
library(tidyquant)
library(stargazer)

BTC  <- tq_get("BTC-USD")
hash  <- read_csv("BCHAIN-HRATE.csv") %>% rename(date=Date,hash=Value)
df  <- hash %>% inner_join(BTC)
df$hash_dif  <- c(NA,diff(df$hash))
df$hash_dif <- 	ifelse(df$hash_dif==0,0.0001,df$hash_dif)
df  <- df %>% mutate(p_dif  = close-open)
df$p  <- (df$close+df$open)/2
df$p_dif  <- ifelse(df$p_dif==0,0.0001,df$p_dif)
df$p_dif2  <- c(NA,diff(df$p))
df$down  <- ifelse(0>df$p_dif2,1,0)
#lag_dis  <- 30*12
df  <- df %>% arrange(date)
lag_dis  <- 30 
df$pw  <- df$p-lag(df$p,lag_dis)
df$pw <- ifelse(df$pw==0,0.0001,df$pw)
df$down  <- ifelse(df$pw<0,1,0)
df$hw  <- df$hash-lag(df$hash,lag_dis)
df$hw <- ifelse(df$hw==0,0.0001,df$hw)
library(lubridate)
df$month  <- as.factor((month.abb[month(df$date)]))
df$year  <- as.factor(year(df$date))
model1  <- (lm(log(hw)~lag(hash,360)+year+month+log(pw),data=df))
summary(model1)
acf(model1$resid)
pacf(model1$resid)
model1  <- (lm(log(hw)~as.factor(year(date))+as.factor(month(date))+log(pw),data=df))
summary(lm(hw~as.factor(year(date))+as.factor(month(date))+lag(hash,lag_dis)+log(pw),data=df))
summary(lm(hash~as.factor(year(date))+as.factor(month(date))+(hash,lag_dis)+log(pw),data=df))
stargazer
summary(model1)
rm(model1)
model1  <- (lm(log(hw)~as.factor(year(date))+as.factor(month(date))+log(pw),data=filter(df,year(df$date)>2012)))
summary(model1)
pacf(model1$resid)
acf(model1$resid)
plot(model1$resid)

