Linear trendline

Lauderdale
Posts: 13
Joined: Fri Feb 06, 2015 6:49 am
My devices: Iphone 5

Re: Linear trendline

Post by Lauderdale »

Ok here is my final program, hopefully it will help somebody else too.

Code: Select all

print ""
print ""
print "     Calibration v.1.0"
print ".     by 'Lauderdale'"
print ""
varr = 1
button "c" text "touch to continue" at 10,450 size 300,50
while varr = 1
if button_pressed ("c")=1 then varr = 2
end while
button "c" delete
refresh
print ""
print "First you will input the data points (x,y).  After, the program outputs linear and exp fits with r^2."
varr = 2
button "d" text "touch to start" at 10,450 size 300,50
while varr = 2
if button_pressed ("d")=1 then varr = 3
end while
button "d" delete

input "# of data points ":data
totaly = 0
totallny = 0
totalxsq = 0
totalxy = 0
totalxlny = 0
totalx = 0
if data < 5 then
print ""
print ""
print ""
print "Sorry but you need nore data points"
goto end
endif
dim points(data,2)
for k = 0 to data-1
j = k+1
input "x"&j&"":xx
points(k,0) = xx
input "y"&j&"":yy
points(k,1) = yy
lnyy = ln(yy)
totaly = totaly + yy
totallny = totallny + lnyy
totalxsq = totalxsq + xx^2
totalx = totalx + xx
totalxy = totalxy + xx*yy
totalxlny = totalxlny + xx*lnyy
next k

a = (data*totalxy - (totalx*totaly))/(data*totalxsq - (totalx)^2)
b = (totaly - a*totalx)/data
p = (data*totalxlny - totalx*totallny)/(data*totalxsq - totalx^2) 
q = (totallny - p*totalx)/data

avrg = totaly/j
ssa = 0
ssb = 0
ssp = 0
ssq = 0
for m = 0 to data-1
ssa = (points(m,1)-avrg)^2 + ssa
ssb = ((a*points(m,0)+b)-points(m,1))^2 + ssb
ssp = (points(m,1)-avrg)^2 + ssp
ssq = (exp(p*points(m,0)+q)-points(m,1))^2 + ssq
next m

print ""
print "y = ax + b"
print "a = "&a&""
print "b = "&b&""

if ssa = 0 then 
print ""
print ""
print "ambiguous result, r^2 may be 1."
else
rsq = 1 - (ssb/ssa)
rsqln = 1 - (ssq/ssp)
print ""
print "r^2 = "&rsq&""
print ""
print "y = p*q^x"
p = exp(p)
q = exp(q)
print "p = "&p&""
print "q = "&q&""
print ""
print "r^2 = "&rsqln&""

endif
end:


Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Linear trendline

Post by Henko »

Code: Select all

print "ambiguous result, r^2 may be 1."
If ssa=0, then r^2 IS 1 by reasoning :

ssa is a sum of squares, each square being equal or greater than zero.
Hence, if the sum is zero, all squares must be zero.
Looking at the terms, this means that all points lie exactly on one horizontal line. This is the only situation in which ssa can be zero.
That means that the least squares fit will give a 100% precise result. Any fitting algorithm should give that result.
Hence the r^2 or any other correlation indicator should give 1 as a result.

Lauderdale
Posts: 13
Joined: Fri Feb 06, 2015 6:49 am
My devices: Iphone 5

Re: Linear trendline

Post by Lauderdale »

Ok change it if you want in your own app. My argument is that there is no dependence of y on x. Which means there's not an obvious relationship. What if it's a sine curve? Etc.

Post Reply