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 |
Nick Cox <njcoxstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: loop for grid-search |

Date |
Wed, 2 May 2012 09:50:35 +0100 |

You can call this a program if you like but Stata would not agree. A Stata program is defined by a -program- statement and none is evident here. Without a -program- definition that specifies that a program is byable you can get nowhere with your specific question. If you don't understand what this all means, then you are trying to program in Stata without understanding basic concepts that you need to understand, and that won't work. At least three roads lie ahead of you here. The longest road is that you learn how to convert this into a program in Stata's sense by reading the documentation. The shortest road is that you just apply this code to each subset of the data in turn by -drop-ping observations not wanted for each calculation. The other road is to build in a loop over different values of -product-. Evidently you know how to use -foreach-. Nick On Wed, May 2, 2012 at 9:34 AM, econqian222 <maguixianstatalist@gmail.com> wrote: > Hi, all > Thanks for your so many kind help. Unfortunately, I still can’t solve > the problem of writing loop for my grid-search program. Since that my > program can't be combined with “bysort”, that is > Bysort product: “my grid-search program” > My specific data for this program is listed as below: > product y x1hat x2hat sigma > 1 4.65605 3.82133 .5891969 0.1 > 1 8.55465 3.273241 .1132121 0.1 > 1 .5072982 3.273241 .1132121 0.1 > 2 .0668553 3.781385 .112315 . > 2 4.346485 3.999861 .1429252 . > 2 .453282 3.389864 .27451 . > 3 2.514498 2.554792 .432496 0.5 > 3 5.027869 3.869841 .1236284 0.5 > 3 1.121937 3.781385 .112315 0.5 > > How can I run the following grid-search program by product(1,2,3,…)? > > My grid-search program goes as following: > > if sigma1 < 1 | sigma1 == . { > local sigma1_hat = . > local rho1_hat = . > } > local SigmaMin = 1.05 > local SigmaMax = 131.05 > local SigmaJump = 1.0 > local J1 = (`SigmaMax' - `SigmaMin')/`SigmaJump' + 2 > local J2 = 32 > sort product t > > mat uHat = J(`J1',`J2',.) > local row = 2 > > foreach s of numlist `SigmaMin'(`SigmaJump')`SigmaMax'{ > mat uHat[`row',1] = `s' > local col = 2 > local RhoMax = (`s'-1.0)/`s' > local RhoJump = `RhoMax' / (`J2' - 2) > foreach r of numlist 0(`RhoJump')`RhoMax'{ > mat uHat[1,`col'] = `r' > qui gen uTemp = y -`cons'-((`r') / ((((`s')-1)^2) * (1-(`r')))) * > x1hat-((2*(`r') -1) / (((`s')-1) * (1-(`r')))) * x2hat > qui by product: egen sTemp = mean(uTemp) > qui replace sTemp = sTemp*sTemp*(1/period) > qui summ sTemp, d > mat uHat[`row',`col'] = r(sum) > drop uTemp sTemp > local col = `col' + 1 > } > local row = `row' + 1 > } > local MinU = uHat[2,2] > local rho_hat = uHat[1,2] > local sigma_hat = uHat[2,1] > local row = 2 > > foreach s of numlist `SigmaMin'(`SigmaJump')`SigmaMax'{ > local col = 2 > > foreach r of numlist 0(`RhoJump')`RhoMax'{ > if `MinU' > uHat[`row',`col'] { > local MinU = uHat[`row',`col'] > local rho_grid_hat = uHat[1,`col'] > local sigma_grid_hat = uHat[`row',1] > } > local col = `col' + 1 > } > local row = `row' + 1 > } > foreach var in sigma rho { > capture replace `var'1 = ``var'_grid_hat' > local `var'1_hat = ``var'_grid_hat' > } > > > > * * 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/

**References**:**st: loop for grid-search***From:*econqian222 <maguixianstatalist@gmail.com>

- Prev by Date:
**st: xtcsd does not work - why? - remedy?** - Next by Date:
**st: byable or loop for this program?** - Previous by thread:
**st: loop for grid-search** - Next by thread:
**st: xtcsd does not work - why? - remedy?** - Index(es):