-
Notifications
You must be signed in to change notification settings - Fork 0
/
test1.Rhtml
355 lines (306 loc) · 11.8 KB
/
test1.Rhtml
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
<!--begin.rcode echo=FALSE, results='hide'
options(width = 60, digits = 2, warn=-1)
opts_chunk$set(fig.width=7, fig.height=5, out.width=700, out.height=500, fig.path="figures/models-", warning=FALSE, message=FALSE)
end.rcode-->
<!DOCTYPE html>
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=EB+Garamond' rel='stylesheet' type='text/css'>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=1024, user-scalable=no">
<meta name="author" content="Joshua F. Wiley">
<meta name="author-ORCID" content="0000-0002-0271-6702">
<meta name="keywords" content="R, Statistics, Models">
<meta name="description" content="Visualizing data and estimating models in R">
<title>Introduction to Modeling in R</title>
<!-- Required stylesheet -->
<link rel="stylesheet" href="core/deck.core.css">
<!-- Extension CSS files go here. Remove or add as needed. -->
<link rel="stylesheet" href="extensions/goto/deck.goto.css">
<link rel="stylesheet" href="extensions/menu/deck.menu.css">
<link rel="stylesheet" href="extensions/navigation/deck.navigation.css">
<link rel="stylesheet" href="extensions/status/deck.status.css">
<link rel="stylesheet" href="extensions/hash/deck.hash.css">
<link rel="stylesheet" href="extensions/scale/deck.scale.css">
<!-- Style theme. More available in /themes/style/ or create your own. -->
<link rel="stylesheet" href="themes/style/swiss.css">
<!-- Transition theme. More available in /themes/transition/ or create your own. -->
<link rel="stylesheet" href="themes/transition/horizontal-slide.css">
<!-- Required Modernizr file -->
<script src="modernizr.custom.js"></script>
<script type="text/javascript"
src="https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
</head>
<!--begin.rcode dat, include=FALSE, tidy=FALSE
# load data
load("~/Documents/Research/ROMS/Data/ROMS_analyses.rdata")
d <- na.omit(ROMS.analyses[, c("MCS1", "PCS1", "AGE", "logMAR1", "MASTERY",
"LOT", "HOPE", "CNQTotal", "NEONeuroticism", "COMS1", "nolifeevnt1", "GENDER",
"DeltaTreatmentTime1", "panas_pa1")])
colnames(d) <- c("mentalHealth", "physicalHealth", "age", "logMAR1", "mastery",
"optimism", "hope", "cancerNeeds", "neuroticism", "concern", "lifeEvent",
"gender", "deltaTime", "positiveAffect")
d$lifeEvent <- factor(as.numeric(d$lifeEvent), labels = c("yes", "no"))
rm(ROMS.analyses)
end.rcode-->
<body class="deck-container">
<!-- Begin slides. Just make elements with a class of slide. -->
<section class="slide" id="title-slide">
<center>
<h2>Introduction to Modeling in R</h2>
<h3>Joshua Wiley, M.A.</h3>
<h3>Senior Analyst — <a style="color:grey;" href="http://elkhartgroup.com">Elkhart
Group</a></h3>
</center>
</section>
<section class="slide">
<h2>Overview</h2>
<p>Examples using linear regression and linear regression with smooths</p>
<p><code>ggplot2</code> for graphics and visualization</p>
<p>Synthesize models and visualization to</p>
<ul>
<li>Visualize data to inform models <em>and</em></li>
<li>condition on models to explore data</li>
</ul>
</section>
<section class="slide">
<h2>Basics</h2>
<!--begin.rcode fig.height=3, out.height=300
require(ggplot2)
(p <- ggplot(d, aes(mentalHealth)) + geom_histogram(binwidth=2))
end.rcode-->
<p>How would you summarize the distribution?</p>
</section>
<section class="slide">
<h2>Basics</h2>
<p>You could use the expectation
\[E(y) = <!--rinline I(mean(d$mentalHealth)) --> \]
</p>
<!--begin.rcode fig.height=3, out.height=300
p + geom_vline(xintercept=mean(d$mentalHealth), size=2, colour="red")
end.rcode-->
</section>
<section class="slide">
<h2>Basics</h2>
<!--begin.rcode
(p <- ggplot(d, aes(mastery, mentalHealth)) + geom_point())
end.rcode-->
<p>How would you summarize this distribution?</p>
</section>
<section class="slide">
<h2>Basics</h2>
<p>You could use the <em>conditional</em> expectation
\( E(y | f(x)) \) where \(f(\cdot)\) is some function, say linear</p>
<!--begin.rcode
m <- lm(mentalHealth ~ mastery, data = d)
end.rcode-->
<p>
\[ \widehat{mentalHealth} = <!--rinline I(coef(m)[1]) --> + <!--rinline I(coef(m)[2]) --> mastery \]
</p>
<!--begin.rcode
p + geom_abline(intercept = coef(m)[1], slope = coef(m)[2])
end.rcode-->
</section>
<section class="slide">
<h2>Multiple dimensions</h2>
<p>The conditional expectation generalizes to \(\mathbb{R}^{N}\), but graphs do not.</p>
<!--begin.rcode
coef(lm(mentalHealth ~ mastery + neuroticism, d))[["mastery"]]
end.rcode-->
<p>Under certain conditions, \( E(y | f(x_1, x_2)) \approx E( y | f(x_1) ~~ | ~~ f(x_2 | f(x_1))~) \)</p>
<!--begin.rcode
m1 <- lm(cbind(mentalHealth, mastery) ~ neuroticism, d)
coef(lm(mentalHealth ~ mastery, rd <- as.data.frame(resid(m1))))[["mastery"]]
end.rcode-->
</section>
<section class="slide">
<h2>Multiple dimensions</h2>
<p>We can take advantage of this for data and model visualization</p>
<!--begin.rcode fig.width=10, out.width=1000
p <- ggplot(d, aes(mastery, mentalHealth)) + geom_point() + stat_smooth(method="lm")
require(gridExtra)
grid.arrange(p, p %+% rd, ncol=2)
end.rcode-->
</section>
<section class="slide">
<h2>Multiple dimensions</h2>
<p>This concept also generalizes:
\( E( y | f(x_1, \ldots, x_{n - 1}) ~~ | ~~ f(x_n | f(x_1, \ldots, x_{n - 1}))~) \)
</p>
<!--begin.rcode
rd <- as.data.frame(resid(lm(cbind(mentalHealth, mastery) ~ age + hope + neuroticism, d)))
p %+% rd
end.rcode-->
</section>
<section class="slide">
<h2>Multiple dimensions</h2>
<!--begin.rcode message=FALSE, warning=FALSE
require(GGally)
ggpairs(d[, c(1, 2, 11, 12)], lower=list(continuous = "smooth"))
end.rcode-->
</section>
<section class="slide">
<h2>Other Functional Forms</h2>
<p>So far simple linear functional forms, but \(f(\cdot)\) can be any</p>
<p>When form is unknown, splines or smooth parameters are useful, especially for
"nuissance" variables</p>
<p>Many options -- <code>mgcv</code> package has the <code>gam</code> function for
<a href="http://www.crcpress.com/ecommerce_product/product_detail.jsf?catno=C4746&isbn=0000000000000">generalized additive models</a> with a general smooth function <code>s()</code> defaulting to thin-plate</p>
<p>Also the <code>splines</code> package for b-splines <code>bs()</code> or
natural cubic splines <code>ns()</code> you can use in any modeling function</p>
<!--begin.rcode
require(mgcv)
require(splines)
end.rcode-->
</section>
<section class="slide">
<h2>Other Functional Forms</h2>
<!--begin.rcode tidy=FALSE
# fit and store models
m.linear <- lm(positiveAffect ~ deltaTime, d)
m.quadratic <- lm(positiveAffect ~ deltaTime + I(deltaTime^2), d)
m.smooth <- gam(positiveAffect ~ s(deltaTime, k = 5), data = d)
# data set of raw and predicted values
pdat <- cbind(d[, c("deltaTime", "positiveAffect")],
linear = fitted(m.linear),
quadratic = fitted(m.quadratic),
smooth = fitted(m.smooth))
# plot of raw data with separate coloured lines by model
p <- ggplot(pdat, aes(deltaTime, positiveAffect)) + geom_point() +
geom_line(aes(y = linear), colour = "green", size=1.2) +
geom_line(aes(y = quadratic), colour = "blue", size=1.2) +
geom_line(aes(y = smooth), colour = "red", size=1.2)
end.rcode-->
</section>
<section class="slide">
<h2>Other Functional Forms</h2>
<!--begin.rcode echo=FALSE
print(p)
end.rcode-->
</section>
<section class="slide">
<h2>Other Functional Forms</h2>
<p>Days since treatment, visual acuity (logMAR), and age are nuissance variables; we care about mastery & positive affect</p>
<!--begin.rcode tidy=FALSE
ldat <- melt(d[, c("deltaTime", "logMAR1", "age", "mastery",
"positiveAffect")], id.vars="positiveAffect")
ggplot(ldat, aes(value, positiveAffect)) + geom_point() +
stat_smooth(se=FALSE) + facet_wrap(~variable, scales="free_x")
end.rcode-->
</section>
<section class="slide">
<h2>Other Functional Forms</h2>
<!--begin.rcode tidy=FALSE
# smooth model - k = 10 for high flexibility
m.smooth <- gam(positiveAffect ~ s(deltaTime, k = 10) +
s(logMAR1, k = 10) + s(age, k = 10), data = d)
# linear model only
m.linear <- lm(positiveAffect ~ deltaTime + logMAR1 + age, data = d)
# pull out residuals in a data set
pdat <- data.frame(mastery = d$mastery,
residualPositiveAffect = c(resid(m.smooth), resid(m.linear)),
Type = rep(c("smooth", "linear"), each = nrow(d)))
# make the graph (separate by model type)
p <- ggplot(pdat, aes(mastery, residualPositiveAffect)) +
geom_point() + stat_smooth(method="lm") +
facet_wrap(~ Type)
end.rcode-->
</section>
<section class="slide">
<h2>Other Functional Forms</h2>
<!--begin.rcode echo=FALSE, fig.width=10, out.width=1000
print(p)
end.rcode-->
<!--begin.rcode
# correlation between residuals
cor(resid(m.smooth), resid(m.linear))
end.rcode-->
</section>
<section class="slide">
<h2>Review</h2>
<div style="float: left; width: 18%;">
<p>Packages</p>
<ul>
<li>ggplot2</li>
<li>gridExtra</li>
<li>GGally</li>
<li>mgcv</li>
<li>reshape</li>
</ul>
</div>
<div style="float: left; width: 40%;">
<p>Graphics Functions</p>
<ul>
<li>ggplot() - setup graph</li>
<li>geom_point() - add points</li>
<li>geom_histogram() - histogram</li>
<li>stat_smooth() - add smooth line</li>
<li>ggpairs() - matrix of graphs</li>
<li>aes() - set aesthetics (e.g., x axis, colour)</li>
</ul>
</div>
<div style="float: right; width: 40%;">
<p>Model Functions</p>
<ul>
<li>lm() - linear model</li>
<li>gam() - additive model</li>
<li>coef() - extract coefficients</li>
<li>resid() - extract residuals</li>
<li>fitted() - extract fitted values</li>
<li>s() - smooth a term</li>
<li>I() - R commands in formulae</li>
</ul>
</div>
</section>
<section class="slide">
<h2>Info</h2>
<ul>
<li>Slides: <a href="http://elkhartgroup.com/rmodels.php">
http://elkhartgroup.com/rmodels.php</a></li>
<li>Company: <a href="http://elkhartgroup.com">
http://elkhartgroup.com </a></li>
<li>Me: josh {a t} elkhartgroup.com</li>
</ul>
</section>
<!-- End slides. -->
<!-- Begin extension snippets. Add or remove as needed. -->
<!-- deck.navigation snippet -->
<a href="#" class="deck-prev-link" title="Previous">←</a>
<a href="#" class="deck-next-link" title="Next">→</a>
<!-- deck.status snippet -->
<p class="deck-status">
<span class="deck-status-current"></span>
/
<span class="deck-status-total"></span>
</p>
<!-- deck.goto snippet -->
<form action="." method="get" class="goto-form">
<label for="goto-slide">Go to slide:</label>
<input type="text" name="slidenum" id="goto-slide" list="goto-datalist">
<datalist id="goto-datalist"></datalist>
<input type="submit" value="Go">
</form>
<!-- deck.hash snippet -->
<a href="." title="Permalink to this slide" class="deck-permalink">#</a>
<!-- End extension snippets. -->
<!-- Required JS files. -->
<script src="jquery-1.7.2.min.js"></script>
<script src="core/deck.core.js"></script>
<!-- Extension JS files. Add or remove as needed. -->
<script src="core/deck.core.js"></script>
<script src="extensions/hash/deck.hash.js"></script>
<script src="extensions/menu/deck.menu.js"></script>
<script src="extensions/goto/deck.goto.js"></script>
<script src="extensions/status/deck.status.js"></script>
<script src="extensions/navigation/deck.navigation.js"></script>
<script src="extensions/scale/deck.scale.js"></script>
<!-- Initialize the deck. You can put this in an external file if desired. -->
<script>
$(function() {
$.deck('.slide');
});
</script>
</body>
</html>