moving average

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

moving average

drfg2008
SPSS20, Win7 64bit

how do I calculate the moving average for a time series

Example: I would like to compute a moving average over a length of 20 cases.


* not working example.

input program.
loop a =1 to 1000 by 1.
end case.
end loop.
end file.
end input program.
EXECUTE.

VECTOR v(20).
DO REPEAT v1 to v20.
LOOP #i =1 to 20.
COMPUTE LAG(a,#i).
END LOOP.
END REPEAT.
EXECUTE.

COMPUTE mean_lag=MEAN(v1 to v20).
EXECUTE.
DELETE VARIABLES v1 to v20.

Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Re: moving average

David Marso
Administrator
CREATE PMA20=PMA(a,20).

drfg2008 wrote
SPSS20, Win7 64bit

how do I calculate the moving average for a time series

Example: I would like to compute a moving average over a length of 20 cases.


* not working example.

input program.
loop a =1 to 1000 by 1.
end case.
end loop.
end file.
end input program.
EXECUTE.

VECTOR v(20).
DO REPEAT v1 to v20.
LOOP #i =1 to 20.
COMPUTE LAG(a,#i).
END LOOP.
END REPEAT.
EXECUTE.

COMPUTE mean_lag=MEAN(v1 to v20).
EXECUTE.
DELETE VARIABLES v1 to v20.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: moving average

David Marso
Administrator
However if you truly insist on rolling your own for the sake of some sort of control freak issue the following will suffice ;-)))
DO REPEAT l=1 TO 20.
COMPUTE DMMPMA20=SUM(DMMPMA20,LAG(a,l)/20).
END REPEAT.
IF $CASENUM LE 20 DMMPMA20=$SYSMIS.
EXE.


David Marso wrote
CREATE PMA20=PMA(a,20).

drfg2008 wrote
SPSS20, Win7 64bit

how do I calculate the moving average for a time series

Example: I would like to compute a moving average over a length of 20 cases.


* not working example.

input program.
loop a =1 to 1000 by 1.
end case.
end loop.
end file.
end input program.
EXECUTE.

VECTOR v(20).
DO REPEAT v1 to v20.
LOOP #i =1 to 20.
COMPUTE LAG(a,#i).
END LOOP.
END REPEAT.
EXECUTE.

COMPUTE mean_lag=MEAN(v1 to v20).
EXECUTE.
DELETE VARIABLES v1 to v20.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: moving average

drfg2008
This post was updated on .
In reply to this post by David Marso
perfect, thanks

One more question, since I could not find this function in the function group (dialogue: compute variable): Is there a more complete list of functions of SPSS somewhere in the internet?
Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Re: moving average

David Marso
Administrator
In reply to this post by David Marso
One situation where the control freak might be in  line would be when you wish to calculate other stats not supported by CREATE (for example a moving Variance).
DO REPEAT L=1 TO 20.
+  COMPUTE DMMPMA20=SUM(DMMPMA20,LAG(a,L) /20).
+  COMPUTE DMMSSQ20=SUM(DMMSSQ20,LAG(a,L)**2).
END REPEAT.
DO IF $CASENUM LE 20.
+  COMPUTE DMMPMA20=$SYSMIS.
+  COMPUTE DMMSSQ20=$SYSMIS.
ELSE.
+  COMPUTE DMMVAR20=(DMMSSQ20-DMMPMA20**2*20)/19.
END IF.

David Marso wrote
However if you truly insist on rolling your own for the sake of some sort of control freak issue the following will suffice ;-)))
DO REPEAT l=1 TO 20.
COMPUTE DMMPMA20=SUM(DMMPMA20,LAG(a,l)/20).
END REPEAT.
IF $CASENUM LE 20 DMMPMA20=$SYSMIS.
EXE.


David Marso wrote
CREATE PMA20=PMA(a,20).

drfg2008 wrote
SPSS20, Win7 64bit

how do I calculate the moving average for a time series

Example: I would like to compute a moving average over a length of 20 cases.


* not working example.

input program.
loop a =1 to 1000 by 1.
end case.
end loop.
end file.
end input program.
EXECUTE.

