-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJoe_Rovalino_Data606_Wk2_HMWK.rmd
224 lines (165 loc) · 9.27 KB
/
Joe_Rovalino_Data606_Wk2_HMWK.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
---
title: "Chapter 2 - Summarizing Data"
author: "Joe Rovalino"
output:
pdf_document:
extra_dependencies:
- geometry
- multicol
- multirow
html_document:
df_print: paged
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
**Stats scores**. (2.33, p. 78) Below are the final exam scores of twenty introductory statistics students.
57, 66, 69, 71, 72, 73, 74, 77, 78, 78, 79, 79, 81, 81, 82, 83, 83, 88, 89, 94
```{r}
y <-as.integer(c(57, 66, 69, 71, 72, 73, 74, 77, 78, 78, 79, 79, 81, 81, 82, 83, 83, 88, 89, 94))
y
typeof(y)
ss <- data.frame(x = c(1:20), y)
ss
```
Create a box plot of the distribution of these scores. The five number summary provided below may be useful.
```{r}
boxplot(ss$y)
summary(ss$y)
```
\begin{center}
\renewcommand\arraystretch{1.5}
\begin{tabular}{ccccc}
Min & Q1 & Q2 (Median) & Q3 & Max \\
\hline
57 & 72.5 & 78.5 & 82.5 & 94 \\
\end{tabular}
\end{center}
```{r, echo = FALSE}
scores <- c(57, 66, 69, 71, 72, 73, 74, 77, 78, 78, 79, 79, 81, 81, 82, 83, 83, 88, 89, 94)
```
--------------------------------------------------------------------------------
\clearpage
**Mix-and-match**. (2.10, p. 57) Describe the distribution in the histograms below and match them to the box plots.
```{r, echo=FALSE, message=FALSE, warning=FALSE, fig.width=10, fig.height=3}
library(openintro)
set.seed(7365)
sym = rnorm(1000, mean = 60, sd = 3)
uni = runif(1000, min = 0, max = 100)
rs = rgamma(1000, shape = 3, rate = 2)
par(mar=c(4, 3.6, 0, 0), las = 1, mgp = c(2.7, 0.7, 0),
mfrow = c(1,6),
cex.lab = 1.5, cex.axis = 1.5)
histPlot(sym, xlab = "(a)", ylab = "", col = COL[1], axes = FALSE)
axis(1, seq(50,70,10))
histPlot(uni, xlab = "(b)", ylab = "", col = COL[1], axes = FALSE)
axis(1, seq(0,100,50))
histPlot(rs, xlab = "(c)", ylab = "", col = COL[1], axes = FALSE)
axis(1, seq(0,6,2))
boxPlot(rs, xlab = "(1)", ylab = "", col = COL[1,3])
boxPlot(sym, xlab = "(2)", ylab = "", col = COL[1,3])
boxPlot(uni, xlab = "(3)", ylab = "", col = COL[1,3])
```
JR Answer:
histplot a matches with boxplot 2
histplot b matches with boxplot 3
histplot c matches with boxplot 1
--------------------------------------------------------------------------------
\clearpage
**Distributions and appropriate statistics, Part II**. (2.16, p. 59) For each of the following, state whether you expect the distribution to be symmetric, right skewed, or left skewed. Also specify whether the mean or median would best represent a typical observation in the data, and whether the variability of observations would be best represented using the standard deviation or IQR. Explain your reasoning.
(a) Housing prices in a country where 25% of the houses cost below $350,000, 50% of the houses cost below $450,000, 75% of the houses cost below $1,000,000 and there are a meaningful number of houses that cost more than $6,000,000.
JR Answer:
(a) is right skewed. Median would be best representative as it is "more robust statistic because extreme observations have little effect" pg 51 OSv4.pdf. IQR would best represent the variability as the robust statistic.
(b) Housing prices in a country where 25% of the houses cost below $300,000, 50% of the houses cost below $600,000, 75% of the houses cost below $900,000 and very few houses that cost more than $1,200,000.
JR Answer:
(b) is symmetric. Either the mean or median would represent a typical observation.
IQR or SD would represent the variability since symmetric.
(c) Number of alcoholic drinks consumed by college students in a given week. Assume that most of these students don’t drink since they are under 21 years old, and only a few drink excessively.
JR Answer:
(c) is right skewed. Median would be best to have least impact from outliers. IQR qould better handle the variability from the outliers.
(d) Annual salaries of the employees at a Fortune 500 company where only a few high level
executives earn much higher salaries than the all other employees.
JR Answer:
right skewed. Median would be best to have least impact from outliers. IQR qould better handle the variability from the outliers.
--------------------------------------------------------------------------------
\clearpage
**Heart transplants.** (2.26, p. 76) The Stanford University Heart Transplant Study was conducted to determine whether an experimental heart transplant program increased lifespan. Each patient entering the program was designated an official heart transplant candidate, meaning that he was gravely ill and would most likely benefit from a new heart. Some patients got a transplant and some did not. The variable *transplant* indicates which group the patients were in; patients in the treatment group got a transplant and those in the control group did not. Of the 34 patients in the control group, 30 died. Of the 69 people in the treatment group, 45 died. Another variable called *survived* was used to indicate whether or not the patient was alive at the end of the study.
```{r, echo=FALSE, message=FALSE, warning=FALSE, fig.show="hold", out.width="50%", fig.height=3}
library(openintro)
data(heartTr)
# mosaic plot -------------------------------------------------------
par(mar = c(0, 0, 0, 0), las = 1, mgp = c(2.7, 0.9, 0))
mosaicplot(transplant ~ survived, data = heartTr,
main = "", xlab = "", ylab = "", color = COL[1],
cex.axis = 1.5)
# box plot ----------------------------------------------------------
par(mar = c(2, 4.8, 0, 0), las = 1, mgp = c(3.5, 0.7, 0),
cex.lab = 1.5, cex.axis = 1.5)
boxPlot(heartTr$survtime, fact = heartTr$transplant,
ylab = "Survival Time (days)", col = COL[1,2])
```
(a) Based on the mosaic plot, is survival independent of whether or not the
patient got a transplant? Explain your reasoning.
JR Answer:
Looking at the Mosiac plot the treatment group had a little more then 12% better alive metrics (30/34 = .88 died vs 45/69 = .65 died). If survival was independent of a transplant - would epxect to see a closer number to .88.
(b) What do the box plots below suggest about the efficacy (effectiveness) of the heart transplant treatment.
JR Answer:
The median is higher for survival in the transplant category. Also the Q3 (75 quartile is longer in the treatment category so survimal is clearly longer. (IQR is larger number in the treatment group then in control group)
(c) What proportion of patients in the treatment group and what proportion of
patients in the control group died?
JR Answer:
(30/34 = .88 died vs 45/69 = .65 died)
(d) One approach for investigating whether or not the treatment is effective
is to use a randomization technique.
JR Answer:
4 Techniques: controlling, randomization, replication and blocking.
i. What are the claims being tested?
JR answer:
whether an experimental heart transplant program increased lifespan.
ii. The paragraph below describes the set up for such approach, if we were
to do it without using statistical software. Fill in the blanks with a number
or phrase, whichever is appropriate.
> We write *alive* on ___Note_______ cards representing patients who were
alive at the end of the study, and *dead* on ___note______ cards
representing patients who were not. Then, we shuffle these cards and split them
into two groups: one group of size __69_______ representing treatment, and
another group of size ___34_______ representing control. We calculate the
difference between the proportion of *dead* cards in the treatment and
control groups (treatment - control) and record this value. We repeat this 100
times to build a distribution centered at _____normal____. Lastly, we calculate
the fraction of simulations where the simulated differences in proportions are
___normal______. If this fraction is low, we conclude that it is unlikely to
have observed such an outcome by chance and that the null hypothesis should
be rejected in favor of the alternative.
iii. What do the simulation results shown below suggest about the effectiveness
of the transplant program?
JR Answer:
The effetiveness is not significant since highest proportions are around -1 to 0.
```{r fig.height=3, fig.show="hold", message=FALSE, warning=FALSE, include=FALSE, out.width="50%", results='hide'}
# randomization -----------------------------------------------------
diffs <- DATA606::inference(heartTr$survived, heartTr$transplant,
success = "dead", order = c("treatment","control"),
est = "proportion", type = "ht", method = "simulation",
nsim = 100, null = 0, alternative = "twosided", simdist = TRUE,
seed = 95632)
```
\begin{center}
```{r, results='hide', echo=FALSE, message=FALSE, warning=FALSE, fig.show="hold", out.width="75%", fig.height=3}
# plot randomization distribution -----------------------------------
par(mar = c(3.6, 2.2, 1, 1), las = 1, mgp = c(2.5, 0.7, 0),
cex.axis = 1.5, cex.lab = 1.5)
values <- table(diffs)
plot(diffs, type = "n", xlim = c(-0.25, 0.25),
xlab = "simulated differences in proportions",
ylab = "", axes = FALSE, ylim = c(0, max(values)))
axis(1, at = seq(-0.25, 0.25, 0.05),
labels = c(-0.25, NA,-0.15, NA,-0.05, NA, 0.05, NA, 0.15, NA, 0.25))
for(i in 1:length(diffs)){
x <- diffs[i]
rec <- sum(diffs == x)
points(rep(x, rec), 1:rec, pch = 20, cex = 0.8, col = COL[1])
}
```
\end{center}