#
# p100-fishing.R, 23 Aug 26
#
# Data from:
# THE ECONOMICS OF PC REMANUFACTURING
# G. FERRER
# Data extracted from pdfs at
# https://www.worldradiohistory.com/Personal_Computer_World.htm

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


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


cpu_mod=function(X)
{
proc_mod=glm(log(ipEL[, X]) ~ Date, data=ipEL)
print(summary(proc_mod))

pred=predict(proc_mod)

lines(ipEL$Date[!is.na(ipEL[, X])], exp(pred), col=pal_col[X-1])

}


ip=read.csv("intel-proc.csv")
ip$Date=as.Date(paste0(ip$Date, "-15"), format="%Y-%m-%d")
tp=read.csv("PCW-proc.csv")
tp$Date=as.Date(paste0(tp$Date, "-15"), format="%Y-%m-%d")
ip=rbind(ip, tp)
ip=ip[order(ip$Date), ]

s72=read.csv("SIMM72.csv")
s72$Date=as.Date(paste0(s72$Date, "-15"), format="%Y-%m-%d")
tp=read.csv("PCW-mem.csv")
tp$Date=as.Date(paste0(tp$Date, "-15"), format="%Y-%m-%d")
s72=rbind(s72, tp)
s72=s72[order(s72$Date), ]

pal_col=rainbow(8)

plot(ip$Date[1], 1, type="n", log="y",
	xlim=range(ip$Date), ylim=range(ip[ , -1], na.rm=TRUE),
	xlab="Date", ylab="Retail Price (pounds sterling)")

dum=sapply(2:9, function(X) lines(ip$Date, ip[ , X], type="b", col=pal_col[X-1]))

EndLife=as.Date("1997-01-15")
ipEL=subset(ip, Date <= EndLife)

dum=sapply(3:8, function(X) cpu_mod(X))


legend("bottomright", legend=rev(colnames(ip)[-1]),
                        fill=rev(pal_col), border="white", bty="n")

dev.copy(dev=png, file="cpu-retail-90.png")
dev.off()


pal_col=rainbow(4)

plot(s72$Date[1], 1, type="n", log="y",
	# yaxs="i",
	xlim=range(s72$Date), ylim=range(s72[ , -1], na.rm=TRUE),
	xlab="Date", ylab="Retail Price (pounds sterling)")

dum=sapply(2:5, function(X) lines(s72$Date, s72[ , X], type="b", col=pal_col[X-1]))

legend("topright", legend=c("32 MB", "16 MB", " 8 MB", " 4 MB"), cex=1.2,
                        fill=rev(pal_col), border="white", bty="n")

dev.copy(dev=png, file="mem-retail-90.png")
dev.off()

# dj=read.csv("PCW-DJ.csv")
# 
# DJ=data.frame(Date=dj$Date,
# 		DX2.66=dj$DX2.66,
# 		P75=dj$P75,
# 		P100=dj$P100,
# 		P120=dj$P100,
# 		P133=dj$P133,
# 		P150=dj$P150,
# 		P166=dj$P166,
# 		P200=dj$P200)
# write.csv(DJ, file="PCW-proc.csv", row.names=FALSE)
# 
# DJ=data.frame(Date=dj$Date,
# 		X4MB=dj$X4MB,
# 		X8MB=dj$X8MB,
# 		X16MB=dj$X16MB,
# 		X32MB=dj$X32MB)
# write.csv(DJ, file="PCW-mem.csv", row.names=FALSE)
# 
# DJ=data.frame(Date=dj$Date,
# 		X545MB=dj$X545MB,
# 		X730MB=dj$X730MB,
# 		X845MB=dj$X845MB,
# 		X1080MB=dj$X1080MB,
# 		X1260MB=dj$X1260MB)
# write.csv(DJ, file="PCW-disk.csv", row.names=FALSE)
# 
