setwd("~/Desktop/6.NewProductForecasting")
iphone_data=read.csv(file="iphone_sales.csv", header = TRUE)
Sales=ts(iphone_data$Sales, start=c(2007,3), freq=4)
plot(Sales, type="l", lty=2, col="red", ylab = "", xlab="")
points(Sales,pch=20, col="blue" )
title("Quarterly iPhone Sales(millions)")
Y=cumsum(Sales)
Y
## [1] 0.270 1.389 3.704 5.497 6.214 13.106 17.469 21.262 26.550
## [10] 33.917 42.654 51.406 59.804 73.946 90.181 108.828 129.166 146.239
## [19] 183.283 218.347 244.375 270.285 318.074
Sales
## Qtr1 Qtr2 Qtr3 Qtr4
## 2007 0.270 1.119
## 2008 2.315 1.793 0.717 6.892
## 2009 4.363 3.793 5.288 7.367
## 2010 8.737 8.752 8.398 14.142
## 2011 16.235 18.647 20.338 17.073
## 2012 37.044 35.064 26.028 25.910
## 2013 47.789
Y=ts(Y, start=c(2007,3), freq=4)
plot(Y, type="l", lty=2, col="red", ylab = "", xlab="")
points(Y,pch=20, col="blue" )
title("Cumulative iPhone Sales(millions)")
Y=c(0,Y[1:(length(Y)-1)]) # we want Y_t-1 not Y_t. Y_0 = 0
Y
## [1] 0.000 0.270 1.389 3.704 5.497 6.214 13.106 17.469 21.262
## [10] 26.550 33.917 42.654 51.406 59.804 73.946 90.181 108.828 129.166
## [19] 146.239 183.283 218.347 244.375 270.285
Ysq=Y**2
out=lm(Sales~Y+Ysq)
summary(out)
##
## Call:
## lm(formula = Sales ~ Y + Ysq)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.3056 -1.1951 -0.4017 0.8912 11.0888
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.4752541 1.7170236 0.859 0.400415
## Y 0.2050036 0.0438064 4.680 0.000144 ***
## Ysq -0.0002572 0.0001756 -1.464 0.158639
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.949 on 20 degrees of freedom
## Multiple R-squared: 0.8693, Adjusted R-squared: 0.8563
## F-statistic: 66.54 on 2 and 20 DF, p-value: 1.45e-09
a=out$coef[1]
b=out$coef[2]
c=out$coef[3]
a
## (Intercept)
## 1.475254
b
## Y
## 0.2050036
c
## Ysq
## -0.0002571601
mplus = (-b+sqrt(b**2-4*a*c))/(2*c)
mminus = (-b-sqrt(b**2-4*a*c))/(2*c)
mplus
## Y
## -7.132421
mminus
## Y
## 804.3153
m=mminus
p=a/m
q=b+p
m
## Y
## 804.3153
p
## (Intercept)
## 0.001834174
q
## Y
## 0.2068378
bassModel=function(p,q,m,T=100)
{
S=double(T)
Y=double(T+1)
Y[1]=0
for(t in 1:T)
{
S[t] = p*m +(q-p)*Y[t] -(q/m)*Y[t]**2
Y[t+1]=Y[t]+S[t]
}
return(list(Sales=S, cumSales=cumsum(S)))
}
Spred=bassModel(p,q,m, T=23)$Sales
Spred=ts(Spred,start=c(2007,3), freq=4)
ts.plot(Sales, Spred, col=c("blue", "red"))
legend("topleft", legend=c("actual", "Bass Model"), fill=c("blue", "red"))
# cumulative sales
Spred=bassModel(p,q,m)$Sales
CumSpred=ts(cumsum(Spred),start=c(2007,3), freq=4)
CumSales=ts(cumsum(Sales),start=c(2007,3), freq=4)
ts.plot(CumSales, CumSpred, col=c("blue", "red"))
legend("topleft", legend=c("actual", "Bass Model"), fill=c("blue", "red"))
title("Predicted Cumulative iPhone Sales")