全部版块 我的主页
论坛 经济学人 二区 学术资源/课程/会议/讲座
428 0
2019-12-16
Statatip2:Buildingwithfoorsandceilings<br>
NicholasJ.Cox,UniversityofDurham,UK<br>
n.j.cox@durham.ac.uk<br>
Didyouknowaboutthefloor()andceil()functionsaddedinStata8?<br>
Supposethatyouwanttorounddowninmultiplesofsomefixednumber.<br>
For<br>
concreteness,say,youwanttoroundmpgintheautodatainmultiplesof5sothatany<br>
values10–14getroundedto10,anyvalues15–19to15,etc.mpgissimple,inthat<br>
onlyintegervaluesoccur;inmanyothercases,weclearlyhavefractionalpartstothink<br>
aboutaswell.<br>
Hereisaneasysolution:5*floor(mpg/5).floor()alwaysroundsdowntothe<br>
integerlessthanorequaltoitsargument.ThenamefloorisduetoIverson(1962),the<br>
principalarchitectofAPL,whoalsosuggestedtheexpressivexnotation.Forfurther<br>
discussion,seeKnuth(1997,39)orGraham,Knuth,andPatashnik(1994,chapter3).<br>
Asithappens,5*int(mpg/5)givesexactlythesameresultformpgintheauto<br>
data,butingeneral,whenevervariablesmaybenegativeaswellaspositive,interval*<br>
floor(expression/interval)givesamoreconsistentclassification.<br>
Letuscomparethisbrieflywithotherpossiblesolutions.round(mpg,5)isdifferent,<br>
asthisroundstothenearestmultipleof5,whichcouldbeeitherroundinguporrounding<br>
down.round(mpg-2.5,5)shouldbefinebutisalsoalittletoomuchlikeadodge.<br>
Withrecode(),youneedtwododges,say,-recode(-mpg,-40,-35,-30,-25,-20,<br>
-15,-10).Noteallthenegativesigns;negatingandthennegatingtoreverseitare<br>
necessarybecauserecode()usesitsnumericargumentsasupperlimits;i.e.,itrounds<br>
up.<br>
egen,cut()offersanothersolutionwithoptioncallat(10(5)45).Beingableto<br>
specifyanumlistisnice,ascomparedwithspellingoutacomma-separatedlist,butyou<br>
mustalsoaddalimit,here45,whichwillnotbeused;otherwise,withat(10(5)40),<br>
yourhighestclasswillbemissing.<br>
YutakaAokialsosuggestedtomempg-mod(mpg,5),whichfollowsimmediately<br>
onceyouseethatroundingdownamountstosubtractingtheappropriateremainder.<br>
mod(,),however,doesnotofferacorrespondinglyneatwayofroundingup.<br>
Thefloorsolutiongrowsonone,andithasthemeritthatyoudonotneedtospell<br>
outallthepossibleendvalues,withtheriskofforgettingormistypingsome.Conversely,<br>
recode()andegen,cut()arenotrestrictedtoroundinginequalintervalsandremain<br>
usefulformorecomplicatedproblems.<br>
Withoutrecapitulatingthewholeargumentinsofarasitappliestoroundingup,<br>
floor()’ssiblingceil()(shortforceiling)givesanicewayofroundingupinequal<br>
intervalsandiseasiertoworkwiththanexpressionsbasedonint().
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群