VECTOR v(20).
DO REPEAT v1 to v20.
LOOP #i =1 to 20.
COMPUTE LAG(a,#i).
END LOOP.
END REPEAT.
EXECUTE.

COMPUTE mean_lag=MEAN(v1 to v20).
EXECUTE.
DELETE VARIABLES v1 to v20.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: moving average

Art Kendall
In reply to this post by David Marso
I suggest that you replace
IF $CASENUM LE 20 DMMPMA20=$SYSMIS.

with
IF $CASENUM LE 20 DMMPMA20=-99999999.
missing values DMMPMA20(-99999999).

You can use any value you wish for the missing value.  If all of the values you are summing are non-negative you could use -1.

The resulting value is missing for a reason that that you the user decided.  So a user-missing value is called for.

One of the strengths of SPSS is the distinction between user missing (missing because the user says so) and system missing (missing because the software was unable to follow your instructions).

In the long run reserving $sysmis for system missing is very helpful in debugging syntax.
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: moving average

Art Kendall
In reply to this post by David Marso
I suggest you replace
IF $CASENUM LE 20 DMMPMA20=$SYSMIS.
with
IF $CASENUM LE 20 DMMPMA20= -1.
missing values DMMPMA20 (low thru -1).
value labels DMMPMA20
    -1 'fewer than 20 previous values'.

The messing value can be anything outside the legitimate range of DMMPMA20.

The value of DMMPMA20 is missing for a reason that you the user decided.
One of the strengths of SPSS is the distinction between user-missing (missing because the user says so) and system missing (missing because the system is unable to follow your instructions (i.e., the syntax).

In the long run maintaining this distinction and redrafting the syntax until there are no system missing values is very helpful in quality assurance.

I have been suggesting for a few decades that SPSS have an option to give a warning when a user command specifically assigns a value of $sysmis.

Perhaps some PYTHON person could cobble up something for devcentral that would be part of a  syntax option like the auto indent is.
Art Kendall
Social Research Consultants
On 8/23/2012 6:12 AM, David Marso wrote:
However if you truly insist on rolling your own for the sake of some sort of
control freak issue the following will suffice ;-)))
DO REPEAT l=1 TO 20.
COMPUTE DMMPMA20=SUM(DMMPMA20,LAG(a,l)/20).
END REPEAT.
IF $CASENUM LE 20 DMMPMA20=$SYSMIS.
EXE.



David Marso wrote
CREATE PMA20=PMA(a,20).


drfg2008 wrote
SPSS20, Win7 64bit

how do I calculate the moving average for a time series

Example: I would like to compute a moving average over a length of 20
cases.


* not working example.

input program.
loop a =1 to 1000 by 1.
end case.
end loop.
end file.
end input program.
EXECUTE.

