-Hello community ,

The following code , I got from a website shown here in the Source.

I am new to R .

I am having trouble identifying a fix to the following problem,

"Error in plot.xy(xy.coords(x, y), type = type, ...) :

plot.new has not been called yet"

My trouble starts from the following line of code,

suppressWarnings(points(x = hist_end, y = Y.hist[hist_start+hist_end-1], pch = 21, bg = "green"))

it also returns for each of the lines(...........) plots that occur after this line also

I have tried all the problems outlined from R community, from adjusting the par() to unticking the output file in the preference pane.

Here under is the full code

I'd really appreciate expert advice on this one.

Thank you

Enda

```
---
title: "Financial Time series forecasting, Black-Scholes-Merton model"
output:
html_document:
df_print: paged
---
Source: https://datascienceplus.com/financial-time-series-forecasting-an-easy-approach/
```{r}
suppressPackageStartupMessages(library(timeSeries))
suppressPackageStartupMessages(library(quantmod))
suppressPackageStartupMessages(library(PerformanceAnalytics))
suppressPackageStartupMessages(library(ggplot2))
```
```{r}
knitr::opts_chunk$set(error = TRUE)
```
# downloading stock price
```{r}
getSymbols("AAPL")
apple<-tail(AAPL,n=100)
```
# using adjusted close price
```{r}
Y <- coredata(AAPL[,"AAPL.Adjusted"])
```
# history time span
```{r}
hist_start <- 1
hist_end <- 100
hist <- c(hist_start:hist_end)
```
# historical prices
```{r}
Y.hist <- Y[hist]
```
# historical returns = (Y(t1)-Y(t0))/Y(t0)
```{r}
Y.hist.ret <- returns(Y.hist)
```
# standard deviation computed based on history
```{r}
(sv_hist <- sd(Y.hist.ret, na.rm=T))
```
# 95% confidence interval
```{r}
n <- length(hist)
sv_hist_low <- sqrt((n-1)*sv_hist^2/qchisq(.975, df=n-1))
sv_hist_up <- sqrt((n-1)*sv_hist^2/qchisq(.025, df=n-1))
(sv_hist_confint_95 <- c(sv_hist_low, sv_hist, sv_hist_up))
```
I am going to show a plot outlining future share price evolution.
# relative future time horizon 20 time periods out (s0 days in this case)
```{r}
t <- 3074:3173
```
# martingale hypothesis (the average of future returns is equal to the current value)
```{r}
u <- 0
```
# future expected value as based on normal distribution hypothesis
```{r}
fc <- log(Y.hist[hist_end]) + (u - 0.5*sv_hist^2)*t
```
# lower bound 95% confidence interval
```{r}
fc.lb <- fc - 1.96*sv_hist*sqrt(t)
```
# upper bound 95% confidence interval
```{r}
fc.ub <- fc + 1.96*sv_hist*sqrt(t)
```
# collecting lower, expected and upper values
```{r}
fc_band <- list(lb = exp(fc.lb), m = exp(fc), ub = exp(fc.ub))
```
# absolute future time horizon
```{r}
xt <- c(hist_end + t)
```
# stock price history line plot
```{r}
<-plot(Y[hist_start:(hist_end + max(t))], type='l',
xlim = c(2074, hist_end + max(t)),
ylim = c(75, 250),
xlab = "Time Index",
ylab = "Share Price $",
main = "Apple Share Price ($)",
panel.first = grid())
```
# starting point for our forecast
```{r error=TRUE}
par(mar=c(5.1,4.1,4.1,2.1))
suppressWarnings(points(x = hist_end, y = Y.hist[hist_start+hist_end-1], pch = 21, bg = "green"))
```
# lower bound stock price forecast
```{r}
par(mar=c(5.1,4.1,4.1,2.1))
lines(x = xt, y = fc_band$lb, lty = 'dotted', col = 'violet', lwd = 2)
```
# expected stock price forecast
```{r}
lines(x = xt, y = fc_band$m, lty = 'dotted', col = 'blue', lwd = 2)
```
# upper bound stock price forecast
```{r}
lines(x = xt, y = fc_band$ub, lty = 'dotted', col = 'red', lwd = 2)
```
# added line of code
If you like to have future stock price drift more consistent with its recent history, you may compute a return based on the same time scale of the standard deviation.
```{r}
(mu_hist <- mean(Y.hist.ret, na.rm=T))
```
```{r}
n <- length(hist)
```
# 95% confidence interval for the mean
```{r}
mu_hist_low <- mu_hist - qt(0.975, df=n-1)*sv_hist/sqrt(n)
mu_hist_up <- mu_hist + qt(0.975, df=n-1)*sv_hist/sqrt(n)
(mu_hist_confint_95 <- c(mu_hist_low, mu_hist, mu_hist_up))
```
# drift computed on historical values
```{r}
(u <- mu_hist)
```
```{r}
(curr_share_price <- Y.hist[hist_end])
```
```{r}
t <- 10
(mu_t <- log(curr_share_price) + u - 0.5*sv_hist^2)*t
```
```{r}
(sv_t <- sv_hist*sqrt(t))
```
target 10% above current price
```{r}
(target_share_price <- 1.10*curr_share_price)
```
Probability the share price at time t is >= the target price
```{r}
pnorm(q = log(target_share_price),
mean = mu_t,
sd = sv_t,
lower.tail = FALSE)
```
```