#
# stoneman-fishing.R, 16 Aug 26
#
# Data from:
# Technological Diffusion and the Computer Revolution: The UK experience
# Paul Stoneman
# The British Commercial Computer Digest, 10th edition


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


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


p122=read.csv("stoneman-p122.csv")
stock=as.matrix(p122[, -1])


xbounds=1959:1970
st59=stock[xbounds-1953, xbounds-1953]
newcomp=diag(st59)

pal_col=rainbow(length(xbounds))

plot(1, type="n", log="y",
	xlim=range(xbounds), ylim=range(st59, na.rm=TRUE),
	xlab="Year", ylab="Computers")

dum=sapply(xbounds-1958, function(X) lines(xbounds, st59[, X], type="b", col=pal_col[X]))


lc_mod=glm(log(newcomp) ~ xbounds)
summary(lc_mod)

pred=predict(lc_mod)
lines(xbounds, exp(pred), col="grey")

text(1963, 1000, paste0(round(100*(exp(coef(lc_mod)[2])-1), 0), "%"), pos=2)

lifetime=12:17
x66=p122$X1966[lifetime]

x66_mod=glm(log(x66) ~ lifetime, subset=(lifetime > 12))
summary(x66_mod)

pred=predict(x66_mod)
lines(1953+lifetime[-1], exp(pred), col="grey")

text(1968, 1000, paste0(round(100*(exp(coef(x66_mod)[2])-1), 0), "%"), pos=4)

dev.copy(dev=png, file="UK-comp-buy_1959-70.png")
dev.off()

# Figures are nomalised to 1963 pounds sterling.
# Multiply by 27.18 to get equivalent 2026 values
# https://www.officialdata.org/uk/inflation/1963?amount=100

p126=read.csv("stoneman-p126.csv")

inf_adjust=27.18

pal_col=rainbow(ncol(p126)-2)

plot(1, type="n", log="y",
	xlim=range(p126$Year), ylim=inf_adjust*range(p126, na.rm=TRUE)/1e3,
	xlab="Year", ylab="Million pounds sterling (2026)")

dum=sapply(1:26, function(X) lines(p126$Year, inf_adjust*p126[, X+1]/1e3, type="b", col=pal_col[X]))
lines(p126$Year, inf_adjust*p126$Total/1e3, type="b", col="black")

text(1965, 8000, "Total", cex=1.2)

legend("bottom", legend=1:26, horiz=TRUE, x.intersp=-1, text.width=0.15,
                        fill=pal_col, border="white", bty="n")


tot_mod=glm(log(inf_adjust*Total/1e3) ~ Year, data=p126)
summary(tot_mod)

pred=predict(tot_mod)
lines(p126$Year, exp(pred), col="grey")

dev.copy(dev=png, file="UK-comp-spent_1962-70.png")
dev.off()


cust=read.csv("10th_section-7.csv")

pal_col=rainbow(2)

usr_cnt=count(cust$User)
inst_cnt=count(usr_cnt$freq)

plot(inst_cnt, log="xy", col=pal_col[2],
	xlab="Installed computers", ylab="Organisations")

cust_mod=glm(log(freq) ~ log(x), data=inst_cnt, subset=(x < 13))
summary(cust_mod)

xbounds=1:9
pred=predict(cust_mod, newdata=data.frame(x=xbounds))

lines(xbounds, exp(pred), col=pal_col[1])

dev.copy(dev=png, file="UK-org-comp-installed_1969.png")
dev.off()

comp_cnt=count(cust$Computer)
comp_cnt=comp_cnt[order(comp_cnt$freq, decreasing=TRUE), ]
comp_cnt$perc=round(100*comp_cnt$freq/nrow(cust), digits=0)

head(comp_cnt)

