-------------
      name:  <unnamed>
       log:  /Users/eric/Work/MB/ATCR/200/Labs/lab 3.smcl
  log type:  smcl
 opened on:  20 Sep 2018, 11:12:27

.
. use class.dta, clear

.
. describe

Contains data from class.dta
  obs:           104
 vars:             9                          11 Jul 2018 05:44
 size:         2,600
-------------
              storage   display    value
variable name   type    format     label      variable label
-------------


apple_any         byte      %10.0g                      any apple devices


apple_n           byte      %10.0g                      # apples devices


children_any      byte      %10.0g                      any children


children_n        byte      %10.0g                      # children


coffee_n          double    %10.0g                      # cups coffee/day


duration          int       %10.0g                      seconds to complete survey


female            byte      %9.0g        yesno          female sex


height            double    %14.2f                      height (inches)


year              int       %9.0g


-------------
Sorted by:

.
. /* 1. Summarize the variables, by year.  Which variables are categorical and
> which are continuous?  Can we tell whether categorical variables are ordinal
> from the descriptive statistics? */
.
. bysort year: sum

-------------
-> year = 2016

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
   apple_any |         55    .9454545    .2291839          0          1
     apple_n |         55    3.090909    1.404658          0          6
children_any |         55    .1454545     .355808          0          1
  children_n |         55    .1818182    .4749623          0          2
    coffee_n |         55    1.581818    1.911863          0         11
-------------+---------------------------------------------------------
    duration |         55    104.7636    207.3497         21       1581
      female |         55    .5272727    .5038572          0          1
      height |         55    67.58364    4.502006         57         75
        year |         55        2016           0       2016       2016

-------------
-> year = 2017

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
   apple_any |         49     .877551    .3312007          0          1
     apple_n |         49    3.367347    3.521919          0         22
children_any |         49    .1836735    .3912304          0          1
  children_n |         49    .3061224    .6832545          0          2
    coffee_n |         49    1.653061    1.169063          0          5
-------------+---------------------------------------------------------
    duration |         49    96.55102     97.3683          1        520
      female |         49    .6530612    .4809288          0          1
      height |         48    75.54167      64.149         60        510
        year |         49        2017           0       2017       2017


.
. /* 2. Were there more females in 2016 or 2017?  Should we use counts or
> percentages to answer this question? */
.
. tab year female, row

+----------------+
| Key            |
+----------------+
|   frequency    |
| row percentage |
+----------------+

           |      female sex
      year |        no        yes |     Total
-----------+----------------------+----------
      2016 |        26         29 |        55
           |     47.27      52.73 |    100.00
-----------+----------------------+----------
      2017 |        17         32 |        49
           |     34.69      65.31 |    100.00
-----------+----------------------+----------
     Total |        43         61 |       104
           |     41.35      58.65 |    100.00

.
. /* 3. Assuming that there should be an even split of 50% males & 50% females,
> calculate the probability of seeing the number of females in each of the 2
> years using the binomial distribution. */
.
. forvalues year = 2016/2017 {
  2.         qui sum female if year==`year'
  3.         local pbf = round(binomialtail(`r(N)',`r(sum)',.5),.01)
  4.         di as result "Probability of at least `r(sum)'/`r(N)' (`=round(`r(mean)'*100,.01)'%) women in `year' class: `pbf'"
  5.         }
Probability of at least 29/55 (52.73%) women in 2016 class: .39
Probability of at least 32/49 (65.31%) women in 2017 class: .02

.
. /* 4. Look at boxplots of the continuous variables.  Are there any outliers?
> What error could account for the maximum value of height? */
.
. set graphics off

. foreach x of varlist *_n duration height {
  2.         graph box `x', over(year) name(box_`x', replace)
  3.         }

. set graphics on

. graph combine box_apple_n box_children_n box_coffee_n box_duration box_height, ///
>         name(boxplots_combined, replace)

.
. /* 5. What is the probability of having no children in 2016? In 2017? */
. tab year children_any, row

+----------------+
| Key            |
+----------------+
|   frequency    |
| row percentage |
+----------------+

           |     any children
      year |         0          1 |     Total
