Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: Rsource fails after first line


From   Richard Herron <richard.c.herron@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Rsource fails after first line
Date   Thu, 10 Nov 2011 07:35:20 -0500

OK, thanks, Roger! The R code runs fine by itself (I was running it by
itself until I realized that I could call R from Stata). The R code
uses the -foreign- and -quantmod- packages.

# housekeeping
library("quantmod")
library("foreign")
#setwd("C:/Users/richard/Documents/research/term_paper_econ824/")

# get data from yahoo finance
first <- Sys.Date() - 20*365
last <- Sys.Date()
etf <- c("SPY", "DVY", "GLD", "XME", "BIL", "ITE", "TLO", "GSG", "SLV")
fred <- c("GDP", "CFNAI", "CPIAUCSL", "INDPRO", "MICH")
getSymbols(etf, from=first, to=last)
getSymbols(fred, src="FRED")

# temp function to properly format xts objects from getSymbols()
f <- function(x, src) {
    header <- colnames(x)
    if (src == "yahoo") {
        symbol <- substr(header[1], 1, 3)
        colnames(x) <- tolower(substr(header, 5, 20))
        x <- x$adjusted
    }
    if (src == "FRED") {
        symbol <- header[1]
    }
    colnames(x) <- "price"
    x <- data.frame("symbol"=symbol, "date"=index(x), x)
    rownames(x) <- NULL
    return(x)
}

# create panel for stata
list_etf <- list(SPY, DVY, GLD, XME, BIL, ITE, TLO, GSG, SLV)
list_fred <- list(GDP, CFNAI, CPIAUCSL, INDPRO, MICH)
list_etf <- lapply(list_etf, f, src="yahoo")
list_fred <- lapply(list_fred, f, src="FRED")
df_etf <- do.call("rbind", list_etf)
df_fred <- do.call("rbind", list_fred)

# save to file
write.dta(df_etf, "etf_data.dta")
write.dta(df_fred, "fred_data.dta")

On Thu, Nov 10, 2011 at 07:25, Roger B. Newson <r.newson@imperial.ac.uk> wrote:
> As far as I can see, it is R that seems to be terminating, not Stata. What
> does your file "get_data.R" look like in full?
>
> Best wishes
>
> Roger
>
> Roger B Newson BSc MSc DPhil
> Lecturer in Medical Statistics
> Respiratory Epidemiology and Public Health Group
> National Heart and Lung Institute
> Imperial College London
> Royal Brompton Campus
> Room 33, Emmanuel Kaye Building
> 1B Manresa Road
> London SW3 6LR
> UNITED KINGDOM
> Tel: +44 (0)20 7352 8121 ext 3381
> Fax: +44 (0)20 7351 8322
> Email: r.newson@imperial.ac.uk
> Web page: http://www.imperial.ac.uk/nhli/r.newson/
> Departmental Web page:
> http://www1.imperial.ac.uk/medicine/about/divisions/nhli/respiration/popgenetics/reph/
>
> Opinions expressed are those of the author, not of the institution.
>
> On 09/11/2011 23:24, Richard Herron wrote:
>>
>> I am using -rsource- from SSC by Roger Newsom, but it fails after the
>> first line of actual R code (i.e., the comments source, but it returns
>> to Stata after the first R command).
>>
>> Here is the relevant code:
>>
>> global working_dir C:\users\richard\documents\research\term_paper_econ824
>> global Rterm_path `"C:\Program Files\R\R-2.13.1\bin\x64\Rterm.exe"'
>> rsource using `"$working_dir\get_data.R"'
>>
>> And the output, which doesn't provide an error, just returns to Stata:
>>
>> . rsource using `"$working_dir\get_data.R"'
>> Assumed R program path: "C:\Program Files\R\R-2.13.1\bin\x64\Rterm.exe"
>>
>> Beginning of R output from source file:
>> C:\users\richard\documents\research\term_paper_econ824\get_data.R
>>
>> R version 2.13.1 (2011-07-08)
>> Copyright (C) 2011 The R Foundation for Statistical Computing
>> ISBN 3-900051-07-0
>> Platform: x86_64-pc-mingw32/x64 (64-bit)
>>
>> R is free software and comes with ABSOLUTELY NO WARRANTY.
>> You are welcome to redistribute it under certain conditions.
>> Type 'license()' or 'licence()' for distribution details.
>>
>>   Natural language support but running in an English locale
>>
>> R is a collaborative project with many contributors.
>> Type 'contributors()' for more information and
>> 'citation()' on how to cite R or R packages in publications.
>>
>> Type 'demo()' for some demos, 'help()' for on-line help, or
>> 'help.start()' for an HTML browser interface to help.
>> Type 'q()' to quit R.
>>
>>> # get ETF data from yahoo finance
>>>
>>> # housekeeping
>>> library("quantmod")
>>
>> End of R output from source file:
>> C:\users\richard\documents\research\term_paper_econ824\get_data.R
>>
>> I am using Stata 11.2 and R 2.13-1 on a Windows 7 laptop. Any ideas?
>> Thanks!
>> *
>> *   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/
>

*
*   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/


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