0

我把代码留在这里。我相信一些精通 R 的人可以帮助我解决这个问题。

#Causal Impact
#define pre and post period dates
start = "2018-01-01"
treatment = "2020-10-01"
end = "2021-06-10"

#retrieve data
#install.packages("tseries")
library(tseries)

Bitcoin <- get.hist.quote (instrument = "BTC-USD",
                            start = start,
                            end = end,
                            quote = "Close",
                            compression = "d")


Ethereum <- get.hist.quote (instrument = "ETH-USD",
                           start = start,
                           end = end,
                           quote = "Close",
                           compression = "d")


Cordana <- get.hist.quote (instrument = "ADA-USD",
                           start = start,
                           end = end,
                           quote = "Close",
                           compression = "d")




Litecoin <- get.hist.quote (instrument = "LTC-USD",
                           start = start,
                           end = end,
                           quote = "Close",
                           compression = "d")



Tether <- get.hist.quote (instrument = "USDT-USD",
                                    start = start,
                                    end = end,
                                    quote = "Close",
                                    compression = "d")



Tran <- get.hist.quote (instrument = "TRX-USD",
                                   start = start,
                                   end = end,
                                   quote = "Close",
                                   compression = "d")



EOS <- get.hist.quote (instrument = "EOS-USD",
                        start = start,
                        end = end,
                        quote = "Close",
                        compression = "d")


Dogecoin <- get.hist.quote (instrument = "DOGE-USD",
                            start = start,
                            end = end,
                            quote = "Close",
                            compression = "d")




#plotting the data
#install.packages("ggplot2")

library(ggplot2)

autoplot(Bitcoin) + xlab("time") + ylab("Bitcoin Price (BTC-USD)") + ggtitle("Bitcoin Price between 2020-01-01 and 2021-06-19") + 
  theme_bw() +
  scale_x_date(date_breaks = "2 months") + 
  theme(axis.title.x = element_text(size = 14, face = "bold", vjust = -1),
        axis.title.y = element_text(size = 20, face = "bold")) + 
  theme(plot.title = element_text(size = 8, face = "bold")) + 
  theme(axis.text = element_text(size = 8, face = "bold")) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

autoplot(Ethereum) + xlab("time") + ylab("Ethereum Price (ETH-USD)") + ggtitle("Ethereum Price between 2020-01-01 and 2021-06-19") + 
  theme_bw() +
  scale_x_date(date_breaks = "2 months") + 
  theme(axis.title.x = element_text(size = 14, face = "bold", vjust = -1),
        axis.title.y = element_text(size = 20, face = "bold")) + 
  theme(plot.title = element_text(size = 8, face = "bold")) + 
  theme(axis.text = element_text(size = 8, face = "bold")) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

autoplot(Cordana) + xlab("time") + ylab("Cordana Price (ADA-USD)") + ggtitle("Cordana Price between 2020-01-01 and 2021-06-19") + 
  theme_bw() +
  scale_x_date(date_breaks = "2 months") + 
  theme(axis.title.x = element_text(size = 14, face = "bold", vjust = -1),
        axis.title.y = element_text(size = 20, face = "bold")) + 
  theme(plot.title = element_text(size = 8, face = "bold")) + 
  theme(axis.text = element_text(size = 8, face = "bold")) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

autoplot(Litecoin) + xlab("time") + ylab("Litecoin Price (LTC-USD)") + ggtitle("Litecoin Price between 2020-01-01 and 2021-06-19") + 
  theme_bw() +
  scale_x_date(date_breaks = "2 months") + 
  theme(axis.title.x = element_text(size = 14, face = "bold", vjust = -1),
        axis.title.y = element_text(size = 20, face = "bold")) + 
  theme(plot.title = element_text(size = 8, face = "bold")) + 
  theme(axis.text = element_text(size = 8, face = "bold")) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

autoplot(Tether) + xlab("time") + ylab("Tether Price (USDT-USD)") + ggtitle("Tether Price between 2020-01-01 and 2021-06-19") + 
  theme_bw() +
  scale_x_date(date_breaks = "2 months") + 
  theme(axis.title.x = element_text(size = 14, face = "bold", vjust = -1),
        axis.title.y = element_text(size = 20, face = "bold")) + 
  theme(plot.title = element_text(size = 8, face = "bold")) + 
  theme(axis.text = element_text(size = 8, face = "bold")) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

autoplot(Tran) + xlab("time") + ylab("Tran Price (TRX-USD)") + ggtitle("Tran Price between 2020-01-01 and 2021-06-19") + 
  theme_bw() +
  scale_x_date(date_breaks = "2 months") + 
  theme(axis.title.x = element_text(size = 14, face = "bold", vjust = -1),
        axis.title.y = element_text(size = 20, face = "bold")) + 
  theme(plot.title = element_text(size = 8, face = "bold")) + 
  theme(axis.text = element_text(size = 8, face = "bold")) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))


autoplot(EOS) + xlab("time") + ylab("EOS Price (EOS-USD)") + ggtitle("EOS Price between 2020-01-01 and 2021-06-19") + 
  theme_bw() +
  scale_x_date(date_breaks = "2 months") + 
  theme(axis.title.x = element_text(size = 14, face = "bold", vjust = -1),
        axis.title.y = element_text(size = 20, face = "bold")) + 
  theme(plot.title = element_text(size = 8, face = "bold")) + 
  theme(axis.text = element_text(size = 8, face = "bold")) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))



series <- cbind(Dogecoin, Bitcoin, Ethereum, Cordana, Litecoin)
series <- na.omit(series)

m <- data.frame(series)

autoplot(series, facets = NULL) + xlab("time") + ylab("Price") + ggtitle("Cryptocurrencies Price between 2020-01-01 and 2021-06-19") + 
  theme_bw() +
  scale_x_date(date_breaks = "2 months") + 
  theme(axis.title.x = element_text(size = 14, face = "bold", vjust = -1),
        axis.title.y = element_text(size = 20, face = "bold")) + 
  theme(plot.title = element_text(size = 8, face = "bold")) + 
  theme(axis.text = element_text(size = 8, face = "bold")) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

#correlation check
dataset_cor <- window(series, start=start, end=end)
dataset_cor <- as.data.frame(dataset_cor)
cor(dataset_cor)


#create pre and post objects
pre.period <- as.Date(c(start, treatment))
post.period <- as.Date(c(treatment, end))

#running causal impact
#install.packages("CausalImpact")
library(CausalImpact)
impact <- CausalImpact(data = series,
                       pre.period = pre.period,
                       post.period = post.period)

我做贝叶斯因果影响。当我运行代码时,一切运行顺利,但最后出现以下错误:错误:period.indices$post.period[1] not greater than period.indices$pre.period[2]

我不明白为什么我有这个问题。请你帮助我好吗?

4

0 回答 0