bassModel <- function(p, q, N, T = 100) {
S = double(T)
Y = double(T + 1)
Y[1] = 0
for(t in 1:T) {
S[t] = p * N + (q - p) * Y[t] - (q / N) * (Y[t] ** 2)
Y[t + 1] = Y[t] + S[t]
}
return(list(sales = S,cumSales = cumsum(S)))
}
Spred <- bassModel(p, q, N)$sales
Spred <- ts(Spred, start = c(2007, 3), freq =4)
Sales <- ts(sales, start = c(2007, 3), freq =4)
ts.plot(Sales, Spred, col = c("blue", "red"))
legend("topleft", c("actual", "Bass Model"), fill = c("blue", "red"))
title(paste("Predicted Sales","(p = ",format(p,digits=4),") ","(q = ",format(q,digits=4),") ","(N = ",format(N,digits=0),")"))
When I executed the code. I received the error information.
Error in strwidth(legend, units = "user", cex = cex, font = text.font) : plot.new has not been called yet
Please share any idea to fix it.