Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Stata module tstf

    Hello,

    Do folks have experience using the Stata module tstf to compute intervention time-series models? I am unable to run the commands provided in the help file. If anyone has any insights or advice on this module, please drop your knowledge here. Much appreciated.

    Gina

  • #2
    Welcome to Statalist.

    I am unable to run the commands provided in the help file.
    There are many possible explanations why the commands fail to run.

    Please a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], as described in section 12 of the FAQ.

    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

    Section 12.1 is particularly pertinent.

    12.1 What to say about your commands and your problem

    Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
    ...
    Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.

    Comment


    • #3
      Hi,

      Thank you for the helpful pointers. I am using Stata 12.

      Allow me to reiterate my problem:

      I am trying to run intervention analysis using a package called tstf developed by Nicola Orsini & Xing-Wu Zhou, Biostatistics Team, from the Department of Public Health Sciences, Karolinska Institutet in Sweden.

      I was able to install the package and pulled up the help file using

      Code:
      ssc install tstf
      help tstf
      I then set the time variable using
      Code:
      tsset time, monthly
      But was unable to run the following command provided in the help file:
      Code:
      tstf lograte if inrange(time, 625, 691), smooth int(676) arima(1,1,1) sarima(1,0,0,12) tabulate grdata eform ytitle("Calling rates per 100,000 smokers") xlabel(625(6)691, angle(45)) xmtick(625(1)691) name(figure2a, replace)
      The error message I received was:
      Code:
      option version() not allowed
      Any ideas as to why this might be happening? Thank you in advance for any insights.

      Best,
      Gina

      Comment


      • #4
        I have a guess, if you care to experiment. It's a consequence of the fact that you are running Stata version 12, a crucial piece of information.
        If you issue the command
        Code:
        doedit "`c(sysdir_plus)'/t/tstf.ado"
        the tstf.ado will be opened in the Do-file Editor window. Search the ado for saveold and you will find the command
        Code:
        quietly saveold `"data_tstf"' , replace nolabel version(11)
        The version() option was not added to the saveold command until Stata version 14, and you are running version 12. I believe if you remove this option from the command and save the ado file, and then quit Stata and relaunch it, you will not get the error message you encountered. However, you may well get some other error message further along in the process.

        The output of ssc describe tstf tells us an email address for Nicola Orsini so perhaps if problems persist he would be able to advise. If it hasn't become obvious, I know nothing about tstf itself.

        Comment


        • #5
          Hello,

          I appreciate the tips but unfortunately, could not advance with running the sample code provided in help file for tstf

          I followed exactly the instructions listed above, but encounter a new error:
          Code:
          the R script did not run, see what's wrong in the file tstf_to_r.R

          Comment


          • #6
            I am in waaay over my head here.

            I installed tstf on my system.

            The output of help tstf tells us,the tstf command is used to feed Stata data into R to run the arimax package. So the first thing necessary is the R be installed on your system.

            Using the output of help tstf I created a do-file to test the program.
            Code:
            use http://www.stats4life.se/data/quitline, clear
            tsset time, monthly
            
            tstf lograte if inrange(time, 625, 691), smooth int(676) arima(1,1,1) sarima(1,0,0,12) tabulate grdata eform ytitle("Calling rates per 100,000 smokers") xlabel(625(6)691, angle(45)) xmtick(625(1)691) name(figure2a, replace)
            I ran this and tstf failed with the same message you obtained. I saw that tstf opened the file tstf_to_r.R in the viewer window so I looked at it, but it's just the R source code that tstf attempted to run, not a log or anything helpful to tell me why it did not run.

            By examining the source code for tstr.ado using viewsource tstr.ado I found that tstr was looking for R in the wrong place on my system, so in my testing do-file I added the rpath option to the tstr command to point it to my installation of R.

            I reran the do-file and it again failed.

            I noticed the following R code from tstf_to_r.R (which was open in the viewer window again)
            Code:
            library(stats)
            library(foreign)
            library(TSA)
            library(car)
            library(aod)
            which suggests that several libraries are required for arimax to use, so I ran those five commands in R and found that TSA, car, and aod were not installed. I ran the following commands in R
            Code:
            install.packages("TSA")
            install.packages("car")
            install.packages("aod")
            which installed the three missing packages. For one package, I was presented with the option of an older executable or a newer source that would need compiling; I chose the older executable.

            I quit and relaunched R and again ran the 5 library commands, and this time didn't seem to fail.

            I quit R and in Stata reran the do-file and got the following results.
            Code:
            . use http://www.stats4life.se/data/quitline, clear
            (Swedish Smoking Quitline 1999-2007)
            
            . tsset time, monthly
                    time variable:  time, 1999m1 to 2017m8
                            delta:  1 month
            
            .
            . tstf lograte if inrange(time, 625, 691), smooth int(676) arima(1,1,1) sarima(1,0,0,12) tabulat
            > e grdata eform ytitle("Calling rates per 100,000 smokers") xlabel(625(6)691, angle(45)) xmtick
            > (625(1)691) name(figure2a, replace)
            
            ARIMA regression with a smooth transfer function    No. of obs    =         67
            Optimization = CSS-ML
            Log likelihood = 46.277649
            Sample = 2012m2 - 2017m8   Intervention starts = 2016m5
            ------------------------------------------------------------------------------
                 lograte |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
            -------------+----------------------------------------------------------------
            ARIMA        |
                     ar1 |   .2756694   .1477561     1.87   0.062    -.0139272     .565266
                     ma1 |  -.9252127   .0590685   -15.66   0.000    -1.040985   -.8094405
                    sar1 |   .7780222   .0740032    10.51   0.000     .6329786    .9230658
            -------------+----------------------------------------------------------------
            TRANSFER     |
                   delta |   .8718121   .0645893    13.50   0.000     .7452194    .9984048
                   omega |   .0623748   .0227997     2.74   0.006     .0176882    .1070615
            ------------------------------------------------------------------------------
            
            Table of effects k units of time after intervention
            
                   time    k   exp(Eff)     LB     UB   P-value  
                 2016m5    0       1.06   1.02   1.11     0.006  
                 2016m6    1       1.12   1.04   1.21     0.003  
                 2016m7    2       1.18   1.06   1.30     0.002  
                 2016m8    3       1.23   1.09   1.38     0.001  
                 2016m9    4       1.27   1.12   1.45     0.000  
                2016m10    5       1.31   1.14   1.51     0.000  
                2016m11    6       1.35   1.16   1.57     0.000  
                2016m12    7       1.38   1.18   1.62     0.000  
                 2017m1    8       1.41   1.20   1.66     0.000  
                 2017m2    9       1.44   1.22   1.70     0.000  
                 2017m3   10       1.46   1.23   1.74     0.000  
                 2017m4   11       1.48   1.24   1.77     0.000  
                 2017m5   12       1.50   1.24   1.81     0.000  
                 2017m6   13       1.51   1.25   1.84     0.000  
                 2017m7   14       1.53   1.25   1.87     0.000  
                 2017m8   15       1.54   1.25   1.90     0.000  
            
            .
            end of do-file
            Click image for larger version

Name:	figure2a.png
Views:	1
Size:	200.2 KB
ID:	1527873
            Last edited by William Lisowski; 07 Dec 2019, 15:03.

            Comment


            • #7
              I am assuming this builds on your previous suggestion to re-save the tstf.ado file after removing the saveold command?

              R is installed on my PC as well as the 5 libraries.

              I reckon the trouble is that the paths on which I have Stata and R installed are not communicating with each other.

              In my tstf command (below) I specified into the rpath argument, the output of running getwd() in R.

              Code:
              tstf lograte if inrange(time, 625, 691), pathr("folder-in-which-R-is-installed") ///
              smooth int(676) arima(1,1,1) sarima(1,0,0,12) ///
              tabulate grdata eform ytitle("Calling rates per 100,000 smokers") xlabel(625(6)691, angle(45)) ///
              xmtick(625(1)691) name(figure2a, replace)


              When I run tstf in Stata with the pathr argument specified I get the same error message as before, and the tstf_to_r.R file appears in the viewer window. The setwd argument specifies the path on which I have Stata installed on my computer. Is it supposed to do that?

              Code:
              setwd("C:/Users/gmoreno/Desktop/000_Stata 12")
              rm(list=ls())
              library(stats)
              library(foreign)
              library(TSA)
              library(car)
              library(aod)
              ...

              Comment


              • #8
                I am assuming this builds on your previous suggestion to re-save the tstf.ado file after removing the saveold command
                To be precise, this builds on my previous suggestion to re-save the tstf.ado file after removing the version(11) option from the saveold command in that ado, since you are running Stata release 12.

                In my tstf command (below) I specified into the rpath argument, the output of running getwd() in R.
                You misunderstand.

                In most cases, and certainly in Stata and R, "wd" is short for "working directory" which is the directory in which you are running your programs, the directory from which files with no path before the name are read and written. Programs are routinely installed in system directories, not in the user's home directories where users keep their documents and data.

                The R function getwd() returns the path to the current working directory for the session of R in which the function is used. Users can change the current working directory using the setwd() function within their session of R.

                What you need is the path to R.exe, wherever it has been installed on your system. I'm no longer a Windows user so I cannot tell you how to find that executable and the path to it if you do not know how to do so.

                Code:
                The setwd argument specifies the path on which I have Stata installed on my computer. Is it supposed to do that?
                If you launch Stata and type the Stata command pwd, that will display your current working directly (like the getwd() function in R.) It will be the path that the R setwd() function will specify in the first line of of the tstf_to_r.R file.

                Usually this is not the directory within which Stata is installed, but then, an authorized installation of Stata will not without special effort install Stata on the desktop.

                Comment


                • #9
                  Okay, I am a bit at a loss here, but here's what I did.

                  I did re-save the tstf.ado file after removing the version(11) option from the saveold command in that ado. To be clear, the saveold command is still there, but "version(11)" has been removed from it.

                  Next, I now located the path to R.exe on my PC and I specified that in my tstf command -

                  Code:
                  tstf lograte if inrange(time, 625, 691), smooth int(676) arima(1,1,1) sarima(1,0,0,12) ///
                  tabulate grdata eform ytitle("Calling rates per 100,000 smokers") xlabel(625(6)691, ///
                  angle(45)) xmtick(625(1)691) name(figure2a, replace) pathr("C:\Program Files\R\R-3.6.1\bin")
                  But the error I obtain is the same -

                  Code:
                  the R script did not run, see what's wrong in the file tstf_to_r.R
                  And the tstf_to_r.R reads as follows -

                  Code:
                  setwd("C:/Users/gmoreno/Desktop/000_Stata 12")
                  rm(list=ls())
                  library(stats)
                  library(foreign)
                  library(TSA)
                  library(car)
                  library(aod)
                  mydata <- read.dta("data_tstf.dta")
                  datatstf <- data.frame(mydata)
                  fit.arimax <- arimax(datatstf[["lograte"]],
                  order=c( 1, 1, 1),
                  seasonal=list(order=c( 1, 0, 0), period=12),
                  include.mean = TRUE,
                  xtransf = datatstf[["IVAR"]], transfer=list(c(1,0)),
                  method = "CSS-ML" )
                  beta = data.frame(names = attr(fit.arimax[["coef"]], "names"), beta = fit.arimax[["coef"]])
                  varcov = data.frame(fit.arimax[["var.coef"]])
                  stats = data.frame(sigma2 = fit.arimax[["sigma2"]], loglik = fit.arimax[["loglik"]],
                  aic = fit.arimax[["aic"]])
                  write.dta(beta, file = "C:/Users/gmoreno/Desktop/000_Stata 12/_tstf_b.dta")
                  write.dta(varcov, file = "C:/Users/gmoreno/Desktop/000_Stata 12/_tstf_V.dta")
                  write.dta(stats, file = "C:/Users/gmoreno/Desktop/000_Stata 12/_tstf_stats.dta")
                  Any ideas on how to proceed?

                  Comment


                  • #10
                    The path name needs to give the full path to the executable, not just the directory.
                    Code:
                    tstf lograte if inrange(time, 625, 691), smooth int(676) arima(1,1,1) sarima(1,0,0,12) ///
                    tabulate grdata eform ytitle("Calling rates per 100,000 smokers") xlabel(625(6)691, ///
                    angle(45)) xmtick(625(1)691) name(figure2a, replace) pathr("C:\Program Files\R\R-3.6.1\bin\R.exe")
                    If again you receive the same error message, issue the following Stata command
                    Code:
                    rsource using tstf_to_r.R , lsource roptions(--slave) pathr("C:\Program Files\R\R-3.6.1\bin\R.exe")
                    noting that for the rsource command the option is pathr() rather than rpath(). This will attempt to run the R command and will display to the Stata Results window what happens, which should perhaps help you figure out what is failing.

                    Comment


                    • #11
                      Specifying the full path to the executable, as you listed above, worked, and I received the same output as you did.

                      Thank you so much for all this trouble shooting! I appreciate the guidance.

                      Comment


                      • #12
                        Hello,

                        I have experienced the same problem usign STATA 16.0 and I followed all the instructions listed above specifying the directory of R, but the problems continue.

                        As you suggest, I used the code: rsource using tstf_to_r.R , lsource roptions(--slave) rpath("C:\Program Files\R\R-4.0.3\bin\R.exe")

                        And I obtained the following output:

                        Assumed R program path: "C:\Program Files\R\R-4.0.3\bin\R.exe"
                        Beginning of listing of R source code
                        setwd("C:/Users/USER/Documents")
                        rm(list=ls())
                        library(stats)
                        library(foreign)
                        library(TSA)
                        library(car)
                        library(aod)
                        mydata <- read.dta("data_tstf.dta")
                        datatstf <- data.frame(mydata)
                        fit.arimax <- arimax(datatstf[["lograte"]],
                        order=c( 1, 1, 1),
                        seasonal=list(order=c( 1, 0, 0), period=12),
                        include.mean = TRUE,
                        xtransf = datatstf[["IVAR"]], transfer=list(c(1,0)),
                        method = "CSS-ML" )
                        beta = data.frame(names = attr(fit.arimax[["coef"]], "names"), beta = fit.arimax[["coef"]])
                        varcov = data.frame(fit.arimax[["var.coef"]])
                        stats = data.frame(sigma2 = fit.arimax[["sigma2"]], loglik = fit.arimax[["loglik"]],
                        aic = fit.arimax[["aic"]])
                        write.dta(beta, file = "C:/Users/USER/Documents/_tstf_b.dta")
                        write.dta(varcov, file = "C:/Users/USER/Documents/_tstf_V.dta")
                        write.dta(stats, file = "C:/Users/USER/Documents/_tstf_stats.dta")
                        End of listing of R source code
                        Beginning of R output
                        End of R output




                        Last edited by Janeth Tenorio; 11 Feb 2021, 13:13.

                        Comment

                        Working...
                        X