Programa: experto en ciencias de datos

##Seccion A

Empezamos leyendo la base de datos de precio de apertura y cierre del bitcoin obtenida en la pagina Yahoo finanzas, aumentamos la variable anio y mes.

BTC_USD <- read.csv("C:/Users/Admin/Desktop/BTC-USD.csv")
BTC_USD<-BTC_USD%>%
  mutate(Date=as.Date(Date),anio=year(Date),mes=month(Date))

Seguidamente, usamos la funcion seq para trabajar con series de tiempo

BTC_USD$Date<-seq(as.Date("2015/01/01"),as.Date("2021/12/31"),by="day")

Ahora bien, creamos un nuevo data frame y agrupamos los datos por anio, en efecto:

datosagg<-BTC_USD%>%
  mutate(anio=year(Date),mes=month(Date))%>%
  select(anio,mes,everything())%>%
  select(-Date)%>%
  select(-Close,-Volume,-mes,-Low,-High)%>%
  group_by(anio)%>%
  summarise_each(list(sum))
datosagg=round(datosagg,2)

Usamos la funcion melt

mme<-melt(datosagg,id.vars = c("anio"))

usamos ggplot2 para crear un objeto donde este el diagarama de caja y obtenemos lo siguiente:

an_btc<-ggplot(mme,aes(x=factor(anio),y=value))+
  geom_boxplot(aes(fill=variable))+
  facet_wrap(~variable,scales = "free")+
  geom_jitter(width = 0.1,alpha=0.2,aes(color=variable))+
  theme(legend.position = "none")
an_btc<-an_btc+
  transition_manual(anio,cumulative=T)+
    labs(title = "EVOLUCIƓN DEL BTC EN USD :{current_frame}")
animate(an_btc, end_pause = 4,width=1100,height=600,fps=4)

anim_save(file="animacion_btc.gif",animation = last_animation())

Ahora hacemos un grafico de barras no apiladas con el precio de apertura y el precio ajustado de cierre en cada aƱo del BTC

barr<-ggplot(mme,aes(x=anio,y=value,fill=variable))+
  geom_bar(stat = "identity")+
  geom_text(aes(y=value,label=value),position = position_stack(vjust = 0.5),size=4)+
  labs(title = "Importaciones de Perú",subtitle = "Acumulada de Enero a diciembre",caption = "Fuente: BCP\n Elaboración: Ing. Bryan Morales",
       x="periodo acumulado", y="Millones de dolares")
barr<-barr+
  transition_manual(anio,cumulative=T)+
  labs(title = "EVOLUCIƓN DEL BTC EN USD :{current_frame}")

animate(barr, end_pause = 4,width=1100,height=600,fps=4)

anim_save(file="animacionn.gif",animation = last_animation())

Grafico de lineas usando facet, para esto creamos un nuevo data frame llamado bt2, esto es

bt2<-BTC_USD%>%
  select(-Volume,-Low,-mes,-anio,-Close)
melt12<-melt(bt2,id.vars = "Date")

Graficamos el cierre en cada anio del BTC

ggplot(data = melt12,aes(x=Date,y=value))+
  geom_line()+
  facet_wrap(.~variable,scales = "free",ncol = 3)+
  geom_smooth(aes(color="promedio"), se=F,method = "lm",formula = y~x,show.legend = T)+
  #geom_ma(aes(color="MA(12)"),ma_fun =SMA,n=12,size=1, show.legend=T)+
  scale_colour_manual(name="leyendas",values = c("promedio"="black",
                                                 "MA(12)"="red"),labels=c("promedio","media movil 12"))+
  geom_smooth(method = "lm")+
  scale_x_date(breaks = "4 month")+
  theme(legend.position = "bottom", axis.text.x = element_text(angle = 90,hjust = 1,size = 7))

#SecciOn B Escogemos una variable y construimos un grafico de series de tiempo, transformamos la data a serie temporal

datosagg<-datosagg%>%
  select(Open)
 
datosagg=round(datosagg,2)

tsimpobit<-ts(datosagg,start = c(2015, 1))  

graficamos y obtenemos la serie de tiempo para la variable Open

dygraph(tsimpobit,main = "Evolución de precio de apertura del BTC",xlab = "PERIODO",ylab = "Millones de usdt") %>%
  dyOptions(fillGraph = T,fillAlpha = 0.04,drawPoints = T, pointSize = 3,pointShape = "triangle",gridLineColor = "blue")%>%
  dyHighlight(highlightCircleSize = 8, highlightSeriesBackgroundAlpha = 1,highlightSeriesOpts = list(highlightSeriesBackgroundAlpha=1, highlightSeriesOpts=list(strokenwidth=2)))%>%
  dyAnnotation("2016-01-01",text = "IE",tooltip = "Inicio recesivo")%>%
  dyShading(from = "2016-02-01",to="2016-12-01",color = "#99d8c9")%>%
  dyShading(from="2018-02-01", to="2018-12-01",color = "#99d8c9")%>%
  dyEvent("2016-01-01","Inicio recesivo",labelLoc = "top")%>%
  dyLegend(show = "follow")