Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Richard Herron <richard.c.herron@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: Rolling Means and Standard Deviations |

Date |
Thu, 1 Dec 2011 10:07:04 -0500 |

I try to avoid -rolling- for easy calculations that I plan to run more thanonce or twice. I usually do rolling univariate regressions, but I tweakedthe code to do only mean and sd. My Stata program syntax is still really crude, so I would appreciate anyfeedback. Regardless, this should be much faster than -rolling- if you'reever doing more than one firm. * begin code* my programcapture program drop rolling_mean_sdprogram rolling_mean_sd version 11.2 syntax varlist(numeric min=1 max=1), window(real) * get dependent and indpendent vars from varlist tempvar x x2 xs x2s tokenize "`varlist'" generate `x' = `1' local w = `window' * generate sums generate `x2' = `x' * `x' generate `xs' = sum(`x') generate `x2s' = sum(`x2') * generate mean, variance, and sd generate meanx = s`w'.`xs' / `w' generate sdx = sqrt((s`w'.`x2s' - s`w'.`xs' * s`w'.`xs' / `w') / `w') end * get datause http://www.stata-press.com/data/r11/ibm,cleartsset tgenerate ibmadj = ibm - irxgenerate spxadj = spx - irx * callrolling_mean_sd ibmadj, window(200)list in -25/l * end code On Wed, Nov 30, 2011 at 08:04, Scott Merryman <scott.merryman@gmail.com> wrote: > > Take a look at the last example in the -rolling- help file - your > syntax does not make any sense. > > Perhaps you want something like this: > > use http://www.stata-press.com/data/r11/ibm,clear > tsset t > generate ibmadj = ibm - irx > generate spxadj = spx - irx > rolling mean_ibm=r(mean) sd_ibm=r(sd), window(200) saving(ibm, > replace) keep(date): sum ibmadj > rolling mean_spx=r(mean) sd_spx=r(sd), window(200) saving(spx, > replace) keep(date): sum spxadj > merge 1:1 date using ibm, nogenerate > merge 1:1 date using spx, nogenerate > l in -20/l > > > Scott > > > > On Wed, Nov 30, 2011 at 6:40 AM, David Ashcraft > <ashcraftd@rocketmail.com> wrote: > > Hi Statalist: > > > > I am trying to find the rolling mean and standard deviation. I know that I can use -rolling- command but somehow lost my track. Please see the example below: > > > > use http://www.stata-press.com/data/r11/ibm > > tsset t > > generate ibmadj = ibm - irx > > generate spxadj = spx - irx > > > > now I want to generate means and standard deviation of both ibmadj and spxadj with window of 200 days. I looked in the Stata manual, it mentioned about the command. I believe, I should do the following: > > > > rolling _sd, window(200) saving(means, replace) keep(date): gen sd=r(sd) > > rolling _mean, window(200) saving(means, append) keep(date): gen mean=r(mean) > > > > > > But this is not working. Any help will be greatly appreciated. > > Regards > > > > David > > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/statalist/faq > * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

- Prev by Date:
**st: Transformations** - Next by Date:
**re: st: New command -pvenn- on SSC** - Previous by thread:
**st: Transformations** - Next by thread:
**re: st: New command -pvenn- on SSC** - Index(es):