Post by mpranger on Feb 6, 2022 19:49:49 GMT -5
Here's a working example. Just copy, paste and run. It works.
The first thing you may notice is how simplistic it is. I wrote
this the way I wish I could have read it before I had to hack it out.
You may prefer to use arrays and nested loops. Go for it.
But if you want something you can read and understand,
This is it.
You can change the data to whatever you want and use this
to solve for whatever correlations you want.
'https://www.vedantu.com/formula/linear-regression-formula
' The website above shows the formula.
' The sample data here is from the book CONQUERING RISK P. 85"
' In this example we can determine the point differential
' for turn overs for a football team using 10 games of data.
' You can use up to 12 games but this example only uses 10
' if you don't use any listed variables then set those variables
' to zero. Example x11=0, x12=0 ---- y11=0, y12=0
' The first X variable is 2
' The first y variable is 7
' That means the team got two turnovers
' and the score differential was 7
' etc;
' In addition to the X Y data columns you will need to set the variable
' xe to the number of entries.[/div]
xe=10 'number of entries
' --------------------------------- DATA BELOW ---------------------------
'turnover differential
x1=2
x2=1
x3=0
x4=0
x5=-1
x6=4
x7=3
x8=-2
x9=-1
x10=-1
x11=0
x12=0
xtot=xtot+x1+x2+x3+x4+x5+x6+x7+x8+x9+x10+x11+x12
'points differential
y1=7
y2=10
y3=-3
y4=6
y5=5
y6=15
y7=12
y8=-7
y9=-2
y10=3
y11=0
y12=0
ytot=ytot+y1+y2+y3+y4+y5+y6+y7+y8+y9+y10+y11+y12
' --------------------------------- DATA ABOVE ---------------------------
xx1=x1^2
xx2=x2^2
xx3=x3^2
xx4=x4^2
xx5=x5^2
xx6=x6^2
xx7=x7^2
xx8=x8^2
xx9=x9^2
xx10=x10^2
xxtot=xxtot+xx1+xx2+xx3+xx4+xx5+xx6+xx7+xx8+xx9+xx10+xx11+xx12
xy1=x1*y1
xy2=x2*y2
xy3=x3*y3
xy4=x4*y4
xy5=x5*y5
xy6=x6*y6
xy7=x7*y7
xy8=x8*y8
xy9=x9*y9
xy10=x10*y10
xytot=xytot+xy1+xy2+xy3+xy4+xy5+xy6+xy7+xy8+xy9+xy10+xy11+xy12
b= (xe*xytot-xtot*ytot) / (xe*xxtot-xtot^2)
'-------------- now a -----------
a=(ytot-b*xtot)/xe
print
print
print using ("##.##",b)
print using ("##.##",a)
print
print "Each turnover this team gets increases"
print "the point differential favorably by ";using ("##.##",b);" ";"points"