Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Slope Formula Continued...

12 views
Skip to first unread message

Allen

unread,
Feb 9, 2012, 12:44:52 PM2/9/12
to
Thanks to all who provided input on my initial inquiry about a compute
function in SPSS which would calculate a slope (simple regression
slope using an X and Y value). I had started some code to calculate
it and for the experience decided to finish it.

The code calcuates the slope formula - Sigma(Y-Ymean)*(X-Xmean)/
Sigma(X-Xmean)squared

I have a time series of 10 data points (years in this case) and I want
to calculate a slope using years 1 to 5,
2 to 6, 3 to 7, etc. so I end up with 6 calculated slopes. The code
below works fine, however, I couldn't figure
out how to calculate the slope in one step, I had to do the numerator
first, denominator second and then divide
them. Any help would be appreciated.

data list list (",")/ @2001 @2002 @2003 @2004 @2005 @2006 @2007 @2008
@2009 @2010.
begin data
48.75,52.71,52.79,58.31,55.86,55.05,54.78,49.55,42.79,46.24
42.25,36.07,42.95,33.52,44.83,40.76,27.47,27.26,25.36,29.97
end data.
exe.

numeric Ymean1 to Ymean6 (f2.1).
vector A=@2001 to @2010/D=Ymean1 to Ymean6.
loop #F=1 to 10.
compute D(#F)=mean(A(#F),A(#F+1),A(#F+2),A(#F+3),A(#F+4)).
end loop if #F+4 eq 10.
exe.

numeric Yr1 to Yr10 (f1.0).
exe.
vector H=Yr1 to Yr10.
loop #I=1 to 10.
compute H(#I)=1.
end loop.
exe.

compute Xmean=3.
exe.

***************************Numerator Of The Slope
Formula*******************************************

numeric NSlopeYr1 to NSlopeYr6 (f3.2).
vector A=@2001 to @2010/B=Ymean1 to Ymean6/C=Yr1 to Yr10/D=NSlopeYr1
to NSlopeYr6.
loop #E=1 to 10.
compute D(#E)=(A(#E)-B(#E))*(C(#E)-Xmean)+(A(#E+1)-B(#E))*((C(#E)
+1)-Xmean)+(A(#E+2)-B(#E))*((C(#E)+2)-Xmean)+
(A(#E+3)-B(#E))*((C(#E)+3)-Xmean)+(A(#E+4)-B(#E))*((C(#E)+4)-
Xmean).
end loop if (#E+4)=10.
exe.

*************************Denominator Of The Slope
Formula****************************************

numeric DSlopeYr1 to DSlopeYr6 (f3.2).
vector A=@2001 to @2010/B=Ymean1 to Ymean6/C=Yr1 to Yr10/D=DSlopeYr1
to DSlopeYr6.
loop #E=1 to 10.
compute D(#E)=((C(#E)-Xmean)**2+((C(#E+1)+1)-Xmean)**2)+(((C(#E+2)+2)-
Xmean)**2)+(((C(#E+3)+3)-Xmean)**2)+(((C(#E+4)+4)-Xmean)**2).
end loop if (#E+4)=10.
exe.

************************Numerator Divided By The Denominator For
Slope**************************

numeric SlopeYr1 to SlopeYr6 (f3.2).
vector A=NSlopeYr1 to NSlopeYr6/B=DSlopeYr1 to DSlopeYr6/C=SlopeYr1 to
SlopeYr6.
loop #D=1 to 6.
compute C(#D)=(A(#D)/B(#D)).
end loop.
exe.

David

unread,
Feb 9, 2012, 3:04:57 PM2/9/12
to
Alan,
You are making this **MUCH** more difficult than it is!
**Noting the following:
* SUM(X-Xbar)**2 = Sum(X**2)-(Sum(X)**2)/N
* SUM[(X-Xbar)*(Y-Ybar)]=Sum(X*Y)-Sum(X)*Sum(Y)/N
Since X is always 1...5, The Sum(X) always=15, Sum(X**2)=55 etc
55-(15**2)/5=55-255/5=55-45=10
ie Your denominator is *ALWAYS* 10!!!!!!!!!!!!
-----
It is as simple as the following:
data list list (",")/ @2001 @2002 @2003 @2004 @2005 @2006 @2007 @2008
@2009 @2010.
begin data
48.75,52.71,52.79,58.31,55.86,55.05,54.78,49.55,42.79,46.24
42.25,36.07,42.95,33.52,44.83,40.76,27.47,27.26,25.36,29.97
end data.

VECTOR data=@2001 TO @2010 /#XY(6) /#X(6) / Slope(6).
LOOP #=1 TO 6.
COMPUTE #XY(#)=0.
COMPUTE #X(#) =0.
COMPUTE #TIME=1.
+ LOOP ##=# TO #+4.
+ COMPUTE #XY(#)=SUM(#XY(#),data(##)*#TIME).
+ COMPUTE #X(#) =SUM(#X(#),data(##)).
+ COMPUTE #TIME=#TIME+1.
+ END LOOP.
+ COMPUTE Slope(#)=(#XY(#)- #X(#)*3 )/ 10.
END LOOP.
FORMATS SLOPE1 TO SLOPE6 (F5.3).
LIST SLOPE1 TO SLOPE6.
-----------------------

Allen

unread,
Feb 13, 2012, 9:58:54 AM2/13/12
to
> > exe.- Hide quoted text -
>
> - Show quoted text -

David:

Thank you!

This is a thing of beauty, now I need to figure out the mechanics
\logistics of the use of the vectors and loops
so that I can apply them to other problems. It is sad that this
excites me almost as much as beer.

Bruce Weaver

unread,
Feb 13, 2012, 1:29:18 PM2/13/12
to
On 13/02/2012 9:58 AM, Allen wrote:

>
> David:
>
> Thank you!
>
> This is a thing of beauty, now I need to figure out the mechanics
> \logistics of the use of the vectors and loops
> so that I can apply them to other problems. It is sad that this
> excites me almost as much as beer.


What brand of beer are you drinking? It might be time to switch to
something better. :-|

--
Bruce Weaver
bwe...@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/Home
"When all else fails, RTFM."

David

unread,
Feb 13, 2012, 6:18:24 PM2/13/12
to
On Feb 13, 1:29 pm, Bruce Weaver <bwea...@lakeheadu.ca> wrote:
> On 13/02/2012 9:58 AM, Allen wrote:
>
>
>
> > David:
>
> > Thank you!
>
> > This is a thing of beauty, now I need to figure out the mechanics
> > \logistics of the use of the vectors and loops
> > so that I can apply them to other problems.  It is sad that this
> > excites me almost as much as beer.
>
> What brand of beer are you drinking?  It might be time to switch to
> something better.  :-|
>
> --
> Bruce Weaver
> bwea...@lakeheadu.cahttp://sites.google.com/a/lakeheadu.ca/bweaver/Home
> "When all else fails, RTFM."

Ah Bruce, Yer just jaded ;-)
0 new messages