#
# 2605-fishing.R, 17 May 26
#
# Data from:
# Project Life Cycles in Open-Source Software
# Sanjiv R. Das, Andrii Ieroshenko, Piyush Jain, David Qiu, Michael Chin, Brian E. Granger

library("colorspace")
library("plyr")


par(bty="l")
par(las=1)
par(pch="+")
pal_col=rainbow(3)


mk_cust_mod=function(days, alg_str="default")
{
# Bass model
# Cumulative number
# cust_mod=nls(cum_contrib ~ m*(1-exp((p+q)*month))/(1+q/p*exp((p+q)*month)),
# Rate per month
cust_mod=nls(contributors ~ m*(p*(p+q)^2*exp((p+q)*month))/((p*exp((p+q)*month)+q)^2),
                data=proj, subset=1:days,
                algorithm=alg_str, # need for limited data convergence
#                control=list(minFactor=1e-5),
#                trace=TRUE,
		start=list(m=1e4, p=1e-3, q=1e-2))
# 		start=list(m=2.1, p=-1e-3, q=-1e-2))

return(cust_mod)
}


proj=read.csv("data/numpy-numpy-monthly.csv", as.is=TRUE)
proj$cum_contrib=cumsum(proj$contributors)

proj$month=1:nrow(proj)
x_bounds=proj$month

plot(proj$month, proj$contributors, col=pal_col[2],
        xaxs="i", yaxs="i",
        xlim=c(0, nrow(proj)),
        xlab="Month", ylab="Developers")

bass_mod=mk_cust_mod(nrow(proj))
summary(bass_mod)

pred=predict(bass_mod, newdata=data.frame(month=x_bounds))
lines(pred, col=pal_col[1])

bass_mod=mk_cust_mod(trunc(nrow(proj)*0.75))
summary(bass_mod)

pred=predict(bass_mod, newdata=data.frame(month=x_bounds))
lines(pred, col=pal_col[3])


legend(x="topleft", legend=c("100% of data fitted", "Data", "75% of data fitted"), bty="n", fill=pal_col, cex=1.2)

dev.copy(dev=png, file="numpy-monthly-contrib.png")
dev.off()

