Making interactive presentations in Stata

Maarten L. Buis University of Konstanz

    Example presentation: stdtable, a package to standardize tables

        Standardized table

           The influence of marginal distributions

               Odds ratios

           Standardizing tables

           Displaying results from stdtable

        Iterative Proportional Fitting

           Making all the row totals 100

           Making all the colum totals 100

           Repeat

           Can all tables be standardized?(ancillary)

    How to create a smcl presentation

        creating the presentation

           Getting started

           Adding text

           Different kinds of slides

           Changing the look of the table of contents

        creating a handout

           Initial handout

           Adding graphs to the handout

           Adding contents or output of a .do file to the handout

-------------------------------------------------------------------------------
Example presentation: stdtable, a package to standardize tables -- Standardized
table
-------------------------------------------------------------------------------

The influence of marginal distributions

Consider the example below

It shows the race of the husband and the race of the wife for couples living in the USA that got married between 2010 and 2016

The races are very unequaly distributed in the USA

We can control for one marginal distribution by computing row or collumn percentages.

. use homogamy, clear (American Community Survey 2008 - 2016)

. tab racem racef [fw=freq] if marcoh == 2010, row nofreq

| race wife race husband | white, no white, hi black native am | Total -----------------+--------------------------------------------+---------- white, non-hisp. | 90.88 4.94 1.09 0.47 | 100.00 white, hisp. | 20.28 76.00 1.46 0.54 | 100.00 black | 15.70 4.77 77.05 0.54 | 100.00 native amer. | 43.36 8.50 2.11 43.45 | 100.00 asian | 12.95 2.77 0.74 0.14 | 100.00 -----------------+--------------------------------------------+---------- Total | 67.34 15.75 9.27 0.78 | 100.00

| race wife race husband | asian | Total -----------------+-----------+---------- white, non-hisp. | 2.62 | 100.00 white, hisp. | 1.72 | 100.00 black | 1.94 | 100.00 native amer. | 2.58 | 100.00 asian | 83.40 | 100.00 -----------------+-----------+---------- Total | 6.87 | 100.00

    We see racial homogamy: people tend to marry someone of the same race

The exception appears to be the native Americans: only 43% of native American men married native American women and 43% married a non-hispanic white women.

However, there are many more white women around than native american women (67% versus 0.8%).

If native Americans were mixing around randomly, then we would expect much more native American men marrying white women.

Apperently native American men still prefer native American women but because there are so many more white women around he will still have a good chance of eventually marrying a white women.

There is something mechanical about this, and it is common (in sociology) to want to look at the pattern in the table nett of this influence of the marginal distributions.

We could do this by looking at >> odds ratios , but here I want to show an alternative.

-------------------------------------------------------------------------------

index >>

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Example presentation: stdtable, a package to standardize tables -- Standardized
table
-------------------------------------------------------------------------------

Standardizing tables

When we do a chi squared test for cross-tabulations we compare observed cell counts with predicted cell counts

For these predicted cell counts we assume that the margins remain as observed, but otherwise there is no association between the variables (the odds ratios are all 1).

In the table of predicted cell counts the only pattern is due to the margins.

. tab racem racef [fw=freq] if marcoh==2010, exp

+--------------------+ | Key | |--------------------| | frequency | | expected frequency | +--------------------+

| race wife race husband | white, no white, hi black native am | Total -----------------+--------------------------------------------+---------- white, non-hisp. |11,842,951 643,588 142,322 61,383 |13,031,939 | 8775254.4 2052706.4 1207519.6 101,031.0 |13031939.0 -----------------+--------------------------------------------+---------- white, hisp. | 600,321 2,249,673 43,114 16,003 | 2,960,046 | 1993192.0 466,247.2 274,273.3 22,948.0 | 2960046.0 -----------------+--------------------------------------------+---------- black | 323,559 98,197 1,587,673 11,102 | 2,060,467 | 1387446.8 324,551.4 190,919.7 15,973.9 | 2060467.0 -----------------+--------------------------------------------+---------- native amer. | 59,096 11,584 2,879 59,227 | 136,297 | 91,777.7 21,468.6 12,629.1 1,056.7 | 136,297.0 -----------------+--------------------------------------------+---------- asian | 137,657 29,398 7,867 1,537 | 1,063,187 | 715,913.1 167,466.3 98,513.3 8,242.4 | 1063187.0 -----------------+--------------------------------------------+---------- Total |12,963,584 3,032,440 1,783,855 149,252 |19,251,936 |12963584.0 3032440.0 1783855.0 149,252.0 |19251936.0

| race wife race husband | asian | Total -----------------+-----------+---------- white, non-hisp. | 341,695 |13,031,939 | 895,427.6 |13031939.0 -----------------+-----------+---------- white, hisp. | 50,935 | 2,960,046 | 203,385.4 | 2960046.0 -----------------+-----------+---------- black | 39,936 | 2,060,467 | 141,575.2 | 2060467.0 -----------------+-----------+---------- native amer. | 3,511 | 136,297 | 9,365.0 | 136,297.0 -----------------+-----------+---------- asian | 886,728 | 1,063,187 | 73,051.8 | 1063187.0 -----------------+-----------+---------- Total | 1,322,805 |19,251,936 | 1322805.0 |19251936.0

    Can't we reverse that? Keep the association as observed but fix the
    margins.

For example we could fix all margins at a 100.

That way we can look at the proportion native American men marrying native American women when they weren't such a small group.

This is what the stdtable package does.

. stdtable racem racef [fw=freq] if marcoh == 2010, cellwidth(9)

------------------------------------------------------------------------------ | race wife race husband | white, no white, hi black native am asian Total -----------------+------------------------------------------------------------ white, non-hisp. | 75.1 9.58 4.15 5.43 5.73 100 white, hisp. | 9.33 82 3.08 3.47 2.09 100 black | 3.99 2.84 90 1.91 1.3 100 native amer. | 6.32 2.91 1.42 88.4 .993 100 asian | 5.27 2.64 1.39 .822 89.9 100 | Total | 100 100 100 100 100 500 ------------------------------------------------------------------------------

    To illustrate what keeping the association as observed means we can look
    at the odds ratios in the original table and in the standardized table

Below we look at the odds ratio comparing the odds of marrying a native american women versus a white women when one is a native american or a white man.

This odds ratio in the original data is the same as that odds ratio in the standardized table

. stdtable racem racef [fw=freq] if marcoh == 2010, /// > raw format(%10.6g) cellwidth(10)

----------------------------------------------------------------------------- | race wife race husband | white, non white, his black native ame asian -----------------+----------------------------------------------------------- white, non-hisp. | 75.1003 9.58034 4.152 5.43302 5.73431 | 11842951 643588 142322 61383 341695 | white, hisp. | 9.325 82.0306 3.08096 3.46959 2.09383 | 600321 2249673 43114 16003 50935 | black | 3.98532 2.83922 89.9651 1.90863 1.30177 | 323559 98197 1587673 11102 39936 | native amer. | 6.31711 2.90676 1.41581 88.3671 .993234 | 59096 11584 2879 59227 3511 | asian | 5.27224 2.64305 1.38615 .82164 89.8768 | 137657 29398 7867 1537 886728 | Total | 100 100 100 100 100 | 12963584 3032440 1783855 149252 1322805 -----------------------------------------------------------------------------

----------------------------- | race wife race husband | Total -----------------+----------- white, non-hisp. | 100 | 13031939 | white, hisp. | 100 | 2960046 | black | 100 | 2060467 | native amer. | 100 | 136297 | asian | 99.9999 | 1063187 | Total | 500 | 19251936 -----------------------------

. di (59227/59096)/(61383/11842951) 193.36304

. di (88.3671/ 6.31711)/(5.43302/ 75.1003) 193.36262

-------------------------------------------------------------------------------

<< index >>

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Example presentation: stdtable, a package to standardize tables -- Standardized
table
-------------------------------------------------------------------------------

Displaying results from stdtable

We can add options to make the table look better

. stdtable racem racef [fw=freq] if marcoh == 2010, /// > cellwidth(9) format(%5.0f)

------------------------------------------------------------------------------ | race wife race husband | white, no white, hi black native am asian Total -----------------+------------------------------------------------------------ white, non-hisp. | 75 10 4 5 6 100 white, hisp. | 9 82 3 3 2 100 black | 4 3 90 2 1 100 native amer. | 6 3 1 88 1 100 asian | 5 3 1 1 90 100 | Total | 100 100 100 100 100 500 ------------------------------------------------------------------------------

    Alternatively we can replace the data in memory with standardized counts
    and use those to create a graph

