10 Combining Stata and R

10.1 Introduction

One of the virtues of processing your dynamic documents through R is that you can use more than one programming language in a single document. Many of us are multi-lingual, and it is often quicker and easier to execute part of a project in one language, while completing your work in another. This is especially common when you are in the process of learning a new language, or if part of your work involves a specialized language with limited capabilities.

10.2 Switching Languages

You tell knitr which language to use for each code chunk. This is specified in the code chunk header.

10.2.1 Using Stata

A regression in Stata.

The code chunk is

```{stata}
sysuse auto
regress mpg weight
```

The result of including it in your document is

sysuse auto
regress mpg weight
(1978 automobile data)

      Source |       SS           df       MS      Number of obs   =        74
-------------+----------------------------------   F(1, 72)        =    134.62
       Model |   1591.9902         1   1591.9902   Prob > F        =    0.0000
    Residual |  851.469256        72  11.8259619   R-squared       =    0.6515
-------------+----------------------------------   Adj R-squared   =    0.6467
       Total |  2443.45946        73  33.4720474   Root MSE        =    3.4389

------------------------------------------------------------------------------
         mpg | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
      weight |  -.0060087   .0005179   -11.60   0.000    -.0070411   -.0049763
       _cons |   39.44028   1.614003    24.44   0.000     36.22283    42.65774
------------------------------------------------------------------------------

10.2.2 Using R

A regression in R, using very similar data!

The code chunk is

```{r cars}
summary(lm(mpg ~ wt, data=mtcars))
```

The result of including it in your document is

summary(lm(mpg ~ wt, data=mtcars))

Call:
lm(formula = mpg ~ wt, data = mtcars)

Residuals:
    Min      1Q  Median      3Q     Max 
-4.5432 -2.3647 -0.1252  1.4096  6.8727 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
wt           -5.3445     0.5591  -9.559 1.29e-10 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 3.046 on 30 degrees of freedom
Multiple R-squared:  0.7528,    Adjusted R-squared:  0.7446 
F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10

(This page was written using Statamarkdown version 0.9.2.)