-------------
      name:  <unnamed>
       log:  /Users/eric/Work/MB/ATCR/200/Labs/lab 3.smcl
  log type:  smcl
 opened on:   9 Jul 2018, 04:54:55

.
. ********************************************************************************
. * Binomial distribution.
.
. * The probability of being selected by random digit dialing for a political survey in your area code is 0.025. The surveys are conducted randomly each month for one year.  What is the probability that you will not be selected in the entire year (i.e. the probability you will not be selected in any of 12 months)?  Use the binomial formula.
.
. * We will do this two ways using built-in three built-in Stata functions.  Briefly check the help files for these functions
.
. help comb

.
. help binomialtail

.
. help binomialp

.
. * Now we implement the binomial formula directly using comb(N,k), which evaluates the combinatoric (N choose k), and the exponentiation operator ^.
.
. display as result 1-comb(12,0)*0.025^0*(1-.025)^12
.26200165

.
. * The as result option in the display command bolds the output. An easier way to do this is to use the binomialtail function.
.
. di as result binomialtail(12,1,.025)
.26200165

.
. * Now we calculate the probability of being chosen once or twice, first using the binomial forumla
.
. di as result comb(12,1)*0.025^1*(1-.025)^11+comb(12,2)*0.025^2*(1-.025)^10
.25910001

.
. * and now using the binomialp function
. di as result binomialp(12,1,.025)+binomialp(12,2,.025)
.25910001

.
. * Now we use binomialtail to get the probability of being selected 2 times or fewer:
. di as result 1-binomialtail(12,3,.025)
.99709836