. stdtable racem racef [fw=freq], /// > cellwidth(9) format(%5.0f) /// > by(marcoh) replace

------------------------------------------------------------------------------ year last | marriage and | race wife race husband | white, no white, hi black native am asian Total -----------------+------------------------------------------------------------ 1960-1969 | white, non-hisp. | 90 3 0 5 2 100 white, hisp. | 3 94 1 2 1 100 black | 0 0 98 0 0 100 native amer. | 5 2 1 93 0 100 asian | 2 1 0 0 97 100 | Total | 100 100 100 100 100 500 -----------------+------------------------------------------------------------ 1970-1979 | white, non-hisp. | 88 4 1 5 2 100 white, hisp. | 4 93 1 2 1 100 black | 1 1 97 1 0 100 native amer. | 5 2 1 92 0 100 asian | 2 1 0 0 96 100 | Total | 100 100 100 100 100 500 -----------------+------------------------------------------------------------ 1980-1989 | white, non-hisp. | 86 5 1 5 3 100 white, hisp. | 5 91 1 2 1 100 black | 1 1 96 1 1 100 native amer. | 5 2 1 92 1 100 asian | 3 1 1 1 95 100 | Total | 100 100 100 100 100 500 -----------------+------------------------------------------------------------ 1990-1999 | white, non-hisp. | 83 6 2 5 4 100 white, hisp. | 6 89 1 2 1 100 black | 2 1 95 1 1 100 native amer. | 6 2 1 90 1 100 asian | 3 1 1 1 94 100 | Total | 100 100 100 100 100 500 -----------------+------------------------------------------------------------ 2000-2009 | white, non-hisp. | 78 8 3 6 5 100 white, hisp. | 8 85 2 3 2 100 black | 3 2 92 1 1 100 native amer. | 7 3 1 89 1 100 asian | 4 2 1 1 91 100 | Total | 100 100 100 100 100 500 -----------------+------------------------------------------------------------ 2010-2016 | white, non-hisp. | 75 10 4 5 6 100 white, hisp. | 9 82 3 3 2 100 black | 4 3 90 2 1 100 native amer. | 6 3 1 88 1 100 asian | 5 3 1 1 90 100 | Total | 100 100 100 100 100 500 ------------------------------------------------------------------------------

. . tabplot racem marcoh [iw=std], /// > by(racef, compact note("") cols(5)) /// > xlab( 1(1)6, angle(35) labsize(vsmall) ) /// > showval( format(%5.0f))

-------------------------------------------------------------------------------

<< index >>

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Example presentation: stdtable, a package to standardize tables -- Iterative
Proportional Fitting
-------------------------------------------------------------------------------

Making all the row totals 100

stdtable standardizes a table using Iterative Proportional Fitting

To illustrate how that works we will do a few iterations in Mata

Notice, that I added the option matcell(data) to the tab command. This leaves behind the table as a Stata matrix named data, which in turn can be read into Mata

. use homogamy, clear (American Community Survey 2008 - 2016)

. tab racem racef [fw=freq] if marcoh==2010, matcell(data)

| race wife race husband | white, no white, hi black native am | Total -----------------+--------------------------------------------+---------- white, non-hisp. |11,842,951 643,588 142,322 61,383 |13,031,939 white, hisp. | 600,321 2,249,673 43,114 16,003 | 2,960,046 black | 323,559 98,197 1,587,673 11,102 | 2,060,467 native amer. | 59,096 11,584 2,879 59,227 | 136,297 asian | 137,657 29,398 7,867 1,537 | 1,063,187 -----------------+--------------------------------------------+---------- Total |12,963,584 3,032,440 1,783,855 149,252 |19,251,936

| race wife race husband | asian | Total -----------------+-----------+---------- white, non-hisp. | 341,695 |13,031,939 white, hisp. | 50,935 | 2,960,046 black | 39,936 | 2,060,467 native amer. | 3,511 | 136,297 asian | 886,728 | 1,063,187 -----------------+-----------+---------- Total | 1,322,805 |19,251,936

. mata ------------------------------------------------- mata (type end to exit) ----- : data = st_matrix("data")

: data 1 2 3 4 5 +--------------------------------------------------------+ 1 | 11842951 643588 142322 61383 341695 | 2 | 600321 2249673 43114 16003 50935 | 3 | 323559 98197 1587673 11102 39936 | 4 | 59096 11584 2879 59227 3511 | 5 | 137657 29398 7867 1537 886728 | +--------------------------------------------------------+

: end -------------------------------------------------------------------------------

    If we divide all cell entries by the rowsum, then the new rowsum will be
    1.

Multiply the new cell entries by a 100, and the rowsum will be a 100.

. mata ------------------------------------------------- mata (type end to exit) ----- : muhat = data

: : muhat = muhat:/rowsum(muhat):*100

: muhat 1 2 3 4 5 +-----------------------------------------------------------------------+ 1 | 90.87635386 4.938543681 1.09210149 .4710197001 2.621981272 | 2 | 20.28079969 76.00128512 1.456531419 .5406334902 1.720750286 | 3 | 15.70318768 4.765764266 77.05403678 .5388098912 1.938201388 | 4 | 43.3582544 8.499086554 2.112298877 43.45436803 2.575992135 | 5 | 12.94758119 2.765082718 .7399450896 .1445653493 83.40282566 | +-----------------------------------------------------------------------+

: rowsum(muhat) 1 +-------+ 1 | 100 | 2 | 100 | 3 | 100 | 4 | 100 | 5 | 100 | +-------+

: colsum(muhat) 1 2 3 4 5 +-----------------------------------------------------------------------+ 1 | 183.1661768 96.96976233 82.45491365 45.14939647 92.25975074 | +-----------------------------------------------------------------------+

: end -------------------------------------------------------------------------------

-------------------------------------------------------------------------------

<< index >>

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Example presentation: stdtable, a package to standardize tables -- Iterative
Proportional Fitting
-------------------------------------------------------------------------------

Making all the colum totals 100

The row totals are as we want them, but the column totals are not. What if we repeat this process for the columns?

. mata ------------------------------------------------- mata (type end to exit) ----- : muhat = muhat:/colsum(muhat):*100

: muhat 1 2 3 4 5 +-----------------------------------------------------------------------+ 1 | 49.61415663 5.092869738 1.324483213 1.043246947 2.84195573 | 2 | 11.07234973 78.37627244 1.766458 1.197432374 1.865114822 | 3 | 8.573191814 4.914691087 93.44990294 1.193393342 2.100809262 | 4 | 23.67153978 8.7646771 2.561762281 96.24573402 2.792108275 | 5 | 7.068762046 2.851489631 .8973935656 .3201933151 90.40001191 | +-----------------------------------------------------------------------+

: rowsum(muhat) 1 +---------------+ 1 | 59.91671226 | 2 | 94.27762737 | 3 | 110.2319884 | 4 | 134.0358215 | 5 | 101.5378505 | +---------------+

: colsum(muhat) 1 2 3 4 5 +-------------------------------+ 1 | 100 100 100 100 100 | +-------------------------------+

: end -------------------------------------------------------------------------------

    Now the column totals are as we want them, but now the row totals are a
    bit off.

However, the row totals are better than in the original table, so maybe we need to repeat this process a couple of times?

-------------------------------------------------------------------------------

<< index >>

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Example presentation: stdtable, a package to standardize tables -- Iterative
Proportional Fitting
-------------------------------------------------------------------------------

Repeat

. mata ------------------------------------------------- mata (type end to exit) ----- : muhat = muhat:/rowsum(muhat):*100

: muhat 1 2 3 4 5 +-----------------------------------------------------------------------+ 1 | 82.80520536 8.499915209 2.210540538 1.741161869 4.743177025 | 2 | 11.74440855 83.13347994 1.873676767 1.270112971 1.978321765 | 3 | 7.777408296 4.458498078 84.77566654 1.082619808 1.905807281 | 4 | 17.66060708 6.539055758 1.911251972 71.80597916 2.083106027 | 5 | 6.961701487 2.808302143 .8838020122 .315343799 89.03085056 | +-----------------------------------------------------------------------+

: rowsum(muhat) 1 +-------+ 1 | 100 | 2 | 100 | 3 | 100 | 4 | 100 | 5 | 100 | +-------+

: colsum(muhat) 1 2 3 4 5 +-----------------------------------------------------------------------+ 1 | 126.9493308 105.4392511 91.65493783 76.21521761 99.74126266 | +-----------------------------------------------------------------------+

