1
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.

  • 1
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 21 '18 at 19:40

0 Answers0