-----------+----------------------+----------
      2016 |        47          8 |        55
           |     85.45      14.55 |    100.00
-----------+----------------------+----------
      2017 |        40          9 |        49
           |     81.63      18.37 |    100.00
-----------+----------------------+----------
     Total |        87         17 |       104
           |     83.65      16.35 |    100.00

.
. /* 6. What is the mean number of cups of coffee/day for each year?  Is the
> mean estimator here?  What might make more sense?  How would you describe the
> distribution of this variable?  */
.
. bysort year: sum coffee_n, detail

-------------
-> year = 2016

                      # cups coffee/day
-------------------------------------------------------------
      Percentiles      Smallest
 1%            0              0
 5%            0              0
10%            0              0       Obs                  55
25%            0              0       Sum of Wgt.          55

50%            1                      Mean           1.581818
                        Largest       Std. Dev.      1.911863
75%            3              4
90%            3              4       Variance       3.655219
95%            4              6       Skewness        2.44075
99%           11             11       Kurtosis       12.01207

-------------
-> year = 2017

                      # cups coffee/day
-------------------------------------------------------------
      Percentiles      Smallest
 1%            0              0
 5%            0              0
10%            0              0       Obs                  49
25%            1              0       Sum of Wgt.          49

50%            2                      Mean           1.653061
                        Largest       Std. Dev.      1.169063
75%            2              3
90%            3              4       Variance       1.366709
95%            4              4       Skewness       .4653882
99%            5              5       Kurtosis        3.14316


. graph box coffee_n, over(year) name(box_cn, replace)

.
. /* 7. Calculate 95% confidence intervals for the mean duration (in seconds)
> that it took to complete the survey, by year.  Which year appeared to take
> longer?  Do the confidence bounds overlap for the 2 years?  Calculate 90%
> confidence bounds.  Do these confidence intervals overlap? Also examine
> boxplots of these data, again stratified by year.  Are there outliers?
> Which group of students took longer?  If you remove any outliers do the
> results look different?  Are the confidence intervals similar? */
.
. mean duration, over(year) cformat(%8.2f)

Mean estimation                   Number of obs   =        104

         2016: year = 2016
         2017: year = 2017

-------------+------------------------------------------------
         Over |       Mean    Std. Err.      [95% Con f. Interval]
-------------+------------------------------------------------
duration     |
        2016 |      104.76       27.96          49.31       160.21
        2017 |       96.55       13.91          68.96       124.14
-------------+------------------------------------------------

. mean duration, over(year) cformat(%8.2f) level(90)

Mean estimation                   Number of obs   =        104

         2016: year = 2016
         2017: year = 2017

-------------+------------------------------------------------
         Over |       Mean    Std. Err.      [90% Con f. Interval]
-------------+------------------------------------------------
duration     |
        2016 |      104.76       27.96          58.36       151.17
        2017 |       96.55       13.91          73.46       119.64
-------------+------------------------------------------------

. graph box duration, over(year) name(box_dur)

. * remove some big values
. preserve

. replace duration = . if duration>250
(4 real changes made, 4 to missing)

. mean duration, over(year) cformat(%8.2f)

Mean estimation                   Number of obs   =        100

         2016: year = 2016
         2017: year = 2017

-------------+------------------------------------------------
         Over |       Mean    Std. Err.      [95% Con f. Interval]
-------------+------------------------------------------------
duration     |
        2016 |       77.43        5.97          65.58        89.27
        2017 |       75.24        6.54          62.26        88.22
-------------+------------------------------------------------

. restore

.
. /* 8. Create histograms of cups of coffee yesterday for each year, describe
> the distribution of the data.  What would you expect to see if you took 40
> samples of size 20 from the data for each year.  Why? */
.
. forvalues year = 2016/2017 {
  2.         histogram coffee_n if year==`year', bin(10) name(cn_`year', replace)
  3.         }
(bin=10, start=0, width=1.1)
(bin=10, start=0, width=.5)

.
. log close
      name:  <unnamed>
       log:  /Users/eric/Work/MB/ATCR/200/Labs/lab 3.smcl
  log type:  smcl
 closed on:  20 Sep 2018, 11:12:35
-------------