: : muhat = muhat:/colsum(muhat):*100

: muhat 1 2 3 4 5 +-----------------------------------------------------------------------+ 1 | 65.22697272 8.061433591 2.411807362 2.284533094 4.75548123 | 2 | 9.251256767 78.84490742 2.044272585 1.666482116 1.9834537 | 3 | 6.126387786 4.228499378 92.49438006 1.420477225 1.910751108 | 4 | 13.91154012 6.201728187 2.085268964 94.21475319 2.088509782 | 5 | 5.483842604 2.66343142 .9642710291 .4137543772 89.26180418 | +-----------------------------------------------------------------------+

: rowsum(muhat) 1 +---------------+ 1 | 82.740228 | 2 | 93.79037259 | 3 | 106.1804956 | 4 | 118.5018002 | 5 | 98.78710361 | +---------------+

: colsum(muhat) 1 2 3 4 5 +-------------------------------+ 1 | 100 100 100 100 100 | +-------------------------------+

: end -------------------------------------------------------------------------------

    Notice that each time we get a bit closer to our goal

Iterative Proportional Fitting continues till convergence.

-------------------------------------------------------------------------------

<< index >>

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
How to create a smcl presentation -- creating the presentation
-------------------------------------------------------------------------------

Getting started

I typically start with creating the examples.

I typically do this in a single .do file and use comments to structure my ideas

Here is the .do file for the example presentation

// show row percentages (esp. nat. amer.) to illustrate influence of margins 
use homogamy, clear
tab racem racef [fw=freq] if marcoh == 2010, row nofreq

// considerable OR nat. amer. wife v. white whive for nativ. amer. or white husband
// digression?
tab racem racef [fw=freq] if marcoh == 2010
di 59227 / 59096
di 61383 / 11842951
di (59227 / 59096)/(61383 / 11842951)

// expected counts
tab racem racef [fw=freq] if marcoh==2010, exp

// standardized table
stdtable racem racef [fw=freq] if marcoh == 2010 

// show that ORs in standardized and raw data are equal
stdtable racem racef [fw=freq] if marcoh == 2010, raw format(%10.6g)
di (59227/59096)/(61383/11842951)
di (88.3671/ 6.31709)/(5.43302/ 75.1003)

// nicer format
stdtable racem racef [fw=freq] if marcoh == 2010, ///
         cellwidth(9) format(%5.0f)  

// graph results		 
stdtable racem racef [fw=freq],              ///
    cellwidth(9) format(%5.0f)               ///
    by(marcoh) replace
	
tabplot racem marcoh [iw=std],               ///
    by(racef, compact note("") cols(5))      ///
    xlab( 1(1)6, angle(35) labsize(vsmall) ) ///
    showval( format(%5.0f))

// Illustrate how IPF works
// first load data in Mata
use homogamy, clear
tab racem racef [fw=freq] if marcoh==2010, matcell(data)
mata
data = st_matrix("data")
data
end

// fix the rows to 100
mata
muhat = data

muhat = muhat:/rowsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end

// fix the cols to 100
mata
muhat = muhat:/colsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end

// repeat
mata
muhat = muhat:/rowsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)

muhat = muhat:/colsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end

Next step is to Indicate when a slide begins with //slide and ends with //endslide

With the //title one specifies the title of the slide

Examples are indicated by //ex and //endex

Sections are indicated by //section section_name

This is what we get:

//section Standardized tables
//slide ------------------------------------------------------------------------
//title The influence of marginal distributions
//ex 
use homogamy, clear
tab racem racef [fw=freq] if marcoh == 2010, row nofreq
//endex
//endslide ---------------------------------------------------------------------

//slide ------------------------------------------------------------------------
//title Odds ratios
// digression?
//ex
tab racem racef [fw=freq] if marcoh == 2010
di 59227 / 59096
di 61383 / 11842951
di (59227 / 59096)/(61383 / 11842951)
//endex
//endslide ---------------------------------------------------------------------

//slide ------------------------------------------------------------------------
//title standardizing tables

//ex
tab racem racef [fw=freq] if marcoh==2010, exp
//endex

//ex
stdtable racem racef [fw=freq] if marcoh == 2010 
//endex

// show that ORs in standardized and raw data are equal

//ex
stdtable racem racef [fw=freq] if marcoh == 2010, raw format(%10.6g)
di (59227/59096)/(61383/11842951)
di (88.3671/ 6.31709)/(5.43302/ 75.1003)
//endex
//endslide ---------------------------------------------------------------------


//slide ------------------------------------------------------------------------
//title Displaying results from stdtable
//ex
stdtable racem racef [fw=freq] if marcoh == 2010, ///
         cellwidth(9) format(%5.0f)  
//endex

//ex
stdtable racem racef [fw=freq],              ///
    cellwidth(9) format(%5.0f)               ///
    by(marcoh) replace
	
tabplot racem marcoh [iw=std],               ///
    by(racef, compact note("") cols(5))      ///
    xlab( 1(1)6, angle(35) labsize(vsmall) ) ///
    showval( format(%5.0f))
//endex
//endslide ---------------------------------------------------------------------

//section Illustrate how IPF works
//slide ------------------------------------------------------------------------
//title Making all the row totals 100

// first load data in Mata
//ex
use homogamy, clear
tab racem racef [fw=freq] if marcoh==2010, matcell(data)
mata
data = st_matrix("data")
data
end
//endex

//ex
mata
muhat = data

muhat = muhat:/rowsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end
//endex
//endslide ---------------------------------------------------------------------

//slide ------------------------------------------------------------------------
//title  Making all the colum totals 100
//ex
mata
muhat = muhat:/colsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end
//endex
//endslide ---------------------------------------------------------------------

//slide ---------------------------------------------------------------------
//title repeat
//ex
mata
muhat = muhat:/rowsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)

muhat = muhat:/colsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end
//endex
//endslide

This is already a sourcefile we can use for smclpres

. smclpres using example_pres02.do , replace dir(output) to view the presentation: first change the directory to where the presentation is stored: cd "c:\mijn documenten\projecten\stata\sug\konstanz18\output\output" Then type: view example_pres02.smcl

. cd output c:\mijn documenten\projecten\stata\sug\konstanz18\output\output

. dir <dir> 6/22/18 14:44 . <dir> 6/22/18 14:44 .. 0.3k 6/22/18 14:44 example_pres02.smcl 0.3k 6/22/18 11:15 example_pres03.smcl 0.4k 6/22/18 11:15 example_pres04.smcl 1.0k 6/22/18 11:15 example_pres05.smcl 0.4k 6/22/18 11:15 example_pres06.smcl 0.8k 6/22/18 11:15 index.smcl 0.5k 6/22/18 14:44 slide1.smcl 0.1k 6/22/18 14:44 slide1ex1.do 0.5k 6/22/18 14:44 slide2.smcl 0.1k 6/22/18 14:44 slide2ex1.do 0.9k 6/22/18 14:44 slide3.smcl 0.0k 6/22/18 14:44 slide3ex1.do 0.0k 6/22/18 14:44 slide3ex2.do 0.1k 6/22/18 14:44 slide3ex3.do 1.0k 6/22/18 14:44 slide4.smcl 0.1k 6/22/18 14:44 slide4ex1.do 0.3k 6/22/18 14:44 slide4ex2.do 0.8k 6/22/18 14:44 slide5.smcl 0.1k 6/22/18 14:44 slide5ex1.do 0.1k 6/22/18 14:44 slide5ex2.do 0.5k 6/22/18 14:44 slide6.smcl 0.1k 6/22/18 14:44 slide6ex1.do 0.6k 6/22/18 14:44 slide7.smcl 0.2k 6/22/18 14:44 slide7ex1.do 0.8k 6/22/18 11:15 slide8.smcl

. cd .. c:\mijn documenten\projecten\stata\sug\konstanz18\output

-------------------------------------------------------------------------------

<< index >>

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
How to create a smcl presentation -- creating the presentation
-------------------------------------------------------------------------------

Adding text

You can add a text block by starting a line with /*txt and ending it with txt*/

The text will be formatted using SMCL, which is documented in help smcl

The most important directives are:

{pstd} starts a standard paragraph

{pmore} starts an indented paragraph

{p_end} ends a paragraph

{help cmd_name} adds a link to the helpfile of cmd_name.

The sourcefile after including text looks like this:

//section Standardized tables
//slide ------------------------------------------------------------------------
//title The influence of marginal distributions