VECTOR v(20).
DO REPEAT v1 to v20.
LOOP #i =1 to 20.
COMPUTE LAG(a,#i).
END LOOP.
END REPEAT.
EXECUTE.

COMPUTE mean_lag=MEAN(v1 to v20).
EXECUTE.
DELETE VARIABLES v1 to v20.


      



-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/moving-average-tp5714811p5714813.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD


===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: moving average

David Marso
Administrator
In reply to this post by Art Kendall
Actually in this case the value is undefined and has values only as an artifact of the algorithm used to compute the values prior to case 21.
It would actually be better code to replace my previous pre coffee mess with the following and avoid the issue all together.
--
DO IF $CASENUM GT 20.
+  DO REPEAT l=1 TO 20.
+    COMPUTE DMMPMA20=SUM(DMMPMA20,LAG(a,l) /20).
+    COMPUTE DMMSSQ20=SUM(DMMSSQ20,LAG(a,l)**2).
+  END REPEAT.
+  COMPUTE DMMVAR20=(DMMSSQ20-DMMPMA20**2*20)/19.
END IF.

Art Kendall wrote
I suggest that you replace
IF $CASENUM LE 20 DMMPMA20=$SYSMIS.

with
IF $CASENUM LE 20 DMMPMA20=-99999999.
missing values DMMPMA20(-99999999).

You can use any value you wish for the missing value.  If all of the values you are summing are non-negative you could use -1.

The resulting value is missing for a reason that that you the user decided.  So a user-missing value is called for.

One of the strengths of SPSS is the distinction between user missing (missing because the user says so) and system missing (missing because the software was unable to follow your instructions).

In the long run reserving $sysmis for system missing is very helpful in debugging syntax.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: moving average

David Marso
Administrator
Applying a little more thought to the question -face/palm- that the previous code is an truly atrocious way to do this.  Since all the crap is already accumulated there is no need to redo the whole ball of wax for cases beyond 21.
Simply SUBTRACT f(lag21) and add f(lag1) to the LAG of previous accumulation for later cases. DOH!!!
This is another classic example of code revision/iterative refinement (I sometimes get it right on the first try)  Even better use a MACRO to avoid the magic numbers (20,21,19)!
--
DO IF $CASENUM EQ 21.
+  DO REPEAT L=1 TO 20.
+    COMPUTE DMMPMA20=SUM(DMMPMA20,LAG(a,L) /20).
+    COMPUTE DMMSSQ20=SUM(DMMSSQ20,LAG(a,L)**2).
+  END REPEAT.
ELSE IF $CASENUM  GT 21.
+  COMPUTE DMMPMA20=SUM(LAG(DMMPMA20),(LAG(a,1)-LAG(a,21))/20).
+  COMPUTE DMMSSQ20=SUM(LAG(DMMSSQ20), LAG(a,1)**2-LAG(a,21)**2).
END IF.
COMPUTE DMMVAR20=(DMMSSQ20-DMMPMA20**2*20)/19.

David Marso wrote
Actually in this case the value is undefined and has values only as an artifact of the algorithm used to compute the values prior to case 21.
It would actually be better code to replace my previous pre coffee mess with the following and avoid the issue all together.
--
DO IF $CASENUM GT 20.
+  DO REPEAT l=1 TO 20.
+    COMPUTE DMMPMA20=SUM(DMMPMA20,LAG(a,l) /20).
+    COMPUTE DMMSSQ20=SUM(DMMSSQ20,LAG(a,l)**2).
+  END REPEAT.
+  COMPUTE DMMVAR20=(DMMSSQ20-DMMPMA20**2*20)/19.
END IF.

Art Kendall wrote
I suggest that you replace
IF $CASENUM LE 20 DMMPMA20=$SYSMIS.

with
IF $CASENUM LE 20 DMMPMA20=-99999999.
missing values DMMPMA20(-99999999).

You can use any value you wish for the missing value.  If all of the values you are summing are non-negative you could use -1.

The resulting value is missing for a reason that that you the user decided.  So a user-missing value is called for.

One of the strengths of SPSS is the distinction between user missing (missing because the user says so) and system missing (missing because the software was unable to follow your instructions).

In the long run reserving $sysmis for system missing is very helpful in debugging syntax.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: moving average

Jim Marks
In reply to this post by drfg2008

Prior Moving average:

CREATE
  /PMA_20=PMA(VAR001 20).


Centred moving average:

CREATE
  /cma_20=MA(VAR002 20).

I think this will work without the time-series module...

Jim Marks
Sr Market Research Manager
National Market Research
Kaiser Foundation Health Plan of the Mid-Atlantic States, Inc.
2101 E. Jefferson St.
Rockville, MD 20852
Phone: (301) 816-6822
Cell Phone: (301) 456-6164

NOTICE TO RECIPIENT:  If you are not the intended recipient of this e-mail, you are prohibited from sharing, copying, or otherwise using or disclosing its contents.  If you have received this e-mail in error, please notify the sender immediately by reply e-mail and permanently delete this e-mail and any attachments without reading, forwarding or saving them.  Thank you.




From:        drfg2008 <[hidden email]>
To:        [hidden email]
Date:        08/23/2012 06:02 AM
Subject:        moving average
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




SPSS20, Win7 64bit

how do I calculate the moving average for a time series

Example: I would like to compute a moving average over a length of 20 cases.


* not working example.

input program.
loop a =1 to 1000 by 1.
end case.
end loop.
end file.
end input program.
EXECUTE.

VECTOR v(20).
DO REPEAT v1 to v20.
LOOP #i =1 to 20.
COMPUTE LAG(a,#i).
END LOOP.
END REPEAT.
EXECUTE.

COMPUTE mean_lag=MEAN(v1 to v20).
EXECUTE.
DELETE VARIABLES v1 to v20.





-----
Dr. Frank Gaeth
FU-Berlin

--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/moving-average-tp5714811.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD

Reply | Threaded
Open this post in threaded view
|

Automatic reply: moving average

Dean Howlin

I am out of the office today. Please contact my colleagues Niall or Stephen if you need assistance.

[hidden email];[hidden email]