.
. * Now we will get summary statistics and plot the binomial distribution with first 10, then 100, trials and varying probabilities of success.  Using loops, we'll first make a dataset with 11 or 101 observations, one for each possible number of successes, then calculate the corresponding probability.
.
. foreach n in 10 100 {
  2.         clear
  3.         set obs `=`n'+1' // note syntax to plug in the sample size + 1
  4.         gen x = _n-1 // _n is the observation number
  5.         foreach p in .5 .18 .82 {
  6.
.                 * calculate mean and SD
.                 local mean = round(`n'*`p',.01)
  7.                 local sd = round(sqrt(`n'*`p'*(1-`p')),.01)
  8.                 di as result "N = `n', P = `p', mean = `mean', SD = `sd'"
  9.
.                 * calculate probability of each number of successes
.                 local pct = `p'*100 // variable and plot names can't include "."
 10.                 gen p`pct' = binomialp(`n',x,`p')
 11.                 * plot the distribution
.                 twoway (bar p`pct' x, fcolor(blue)), ///
>                         title("N = `n', P = `pct'%") caption("mean = `mean', SD = `sd'") ///
>                         name(b`n'_`pct', replace)
 12.                 }
 13.         }


number of observations (_N)  was 0,
now 11


N = 10, P = .5, mean = 5, SD = 1.58
N = 10, P = .18, mean = 1.8, SD = 1.21
N = 10, P = .82, mean = 8.199999999999999, SD = 1.21


number of observations (_N)  was 0,
now 101


N = 100, P = .5, mean = 50, SD = 5
N = 100, P = .18, mean = 18, SD = 3.84
N = 100, P = .82, mean = 82, SD = 3.84

. * Note that Stata has trouble rounding one of the means; this comes from the conversion back from binary to decimal notation.  Now combine the plots to make them easier to compare.
.
. graph combine b10_50 b10_18 b10_82 b100_50 b100_18 b100_82, rows(2)

.
. * Are the plots consistent with the calculated means and SDs?
.
. * According to the CDC, 20% of all adult men in the U.S. smoke cigarettes.  If you selected repeated samples of size 12 from all adult males in the U.S., what would be the mean number of men who do not smoke? What would be the mean number of men who smoke? What would be the standard deviation?
.
. di as result "Mean # non-smokers = " round(12*.8,.01)
Mean # non-smokers = 9.6

. di as result "Mean # smokers = " round(12*.2,.01)
Mean # smokers = 2.4

. di as result "SD of distribution = " round(sqrt(12*.8*.2),.01)
SD of distribution = 1.39

.
. * Suppose you select a sample of 12 men and find that 6 of them smoke.  Assuming .20 is the true probability of smoking among men, what is the probability that you would have a sample this extreme?
.
. di as result binomialtail(12,6,.2)
.01940528

.
. ********************************************************************************
. * Hypergeometric distribution
.
. * A binomial outcome k can be thought of as the number of successes in a sample of size n, sampled with replacement from a population of size N, in which the proportion of successes K/N=p. The corresponding hypergeometric outcome k is the number of successes in a sample of size n, in this case sampled without replacement from the same population.  Commonly we assume that the population is so large that sampling with or without replacement is of no practical importance.  But what if N is smalle?
.
. * Dr. Allen gives this New York City-based example:  Having bought a bag of 30 roasted chestnuts, you walk home eating them with gusto, and arrive home having eaten 20.  In opening the remaining 10, you find that 7 contain worms.  What is the probability that none if the first 20 contains worms?
.
. * First check the help for the hypergeometric probability functions.
.
. help hypergeometricp

.
. help hypergeometric

.
. * We can rephrase the problem as follows:  In a sample of 20 sampled without replacement from a population of size 30 including 7 "successes", what is the probability of drawing 0 successes?
.
. di as result "probability you recently ate no wormy chestnuts = " ///
>         round(hypergeometric(30,7,20,0),.000001)
probability you recently ate no wormy chestnuts = .000059

.
. * We can also calculate this from the formula
. di as result "probability you recently ate no wormy chestnuts = " ///
>         round(comb(7,0)*comb(23,20)/comb(30,20),.000001)
probability you recently ate no wormy chestnuts = .000059

.
. * Note that we could have used either hypergeometric function in this case.
.
. ********************************************************************************
. * Normal distribution
.
. * First, familiarize yourself with the Stata normal cumulative probability  and inverse cumulative probability functions
.
. help normal

.
. help invnormal

.
. * The lengths of adult men's feet are approximately normal, with mean 11 inches, and standard deviation 1.5 inches.
.
. * What is the probability of a foot length of more than 13 inches? Because the argument to the normal function is Z-score (i.e., the observed value minus the mean, divided by the SD), we calculate that first.  Using a local variable avoids typing in a calculated number, which is prone to error. Before running this next bit of code, try to put in words what it is doing.
.
. local z = (13-11)/1.5

. di as result "probability of foot length >=13 inches: " ///
>         round(1-normal(`z'),.001)
probability of foot length >=13 inches: .091

.
. * We could also do this in a single step, because the normal function can calculate its argument on the fly.  (Stata is inconsistent about this.)
.
. di as result "probability of foot length >=13 inches: " ///
>         round(1-normal((13-11)/1.5),.001)
probability of foot length >=13 inches: .091

.
. * What is the probability of a foot length between 10 and 12 inches?
.
. local z10 = (10-11)/1.5

. local z12 = (12-11)/1.5

. local p1012 = round(normal(`z12')-normal(`z10'),.01)

. di as result "probability of foot length 10-12 inches: `p1012'"
probability of foot length 10-12 inches: .5

.
. * This could also have been done in a single step; try that if you like.
.
. * What are the foot lengths at the 25th percentile and 75th percentile? The argument of the invnormal function is a probability and the output is a Z-score, from which we calculate the observed value using the mean and SD.
.
. di as result "25th pctile = " round(11+invnormal(.25)*1.5,.01) ///
>         "  inches"  _n "75th pctile = " round(11+invnormal(.75)*1.5,.01) " inches"
25th pctile = 9.99  inches
75th pctile = 12.01 inches

.
. * The probability of 0.04 that a randomly chosen adult male foot length will be less than how many inches?
.
. di as result "4th pctile = " round(11+invnormal(.04)*1.5,.01) "inches"
4th pctile = 8.37inches

.
. * Stata had trouble with the rounding again.
.
. * We could also have done this using simulation, which is commonly used when a distribution isn't well understood (in contrast to the Normal distribution).  To get reasonably reliable estimates, we'll use a large simulated sample of 100,000, and also set the seed for the random number generator, to make the results reproducible.
.
. clear

. set obs 100000


number of observations (_N)  was 0,
now 100,000


. set seed 200

. gen fl = rnormal(11,1.5)

. label var fl "foot length"

.
. * Following lab 1, we'll use the ordering of the elements of the recode command to ensure that any values of 13 are counted in the >= 13 category.
.
. recode fl (13/max=1 ">=13 inches") (min/13=0 "<13 inches"), gen(fl_13up)
(100000 differences between fl and fl_13up)

. label var fl_13up "foot length"

. tab fl_13up

foot length |      Freq.     Percent        Cum.
------------+-----------------------------------
 <13 inches |     90,812       90.81       90.81
>=13 inches |      9,188        9.19      100.00
------------+-----------------------------------
      Total |    100,000      100.00

.
. recode fl (10/12=1 "10-12 inches") (min/10 12/max=0 "<10, >12 inches"), gen(fl_1012)
(100000 differences between fl and fl_1012)

. label var fl_1012 "foot length"

. tab fl_1012

    foot length |      Freq.     Percent        Cum.
----------------+-----------------------------------
<10, >12 inches |     50,779       50.78       50.78
   10-12 inches |     49,221       49.22      100.00
----------------+-----------------------------------
          Total |    100,000      100.00

.
. * The result is close to the theoretical value of 9.1%.  We can also get the quantiles of the distribution as local variables, then use the command return list to see the results.
.
. _pctile fl, percentiles(4 25 75)

. return list

scalars:
                 r(r1) =  8.369423389434814
                 r(r2) =  9.983631134033203
                 r(r3) =  12.02052927017212

.
. * Results are again close to the theoretical values. To get a sense of simulation error, try redoing this with a smaller simulated sample.
.
. ********************************************************************************
. * Poisson and negative binomial distributions
.
. * Count outcomes are commonly encountered in epidemiology.  Examples include the number of cases of incident disease in a given number of person-years of follow-up, the number of binge drinking episodes reported between study visits, and so on.
.
. * Check out the four Poisson probability functions:
.
. help poissonp

. help poisson

. help poissontail

. help invpoisson

. help invpoissontail

.
. * Suppose you accrue 10,545 person-years of follow-up in your cohort, and the population incidence of myocardial infarction is 2.5% (i.e., 2.5 cases per 100 person-years).  Assuming that the distribution is Poisson, which is reasonable in this case, what is the expected number of cases?  We'll save the expected number of cases as a global variable, which will be remembered even if you run the commands below one at at time; note that we call global variables using a different syntax.
.
. global mean = 10545*.025

. di as result "Expected number of cases = $En"
Expected number of cases =

.
. * What is the probability of observing at least 300 MI cases?
.
. di as result "probability of observing >=300 MI cases: " ///
>         round(poissontail($mean,300),.001)
probability of observing >=300 MI cases: .015

.
. * What is the chance of observing between 250 and 275 cases?
.
. di as result "probability of observing 250-275 MI cases: " ///
>         round(poisson($mean,275)-poisson($mean,249),.001)
probability of observing 250-275 MI cases: .576

.
. * What is the IQR of the distribution?
.
. local q25 = round(invpoisson($mean, .25),.1)

. local q75 = round(invpoisson($mean, .75),.1)

. di as result ///
>         "IQR of Poisson distribution with mean `=round($mean,.1)': `q25'-`q75'"
IQR of Poisson distribution with mean 263.6: 274.8-252.9

.
. * Although the Poisson model might be adequate for the number of MIs, the number of binge drinking episodes might be "over-dispersed", with variance greater than its mean.  The negative binomial distribution, with parameters n, k, and p, gives the probabililty of observing k failures before n successes are observed, where p is the probability of success.
.
. * That of course is a counter-intuitive way to think of the distribution. Consider that the mean of the distribution of k is n(1-p)/p and its variance is n(1-p)/p^2.  A little algebra shows that p is the ratio of the mean to the variance, and n is given by mean^2/(variance-mean). Consider a negative binomial distribution with mean 2 and variance 5 (so it is over-dispersed, relative to the Poisson model).  We can check to see that these formulas are correct.
.
. local m = 2

. local v = 5

. local p = `m'/`v'

. local n = `m'^2/(`v'-`m')