/*txt
{pstd}
Consider the example below

{pstd}
It shows the race of the husband and the race of the wife for couples living in 
the USA that got married between 2010 and 2016

{pstd}
The races are very unequaly distributed in the USA

{pstd}
We can control for one marginal distribution by computing row or collumn 
percentages.
txt*/

//ex
use homogamy, clear
tab racem racef [fw=freq] if marcoh == 2010, row nofreq
//endex

/*txt
{pstd}
We see racial homogamy: people tend to marry someone of the same race

{pstd}
The exception appears to be the native Americans: only 43% of native American men
married native American women and 43% married a non-hispanic white women.

{pstd}
However, there are many more white women around than native american women (67% 
versus 0.8%).

{pstd}
If native Americans were mixing around randomly, then we would expect much more
native American men marrying white women.

{pstd}
Apperently native American men still prefer native American women but because 
there are so many more white women around he will still have a good chance of 
eventually marrying a white women. 

{pstd}
There is something mechanical about this, and it is common (in sociology) to want
to look at the pattern in the table nett of this influence of the marginal 
distributions.

{pstd}
We could do this by looking at odds ratios, but here I want to show an alternative.
txt*/

//endslide ---------------------------------------------------------------------

//slide ------------------------------------------------------------------------
//title Odds ratios
// digression?

/*txt
{pstd}
The odds is the number of "successes" per "failure"

{pstd}
The odds ratio is a ratio of odds, and this measure of association is indpendent
of the marginal distributions
txt*/

//ex
tab racem racef [fw=freq] if marcoh == 2010
di 59227 / 59096
di 61383 / 11842951
di (59227 / 59096)/(61383 / 11842951)
//endex

/*txt
{pstd}
In this case we find about 1 native american man marrying a native american women
for every native american man marrying a white women 

{pmore}
odds of marrying a native american women compared to a white women is "only" 1 
for native american man

{pstd}
We find about 0.005 white man marrying a native american women for every white 
man marrying a white women. 

{pmore}
odds of marrying a native american women compared to a white women is 0.05 for 
white man.

{pstd}
The odds of marrying a native american women compared to a white women is 193 
times larger for native American man than for white man. 

txt*/

//endslide ---------------------------------------------------------------------

//slide ------------------------------------------------------------------------
//title standardizing tables
/*txt
{pstd}
When we do a chi squared test for cross-tabulations we compare observed cell counts 
with predicted cell counts 

{pstd}
For these predicted cell counts we assume that the margins remain as observed, 
but otherwise there is no association between the variables (the odds ratios are
all 1).

{pstd}
In the table of predicted cell counts the only pattern is due to the margins.
txt*/

//ex
tab racem racef [fw=freq] if marcoh==2010, exp
//endex

/*txt
{pstd}
Can't we reverse that? Keep the association as observed but fix the margins. 

{pstd}
For example we could fix all margins at a 100.

{pstd}
That way we can look at the proportion native American men marrying native American
women when they weren't such a small group.

{pstd}
This is what the {helpb stdtable} package does.
txt*/

//ex
stdtable racem racef [fw=freq] if marcoh == 2010 
//endex

/*txt
{pstd}
To illustrate what keeping the association as observed means we can look at the 
odds ratios in the original table and in the standardized table 

{pstd}
Below we look at the odds ratio comparing the odds of marrying a native american 
women versus a white women when one is a native american or a white man.

{pstd}
This odds ratio in the original data is the same as that odds ratio in the
standardized table
txt*/

//ex
stdtable racem racef [fw=freq] if marcoh == 2010, raw format(%10.6g)
di (59227/59096)/(61383/11842951)
di (88.3671/ 6.31709)/(5.43302/ 75.1003)
//endex
//endslide ---------------------------------------------------------------------


//slide ------------------------------------------------------------------------
//title Displaying results from stdtable
/*txt
{pstd}
We can add options to make the table look better
txt*/

//ex
stdtable racem racef [fw=freq] if marcoh == 2010, ///
         cellwidth(9) format(%5.0f)  
//endex

/*txt
{pstd}
Alternatively we can replace the data in memory with standardized counts and 
use those to create a graph
txt*/

//ex
stdtable racem racef [fw=freq],              ///
    cellwidth(9) format(%5.0f)               ///
    by(marcoh) replace
	
tabplot racem marcoh [iw=std],               ///
    by(racef, compact note("") cols(5))      ///
    xlab( 1(1)6, angle(35) labsize(vsmall) ) ///
    showval( format(%5.0f))
//endex
//endslide ---------------------------------------------------------------------

//section Illustrate how IPF works
//slide ------------------------------------------------------------------------
//title Making all the row totals 100
/*txt
{pstd}
{cmd:stdtable} standardizes a table using Iterative Proportional Fitting

{pstd}
To illustrate how that works we will do a few iterations in Mata

{pstd}Notice, that I added the option {cmd:matcell(data)} to the {cmd:tab} 
command. This leaves behind the table as a Stata matrix named data, which in turn
can be read into Mata{p_end}
txt*/

//ex load the data in Mata
use homogamy, clear
tab racem racef [fw=freq] if marcoh==2010, matcell(data)
mata
data = st_matrix("data")
data
end
//endex

/*txt
{pstd}If we divide all cell entries by the rowsum, then the new rowsum will be 1.{p_end}

{pstd}Multiply the new cell entries by a 100, and the rowsum will be a 100.{p_end}
txt*/

//ex adjust the rows to sum to 100
mata
muhat = data

muhat = muhat:/rowsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end
//endex
//endslide ---------------------------------------------------------------------

//slide ------------------------------------------------------------------------
//title Making all the colum totals 100

/*txt
{pstd}The row totals are as we want them, but the column totals are not. What
if we repeat this process for the columns?{p_end}
txt*/

//ex adjust the columns to sum to 100
mata
muhat = muhat:/colsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end
//endex

/*txt
{pstd}Now the column totals are as we want them, but now the row totals are a
bit off.{p_end}

{pstd}However, the row totals are better than in the original table, so maybe
we need to repeat this process a couple of times?{p_end}
txt*/
//endslide ---------------------------------------------------------------------

//slide ------------------------------------------------------------------------
//title Repeat
//ex repeat adjusting rows and colums
mata
muhat = muhat:/rowsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)

muhat = muhat:/colsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end
//endex
/*txt
{pstd}Notice that each time we get a bit closer to our goal{p_end}

{pstd}
Iterative Proportional Fitting continues till convergence.
txt*/
//endslide ---------------------------------------------------------------------

. smclpres using example_pres03.do , replace dir(output) to view the presentation: first change the directory to where the presentation is stored: cd "c:\mijn documenten\projecten\stata\sug\konstanz18\output\output" Then type: view example_pres03.smcl

-------------------------------------------------------------------------------

<< index >>

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
How to create a smcl presentation -- creating the presentation
-------------------------------------------------------------------------------

Different kinds of slides

The normal slides (what we have used thus far) represent the main linear flow of the presentation.

We can also add digression slides. The arrows at the bottom of a slide will skip it, but you must add a link to it in a text block on the previous regular slide.

So during the presentation, the presenter can easily decide whether or not to skip the digression slide.

You specify the digression slide using //digr and //enddigr

You specify the where the link will appear in the previous slide using /*digr*/

You specify the label used for the link using //label

Alternatively we can add a ancillary slide, which can only be accessed from the index slide

This type of slide serves the purpose of an appendix.

You specify the ancillary slide using //anc and //endanc

The sourcefile after including a digression and an ancillary slide looks like this:

//section Standardized tables
//slide ------------------------------------------------------------------------
//title The influence of marginal distributions

/*txt
{pstd}
Consider the example below

{pstd}
It shows the race of the husband and the race of the wife for couples living in 
the USA that got married between 2010 and 2016

{pstd}
The races are very unequaly distributed in the USA

{pstd}
We can control for one marginal distribution by computing row or collumn 
percentages.
txt*/

//ex
use homogamy, clear
tab racem racef [fw=freq] if marcoh == 2010, row nofreq
//endex

/*txt
{pstd}
We see racial homogamy: people tend to marry someone of the same race

{pstd}
The exception appears to be the native Americans: only 43% of native American men
married native American women and 43% married a non-hispanic white women.

{pstd}
However, there are many more white women around than native american women (67% 
versus 0.8%).

{pstd}
If native Americans were mixing around randomly, then we would expect much more
native American men marrying white women.

{pstd}
Apperently native American men still prefer native American women but because 
there are so many more white women around he will still have a good chance of 
eventually marrying a white women. 

{pstd}
There is something mechanical about this, and it is common (in sociology) to want
to look at the pattern in the table nett of this influence of the marginal 
distributions.

{pstd}
We could do this by looking at /*digr*/ , but here I want to show an alternative.
txt*/

