[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
Phil Schumm <pschumm@uchicago.edu> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: R and Stata efficiency |

Date |
Tue, 10 Jul 2007 10:40:20 -0500 |

On Jul 10, 2007, at 9:42 AM, David Airey wrote:

One other comments from the R user regarding efficiency was about nesting functions:

One other issue is that the result of a command (sic in Stata) cannot be used directly as the argument of another commmand. In R, I can do something like

dhdbeta = A %*% diag(exp(aacoef))

in which, exp() gives the exponential values of the elements in aacoef, then diag() makes a matrix with the diagonal being those exponential values, then the matrix is multiplied on the left by another matrix A. The exp() and diag() are calculated on the fly and discarded afterwards. Their values are not used elsewhere and thus there is no need to save them explicitly just for the next step. While in Stata, I have save them.

That's true, and it's because in Stata, we do most things with commands rather than function calls; e.g., the difference between

est replay results

and

summary(results)

You can use the constructions `= ' and `: ' to get inline access to Stata's expression evaluator and extended macro functions, respectively (see [P] macro), but those are only a small part of what one does at the command line.

Perhaps this issue surfaces most frequently when users complain that they can't type

reg log(y) x

I have no idea whether StataCorp has considered making this possible, however, even if it did, it would not remove the fundamental distinction between a Stata command and a function call.

Note that there are good arguments for the way Stata works. One important one is ease of use; all Stata commands (if they're written according to StataCorp's specs) follow a common, simple syntax, which makes them very easy to use interactively. When using R interactively I am constantly having to consult the man pages to remind myself of the signatures for various functions (though, in fairness, if I used it more frequently I'd remember more).

Finally, if you start using Mata, you'll see that it's a lot more like R in this respect (i.e., most statements are either assignment and/or function calls, and you can nest function calls arbitrarily). I do most of my statistical programming in Mata these days (except for rather thin wrappers in Stata to implement the UI), and recently have done a bit of working porting an existing R package into Stata. In that context, I find that I am able to write just as "efficient" code (in the sense of fewer lines) in Mata (or even more so) than the original in R.

-- Phil

*

* For searches and help try:

* http://www.stata.com/support/faqs/res/findit.html

* http://www.stata.com/support/statalist/faq

* http://www.ats.ucla.edu/stat/stata/

**References**:**re: st: R and Stata efficiency***From:*David Airey <david.airey@Vanderbilt.Edu>

- Prev by Date:
**RE: st: nesting functions in R** - Next by Date:
**st: nesting functions in R (correction)** - Previous by thread:
**RE: st: nesting functions in R** - Next by thread:
**st: matrix - how to copy row names into data?** - Index(es):

© Copyright 1996–2014 StataCorp LP | Terms of use | Privacy | Contact us | What's new | Site index |