. clear

. set obs 100000


number of observations (_N)  was 0,
now 100,000


. gen y = rnbinomial(`n',`p')

. tabstat y, s(mean var)

variable   |      mean  variance
-------------+--------------------
y   |   1.99457   4.95259
-------------+--------------------

. clear

.
. * Now familiarize with the negative binomial probability functions
.
. help nbinomialp

. help nbinomial

. help nbinomialtail

. help invbinomial

. help invbinomialtail

.
. * Use these functions to calculate the probability of observing exactly 10 failures before the 5th success, if the probability of success is .4. Also calculate the mean and variance of the distribution.
.
. di as result "Probability of 10 failures before 5th success = " ///
>         round(nbinomialp(5,10,.4),.01)
Probability of 10 failures before 5th success = .06

. di as result "Mean = " 5*.6/.4 "  Variance = " 5*.6/.4^2
Mean = 7.5  Variance = 18.75

.
. * Now calculate the probability of observing 5-10 failures before the 7th success, with success probabiity .6.
.
. di as result  "Probability of 5-10 failures before 7th success = " ///
>         round(nbinomial(7,10,.6)-nbinomial(7,4,.6),.01)
Probability of 5-10 failures before 7th success = .43

.
. ********************************************************************************
. * Exponential failure time distribution
.
. * The exponential distribution describes time to an endpoint if the failure rate is a constant, and is closely related to the Poisson distribution, as explained in class. Stata parameterizes the distribution in terms of the its mean or scale, which is the inverse of the failure rate per unit time.  Have a look at the stat functions for this distribution:
.
. help exponential

. help invexponential

.
. * Suppose the failure rate of 10% per year, so the mean failure time is 10 years.  What is the probability of failing within 2, 5, and 10 time units?
.
. forvalues t = 1/10 {
  2.         local p_`t' = round(exponential(10,`t'),.01)
  3.         di as result "Probability of failure within `t' years: `p_`t''"
  4.         }
Probability of failure within 1 years: .1
Probability of failure within 2 years: .18
Probability of failure within 3 years: .26
Probability of failure within 4 years: .33
Probability of failure within 5 years: .39
Probability of failure within 6 years: .45
Probability of failure within 7 years: .5
Probability of failure within 8 years: .55
Probability of failure within 9 years: .59
Probability of failure within 10 years: .63

.
. * Note that the cumulative risk is not a linear function of time.  Why?
.
. * What is the probability of surviving more than 8 years?  Note that the exponentialtail function is not what we want for this purpose.
.
. di as result "Probability of surviving >8 years: " 1-round(exponential(10,8),.01)
Probability of surviving >8 years: .45

.
. * Finally, get the IQR of the distribution
. local q25 = round(invexponential(10, .25),.01)

. local q75 = round(invexponential(10, .75),.01)

. di as result ///
>         "IQR of exponential distribution with mean 10: `q25'-`q75' years"
IQR of exponential distribution with mean 10: 2.88-13.86 years

.
. log close
      name:  <unnamed>
       log:  /Users/eric/Work/MB/ATCR/200/Labs/lab 3.smcl
  log type:  smcl
 closed on:   9 Jul 2018, 04:55:05
-------------