//endslide ---------------------------------------------------------------------

//digr -------------------------------------------------------------------------
//title Odds ratios
//label odds ratios
// digression?

/*txt
{pstd}
The odds is the number of "successes" per "failure"

{pstd}
The odds ratio is a ratio of odds, and this measure of association is indpendent
of the marginal distributions
txt*/

//ex
tab racem racef [fw=freq] if marcoh == 2010
di 59227 / 59096
di 61383 / 11842951
di (59227 / 59096)/(61383 / 11842951)
//endex

/*txt
{pstd}
In this case we find about 1 native american man marrying a native american women
for every native american man marrying a white women 

{pmore}
odds of marrying a native american women compared to a white women is "only" 1 
for native american man

{pstd}
We find about 0.005 white man marrying a native american women for every white 
man marrying a white women. 

{pmore}
odds of marrying a native american women compared to a white women is 0.05 for 
white man.

{pstd}
The odds of marrying a native american women compared to a white women is 193 
times larger for native American man than for white man. 

txt*/

//enddigr ----------------------------------------------------------------------

//slide ------------------------------------------------------------------------
//title standardizing tables
/*txt
{pstd}
When we do a chi squared test for cross-tabulations we compare observed cell counts 
with predicted cell counts 

{pstd}
For these predicted cell counts we assume that the margins remain as observed, 
but otherwise there is no association between the variables (the odds ratios are
all 1).

{pstd}
In the table of predicted cell counts the only pattern is due to the margins.
txt*/

//ex
tab racem racef [fw=freq] if marcoh==2010, exp
//endex

/*txt
{pstd}
Can't we reverse that? Keep the association as observed but fix the margins. 

{pstd}
For example we could fix all margins at a 100.

{pstd}
That way we can look at the proportion native American men marrying native American
women when they weren't such a small group.

{pstd}
This is what the {helpb stdtable} package does.
txt*/

//ex
stdtable racem racef [fw=freq] if marcoh == 2010 
//endex

/*txt
{pstd}
To illustrate what keeping the association as observed means we can look at the 
odds ratios in the original table and in the standardized table 

{pstd}
Below we look at the odds ratio comparing the odds of marrying a native american 
women versus a white women when one is a native american or a white man.

{pstd}
This odds ratio in the original data is the same as that odds ratio in the
standardized table
txt*/

//ex
stdtable racem racef [fw=freq] if marcoh == 2010, raw format(%10.6g)
di (59227/59096)/(61383/11842951)
di (88.3671/ 6.31709)/(5.43302/ 75.1003)
//endex
//endslide ---------------------------------------------------------------------


//slide ------------------------------------------------------------------------
//title Displaying results from stdtable
/*txt
{pstd}
We can add options to make the table look better
txt*/

//ex
stdtable racem racef [fw=freq] if marcoh == 2010, ///
         cellwidth(9) format(%5.0f)  
//endex

/*txt
{pstd}
Alternatively we can replace the data in memory with standardized counts and 
use those to create a graph
txt*/

//ex
stdtable racem racef [fw=freq],              ///
    cellwidth(9) format(%5.0f)               ///
    by(marcoh) replace
	
tabplot racem marcoh [iw=std],               ///
    by(racef, compact note("") cols(5))      ///
    xlab( 1(1)6, angle(35) labsize(vsmall) ) ///
    showval( format(%5.0f))
//endex
//endslide ---------------------------------------------------------------------

//section Illustrate how IPF works
//slide ------------------------------------------------------------------------
//title Making all the row totals 100
/*txt
{pstd}
{cmd:stdtable} standardizes a table using Iterative Proportional Fitting

{pstd}
To illustrate how that works we will do a few iterations in Mata

{pstd}Notice, that I added the option {cmd:matcell(data)} to the {cmd:tab} 
command. This leaves behind the table as a Stata matrix named data, which in turn
can be read into Mata{p_end}
txt*/

//ex load the data in Mata
use homogamy, clear
tab racem racef [fw=freq] if marcoh==2010, matcell(data)
mata
data = st_matrix("data")
data
end
//endex

/*txt
{pstd}If we divide all cell entries by the rowsum, then the new rowsum will be 1.{p_end}

{pstd}Multiply the new cell entries by a 100, and the rowsum will be a 100.{p_end}
txt*/

//ex adjust the rows to sum to 100
mata
muhat = data

muhat = muhat:/rowsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end
//endex
//endslide ---------------------------------------------------------------------

//slide ------------------------------------------------------------------------
//title Making all the colum totals 100

/*txt
{pstd}The row totals are as we want them, but the column totals are not. What
if we repeat this process for the columns?{p_end}
txt*/

//ex adjust the columns to sum to 100
mata
muhat = muhat:/colsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end
//endex

/*txt
{pstd}Now the column totals are as we want them, but now the row totals are a
bit off.{p_end}

{pstd}However, the row totals are better than in the original table, so maybe
we need to repeat this process a couple of times?{p_end}
txt*/
//endslide ---------------------------------------------------------------------

//slide ------------------------------------------------------------------------
//title Repeat
//ex repeat adjusting rows and colums
mata
muhat = muhat:/rowsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)

muhat = muhat:/colsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end
//endex
/*txt
{pstd}Notice that each time we get a bit closer to our goal{p_end}

{pstd}
Iterative Proportional Fitting continues till convergence.
txt*/
//endslide ---------------------------------------------------------------------

//anc --------------------------------------------------------------------------
//title Can all tables be standardized?

/*txt
{pstd}Consider the following table{p_end}

        0 0 2
        1 5 2
        8 7 0
	
{pstd}In order to make the first row total 100, the top right cell {it:must} be 
100{p_end}	

{pstd}In order to make the last column total 100, the top right cell {it:cannot} 
be 100{p_end}

{pstd}This is an example of a table that cannot be standardized. The Mata program
we created above will stop after 30 iterations, but the condition 
{cmd:mreldif(muhat2,muhat)>1e-8} will not be met. In other words the algorithm 
has not converged. The {cmd:stdtable} command will give a more explicit warning 
when that happens.
txt*/
//endanc -----------------------------------------------------------------------

. smclpres using example_pres04.do , replace dir(output) to view the presentation: first change the directory to where the presentation is stored: cd "c:\mijn documenten\projecten\stata\sug\konstanz18\output\output" Then type: view example_pres04.smcl

-------------------------------------------------------------------------------

<< index >>

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
How to create a smcl presentation -- creating the presentation
-------------------------------------------------------------------------------

Changing the look of the table of contents

You can specify settings for the overall layout of the presentation using the //layout command.

For example, //layout toc title(subsection) specifies that the slide titles are added to the table of content as a subsection.

In our example presentation we add the slide titles to the table of contents, make those slide titles links rather than the sections, make the sections bold, and the digressions italic

The title of the table of contents can be specified with the //toctitle command, and you can add text between the title and the table of contentents with the /*toctxt and toctxt*/ commands

The sourcefile after changing the table contents looks like this:

//layout toc title(subsection) link(subsection) secbold 
//layout toc subsubsecitalic 

//toctitle Standardizing tables in Stata using stdtable

/*toctxt

{center:Maarten L. Buis}
{center:University of Konstanz}
toctxt*/

//section Standardized tables
//slide ------------------------------------------------------------------------
//title The influence of marginal distributions

/*txt
{pstd}
Consider the example below

{pstd}
It shows the race of the husband and the race of the wife for couples living in 
the USA that got married between 2010 and 2016

{pstd}
The races are very unequaly distributed in the USA

{pstd}
We can control for one marginal distribution by computing row or collumn 
percentages.
txt*/

//ex
use homogamy, clear
tab racem racef [fw=freq] if marcoh == 2010, row nofreq
//endex

/*txt
{pstd}
We see racial homogamy: people tend to marry someone of the same race

{pstd}
The exception appears to be the native Americans: only 43% of native American men
married native American women and 43% married a non-hispanic white women.

{pstd}
However, there are many more white women around than native american women (67% 
versus 0.8%).

{pstd}
If native Americans were mixing around randomly, then we would expect much more
native American men marrying white women.

{pstd}
Apperently native American men still prefer native American women but because 
there are so many more white women around he will still have a good chance of 
eventually marrying a white women. 

{pstd}
There is something mechanical about this, and it is common (in sociology) to want
to look at the pattern in the table nett of this influence of the marginal 
distributions.

{pstd}
We could do this by looking at /*digr*/ , but here I want to show an alternative.
txt*/

