Category Archives: Stata Programs

  • 4

tabstat with asdoc in Stata

Category:asdoc,Uncategorized

asdoc makes some elegant tables when used with tabstat command. There are several custom-made routines in asdoc that creates clean tables from tabstat command. asdoc fully supports the command structure and options of tabstat. And, yes asdoc allows one additional statistics, that is, t-statistics alongside the allowed statistics in tabstat. For reporting purposes, asdoc categorizes tabstat commands in two groups:

(1) stats without a grouping variable

(2) stats over a grouping variable.

 

Tabstat Without-by

If statistics are less than variables, the table is transposed, i.e. statistics are shown in columns, while variables are shown in rows

 

Example 49 : One variable, many stats, including t-statistics

sysuse auto, clear  
asdoc tabstat price , stat(min max mean sd median p1 p99 tstat) replace 

 

Example 50 : Many variables, one statistic

asdoc tabstat price mpg rep78 headroom trunk weight length foreign , stat( mean) replace

 

Example 51 : Many variables, many statistics

asdoc tabstat price mpg rep78 headroom trunk weight length foreign , /// 
stat( max mean sd median p1 p99 tstat) replace

 

Tabstat with-by

 

Example 52 :


bysort foreign: asdoc tabstat price mpg rep78 headroom trunk weight length, stat(mean) replace


OR

asdoc tabstat price mpg rep78 headroom trunk weight length, ///
stat(mean) by(foreign) replace

 

Example 53 : By with many variables and many statistics

bysort foreign: asdoc tabstat price mpg rep78 headroom trunk weight length, ///
stat(mean sd p1 p99 tstat) replace

 



  • 3

asdoc abbreviates / truncates my variable names and labels | Word to Stata

Category:asdoc

Stephen Okiya has asked the following question

I notice that the variable names are truncated in spite of using the option abb(100). Do you know why this is the case?


Answer:

asdoc uses the abbrev() function of Mata. For some reasons, the abbrev() function splits the following sentence in half, no matter which value we set for the abbreviation.

loc vari " Child's age when she/he was first fed something other than breast milk"

. dis abbrev("`vari'", 32)
 Child's age when she/he was firs

 . dis abbrev("`vari'", 100)
 Child's age when she/he was firs

However, we set the second argument of abbrev() function to missing, then the full sentence is show

. dis abbrev("`vari'", .)

Child's age when she/he was first fed something other than breast milk

Therefore, if we prefer not to abbreviate any name or label, just provide missing value for the abb() option of asdoc. So the following will show all the text

asdoc sum Q85, label abb(.)




  • 0

Reporting odd ratios and Chi2 with asdoc

Category:asdoc,Blog

Richard Makurumidze has asked:It seems asdoc does not work with the chi (chi -square) and or (odds ratio) in logistic regression. Is this correct or am making some error?

Richard is referring to the nest option of asdoc that creates the nested regression tables. Without the nest option, asdoc produces detailed regression tables and exports odds ratio as a default option. However, with nest option, users must explicitly declare that they are interested in the odd ratios. This declaration is done using the eform option. In the following examples, I show how to get odd ratios with both the detailed and the nested regressions.


Reporting the odd ratios

We shall use the example data that is available on the Stata web server. The data can be downloaded by typing the following in the Stata command window.

 
webuse lbw, clear


Getting odd ratios in the detailed regression tables

 asdoc logistic low age lwt i.race smoke ptl ht ui, replace 



Getting odd ratios in the nested regression tables

 asdoc logistic low age lwt i.race smoke ptl ht ui, replace nest eform

This is how the output looks like.


Reporting the Chi2

Richard’s second querry is related to reporting the Chi2 test value. Since asdoc tries to find the r-squared values in regression commands, it is possible that this value is not available in some commands such as in the case of the logisitc regression. Users can add additional statistics to the regression table by using the option add(). There is a detailed discussion on this option in the asdoc help file, which we can access by typing:

help asdoc

