-
Notifications
You must be signed in to change notification settings - Fork 8
/
R-Analysis.Rmd
639 lines (439 loc) · 36.3 KB
/
R-Analysis.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
---
title: "R-Analysis"
author: "Data Services @ HSL"
date: "8/9/2018"
output: github_document
---
# AFTERNOON SESSION
### Analysis in R
This session will cover the basics of data analysis in R. We will go over descriptive and inferential statistics including hypothesis testing and assessing assumptions.
This lesson assumes a basic familiarity with R, data frames, and manipulating data with tidyr, dplyr and the pipe `%>%`, and ggplot2.
In this session, we will use the following packages:
- tidyverse
- or readr, dplyr, & ggplot2
```{r lib}
library(tidyverse)
```
### Download data and skeleton script from GitHub into project
Go to GitHub [repo](https://github.com/mariekekjones/BIMS-bootcamp) to get these materials
### NHANES data
The data we're going to work with comes from the National Health and Nutrition Examination Survey (NHANES) program at the CDC. You can read a lot more about NHANES on the [CDC's website](http://www.cdc.gov/nchs/nhanes/) or [Wikipedia](https://en.wikipedia.org/wiki/National_Health_and_Nutrition_Examination_Survey).
NHANES is a research program designed to assess the health and nutritional status of adults and children in the United States. The survey is one of the only to combine both survey questions and physical examinations. It began in the 1960s and since 1999 examines a nationally representative sample of about 5,000 people each year. The NHANES interview includes demographic, socioeconomic, dietary, and health-related questions. The physical exam includes medical, dental, and physiological measurements, as well as several standard laboratory tests. NHANES is used to determine the prevalence of major diseases and risk factors for those diseases. NHANES data are also the basis for national standards for measurements like height, weight, and blood pressure. Data from this survey is used in epidemiology studies and health sciences research, which help develop public health policy, direct and design health programs and services, and expand the health knowledge for the Nation.
We are using a small slice of this data. We're only using a handful of variables from the 2011-2012 survey years on about 5,000 individuals. The CDC uses a [sampling strategy](http://www.cdc.gov/nchs/data/series/sr_02/sr02_162.pdf) to purposefully oversample certain subpopulations like racial minorities. Naive analysis of the original NHANES data can lead to mistaken conclusions because the percentages of people from each racial group in the data are different from general population. The 5,000 individuals here are resampled from the larger NHANES study population to undo these oversampling effects, so you can treat this as if it were a simple random sample from the American population.
Let's read in the data to an object called `nh` and take a look with `View`. Remember, we need to load both the dplyr and readr packages (or tidyverse) for efficiently reading in and displaying this data.
```{r readNH}
# Read in downloaded data using readr package
nh <- read_csv(file="nhanes.csv")
# Show the first few lines of the data and the dimensions
nh
# Optionally bring up data in a viewer window.
# View(nh)
```
This particular excerpt has 5000 observations of 32 variables. We know from our lesson this morning that this dataset contains both children and adults. For us to do analyses with these data, let's focus on adults only.
```{r}
# Create adults only dataset
nha <- nh %>%
filter(Age >= 18)
nha
```
So that we don't get confused between the original nh and the nh adults dataset, let's remove the original
```{r}
rm(nh)
```
A note on characters versus factors: One thing that you immediately notice is that all the categorical variables are read in as _character_ data types. This data type is used for storing strings of text, for example, IDs, names, descriptive text, etc. There's another related data type called _**factors**_.
Factor variables are used to represent categorical variables with two or more _levels_, e.g., "male" or "female" for Gender, or "Single" versus "Committed" for RelationshipStatus. For the most part, statistical analysis treats these two data types the same. It's often easier to leave categorical variables as characters. However, in some cases you may get a warning message alerting you that a character variable was converted into a factor variable during analysis. Generally, these warnings are nothing to worry about. You can, if you like, convert individual variables to factor variables, or simply use dplyr's `mutate_if` to convert all character vectors to factor variables:
```{r}
# investigate Race (categorical variable)
class(nha$Race)
levels(nha$Race)
#create factor variables
nha <- nha %>%
mutate_if(is.character, as.factor)
class(nha$Race)
levels(nha$Race)
```
### Descriptive Statistics
Descriptive statistics are used to describe the basic features of the data. They provide simple summaries about the sample and the measures.
You could do the below operations using dplyr, but remember, this returns a single-row, single-column tibble, _not_ a single scalar value like the above. This is only really useful in the context of grouping and summarizing, so here we will mostly use base R functions.
```{r}
# measures of the center
mean(nha$BMI)
mean(nha$BMI, na.rm = TRUE)
median(nha$BMI, na.rm = TRUE) # right skewed
# histogram to see distribution
nha %>%
ggplot(aes(BMI)) + geom_histogram()
#measures of spread
sd(nha$BMI, na.rm = TRUE)
quantile(nha$BMI, na.rm = TRUE)
quantile(nha$BMI, probs = c(.25, .75), na.rm = TRUE)
range(nha$BMI, na.rm = TRUE)
```
If you run `summary()` on a data frame, you get some very basic summary statistics on each variable in the data.
```{r}
# summary
summary(nha)
```
The `summary()` function is different from **dplyr**'s `summarize()`. It works differently depending on which kind of object you pass to it.
For categorical variables, calculating a mean or standard deviation does not make sense. Instead, we can tabulate the frequencies of each level of the categorical variable.
```{r}
#descriptive stats for discrete variables
table(nha$Race)
#cross tabulation
table(nha$Gender, nha$Race)
```
### ** EXERCISE 1 **
** YOUR TURN **
A. Calculate the median Income in the NHA dataset
B. Find the variance of Weight
C. What is the range of Pulse?
D. Create a density plot showing BMI colored and faceted by Education level
```{r}
#A.
median(nha$Income, na.rm = TRUE)
#B.
var(nha$Weight, na.rm = TRUE)
#C.
range(nha$Pulse, na.rm = TRUE)
#D
nha %>%
ggplot(aes(BMI)) + geom_density(aes(color = Education)) +
facet_wrap(~ Education)
```
### T-tests
Slides
Let's do a few two-sample t-tests to test for the _difference in means between two groups_. The function for a t-test is `t.test()`. See the help for `?t.test`.
1. Are there differences in height for males versus females in this dataset?
To assess this question, first we make sure that a t-test is the correct type of analysis. A t-test tests the difference in 2 means - yes that is what we want to do. Next we need to decide what type of t-test we need to perform by thinking through the assumptions. Domain specific knowledge and exploratory data analyses will help here.
Random sampling -- YES
Independent samples -- YES (men and women are different people - unrelated). Would be paired t-test if we were assessing height of husband-wife pairs or brother-sister pairs
Normality -- ?? well, we need to assess this. We'll discuss this in a few minutes.
Equal variance. Also called homoscedasticity of variances.
?? we could think about the populations of men and women and what we know about height and conclude reasonably that the variance is equal for height
Density plots are an excellent way to assess normality and equal variance all in one plot
```{r}
# EDA
#create overlapping density plots of height colored by gender
nha %>%
ggplot(aes(Height)) + geom_density(aes(fill = Gender, alpha = .5))
```
Normality can be assessed graphically or via hypothesis tests. There are pros and cons to either approach.
Graphically, we could look at a histogram or density plot (like we did above), a boxplot, or a more specialized plot to assess normality called a QQ plot (quantile-quantile plot or quantile comparison plot or normal probability plot). A qq plot graphs the expected data value given a normal distribution on the X axis against the observed data value on the y axis.
Let's have a look for height:
```{r}
#normality is best assessed using qq plots for each sex
nha %>%
ggplot(aes(sample = Height)) +
geom_qq() +
facet_wrap(~Gender)
```
Certain fields love hypothesis tests of normality and sometimes reviewers will specifically request one. There is a theoretical problem with trying to _prove_ a null hypothesis and they are known to reject the null when sample sizes are large. My best advice is to use your brain, subject matter expertise, and graphical assessments as much as possible, but in case you are forced to do a hypothesis test for normality check out shapiro.test()
The least awful seems to be the Shapiro-Wilk (https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3693611/) but there are several options (Kolmogorov-Smirnov, Anderson-Darling, Lillefors). We will not spend time to cover those here because the field is moving towards qq plots.
Now that we know what test to run, let's run it
```{r}
# equal variance, independent samples t-test
?t.test()
t.test(Height ~ Gender, data = nha, var.equal = TRUE)
# if we didn't have normality met, run wilcox.test
wilcox.test(Height ~ Gender, data = nha)
```
### ** EXERCISE 2 **
** YOUR TURN **
Still using the adults (`nha`) dataset, use a test of 2 means to assess whether single or married/cohabitating people (using the RelationshipStatus variable) drink more alcohol (AlcoholYear). (Please feel free to ask for help)
A. First, create plot to explore the distribution of the variables and the relationship between these two variables.
B. Next, consider test assumptions to decide what analysis to run.
C. Run the analysis. Is the association between relationship status and alcohol consumption statistically significant? If so, which group drinks more often?
```{r}
#A.
nha %>%
filter(!is.na(RelationshipStatus)) %>%
ggplot(aes(AlcoholYear)) +
geom_density(aes(fill = RelationshipStatus), alpha = .3)
#B&C based on plots, do wilcox
wilcox.test(AlcoholYear ~ RelationshipStatus, data = nha)
#calculate median and IQR for each group
nha %>%
group_by(RelationshipStatus) %>%
summarize(medAlc = median(AlcoholYear, na.rm = TRUE),
Q1Alc = quantile(AlcoholYear, probs = .25, na.rm = TRUE),
Q3Alc = quantile(AlcoholYear, probs = .75, na.rm = TRUE))
```
### ANOVA and LM
See slides
> Analysis of variance and linear modeling are complex topics that deserve an entire semester dedicated to theory, design, and interpretation. A very good resource is [_An Introduction to Statistical Learning: with Applications in R_](https://www.amazon.com/Introduction-Statistical-Learning-Applications-Statistics/dp/1461471370/ref=sr_1_1?ie=UTF8&qid=1473087847&sr=8-1&keywords=introduction+statistical+learning&tag=gettgenedone-20) by Gareth James, Daniela Witten, Trevor Hastie and Robert Tibshirani. The [PDF](http://www-bcf.usc.edu/~gareth/ISL/ISLR%20Sixth%20Printing.pdf) of the book and all the R code used throughout are [available **free** on the author's website](http://www-bcf.usc.edu/~gareth/ISL/). What follows is a necessary over-simplification with more focus on implementation, and less on theory and design.
Where t-tests and their nonparametric substitutes are used for assessing the differences in means between two groups, ANOVA is used to assess the significance of differences in means between multiple groups. In fact, a t-test is just a specific case of ANOVA when you only have two groups. And both t-tests and ANOVA are just specific cases of linear regression, where you're trying to fit a model describing how a continuous outcome (e.g., BMI) changes with some predictor variable (e.g., diabetic status, race, age, etc.). The distinction is largely semantic -- with a linear model you're asking, "do levels of a categorical variable affect the response?" where with ANOVA or t-tests you're asking, "does the mean response differ between levels of a categorical variable?"
Let's examine the relationship between BMI and relationship status (`RelationshipStatus` was derived from `MaritalStatus`, coded as _Committed_ if MaritalStatus is Married or LivePartner, and _Single_ otherwise). Let's first do this with a t-test, and for now, let's assume that the variances between groups _are_ equal.
```{r}
# t-test with equal variance for BMI explained by relationship status
t.test(BMI ~ RelationshipStatus, data = nha, var.equal = TRUE)
```
It looks like single people have a very slightly higher BMI than those in a committed relationship, but the magnitude of the difference is trivial, and the difference is not significant. Now, let's do the same test in a linear modeling framework. First, let's create the fitted model and store it in an object called `fit`.
```{r}
# Same question run in a linear model framework
fit <- lm(BMI ~ RelationshipStatus, data = nha)
fit
summary(fit)
```
>_**A note on dummy coding:**_ If you have a $k$-level factor, R creates $k-1$ dummy variables, or indicator variables, by default, using the alphabetically first level as baseline. For example, the levels of RelationshipStatus are "Committed" and "Single". R creates a dummy variable called "RelationshipStatusSingle" that's **0** if you're committed, and **1** if you're Single. The linear model is saying for every unit increase in RelationshipStatusSingle, i.e., going from committed to single, results in a 0.341-unit increase in BMI. You can change the ordering of the factors to change the interpretation of the model (e.g., treating Single as baseline and going from Single to Committed). We'll do this in the next section.
```{r}
# same question run as an ANOVA
anova(fit)
```
Now notice a few things:
1. The p-values from all three tests (t-test, ANOVA, and linear regression) are all identical (p=0.1256). This is because they're all identical: a t-test is a specific case of ANOVA, which is a specific case of linear regression.
1. The test statistics are all related. The _t_ statistic from the t-test is **1.532**, which is the same as the t-statistic from the linear regression. If you square that, you get **2.347**, the _F_ statistic from the ANOVA.
1. The `t.test()` output shows you the means for the two groups, Committed and Single. Just displaying the `fit` object itself or running `summary(fit)` shows you the coefficients for a linear model. Here, the model assumes the "baseline" RelationshipStatus level is _Committed_ (first alphabetically), and that the _intercept_ in a regression model (e.g., $\beta_{0}$ in the model $Y = \beta_{0} + \beta_{1}X$) is the mean of the baseline group (28.51 is the mean BMI for Committed people). Being _Single_ results in an increase in BMI of 0.3413. This is the $\beta_{1}$ coefficient in the model. You can easily change the ordering of the levels. See the help for `?factor`, and check out the new [**forcats** package](http://forcats.tidyverse.org/), which provides tools **for** manipulating **cat**egorical variables.
### ANOVA with 3 groups
Recap: t-tests are for assessing the differences in means between _two_ groups. A t-test is a specific case of ANOVA, which is a specific case of a linear model. Let's run ANOVA, but this time looking for differences in means between more than two groups.
Let's look at the relationship between smoking status (Never, Former, or Current), and BMI.
```{r}
# levels of smoking status
levels(nha$SmokingStatus)
# linear model for the relationship between smoking status and BMI
fit <- lm(BMI ~ SmokingStatus, data = nha)
anova(fit)
summary(fit)
```
The F-test on the ANOVA table tells us that there _is_ a significant difference in means between current, former, and never smokers (p=$4.54 \times 10^{-8}$). However, the linear model output might not have been what we wanted. Because the default handling of categorical variables is to treat the alphabetical first level as the baseline, "Current" smokers are treated as baseline, and this mean becomes the intercept, and the coefficients on "Former" and "Never" describe how those groups' means differ from current smokers.
What if we wanted "Never" smokers to be the baseline, followed by Former, then Current? Have a look at `?factor` and `?relevel` to change the factor levels.
```{r}
#change reference category to Never
nha$SmokingStatus <- factor(nha$SmokingStatus, levels = c("Never", "Former", "Current"))
#check re-leveling
levels(nha$SmokingStatus)
# re-create the fit and ANOVA summary
fit <- lm(BMI ~ SmokingStatus, data = nha)
anova(fit)
summary(fit)
```
Notice that the p-value on the ANOVA/regression didn't change, but the coefficients did. _Never_ smokers are now treated as baseline. The intercept coefficient (28.856) is now the mean for _Never_ smokers. The `SmokingStatusFormer` coefficient of .309 shows the apparent increase in BMI that former smokers have when compared to never smokers, but that difference is not significant (p=.24). The `SmokingStatusCurrent` coefficient of -1.464 shows that current smokers actually have a lower BMI than never smokers, and that this decrease is highly significant.
Finally, you can do the typical post-hoc ANOVA procedures on the fit object. For example, the `TukeyHSD()` function will run [_Tukey's test_](https://en.wikipedia.org/wiki/Tukey%27s_range_test) (also known as _Tukey's range test_, the _Tukey method_, _Tukey's honest significance test_, _Tukey's HSD test_ (honest significant difference), or the _Tukey-Kramer method_). Tukey's test computes all pairwise mean difference calculation, comparing each group to each other group, identifying any difference between two groups that's greater than the standard error, while controlling the type I error for all multiple comparisons. First run `aov()` (**not** `anova()`) on the fitted linear model object, then run `TukeyHSD()` on the resulting analysis of variance fit.
```{r}
# check out Tukey's multiple comparisons
TukeyHSD(aov(fit))
plot(TukeyHSD(aov(fit)))
```
This shows that there isn't much of a difference between former and never smokers, but that both of these differ significantly from current smokers, who have significantly lower BMI.
Finally, let's visualize the differences in means between these groups. The **NA** category, which is omitted from the ANOVA, contains all the observations who have missing or non-recorded Smoking Status.
```{r}
# plot results
nha %>%
ggplot(aes(SmokingStatus, BMI)) + geom_boxplot()
# plot results without NA bc ANOVA automatically removed those
nha %>%
filter(!is.na(SmokingStatus)) %>%
ggplot(aes(SmokingStatus, BMI)) + geom_boxplot()
```
### Linear model with 2 continuous variables
See slides
Linear models are mathematical representations of the process that (_we think_) gave rise to our data. The model seeks to explain the relationship between a variable of interest, our _Y_, _outcome_, _response_, or _dependent_ variable, and one or more _X_, _predictor_, or _independent_ variables. Previously we talked about t-tests or ANOVA in the context of a simple linear regression model with only a single predictor variable, $X$:
$$Y = \beta_{0} + \beta_{1}X$$
But you can have multiple predictors in a linear model that are all additive, accounting for the effects of the others:
$$Y = \beta_{0} + \beta_{1}X_{1} + \beta_{2}X_{2} + \epsilon$$
- $Y$ is the response
- $X_{1}$ and $X_{2}$ are the predictors
- $\beta_{0}$ is the intercept, and $\beta_{1}$, $\beta_{2}$ etc are _coefficients_ that describe what 1-unit changes in $X_{1}$ and $X_{2}$ do to the outcome variable $Y$.
- $\epsilon$ is random error. Our model will not perfectly predict $Y$. It will be off by some random amount. We assume this amount is a random draw from a Normal distribution with mean 0 and standard deviation $\sigma$.
_Building a linear model_ means we propose a linear model and then estimate the coefficients and the variance of the error term. Above, this means estimating $\beta_{0}, \beta_{1}, \beta_{2}$ and $\sigma$. This is what we do in R.
Let's look at the relationship between height and weight.
```{r}
# Create LM fit object
fit <- lm(Weight ~ Height, data = nha)
summary(fit)
#for each 1 cm increase in Height, we have a .92 kg increase in weight
#confint(fit) #to get out confidence intervals for LM
```
The relationship is highly significant (P<$2.2 \times 10^{-16}$). The intercept term is not very useful most of the time. Here it shows us what the value of Weight would be when Height=0cm, which could never happen. The Height coefficient is meaningful -- each one cm increase in height results in a 0.92 kg increase in weight. Let's visualize that relationship:
```{r}
#plot these results
nha %>%
ggplot(aes(Height, Weight)) + geom_point() + geom_smooth(method = "lm")
```
By default, this is only going to show the prediction over the range of the data. This is important! You never want to try to extrapolate response variables outside of the range of your predictor(s). For example, the linear model tells us that weight is -73.7kg when height is zero cm. We could extend the predicted model / regression line past the lowest value of the data down to height=0. The bands on the confidence interval tell us that the model is apparently confident within the regions defined by the gray boundary. But this is silly -- we would never see a height of zero, and predicting past the range of the available training data is never a good idea.
After we have fit a model, we need to assess whether the assumptions of linear modeling have been met before we know whether we have a good model or not. We will not have time to go through the assumptions in depth here, but generally they are:
1. Random sampling
1. x and y are related by a a straight line
1. residuals are independent from each other
1. residuals are distributed normally
1. equal variance of residuals (variance is constant across X)
Phew. That is a lot of assumptions. Luckily, R has a wonderful built-in plot function to help you assess the assumptions.
```{r}
# check assumptions of LM
plot(fit)
```
### Multiple regression
Next, let's do a multiple linear regression analysis, where we attempt to model the effect of multiple predictor variables at once on some continuous outcome. First, let's look at the effect of physical activity on testosterone levels. Let's do this with a t-test and linear regression, showing that you get the same results.
```{r}
#t-test for Testosterone ~ PhysActive
t.test(Testosterone~PhysActive, data=nha, var.equal=TRUE)
#lm for Testosterone ~ PhysActive
summary(lm(Testosterone~PhysActive, data=nha))
```
In both cases, the p-value is significant (p=0.01516), and the result suggest that increased physical activity is associated with increased testosterone levels. Does increasing your physical activity increase your testosterone levels? Or is it the other way -- will increased testosterone encourage more physical activity? Or is it none of the above -- is the apparent relationship between physical activity and testosterone levels only apparent because both are correlated with yet a third, unaccounted for variable? Let's throw Age into the model as well.
```{r}
summary(lm(Testosterone~PhysActive+Age, data=nha))
```
This shows us that after accounting for age that the testosterone / physical activity link is no longer significant. Every 1-year increase in age results in a highly significant decrease in testosterone, and since increasing age is also likely associated with decreased physical activity, perhaps age is the confounder that makes this relationship apparent.
Adding other predictors can also swing things the other way. We know that men have much higher testosterone levels than females. Sex is probably the single best predictor of testosterone levels in our dataset. By not accounting for this effect, our unaccounted-for variation remains very high (low $$R^{2}$$). By accounting for Gender, we now reduce the residual error in the model, and the physical activity effect once again becomes significant. Also notice that our model fits much better (higher R-squared), and is much more significant overall.
```{r}
summary(lm(Testosterone ~ PhysActive+Age+Gender, data=nha))
```
We've only looked at the [`summary()`](http://stat.ethz.ch/R-manual/R-patched/library/stats/html/summary.lm.html) and [`anova()`](http://stat.ethz.ch/R-manual/R-patched/library/stats/html/anova.lm.html) functions for extracting information from an [`lm` class object](http://stat.ethz.ch/R-manual/R-patched/library/stats/html/lm.html). There are several other accessor functions that can be used on a linear model object. Check out the help page for each one of these to learn more.
- [`coefficients()`](http://stat.ethz.ch/R-manual/R-patched/library/stats/html/coef.html)
- [`predict.lm()`](http://stat.ethz.ch/R-manual/R-patched/library/stats/html/predict.lm.html)
- [`fitted.values()`](http://stat.ethz.ch/R-manual/R-patched/library/stats/html/fitted.values.html)
- [`residuals()`](http://stat.ethz.ch/R-manual/R-patched/library/stats/html/residuals.html)
### ** EXERCISE 3 **
** YOUR TURN **
The `Work` variable is coded "Looking" (n=159), "NotWorking" (n=1317), and "Working" (n=2230).
A. Fit a linear model of `Income` against `Work`. Assign this to an object called `fit`. What does the `fit` object tell you when you display it directly?
B. Run an `anova()` to get the ANOVA table. Is the model significant?
C. Run a Tukey test to get the pairwise contrasts. (Hint: `TukeyHSD()` on `aov()` on the fit). What do you conclude?
D. Instead of thinking of this as ANOVA, think of it as a linear model. After you've thought about it, get some `summary()` statistics on the fit. Do these results jibe with the ANOVA model?
```{r}
#A. Income ~ Work
fit <- lm(Income ~ Work, data = nha)
fit
#B. anova
anova(fit)
#C. Tukey
plot(TukeyHSD(aov(fit)))
#D. summary()
summary(fit)
```
### DISCRETE VARIABLE ANALYSES
So far we have covered:
1. T-tests -- analyzing differences in one continuous variable between 2 groups
2. ANOVA -- analyzing differences in one continuous variable between 3+ groups
3. LM -- analyzing the impact of one continuous variable on another continuous variable
4. Multiple regression -- analyzing the impact of several continuous variables on another continuous variable
In all of these cases, the dependent variable, i.e., the outcome, or $Y$ variable, was _continuous_, and usually normally distributed. What if our outcome variable is _discrete_, e.g., "Yes/No", "Mutant/WT", "Case/Control", etc.? Here we use a different set of procedures for assessing significant associations.
### Contingency tables
The [`xtabs()`](http://stat.ethz.ch/R-manual/R-patched/library/stats/html/xtabs.html) function is useful for creating contingency tables from categorical variables. Let's create a gender by diabetes status contingency table, and assign it to an object called **`xt`**. After making the assignment, type the name of the object to view it.
```{r}
#cross tabulation of Gender and Diabetes
xt <- xtabs(~Gender+Diabetes, data=nha)
xt
```
There are two useful functions, `addmargins()` and `prop.table()` that add more information or manipulate how the data is displayed. By default, `prop.table()` will divide the number of observations in each cell by the total. But you may want to specify _which margin_ you want to get proportions over. Let's do this for the first (row) margin.
```{r}
# Add marginal totals
addmargins(xt)
# Get the proportional table
prop.table(xt)
#each cell divided by grand total
# That isn't really what we want
# Do this over the first (row) margin only.
?prop.table
prop.table(xt, margin=1)
```
Looks like men have slightly higher rates of diabetes than women. But is this significant?
The chi-square test is used to assess the independence of these two factors. That is, if the null hypothesis that gender and diabetes are independent is true, the we would expect a proportionally equal number of diabetics across each sex. Males seem to be at slightly higher risk than females, but the difference is just short of statistically significant.
```{r}
#chi square for diabetes and gender
chisq.test(xt)
```
An alternative to the chi-square test is [Fisher's exact test](https://en.wikipedia.org/wiki/Fisher%27s_exact_test). Rather than relying on a critical value from a theoretical chi-square distribution, Fisher's exact test calculates the _exact_ probability of observing the contingency table as is. It's especially useful when there are very small _n_'s in one or more of the contingency table cells. Both the chi-square and Fisher's exact test give us p-values of approximately 0.06.
```{r}
# fisher's exact test for diabetes and gender
fisher.test(xt)
```
Let's create a different contingency table, this time looking at the relationship between race and whether the person had health insurance. Display the table with marginal totals.
```{r}
xt <- xtabs(~Race+Insured, data=nha)
addmargins(xt)
```
Let's do the same thing as above, this time showing the proportion of people in each race category having health insurance.
```{r}
prop.table(xt, margin=1)
```
Now, let's run a chi-square test for independence.
```{r}
chisq.test(xt)
```
The result is _highly_ significant. In fact, so significant, that the display rounds off the p-value to something like $<2.2 \times 10^{-16}$. If you look at the help for [`?chisq.test`](https://stat.ethz.ch/R-manual/R-patched/library/stats/html/chisq.test.html) you'll see that displaying the test only shows you summary information, but other components can be accessed. For example, we can easily get the actual p-value, or the expected counts under the null hypothesis of independence.
```{r}
chisq.test(xt)$p.value
chisq.test(xt)$expected
```
We can also make a helpful plot for visualizing categorical data called a mosaic plot: (this is a base R plot, not ggplot2)
```{r, fig.width=8}
# plot for categorical data
mosaicplot(xt, main=NA)
```
Now we can easily see the proportion of each Race that is insured and the proportion of each Race in the dataset overall
### Logistic regression
See slides
What if we wanted to model the discrete outcome, e.g., whether someone is insured, against several other variables, similar to how we did with multiple linear regression? We can't use linear regression because the outcome isn't continuous -- it's binary, either _Yes_ or _No_. For this we'll use _logistic regression_ to model the _log odds_ of binary response. That is, instead of modeling the outcome variable, $Y$, directly against the inputs, we'll model the _log odds_ of the outcome variable.
If $p$ is the probability that the individual is insured, then $\frac{p}{1-p}$ is the [_odds_](https://en.wikipedia.org/wiki/Odds) that person is insured. Then it follows that the linear model is expressed as:
$$log(\frac{p}{1-p}) = \beta_0 + \beta_1 x_1 + \cdots + \beta_k x_k$$
Where $\beta_0$ is the intercept, $\beta_1$ is the increase in the odds of the outcome for every unit increase in $x_1$, and so on.
Logistic regression is a type of _generalized linear model_ (GLM). We fit GLM models in R using the `glm()` function. It works like the `lm()` function except we specify which GLM to fit using the `family` argument. Logistic regression requires `family=binomial`.
The typical use looks like this:
```{r}
# mod <- glm(y ~ x, data=yourdata, family='binomial')
# summary(mod)
```
Before we fit a logistic regression model let's _relevel_ the Race variable so that "White" is the baseline. We saw above that people who identify as "White" have the highest rates of being insured. When we run the logistic regression, we'll get a separate coefficient (effect) for each level of the factor variable(s) in the model, telling you change in log odds that that level has, _as compared to the baseline group_.
```{r, results="hide"}
#Look at levels of Race. The default ordering is alphabetical
levels(nha$Race)
# Let's relevel that where the group with the highest rate of insurance is "baseline"
relevel(nha$Race, ref="White")
# If we're happy with that result, permanently change it using dplyr
nha <- nha %>%
mutate(Race=relevel(Race, ref="White"))
```
Now, let's fit a logistic regression model assessing how the odds of being insured change with different levels of race.
```{r}
#logistic regression of insurance ~ race
fit <- glm(Insured~Race, data=nha, family="binomial")
summary(fit)
```
The `Estimate` column shows the log of the odds ratio -- how the log odds of having health insurance changes at each level of race compared to White. The P-value for each coefficient is on the far right. This shows that _every_ other race has _significantly less_ rates of health insurance coverage. But, as in our multiple linear regression analysis above, are there other important variables that we're leaving out that could alter our conclusions? Lets add a few more variables into the model to see if something else can explain the apparent Race-Insured association. Let's add a few things likely to be involved (Age and Income), and something that's probably irrelevant (hours slept at night).
```{r}
#logistic regression of Insured with lots of predictors
fit <- glm(Insured ~ Age+Income+SleepHrsNight+Race, data=nha, family="binomial")
summary(fit)
```
A few things become apparent:
1. Age and income are both highly associated with whether someone is insured. Both of these variables are highly significant ($P<2.2 \times 10^{-16}$), and the coefficient (the `Estimate` column) is positive, meaning that for each unit increase in one of these variables, the log odds of being insured increases by the corresponding amount.
1. Hours slept per night is not meaningful at all.
1. After accounting for age and income, several of the race-specific differences are no longer statistically significant, but others remain so.
1. The absolute value of the test statistic (column called `z value`) can roughly be taken as an estimate of the "importance" of that variable to the overall model. So, age and income are the most important influences in this model; self-identifying as Hispanic or Mexican are also very highly important, hours slept per night isn't important at all, and the other race categories fall somewhere in between.
There is _much_ more to go into with logistic regression. This lesson only scratches the surface. Missing from this lesson are things like regression diagnostics, model comparison approaches, penalization, interpretation of model coefficients, fitting interaction effects, and much more. Alan Agresti's [_Categorical Data Analysis_](https://www.amazon.com/Categorical-Data-Analysis-Alan-Agresti/dp/0470463635/ref=sr_1_1?ie=UTF8&qid=1473180895&sr=8-1&keywords=categorical+data+analysis&tag=gettgenedone-20) has long been considered the definitive text on this topic. I also recommend Agresti's [_Introduction to Categorical Data Analysis_](https://www.amazon.com/Introduction-Categorical-Data-Analysis/dp/0471226181/ref=sr_1_3?ie=UTF8&qid=1473180895&sr=8-3&keywords=categorical+data+analysis&tag=gettgenedone-20) (a.k.a. "Agresti lite") for a gentler introduction.
### ** EXERCISE 4 **
** YOUR TURN **
1. What's the relationship between diabetes and participating in rigorous physical activity or sports?
A. Create a contingency table with Diabetes status in rows and physical activity status in columns.
B. Display that table with margins.
C. Show the proportions of diabetics and nondiabetics, separately, who are physically active or not.
- Is this relationship significant?
- Create a mosaic plot to visualize the relationship
```{r, include=FALSE, eval=FALSE}
#A.
xt <- xtabs(~Diabetes+PhysActive, data=nha)
#B.
addmargins(xt)
#C.
prop.table(xt, margin=1)
chisq.test(xt)
mosaicplot(xt)
# assocplot(xt)
```
2. Model the same association in a logistic regression framework to assess the risk of diabetes using physical activity as a predictor.
<!-- - First, make Diabetes a factor variable if you haven't already (`nha$Diabetes <- factor(nha$Diabetes)`). -->
A. Fit a model with just physical activity as a predictor, and display a model summary.
B. Add gender to the model, and show a summary.
C. Continue adding weight and age to the model. What happens to the gender association?
D. Continue and add income to the model. What happens to the original association with physical activity?
```{r, include=FALSE, eval=FALSE}
#A.
summary(glm(Diabetes~PhysActive, data=nha, family="binomial"))
#B.
summary(glm(Diabetes~PhysActive+Gender, data=nha, family="binomial"))
#C.
summary(glm(Diabetes~PhysActive+Gender+Age+Weight, data=nha, family="binomial"))
#D.
summary(glm(Diabetes~PhysActive+Gender+Age+Weight+Income, data=nha, family="binomial"))
```