//endslide ---------------------------------------------------------------------

//digr -------------------------------------------------------------------------
//title Odds ratios
//label odds ratios
// digression?

/*txt
{pstd}
The odds is the number of "successes" per "failure"

{pstd}
The odds ratio is a ratio of odds, and this measure of association is indpendent
of the marginal distributions
txt*/

//ex
tab racem racef [fw=freq] if marcoh == 2010
di 59227 / 59096
di 61383 / 11842951
di (59227 / 59096)/(61383 / 11842951)
//endex

/*txt
{pstd}
In this case we find about 1 native american man marrying a native american women
for every native american man marrying a white women 

{pmore}
odds of marrying a native american women compared to a white women is "only" 1 
for native american man

{pstd}
We find about 0.005 white man marrying a native american women for every white 
man marrying a white women. 

{pmore}
odds of marrying a native american women compared to a white women is 0.05 for 
white man.

{pstd}
The odds of marrying a native american women compared to a white women is 193 
times larger for native American man than for white man. 

txt*/

//enddigr ----------------------------------------------------------------------

//slide ------------------------------------------------------------------------
//title standardizing tables
/*txt
{pstd}
When we do a chi squared test for cross-tabulations we compare observed cell counts 
with predicted cell counts 

{pstd}
For these predicted cell counts we assume that the margins remain as observed, 
but otherwise there is no association between the variables (the odds ratios are
all 1).

{pstd}
In the table of predicted cell counts the only pattern is due to the margins.
txt*/

//ex
tab racem racef [fw=freq] if marcoh==2010, exp
//endex

/*txt
{pstd}
Can't we reverse that? Keep the association as observed but fix the margins. 

{pstd}
For example we could fix all margins at a 100.

{pstd}
That way we can look at the proportion native American men marrying native American
women when they weren't such a small group.

{pstd}
This is what the {helpb stdtable} package does.
txt*/

//ex
stdtable racem racef [fw=freq] if marcoh == 2010 
//endex

/*txt
{pstd}
To illustrate what keeping the association as observed means we can look at the 
odds ratios in the original table and in the standardized table 

{pstd}
Below we look at the odds ratio comparing the odds of marrying a native american 
women versus a white women when one is a native american or a white man.

{pstd}
This odds ratio in the original data is the same as that odds ratio in the
standardized table
txt*/

//ex
stdtable racem racef [fw=freq] if marcoh == 2010, raw format(%10.6g)
di (59227/59096)/(61383/11842951)
di (88.3671/ 6.31709)/(5.43302/ 75.1003)
//endex
//endslide ---------------------------------------------------------------------


//slide ------------------------------------------------------------------------
//title Displaying results from stdtable
/*txt
{pstd}
We can add options to make the table look better
txt*/

//ex
stdtable racem racef [fw=freq] if marcoh == 2010, ///
         cellwidth(9) format(%5.0f)  
//endex

/*txt
{pstd}
Alternatively we can replace the data in memory with standardized counts and 
use those to create a graph
txt*/

//ex
stdtable racem racef [fw=freq],              ///
    cellwidth(9) format(%5.0f)               ///
    by(marcoh) replace
	
tabplot racem marcoh [iw=std],               ///
    by(racef, compact note("") cols(5))      ///
    xlab( 1(1)6, angle(35) labsize(vsmall) ) ///
    showval( format(%5.0f))
//endex
//endslide ---------------------------------------------------------------------

//section Illustrate how IPF works
//slide ------------------------------------------------------------------------
//title Making all the row totals 100
/*txt
{pstd}
{cmd:stdtable} standardizes a table using Iterative Proportional Fitting

{pstd}
To illustrate how that works we will do a few iterations in Mata

{pstd}Notice, that I added the option {cmd:matcell(data)} to the {cmd:tab} 
command. This leaves behind the table as a Stata matrix named data, which in turn
can be read into Mata{p_end}
txt*/

//ex load the data in Mata
use homogamy, clear
tab racem racef [fw=freq] if marcoh==2010, matcell(data)
mata
data = st_matrix("data")
data
end
//endex

/*txt
{pstd}If we divide all cell entries by the rowsum, then the new rowsum will be 1.{p_end}

{pstd}Multiply the new cell entries by a 100, and the rowsum will be a 100.{p_end}
txt*/

//ex adjust the rows to sum to 100
mata
muhat = data

muhat = muhat:/rowsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end
//endex
//endslide ---------------------------------------------------------------------

//slide ------------------------------------------------------------------------
//title Making all the colum totals 100

/*txt
{pstd}The row totals are as we want them, but the column totals are not. What
if we repeat this process for the columns?{p_end}
txt*/

//ex adjust the columns to sum to 100
mata
muhat = muhat:/colsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end
//endex

/*txt
{pstd}Now the column totals are as we want them, but now the row totals are a
bit off.{p_end}

{pstd}However, the row totals are better than in the original table, so maybe
we need to repeat this process a couple of times?{p_end}
txt*/
//endslide ---------------------------------------------------------------------

//slide ------------------------------------------------------------------------
//title Repeat
//ex repeat adjusting rows and colums
mata
muhat = muhat:/rowsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)

muhat = muhat:/colsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end
//endex
/*txt
{pstd}Notice that each time we get a bit closer to our goal{p_end}

{pstd}
Iterative Proportional Fitting continues till convergence.
txt*/
//endslide ---------------------------------------------------------------------

//anc --------------------------------------------------------------------------
//title Can all tables be standardized?

/*txt
{pstd}Consider the following table{p_end}

        0 0 2
        1 5 2
        8 7 0
	
{pstd}In order to make the first row total 100, the top right cell {it:must} be 
100{p_end}	

{pstd}In order to make the last column total 100, the top right cell {it:cannot} 
be 100{p_end}

{pstd}This is an example of a table that cannot be standardized. The Mata program
we created above will stop after 30 iterations, but the condition 
{cmd:mreldif(muhat2,muhat)>1e-8} will not be met. In other words the algorithm 
has not converged. The {cmd:stdtable} command will give a more explicit warning 
when that happens.
txt*/
//endanc -----------------------------------------------------------------------

. smclpres using example_pres05.do , replace dir(output) to view the presentation: first change the directory to where the presentation is stored: cd "c:\mijn documenten\projecten\stata\sug\konstanz18\output\output" Then type: view example_pres05.smcl

    In this case the table of contents also serves as a titlepage.

You can include a dedicated titlepage using the //titlepage and //endtitlepage commands.

The sourcefile after adding a title page looks like this:

//layout toc title(subsection) link(subsection) secbold 
//layout toc subsubsecitalic 

//toctitle Table of content

//titlepage --------------------------------------------------------------------
/*txt




{center:{bf:Standardizing tables in Stata using stdtable}}


{center:Maarten L. Buis}
{center:University of Konstanz}
txt*/

//endtitlepage -----------------------------------------------------------------

//section Standardized tables
//slide ------------------------------------------------------------------------
//title The influence of marginal distributions

/*txt
{pstd}
Consider the example below

{pstd}
It shows the race of the husband and the race of the wife for couples living in 
the USA that got married between 2010 and 2016

{pstd}
The races are very unequaly distributed in the USA

{pstd}
We can control for one marginal distribution by computing row or collumn 
percentages.
txt*/

//ex
use homogamy, clear
tab racem racef [fw=freq] if marcoh == 2010, row nofreq
//endex

/*txt
{pstd}
We see racial homogamy: people tend to marry someone of the same race

{pstd}
The exception appears to be the native Americans: only 43% of native American men
married native American women and 43% married a non-hispanic white women.

{pstd}
However, there are many more white women around than native american women (67% 
versus 0.8%).

{pstd}
If native Americans were mixing around randomly, then we would expect much more
native American men marrying white women.

{pstd}
Apperently native American men still prefer native American women but because 
there are so many more white women around he will still have a good chance of 
eventually marrying a white women. 

{pstd}
There is something mechanical about this, and it is common (in sociology) to want
to look at the pattern in the table nett of this influence of the marginal 
distributions.

{pstd}
We could do this by looking at /*digr*/ , but here I want to show an alternative.
txt*/

//endslide ---------------------------------------------------------------------

//digr -------------------------------------------------------------------------
//title Odds ratios
//label odds ratios
// digression?

/*txt
{pstd}
The odds is the number of "successes" per "failure"

{pstd}
The odds ratio is a ratio of odds, and this measure of association is indpendent
of the marginal distributions
txt*/