Below, I show how we can use this option for reporting the Chi2 test value. Please note that Stata regression commands leaves behind several statistics in the e() macro which we can report with asdoc.

 asdoc logistic low age lwt i.race smoke  ui, replace nest add(Chi2, `e(chi2)')

* Add another regression

asdoc logistic low age lwt i.race smoke ptl ht ui, nest add(Chi2, `e(chi2)')

Explanation

Option add() has two elements – the text Chi2 and the macro `e(chi2)’. These two elements are separated by the comma. This is how option add works. The inputs of option add() should be added in pairs of two, each one separated by a comma.


  • 0

asdoc version 2.3.3 : New Features

Category:asdoc,Stata Programs Tags : 

Version 2.3.3.2, dated Feb 23, 2019, of asdoc bring significant improvements to existing routines and introduces few new features. Details are given below. If you have not used asdoc previously, I would encourage you to read this half page quick start to asdoc.


New Features


1.Font style

2. Formatting the header row and header column

3. Revamped the tabulation commands

4. Revamped the table command

5. Extending the detailed regression tables [ Read further details here]

  • 5.1 Added confidence intervals to the detailed regression tables
  • 5.2 Added an option for customizing the significance starts
  • 5.3 Added an option for suppressing significance stars
  • 5.4 Added an option for suppressing confidence intervals

6. Adding support for macOS

6. Improving the output from proportion command

7. Support added for logistic family of regressions

8. Improving table outputs of non-standard outputs i.e. multilevel models

9. eform() option added to nested tables

Detailed discussion and examples are provided in the help file accompanying the new version of asdoc. However, I would like to discuss the first two features in some details below.


1. Setting font style

The default font style is Garamond in the latest version of asdoc. Option font(font_name) can be used to change the font face to any desired font style. In the brackets, we have to write the full name of the font, which is currently installed in the operating system. For example, to set the font face to Arial, we shall type: 

font(Arial)

To produce summary statistics in Times New Roman font, let us use the auto dataset from the system directory

sysuse auto, clear
asdoc sum, font(Times New Roman) replace

Please note that the font() option can be used only at the start of the document. Therefore, it cannot change from table to table when using option append of asdoc.


2. Formatting table headers

In this new version of asdoc, we can easily pass RTF formatting control words to the header row and header columns of the ouput tables. For this purpose, option fhr() is used to format the row headers, i.e. the data given in the first column of each row. Similarly, option fhc() is used to format the column headers, i.e., the data given in the top cells of each column. Both the fhr() and fhc() will pass RTF control words to the final document. See the
following examples.

------------------------------------------------------------
Objective                                    Code to use
------------------------------------------------------------
Format column headers as bold               fhc(\b)
Format column headers as italic             fhc(\i)
Format column headers as bold and italic    fhc(\b \i)
Format row headers as bold                  fhr(\b)
Format row headers as italic                fhr(\b)
Format row headers as bold and italic       fhr(\b \i)
------------------------------------------------------------

So to make a table of descriptive statistics with column headers in bold and row headers in italic font, the code would be:

sysuse auto, clear
asdoc sum, fhr(\i) fhc(\b) replace

  • 1

Getting p-values and t-values with asreg

Category:Blog,Stata Programs

Xi asked the following quesiton:

How can I get p-values and t-values using asreg program?

Introduction to asreg

asreg is a Stata program, written by Dr. Attaullah Shah. The program is available for free and can be downloaded from SSC by typing the following on the Stata command window:

ssc install asreg

asreg was primarily written for rolling/moving / sliding window regressions. However, with the passage of time, several useful ideas were conceived by its creator and users. Therefore, more features were added to this program. 



Getting t-values after asreg

Consider the following example where we use the grunfeld dataset from the Stata web server. The dataset has 20 companies and 20 years of data for each company. We shall estimate the following regression model where the dependent variable is invest and independent variables are mvalue and kstock. Let’s estimate the regression model separately for each company, denoted by i .

In the following lines of code, the letters se after comma causes asreg to report the standard errors for each regression coefficient.

webuse grunfeld, clear
tsset
bys company: asreg invest mvalue kstock, se

asreg generates the regression coefficients, r-squared, adjusted r-squared, number of observations (_Nobs) and standard errors for each coefficient. This is enough information for producing t-values and p-values for the regression coefficients. The t-values can be generated by:

 
gen t_values_Cons = _b_cons / _se_cons
gen t_values_mvalue = _b_mvalue / _se_mvalue
gen t_values_kstock = _b_kstock / _se_kstock



Getting p-values after asreg

Getting p-values is just one step away. We need one additional bit of information from the regression estimates, that is the degrees of freedom. This is usually equal to the number of observations minus the number of parameters being estimated. Since we have two independent variables and one constant, the number of parameters being estimated are 3. asreg returns the number of observation in the variable _Nobs. Therefore, the term _Nobs – 3 in the following lines of code is a way to get the degrees of freedom.


gen p_values_Cons = (2 * ttail(_Nobs-3), abs( _b_cons / _se_cons ))
gen p_values_mvalue = (2 * ttail(_Nobs-3), abs( _b_mvalue / _se_mvalue ))
gen p_values_kstock = (2 * ttail(_Nobs-3), abs( _b_kstock / _se_kstock ))



Verify the Results

Let’s estimate a regression for the first company and compare our estimates with those produced by the Stata regress command.

 reg invest mvalue kstock if company == 1
list _b_cons _se_cons p_values_Cons in 1



Full Code

 webuse grunfeld, clear
tsset
bys company: asreg invest mvalue kstock, se
gen t_values_Cons = _b_cons / _se_cons
gen t_values_mvalue = _b_mvalue / _se_mvalue
gen t_values_kstock = _b_kstock / _se_kstock
gen p_values_Cons = (2 * ttail(_Nobs-3), abs( _b_cons / _se_cons ))
gen p_values_mvalue = (2 * ttail(_Nobs-3), abs( _b_mvalue / _se_mvalue ))
gen p_values_kstock = (2 * ttail(_Nobs-3), abs( _b_kstock / _se_kstock ))
reg invest mvalue kstock if company == 1
list _b_cons _se_cons t_values_Cons p_values_Cons in 1


  • 8

Ordering variables in a nested regression table of asdoc in Stata

Category:asdoc,Blog Tags : 

In this blog entry, I shall highlight one important, yet less known, feature of the option keep() in nested regression tables of asdoc. If you have not used asdoc previously, this half-page introduction will put on fast track. And for a quick start of regression tables with asdoc, you can also watch this YouTube video.

 

Option keep()

There are almost a dozen options in controlling the output of a regression table in asdoc. One of them is the option keep(list of variable names). This option is primarily used for reporting coefficient of the desired variables. However, this option can also be used for changing the order of the variables in the output table. I explore these with relevant examples below.

 

1. Changing the order of variables

Suppose we want to report our regression variables in a specific order, we shall use option keep() and list the variable names in the desired order inside the brackets of option keep(). It is important to note that we have to list all variables which we want to report as omitting any variable from the list will cause asdoc to omit that variable from the output table.

 

An example

Let us use the auto dataset from the system folder and estimate two regressions. As with any other Stata command, we need to add asdoc to the beginning of the command line. We shall nest these regressions in one table, hence we need to use the option nest. Also, we shall use option replace in the first regression to replace any existing output file in the current directory. Let’s say we want to variables to appear in this order in the output file _cons trunk weight turn. Therefore, the variables are listed in this order inside the keep() option. The code and output file are shown below.

sysuse auto, clear
asdoc reg mpg turn, nest replace
asdoc reg mpg turn weight trunk, nest keep(_cons trunk weight turn)

 

 

2. Reporting only needed variables

Option keep is also used for reporting only needed variables, for example, we might not be interested in reporting coefficients of year or industry dummies. In such cases, we shall list the desired variable names inside the brackets of the keep() option. In the above example, if we wish to report only _cons trunk weight , we would just skip the variable turn from the keep option. Again, the variables will be listed in the order in which they are listed inside the keep option.  

sysuse auto, clear
asdoc reg mpg turn, nest replace
asdoc reg mpg turn weight trunk, nest keep(_cons trunk weight)

 

 

Off course, we could also have used option drop(turn) instead of option keep(_cons trunk weight) for dropping variable turn from the output table.

 


 


  • 17

Exporting ttest results from Stata to Word using asdoc

Category:asdoc,Blog,Stata Programs,Uncategorized

 

asdoc installation

If you have not already studied the features of asdoc, you can visit this page that lists the table of contents of what asdoc can do. You can also read this one paragraph introduction to asdoc. The following line of code will install asdoc from SSC

ssc install asdoc
help asdoc

 

Reporting t-tests with asdoc

Before we make the t-test results table for our example data, let us breifly explore the options available in asdoc for making a t-test results table.

Whether it is one-sample t-test or two-sample or other forms, asdoc manages to report the results line by line for each test. asdoc also allows accumulating results from different runs of t-tests. For this purpose, the option rowappend of asdoc really comes handy. With the sub-command ttest , we can use the following options of asdoc to control asdoc behavior.

(1) replace / append

(2) save(filename)

(3) title(text)

(4) fs(#)

(5) hide.

(6) stats()

(7) rowappend.

These options are discussed in detail in Section 1 of asdoc help file. Option stats and rowappend are discussed below:

 

Option stat()

Without stat() option, asdoc reports the number of observations (obs), mean, standard error, t-value, and p-value with t-tests. However, we can select all or few statistics using the stat option. The following table lists the keywords and their details for reporting the desired statistics.

keyword details
n Number of observations
mean Arithmetic mean
se Standard error
df degrees of freedom
obs Number of observations
t t-value
p p-value
sd standard deviation
dif difference in means if two-sample t-test

 

Option rowappned

ttest tables can be constructed in steps by adding results of different t-tests to an existing table one by one using option rowappend. There is only one limitation that the t-tests are performed and asdoc command applied without writing any other results to the file in-between.

 

An example

Suppose we have the following data set with variables r0, r1, r2, r3, and y. The data can be downloaded into Stata by

use http://fintechprofessor.com/ttests.dta, clear

The variables ro-r3 are the numeric variables for which we would like to conduct one-sample ttest whereas variable y is a numeric date variable that tracks years. We wish to conduct a ttest for each of the r0-r3 variables and in each year and make one table from all such tests.

 

Without using a loop

 asdoc ttest R0==0 if Y==2009, replace title(One Sample t-test Results)
asdoc ttest R1==0 if Y==2009, rowappend
asdoc ttest R2==0 if Y==2009, rowappend
asdoc ttest R3==0 if Y==2009, rowappend
asdoc ttest R0==0 if Y==2010, rowappend
asdoc ttest R1==0 if Y==2010, rowappend
asdoc ttest R2==0 if Y==2010, rowappend
asdoc ttest R3==0 if Y==2010, rowappend
asdoc ttest R0==0 if Y==2011, rowappend
asdoc ttest R1==0 if Y==2011, rowappend
asdoc ttest R2==0 if Y==2011, rowappend
asdoc ttest R3==0 if Y==2011, rowappend
asdoc ttest R0==0 if Y==2012, rowappend
asdoc ttest R1==0 if Y==2012, rowappend
asdoc ttest R2==0 if Y==2012, rowappend
asdoc ttest R3==0 if Y==2012, rowappend

And appreciate the results

 

Explanation

1.In the first line of code, we wrote asdoc ttest in the beggining of the line. This is how we use asdoc with Stata commands. We just add asdoc to the beggining of any Stata command and that’s all.

2. We used two options of asdoc in the first line of code: the replace and title(). Replace will replace any existing file with the name Myfile.doc and title will add the specific test as a title to the output file.

3. In the second line of code, we added option rowappend() that will append the results to the existing table in the file Myfile.doc

4. And the process continues untill all ttests are estimated.

 



  • 17

asdoc: Cutomizing the regression output | MS Word from Stata | Confidence Interval, adding stars, etc.

Category:asdoc,Blog,Uncategorized Tags : 

 

Version 2.3 of asdoc adds the following features for reporting detailed regression tables.

1. Reporting confidence interval

2. Suppressing confidence intervals

3. Suppressing the stars which are used to show significance level

4. Customization of significance level for stars

These features are discussed in details below. If you have not already studied the features of asdoc, you can visit this page that lists the table of contents of what asdoc can do. You can also read this one paragraph introduction to asdoc. The following line of code will install this beta version of asdoc from our website

net install asdoc, from(http://fintechprofessor.com) replace
help asdoc

   

Details of the new features

The new features related to creating detailed regression tables with asdoc are discussed below with details. 

 

1. Confidence interval

I received several emails and comments on blog posts suggesting the addition of confidence intervals (CI) to the detailed regression tables created by asdoc. In version 2.3 onwards, confidence intervals are shown by default. This means that we do not have to add an additional option to report CI. See the following example. 

sysuse auto, clear
asdoc reg price mpg rep78 headroom trunk weight length turn , replace

 

 

2. Suppressing the confidence interval

If confidence intervals are not needed, we can use option noci. For example

asdoc reg price mpg rep78 headroom trunk weight length turn , replace noci

 

3. Suppressing stars

Similarly, if we are not interested in reporting significance stars, we can use option nostars. For example, 

 

4. Setting custom significance level

The default significance levels for reporting stars are set at : *** for p-values <=0.01; ** for p-values <=0 .05, and * for p-values <=0.1. However, now we can set our own levels for statistical significance using option setstars. An example of setstars option looks like:

setstars(***@.01, **@.05, *@.1)

As we can see from the above line, setstars separates each argument by a comma. Each argument has three components. The first component is the symbol (in our case it is *) which will be reported for the given significance elve. The second component is the @ sign that connects the significance level with the symbol. And the third component is the value at which the significance level is set. So if we want to report stars such that

* for p-value .001 or less
** for p-value .01 or less
*** for p-value .05 or less

We shall write the option setstars as

setstars(*@.001, **@.01, ***@.05)

Continuing with our example, let us use the above option to report our defined level of stars.

asdoc reg price mpg rep78 headroom trunk weight length turn , replace setstars(*@.001, **@.01, ***@.05)

 

 


  • 6

asdoc: Exporting customized descriptive statistics from Stata to MS Word / RTF

Category:asdoc,Blog

Osama Mahmood has asked : 

If I want to report 25th and 75th percentiles for variables through asdoc, then how would I do that? And what if I do not want to report the Min and Max?

Answer: In this YouTube video, I have shown various methods in which descriptive statistics can be reported using asdoc. What Osama has asked for is possible with the customized descriptive statistics using the stat() option of asdoc. Using option stat(), we can choose from the following statistics. Each of the bold words in the following list represents the control word that can be used to report the required statistic.

N Number of observations
mean Arithmetic mean
sd Standard deviation
semean Stanard error of the mean
sum Sum / total
range Range
min The smallest value
max The largest value
count Counts the number of non-missing observations
var Variance
cv Coefficient of variation
skewness Skewness
kurtosis Kurtosis
iqr Interquartile range
p1 1st percentile
p5 5th percentile
p10 10th percentile
p25 25th percentile
p50 Median or the 50 percentile
p75 75th percentile
p99 99th percentile
tstat t-statistics that the given variable == 0

   

Example 1: Mean, sd, 25th percentile, median, and 75th percentiles

 sysuse auto
asdoc sum, stat(mean sd p25 p50 p75) replace

   

Example 2: Mean, sd, 25th percentile, median, and 75th percentiles, range, t-statistics

 asdoc sum, stat(mean sd p25 p50 p75 range tstat) replace


  • 2

asdoc: Export matrix to MS Word | the case of xttab command in Stata

Category:asdoc,Blog

asdoc provides a variety of ways in which results from various Stata commands can be exported to MS Word or an RTF file. In this blog post,  I show how to export a Stata matrix to MS word. Usually, Stata commands leave results in r() or e() macros and sometimes in a Stata matrix. Consider the example of xttab command.  xttab is a generalization of tabulate oneway. It performs one-way tabulations and decomposes counts into between and within components in panel data. The command returns results in the r(results) matrix which we can then send to MS word.

 

The syntax

asdoc follows the following syntax for exporting matrix to a word document.

asdoc wmat, matrix(matrix_name) [rnames(row names) cnames(row names) replace append other_options]

 

Description

wmat is the command name – an abbreviation for writing matrix. Option matrix() is a required option to get the name of an existing matrix. Option rnames() and cnames() are optional options to specify row names and column names of the matrix. If these options are left blank, existing row and column names of the matrix are used. Other options of asdoc can also be used with wmat. For example, replace will replace an existing output file, while append will append to the existing file. fs() sets the font size, while option title() can be used to specify the title of the matrix in the output file.

An example: The case of xttab command

The dataset that we shall use is from the help file of xttab.

webuse nlswork
xtset id year
xttab race
mat T = r(results)
asdoc wmat, mat(T) replace

 

Explanation

1. The first line downloads the example data

2. The second line declares the data as panel data

3. The third line tabulates the race variable

4. The fourth line creates a matrix with the name T from the xttab command

5. The fifth line writes the T matrix to a Word file. wmat is a sub-command in asdoc for writing matrix data to the output file. The two words after command are options of asdoc. The first option tells asdoc about the name of the matrix that has to be exported. The second option tells asdoc to replace any existing output file.

asdoc produces the following Table.

Results Table

 value

 Overall:Freq

 Overall:Pe~t

 Between:Freq

 Between:Pe~t

 Within:Per~t

r1

1

20180

70.723

3329

70.664

r2

2

8051

28.215

1325

28.126

r3

3

303

1.062

57

1.21

r4

3

28534

100

4711

100

 

Over a grouping variable?

If we wished to do the above for each category of the grouping variable msp, that has two categories i.e., 0 and 1, we can use the if qualifier and append the results to the same file. So

xttab race if msp == 1
mat T = r(results)
asdoc wmat, mat(T) replace title(When msp == 1)
xttab race if msp == 0
mat T = r(results)
asdoc wmat, mat(T) title(When msp == 0)

 

When msp == 1

 

 value

 Overall:Freq

 Overall:Pe~t

 Between:Freq

 Between:Pe~t

 Within:Per~t

r1

1

13321

77.475

2747

75.405

100

r2

2

3682

21.414

853

23.415

100

r3

3

191

1.111

43

1.18

100

r4

3

17194

100

3643

100

100

 


When msp == 0

 

 value

 Overall:Freq

 Overall:Pe~t

 Between:Freq

 Between:Pe~t

 Within:Per~t

r1

1

6853

60.517

2046

65.724

100

r2

2

4359

38.493

1036

33.28

100

r3

3

112

.989

31

.996

100

r4

3

11324

100

3113

100

100