
# File "Rcross".

# MODEL A:

f = function(delta) { (5-2*delta)^2 + (25-4*delta)^2 + (35-6*delta)^2 }
optimise(f, c(0,10))
# f = 46.42857, delta = 5.714286

f1 = function(delta) { (25-4*delta)^2 + (35-6*delta)^2 }
optimise(f1, c(0,10))
# delta = 5.961538

f2 = function(delta) { (5-2*delta)^2 + (35-6*delta)^2 }
optimise(f2, c(0,10))
# delta = 5.5

f3 = function(delta) { (5-2*delta)^2 + (25-4*delta)^2 }
optimise(f3, c(0,10))
# delta = 5.5

# CVSS:
{ (5-2*5.961538)^2 + (25-4*5.5)^2 + (35-6*5.5)^2 }
# 60.93

# MODEL B:

f = function(beta) { (5-2*beta[2]-beta[1])^2 + (25-4*beta[2]-beta[1])^2 +
						(35-6*beta[2]-beta[1])^2 }
nlm(f,c(2,2))
# then e.g. beta1 = nlm(f,c(2,2))$estimate[1]
# result: f = 16.6667, beta1 = -8.33333, beta2 = 7.5

f1 = function(beta) { (25-4*beta[2]-beta[1])^2 + (35-6*beta[2]-beta[1])^2 }
nlm(f1, c(0,10))
# OR: beta2 = (35-25)/(6-4); beta1 = 25-4*beta2
# beta1=beta2=5

f2 = function(beta) { (5-2*beta[2]-beta[1])^2 + (35-6*beta[2]-beta[1])^2 }
nlm(f2, c(0,10))
# OR: beta2 = (35-5)/(6-2); beta1 = 5-2*beta2
# beta1=-10, beta2=7.5

f3 = function(beta) { (5-2*beta[2]-beta[1])^2 + (25-4*beta[2]-beta[1])^2 }
nlm(f3, c(0,10))
# OR: beta2 = (25-5)/(4-2); beta1 = 5-2*beta2
# beta1=-15, beta2=10

# CVSS:
{ (5-2*5-5)^2 + (25-4*7.5-(-10))^2 + (35-6*10-(-15))^2 }
# 225

# MODEL C:

f = function(gamma) { (5-2^gamma)^2 + (25-4^gamma)^2 + (35-6^gamma)^2 }
optimise(f, c(0,10))
# gamma = 2.02841, f = 78.98553

f1 = function(gamma) { (25-4^gamma)^2 + (35-6^gamma)^2 }
optimise(f1, c(0,10))
# gamma = 2.027911

f2 = function(gamma) { (5-2^gamma)^2 + (35-6^gamma)^2 }
optimise(f2, c(0,10))
# gamma = 1.985002

f3 = function(gamma) { (5-2^gamma)^2 + (25-4^gamma)^2 }
optimise(f3, c(0,10))
# gamma = 2.321933

# CVSS:
{ (5-2^2.027911)^2 + (25-4^1.985002)^2 + (35-6^2.321933)^2 }
# 934.33

# GRAPHICAL EXPLORATIONS:

# DATA:
x=c(2,4,6)
y=c(5,25,35)
plot(x,y, xlim=c(0,7), ylim=c(0,60))

# FULL FITS (MODELS a,b,c):
fa = function(x) 5.714286*x
plot(fa, add=TRUE, xlim=c(0,8), col="red")
fb = function(x) { -8.33333 + 7.5*x }
plot(fb, add=TRUE, xlim=c(0,8), col="green")
fc = function(x) x^2.02841
plot(fc, add=TRUE, xlim=c(0,8), col="blue")

# CROSS-VALIDATION PLOT FOR ESTIMATING THE THIRD POINT (MODELS a,b,c):

fa3 = function(x) 5.5*x
plot(fa3, add=TRUE, xlim=c(0,8), col="red")

fb3 = function(x) -15 + 10*x
plot(fb3, add=TRUE, xlim=c(0,8), col="green")

fc3 = function(x) x^(2.321933)
plot(fc3, add=TRUE, xlim=c(0,8), col="blue")