//ex
tab racem racef [fw=freq] if marcoh == 2010
di 59227 / 59096
di 61383 / 11842951
di (59227 / 59096)/(61383 / 11842951)
//endex

/*txt
{pstd}
In this case we find about 1 native american man marrying a native american women
for every native american man marrying a white women 

{pmore}
odds of marrying a native american women compared to a white women is "only" 1 
for native american man

{pstd}
We find about 0.005 white man marrying a native american women for every white 
man marrying a white women. 

{pmore}
odds of marrying a native american women compared to a white women is 0.05 for 
white man.

{pstd}
The odds of marrying a native american women compared to a white women is 193 
times larger for native American man than for white man. 

txt*/

//enddigr ----------------------------------------------------------------------

//slide ------------------------------------------------------------------------
//title standardizing tables
/*txt
{pstd}
When we do a chi squared test for cross-tabulations we compare observed cell counts 
with predicted cell counts 

{pstd}
For these predicted cell counts we assume that the margins remain as observed, 
but otherwise there is no association between the variables (the odds ratios are
all 1).

{pstd}
In the table of predicted cell counts the only pattern is due to the margins.
txt*/

//ex
tab racem racef [fw=freq] if marcoh==2010, exp
//endex

/*txt
{pstd}
Can't we reverse that? Keep the association as observed but fix the margins. 

{pstd}
For example we could fix all margins at a 100.

{pstd}
That way we can look at the proportion native American men marrying native American
women when they weren't such a small group.

{pstd}
This is what the {helpb stdtable} package does.
txt*/

//ex
stdtable racem racef [fw=freq] if marcoh == 2010 
//endex

/*txt
{pstd}
To illustrate what keeping the association as observed means we can look at the 
odds ratios in the original table and in the standardized table 

{pstd}
Below we look at the odds ratio comparing the odds of marrying a native american 
women versus a white women when one is a native american or a white man.

{pstd}
This odds ratio in the original data is the same as that odds ratio in the
standardized table
txt*/

//ex
stdtable racem racef [fw=freq] if marcoh == 2010, raw format(%10.6g)
di (59227/59096)/(61383/11842951)
di (88.3671/ 6.31709)/(5.43302/ 75.1003)
//endex
//endslide ---------------------------------------------------------------------


//slide ------------------------------------------------------------------------
//title Displaying results from stdtable
/*txt
{pstd}
We can add options to make the table look better
txt*/

//ex
stdtable racem racef [fw=freq] if marcoh == 2010, ///
         cellwidth(9) format(%5.0f)  
//endex

/*txt
{pstd}
Alternatively we can replace the data in memory with standardized counts and 
use those to create a graph
txt*/

//ex
stdtable racem racef [fw=freq],              ///
    cellwidth(9) format(%5.0f)               ///
    by(marcoh) replace
	
tabplot racem marcoh [iw=std],               ///
    by(racef, compact note("") cols(5))      ///
    xlab( 1(1)6, angle(35) labsize(vsmall) ) ///
    showval( format(%5.0f))
//endex
//endslide ---------------------------------------------------------------------

//section Illustrate how IPF works
//slide ------------------------------------------------------------------------
//title Making all the row totals 100
/*txt
{pstd}
{cmd:stdtable} standardizes a table using Iterative Proportional Fitting

{pstd}
To illustrate how that works we will do a few iterations in Mata

{pstd}Notice, that I added the option {cmd:matcell(data)} to the {cmd:tab} 
command. This leaves behind the table as a Stata matrix named data, which in turn
can be read into Mata{p_end}
txt*/

//ex load the data in Mata
use homogamy, clear
tab racem racef [fw=freq] if marcoh==2010, matcell(data)
mata
data = st_matrix("data")
data
end
//endex

/*txt
{pstd}If we divide all cell entries by the rowsum, then the new rowsum will be 1.{p_end}

{pstd}Multiply the new cell entries by a 100, and the rowsum will be a 100.{p_end}
txt*/

//ex adjust the rows to sum to 100
mata
muhat = data

muhat = muhat:/rowsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end
//endex
//endslide ---------------------------------------------------------------------

//slide ------------------------------------------------------------------------
//title Making all the colum totals 100

/*txt
{pstd}The row totals are as we want them, but the column totals are not. What
if we repeat this process for the columns?{p_end}
txt*/

//ex adjust the columns to sum to 100
mata
muhat = muhat:/colsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end
//endex

/*txt
{pstd}Now the column totals are as we want them, but now the row totals are a
bit off.{p_end}

{pstd}However, the row totals are better than in the original table, so maybe
we need to repeat this process a couple of times?{p_end}
txt*/
//endslide ---------------------------------------------------------------------

//slide ------------------------------------------------------------------------
//title Repeat
//ex repeat adjusting rows and colums
mata
muhat = muhat:/rowsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)

muhat = muhat:/colsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end
//endex
/*txt
{pstd}Notice that each time we get a bit closer to our goal{p_end}

{pstd}
Iterative Proportional Fitting continues till convergence.
txt*/
//endslide ---------------------------------------------------------------------

//anc --------------------------------------------------------------------------
//title Can all tables be standardized?

/*txt
{pstd}Consider the following table{p_end}

        0 0 2
        1 5 2
        8 7 0
	
{pstd}In order to make the first row total 100, the top right cell {it:must} be 
100{p_end}	

{pstd}In order to make the last column total 100, the top right cell {it:cannot} 
be 100{p_end}

{pstd}This is an example of a table that cannot be standardized. The Mata program
we created above will stop after 30 iterations, but the condition 
{cmd:mreldif(muhat2,muhat)>1e-8} will not be met. In other words the algorithm 
has not converged. The {cmd:stdtable} command will give a more explicit warning 
when that happens.
txt*/
//endanc -----------------------------------------------------------------------

. smclpres using example_pres06.do , replace dir(output) to view the presentation: first change the directory to where the presentation is stored: cd "c:\mijn documenten\projecten\stata\sug\konstanz18\output\output" Then type: view example_pres06.smcl

-------------------------------------------------------------------------------

<< index >>

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
How to create a smcl presentation -- creating a handout
-------------------------------------------------------------------------------

Initial handout

.smcl presentations are good at illustrating how to use Stata

However, they are inconvenient for the audience if they later want to look something up from that presentation

The pres2html command will turn a .smcl presentation into a .html handout

cd output pres2html using example_pres06.smcl, replace cd ..

-------------------------------------------------------------------------------

<< index >>

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
How to create a smcl presentation -- creating a handout
-------------------------------------------------------------------------------

Adding graphs to the handout

Notice that the graph is not displayed in the handout

You can tell pres2html that a graph needs to be added with the command //graph graphname.

The sourcefile after adding that looks like this:

//layout toc title(subsection) link(subsection) secbold 
//layout toc subsubsecitalic 

//toctitle Standardizing tables in Stata using stdtable

/*toctxt

{center:Maarten L. Buis}
{center:University of Konstanz}
toctxt*/

//section Standardized tables
//slide ------------------------------------------------------------------------
//title The influence of marginal distributions

/*txt
{pstd}
Consider the example below

{pstd}
It shows the race of the husband and the race of the wife for couples living in 
the USA that got married between 2010 and 2016

{pstd}
The races are very unequaly distributed in the USA

{pstd}
We can control for one marginal distribution by computing row or collumn 
percentages.
txt*/

//ex
use homogamy, clear
tab racem racef [fw=freq] if marcoh == 2010, row nofreq
//endex

/*txt
{pstd}
We see racial homogamy: people tend to marry someone of the same race

{pstd}
The exception appears to be the native Americans: only 43% of native American men
married native American women and 43% married a non-hispanic white women.

{pstd}
However, there are many more white women around than native american women (67% 
versus 0.8%).

{pstd}
If native Americans were mixing around randomly, then we would expect much more
native American men marrying white women.

{pstd}
Apperently native American men still prefer native American women but because 
there are so many more white women around he will still have a good chance of 
eventually marrying a white women. 

{pstd}
There is something mechanical about this, and it is common (in sociology) to want
to look at the pattern in the table nett of this influence of the marginal 
distributions.

{pstd}
We could do this by looking at /*digr*/ , but here I want to show an alternative.
txt*/

//endslide ---------------------------------------------------------------------

//digr -------------------------------------------------------------------------
//title Odds ratios
//label odds ratios
// digression?

/*txt
{pstd}
The odds is the number of "successes" per "failure"

{pstd}
The odds ratio is a ratio of odds, and this measure of association is indpendent
of the marginal distributions
txt*/

