Bookmark and Share

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

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

Re: st: R for Stata Users

From   "Ben Dwamena" <>
To   <>
Subject   Re: st: R for Stata Users
Date   Fri, 26 Feb 2010 13:52:40 -0500

If you already have the R code (especially if you are going to use it often)  it can be written to file stored through  known adopath with your do- or ado-file using file with write option each time to use the ado/do-file. Linking this with Roger Newson's rsource will allow you to call R,  run you code and output within Stata.

Ben Adarkwa Dwamena, MD

Assistant Professor of  Radiology
Division of Nuclear Medicine
Department of Radiology
University of Michigan Health Systems
B1G505 UH SPC 5028
1500 E. Medical Center Drive
Ann Arbor, MI  48109-5028
734-936-5388 Phone
734-936-8182 Fax

Staff Physician
D748 Nuclear Medicine Service (115), 
VA Ann Arbor Health Care System
2215 Fuller Road
Ann Arbor, MI 48105
734-845-3775 Phone
734-845-3252 Fax

>>> Fred Wolfe <> 2/26/2010 1:21 PM >>>
I think that I agree with everything that has been said about R. I
have the book on order, and I certainly hope that it does more than
the blurb suggests.

I have used R for their -rpart- ("CART" - recursive partitioning) and
-randomForest- programs.  Here is an example of (probably poor) R code
that I used (BTW, I ended up making the publication graphs in Stata!):

   sink("rmd1_r.log", split=TRUE)
   mdbinshort$md_acrcrit <- as.factor(mdbinshort$md_acrcrit)
   cat.rp <- rpart(md_acrcrit ~ .,data=mdbinshort,method="class",xval=100)
   jpeg(filename = "mdbinshort1.jpeg")
   plot(cat.rp,uniform = T)

   cat.rf <- randomForest(md_acrcrit
~.,data=mdbinshort,importance=TRUE, proximity=TRUE)
   ## look at variable importance
   ## Plot variable importance
   jpeg(filename = "mdbinshort2.jpeg")
   varImpPlot(cat.rf,main="Predictors of FM Classification")

To make this work for me, I built all of the analysis files in Stata,
and then converted them to R as in this example:

// md data medium sx but not locations
   keep if mdpure == 1
   keep md_acrcrit mdrps mdpain mdfatig mdsleep mdmood mdcog somat
mdunfresh *sx
   save mdbinmedium, replace
   shell "/Applications/StatTransfer9/st.command" mdbinmedium.dta
mdbinmedium.rdata -y

// md data medium sx but not locations with rps as cat
   keep if mdpure == 1
   keep md_acrcrit rcat mdpain mdfatig mdsleep mdmood mdcog somat mdunfresh *sx
   save mdbinmediumcat, replace
   shell "/Applications/StatTransfer9/st.command" mdbinmediumcat.dta
mdbinmediumcat.rdata -y

The real problem is making all of this work in R if you are not an R
expert. I spent hours with errors and books.

What I would like to see, and that is why I am posting this, is a
Stata program that writes R specific program code. So that if I wanted
to run -rpart-, I could run a Stata program that would create the R
code, call R, and run the program.

I think I am not expert enough to attempt this myself. Perhaps this
list could compile that 5-10 most useful R programs and a community
effort to build translation programs could be undertaken.


On Fri, Feb 26, 2010 at 11:35 AM, Airey, David C
<> wrote:
> .
> Agreed. R is documented that way on purpose according to other R docs. It's the recommended style. But it would be helpful if they had a "verbose" option in their dynamically created help files, like with the Stata online help versus PDF manual help! God I love how fast I can get to helpful help in Stata. Yes, sometimes R has good vignettes, but not enough.
> All major statistical software environments are getting explicit with links to R functionality it seems. JMP (SAS) is building in R functionality to version 9 and SPSS 18  already does. I tried Roger Newson's SSC package to run R in Stata a while ago, and it worked fine. But linking to R is not the problem as Stas points out. You have to soak yourself in R head to toe until you are positively inebriated or pickled, is my guess.
> > Well, as pretty much of R documentation, it gives the minimal amount of
> > information. [...] -Stas

Fred Wolfe
National Data Bank for Rheumatic Diseases
Wichita, Kansas
NDB Office  +1 316 263 2125 Ext 0
Research Office +1 316 686 9195 

*   For searches and help try:

Electronic Mail is not secure, may not be read every day, and should not be used for urgent or sensitive issues

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index