New to R and Cannot get a second line to plot


#1

I am trying to plot an AR(1) with my bitcoin data against the data for the year. My code is as follows but my red line will not plot my AR(1) against my log returns of bitcoin:

bitcoin <- read.csv("Bitcoin.Data.csv", header=TRUE)
bitcoin
bitcoin$Close.Price

##Plotting Price Data against days##

day <- rep(c(1:366), each=1)
day
plot(day, bitcoin$Close.Price, typ='l', main="Bitcoin Price data")



## Converting Price data into Time Series ##

tsbitcoin <- ts(bitcoin$Close.Price, start = c(2012, 1, 3), frequency = 365)

plot(tsbitcoin, xlab= "Year", ylab= "Bitcoin/USD $", main= "Bitcoin Asset Value 01/01/2017 - 01/01/2018")



## Simple returns of daily Price Data##

returns <- diff(bitcoin$Close.Price, lag =1)/bitcoin$Close.Price[-length(bitcoin$Close.Price)]
rday <- rep(c(1:366), each=1)

## Plot Simple returns ##

plot(rday,returns, typ='l')
plot(rday, 1+returns, typ='l', main="simple returns")

## contunuosly compounded returns)##

lreturns <- log(returns+1)
lreturns
plot(rday,lreturns, typ='l', main="log returns")

## Distribution of log retrurns ##
xlabel<-seq(-0.4,0.4, by=0.5)
hist(lreturns,xlim=c(-0.4,0.4),ylim=c(0,1000))

## Attempt to plot risiduals##

par(mfrow = c(2,2))
plot(lreturns,typ='l')

ONLINE CODE FOR GARCH MODEL

## Packages ##

# Data for "Forecasting: principles and practice" ~~~~
library(fpp)
	
# Rmetrics - Markets and Basic Statistics~~~
library(fBasics)

# Rmetrics - Autoregressive Conditional Heteroskedastic Modelling~~~
library(fGarch)

# Rmetrics - Modelling Trends and Unit Roots~~~
library(fUnitRoots)

 # Rmetrics - Modelling ARMA Time Series Processes~~~
library(fArma)

## Basic statistics for log returns##

basicStats(lreturns)

## Converting log returns into Time Series (tslreturns) ##

tslreturns <- ts(lreturns, start = c(366, 1, 3), frequency = 365)

plot(tslreturns, xlab= "Year", ylab= "Returns", main= "Daily Returns of Bitcoin Natural Log- Continuously Compounded")

## ACF & PACF Models ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

##Standard ACF PACF##

par(mfcol = c(2, 1))

acf(lreturns)

pacf(lreturns)

par(mfcol = c(1, 1))

## ACF PACF squared returns ##

par(mfcol = c(2, 1))

acf(lreturns^2)

pacf(lreturns^2)

par(mfcol = c(1, 1))


## First Differenced ACF & PACF ##

par(mfcol = c(2, 1))

acf(diff(lreturns))

pacf(diff(lreturns))

par(mfcol = c(1, 1))

## Discrete White noise ##

set.seed(1)
acf(rnorm(1825))

## models ## ----------------------------------



## AR 1 Model ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ar1model <- arima(lreturns, order=c(1,0,0))
ar1model

plot(tslreturns, typ='l', main= "AR(1) Model against Bitcoin log returns" )
lines(fitted(ar1model), col='red')

#2
  • A sample reproducible working R code (without loading a handful of packages) would be helpful
  • You’re using base R graphics, and while I’m sure there are many on this forum who know enough about base R plotting, you’ll also find that many more are 100% on the ggplot2 and aren’t knowledgeable enough about details of base R (like, for example, me). You may want to try Stack Overflow if you want your answer faster.