//ex
tab racem racef [fw=freq] if marcoh == 2010
di 59227 / 59096
di 61383 / 11842951
di (59227 / 59096)/(61383 / 11842951)
//endex

/*txt
{pstd}
In this case we find about 1 native american man marrying a native american women
for every native american man marrying a white women 

{pmore}
odds of marrying a native american women compared to a white women is "only" 1 
for native american man

{pstd}
We find about 0.005 white man marrying a native american women for every white 
man marrying a white women. 

{pmore}
odds of marrying a native american women compared to a white women is 0.05 for 
white man.

{pstd}
The odds of marrying a native american women compared to a white women is 193 
times larger for native American man than for white man. 

txt*/

//enddigr ----------------------------------------------------------------------

//slide ------------------------------------------------------------------------
//title standardizing tables
/*txt
{pstd}
When we do a chi squared test for cross-tabulations we compare observed cell counts 
with predicted cell counts 

{pstd}
For these predicted cell counts we assume that the margins remain as observed, 
but otherwise there is no association between the variables (the odds ratios are
all 1).

{pstd}
In the table of predicted cell counts the only pattern is due to the margins.
txt*/

//ex
tab racem racef [fw=freq] if marcoh==2010, exp
//endex

/*txt
{pstd}
Can't we reverse that? Keep the association as observed but fix the margins. 

{pstd}
For example we could fix all margins at a 100.

{pstd}
That way we can look at the proportion native American men marrying native American
women when they weren't such a small group.

{pstd}
This is what the {helpb stdtable} package does.
txt*/

//ex
stdtable racem racef [fw=freq] if marcoh == 2010 
//endex

/*txt
{pstd}
To illustrate what keeping the association as observed means we can look at the 
odds ratios in the original table and in the standardized table 

{pstd}
Below we look at the odds ratio comparing the odds of marrying a native american 
women versus a white women when one is a native american or a white man.

{pstd}
This odds ratio in the original data is the same as that odds ratio in the
standardized table
txt*/

//ex
stdtable racem racef [fw=freq] if marcoh == 2010, raw format(%10.6g)
di (59227/59096)/(61383/11842951)
di (88.3671/ 6.31709)/(5.43302/ 75.1003)
//endex
//endslide ---------------------------------------------------------------------


//slide ------------------------------------------------------------------------
//title Displaying results from stdtable
/*txt
{pstd}
We can add options to make the table look better
txt*/

//ex
stdtable racem racef [fw=freq] if marcoh == 2010, ///
         cellwidth(9) format(%5.0f)  
//endex

/*txt
{pstd}
Alternatively we can replace the data in memory with standardized counts and 
use those to create a graph
txt*/

//ex
stdtable racem racef [fw=freq],              ///
    cellwidth(9) format(%5.0f)               ///
    by(marcoh) replace
	
tabplot racem marcoh [iw=std],               ///
    by(racef, compact note("") cols(5))      ///
    xlab( 1(1)6, angle(35) labsize(vsmall) ) ///
    showval( format(%5.0f))
//endex
//graph Graph
//endslide ---------------------------------------------------------------------

//section Illustrate how IPF works
//slide ------------------------------------------------------------------------
//title Making all the row totals 100
/*txt
{pstd}
{cmd:stdtable} standardizes a table using Iterative Proportional Fitting

{pstd}
To illustrate how that works we will do a few iterations in Mata

{pstd}Notice, that I added the option {cmd:matcell(data)} to the {cmd:tab} 
command. This leaves behind the table as a Stata matrix named data, which in turn
can be read into Mata{p_end}
txt*/

//ex load the data in Mata
use homogamy, clear
tab racem racef [fw=freq] if marcoh==2010, matcell(data)
mata
data = st_matrix("data")
data
end
//endex

/*txt
{pstd}If we divide all cell entries by the rowsum, then the new rowsum will be 1.{p_end}

{pstd}Multiply the new cell entries by a 100, and the rowsum will be a 100.{p_end}
txt*/

//ex adjust the rows to sum to 100
mata
muhat = data

muhat = muhat:/rowsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end
//endex
//endslide ---------------------------------------------------------------------

//slide ------------------------------------------------------------------------
//title Making all the colum totals 100

/*txt
{pstd}The row totals are as we want them, but the column totals are not. What
if we repeat this process for the columns?{p_end}
txt*/

//ex adjust the columns to sum to 100
mata
muhat = muhat:/colsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end
//endex

/*txt
{pstd}Now the column totals are as we want them, but now the row totals are a
bit off.{p_end}

{pstd}However, the row totals are better than in the original table, so maybe
we need to repeat this process a couple of times?{p_end}
txt*/
//endslide ---------------------------------------------------------------------

//slide ------------------------------------------------------------------------
//title Repeat
//ex repeat adjusting rows and colums
mata
muhat = muhat:/rowsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)

muhat = muhat:/colsum(muhat):*100
muhat
rowsum(muhat)
colsum(muhat)
end
//endex
/*txt
{pstd}Notice that each time we get a bit closer to our goal{p_end}

{pstd}
Iterative Proportional Fitting continues till convergence.
txt*/
//endslide ---------------------------------------------------------------------

//anc --------------------------------------------------------------------------
//title Can all tables be standardized?

/*txt
{pstd}Consider the following table{p_end}

        0 0 2
        1 5 2
        8 7 0
	
{pstd}In order to make the first row total 100, the top right cell {it:must} be 
100{p_end}	

{pstd}In order to make the last column total 100, the top right cell {it:cannot} 
be 100{p_end}

{pstd}This is an example of a table that cannot be standardized. The Mata program
we created above will stop after 30 iterations, but the condition 
{cmd:mreldif(muhat2,muhat)>1e-8} will not be met. In other words the algorithm 
has not converged. The {cmd:stdtable} command will give a more explicit warning 
when that happens.
txt*/
//endanc -----------------------------------------------------------------------

smclpres using example_pres07.do , replace dir(output) cd output pres2html using example_pres07.do, replace cd ..

-------------------------------------------------------------------------------

<< index >>

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
How to create a smcl presentation -- creating a handout
-------------------------------------------------------------------------------

Adding contents or output of a .do file to the handout

In this presentation I added a lot of links to .do files. In the handout we would want a copy of those in there.

This can be done using the //codefile filename command.

In a teaching context I use this when including some small excercises with the solution in the .do file.

-------------------------------------------------------------------------------

<< index

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
digression
-------------------------------------------------------------------------------

Odds ratios

The odds is the number of "successes" per "failure"

The odds ratio is a ratio of odds, and this measure of association is indpendent of the marginal distributions

. tab racem racef [fw=freq] if marcoh == 2010

| race wife race husband | white, no white, hi black native am | Total -----------------+--------------------------------------------+---------- white, non-hisp. |11,842,951 643,588 142,322 61,383 |13,031,939 white, hisp. | 600,321 2,249,673 43,114 16,003 | 2,960,046 black | 323,559 98,197 1,587,673 11,102 | 2,060,467 native amer. | 59,096 11,584 2,879 59,227 | 136,297 asian | 137,657 29,398 7,867 1,537 | 1,063,187 -----------------+--------------------------------------------+---------- Total |12,963,584 3,032,440 1,783,855 149,252 |19,251,936

| race wife race husband | asian | Total -----------------+-----------+---------- white, non-hisp. | 341,695 |13,031,939 white, hisp. | 50,935 | 2,960,046 black | 39,936 | 2,060,467 native amer. | 3,511 | 136,297 asian | 886,728 | 1,063,187 -----------------+-----------+---------- Total | 1,322,805 |19,251,936

. di 59227 / 59096 1.0022167

. di 61383 / 11842951 .00518308

. di (59227 / 59096)/(61383 / 11842951) 193.36304

    In this case we find about 1 native american man marrying a native
    american women for every native american man marrying a white women

odds of marrying a native american women compared to a white women is "only" 1 for native american man

We find about 0.005 white man marrying a native american women for every white man marrying a white women.

odds of marrying a native american women compared to a white women is 0.05 for white man.

The odds of marrying a native american women compared to a white women is 193 times larger for native American man than for white man.

-------------------------------------------------------------------------------

<< index

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
ancillary
-------------------------------------------------------------------------------

Can all tables be standardized?

Consider the following table

0 0 2 1 5 2 8 7 0 In order to make the first row total 100, the top right cell must be 100

In order to make the last column total 100, the top right cell cannot be 100

This is an example of a table that cannot be standardized, and the algorithm will not converge.

-------------------------------------------------------------------------------

index

-------------------------------------------------------------------------------