-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-programming.html
More file actions
404 lines (363 loc) · 31.9 KB
/
simple-programming.html
File metadata and controls
404 lines (363 loc) · 31.9 KB
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Chapter 7 Simple programming | Script Introduction to Data Analysis</title>
<meta name="description" content="Introduction to Data Analysis" />
<meta name="generator" content="bookdown 0.14 and GitBook 2.6.7" />
<meta property="og:title" content="Chapter 7 Simple programming | Script Introduction to Data Analysis" />
<meta property="og:type" content="book" />
<meta property="og:description" content="Introduction to Data Analysis" />
<meta name="github-repo" content="rstudio/bookdown-demo" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Chapter 7 Simple programming | Script Introduction to Data Analysis" />
<meta name="twitter:description" content="Introduction to Data Analysis" />
<meta name="author" content="Lena Janys" />
<meta name="date" content="2019-11-13" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="prev" href="statistical-functions.html"/>
<link rel="next" href="construction-of-functions.html"/>
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />
<style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
{ position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
{ content: attr(data-line-number);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; pointer-events: all; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li><a href="./">A Minimal Book Example</a></li>
<li class="divider"></li>
<li class="chapter" data-level="1" data-path="intro.html"><a href="intro.html"><i class="fa fa-check"></i><b>1</b> Course Overview</a><ul>
<li class="chapter" data-level="1.1" data-path="intro.html"><a href="intro.html#list"><i class="fa fa-check"></i><b>1.1</b> List of Topics</a></li>
<li class="chapter" data-level="1.2" data-path="intro.html"><a href="intro.html#method"><i class="fa fa-check"></i><b>1.2</b> Methods</a><ul>
<li class="chapter" data-level="1.2.1" data-path="intro.html"><a href="intro.html#data"><i class="fa fa-check"></i><b>1.2.1</b> Data</a></li>
<li class="chapter" data-level="1.2.2" data-path="intro.html"><a href="intro.html#tools"><i class="fa fa-check"></i><b>1.2.2</b> Tools</a></li>
</ul></li>
<li class="chapter" data-level="1.3" data-path="intro.html"><a href="intro.html#exam"><i class="fa fa-check"></i><b>1.3</b> Exam</a></li>
<li class="chapter" data-level="1.4" data-path="intro.html"><a href="intro.html#timetable"><i class="fa fa-check"></i><b>1.4</b> Timetable</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="introduction-to-r.html"><a href="introduction-to-r.html"><i class="fa fa-check"></i><b>2</b> Introduction to R</a><ul>
<li class="chapter" data-level="2.1" data-path="introduction-to-r.html"><a href="introduction-to-r.html#the-r-principle"><i class="fa fa-check"></i><b>2.1</b> The R-Principle</a></li>
<li class="chapter" data-level="2.2" data-path="introduction-to-r.html"><a href="introduction-to-r.html#assigments"><i class="fa fa-check"></i><b>2.2</b> Assigments</a></li>
<li class="chapter" data-level="2.3" data-path="introduction-to-r.html"><a href="introduction-to-r.html#saving-and-removing-objects"><i class="fa fa-check"></i><b>2.3</b> Saving and removing objects</a></li>
<li class="chapter" data-level="2.4" data-path="introduction-to-r.html"><a href="introduction-to-r.html#r-help"><i class="fa fa-check"></i><b>2.4</b> R Help</a></li>
<li class="chapter" data-level="2.5" data-path="introduction-to-r.html"><a href="introduction-to-r.html#using-the-editor"><i class="fa fa-check"></i><b>2.5</b> Using the editor</a></li>
<li class="chapter" data-level="2.6" data-path="introduction-to-r.html"><a href="introduction-to-r.html#working-directory"><i class="fa fa-check"></i><b>2.6</b> Working directory</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="operators.html"><a href="operators.html"><i class="fa fa-check"></i><b>3</b> Operators</a></li>
<li class="chapter" data-level="4" data-path="data-structures-vectors-matrices-lists.html"><a href="data-structures-vectors-matrices-lists.html"><i class="fa fa-check"></i><b>4</b> Data structures: vectors, matrices, lists</a><ul>
<li class="chapter" data-level="4.1" data-path="data-structures-vectors-matrices-lists.html"><a href="data-structures-vectors-matrices-lists.html#numerical-and-non-numeric-objects"><i class="fa fa-check"></i><b>4.1</b> Numerical and non-numeric objects</a></li>
<li class="chapter" data-level="4.2" data-path="data-structures-vectors-matrices-lists.html"><a href="data-structures-vectors-matrices-lists.html#vectors"><i class="fa fa-check"></i><b>4.2</b> Vectors</a></li>
<li class="chapter" data-level="4.3" data-path="data-structures-vectors-matrices-lists.html"><a href="data-structures-vectors-matrices-lists.html#matrices"><i class="fa fa-check"></i><b>4.3</b> Matrices</a></li>
<li class="chapter" data-level="4.4" data-path="data-structures-vectors-matrices-lists.html"><a href="data-structures-vectors-matrices-lists.html#lists"><i class="fa fa-check"></i><b>4.4</b> Lists</a></li>
<li class="chapter" data-level="4.5" data-path="data-structures-vectors-matrices-lists.html"><a href="data-structures-vectors-matrices-lists.html#data-frames"><i class="fa fa-check"></i><b>4.5</b> Data Frames</a></li>
<li class="chapter" data-level="4.6" data-path="data-structures-vectors-matrices-lists.html"><a href="data-structures-vectors-matrices-lists.html#working-with-logical-operators"><i class="fa fa-check"></i><b>4.6</b> Working with logical operators</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="exercises-simple-data-structures.html"><a href="exercises-simple-data-structures.html"><i class="fa fa-check"></i><b>5</b> Exercises: Simple Data structures</a><ul>
<li class="chapter" data-level="5.1" data-path="exercises-simple-data-structures.html"><a href="exercises-simple-data-structures.html#r-as-a-calculator"><i class="fa fa-check"></i><b>5.1</b> R as a calculator</a></li>
<li class="chapter" data-level="5.2" data-path="exercises-simple-data-structures.html"><a href="exercises-simple-data-structures.html#numerical-and-nonnumerical-objects"><i class="fa fa-check"></i><b>5.2</b> Numerical and Nonnumerical Objects</a></li>
<li class="chapter" data-level="5.3" data-path="exercises-simple-data-structures.html"><a href="exercises-simple-data-structures.html#vectors-1"><i class="fa fa-check"></i><b>5.3</b> Vectors</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="statistical-functions.html"><a href="statistical-functions.html"><i class="fa fa-check"></i><b>6</b> Statistical functions</a><ul>
<li class="chapter" data-level="6.1" data-path="statistical-functions.html"><a href="statistical-functions.html#sums-and-frequencies"><i class="fa fa-check"></i><b>6.1</b> Sums and frequencies</a></li>
<li class="chapter" data-level="6.2" data-path="statistical-functions.html"><a href="statistical-functions.html#frequencies"><i class="fa fa-check"></i><b>6.2</b> Frequencies</a></li>
<li class="chapter" data-level="6.3" data-path="statistical-functions.html"><a href="statistical-functions.html#statistical-distributions"><i class="fa fa-check"></i><b>6.3</b> Statistical Distributions</a><ul>
<li class="chapter" data-level="6.3.1" data-path="statistical-functions.html"><a href="statistical-functions.html#normal-distribution"><i class="fa fa-check"></i><b>6.3.1</b> Normal distribution</a></li>
<li class="chapter" data-level="6.3.2" data-path="statistical-functions.html"><a href="statistical-functions.html#binomial-distribution"><i class="fa fa-check"></i><b>6.3.2</b> Binomial distribution</a></li>
</ul></li>
<li class="chapter" data-level="6.4" data-path="statistical-functions.html"><a href="statistical-functions.html#multiplication-of-a-random-variable-with-a-constant"><i class="fa fa-check"></i><b>6.4</b> Multiplication of a random variable with a constant</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="simple-programming.html"><a href="simple-programming.html"><i class="fa fa-check"></i><b>7</b> Simple programming</a><ul>
<li class="chapter" data-level="7.1" data-path="simple-programming.html"><a href="simple-programming.html#using-for-loops"><i class="fa fa-check"></i><b>7.1</b> Using for-loops</a></li>
<li class="chapter" data-level="7.2" data-path="simple-programming.html"><a href="simple-programming.html#the-if-condition"><i class="fa fa-check"></i><b>7.2</b> The if-Condition</a></li>
<li class="chapter" data-level="7.3" data-path="simple-programming.html"><a href="simple-programming.html#the-while-loop"><i class="fa fa-check"></i><b>7.3</b> The while-loop</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="construction-of-functions.html"><a href="construction-of-functions.html"><i class="fa fa-check"></i><b>8</b> Construction of functions</a><ul>
<li class="chapter" data-level="8.1" data-path="construction-of-functions.html"><a href="construction-of-functions.html#simple-functions"><i class="fa fa-check"></i><b>8.1</b> Simple functions</a></li>
<li class="chapter" data-level="8.2" data-path="construction-of-functions.html"><a href="construction-of-functions.html#functions-with-multiple-output-objects"><i class="fa fa-check"></i><b>8.2</b> Functions with multiple output objects</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="r-exercises.html"><a href="r-exercises.html"><i class="fa fa-check"></i><b>9</b> R Exercises</a><ul>
<li class="chapter" data-level="9.1" data-path="r-exercises.html"><a href="r-exercises.html#matrices-1"><i class="fa fa-check"></i><b>9.1</b> Matrices</a></li>
<li class="chapter" data-level="9.2" data-path="r-exercises.html"><a href="r-exercises.html#sums-and-frequencies-1"><i class="fa fa-check"></i><b>9.2</b> Sums and frequencies</a></li>
<li class="chapter" data-level="9.3" data-path="r-exercises.html"><a href="r-exercises.html#logical-operators"><i class="fa fa-check"></i><b>9.3</b> Logical Operators</a></li>
<li class="chapter" data-level="9.4" data-path="r-exercises.html"><a href="r-exercises.html#statistical-distributions-1"><i class="fa fa-check"></i><b>9.4</b> Statistical Distributions</a><ul>
<li class="chapter" data-level="9.4.1" data-path="r-exercises.html"><a href="r-exercises.html#exercise-1"><i class="fa fa-check"></i><b>9.4.1</b> Exercise 1</a></li>
<li class="chapter" data-level="9.4.2" data-path="r-exercises.html"><a href="r-exercises.html#exercise-2"><i class="fa fa-check"></i><b>9.4.2</b> Exercise 2</a></li>
<li class="chapter" data-level="9.4.3" data-path="r-exercises.html"><a href="r-exercises.html#exercise-3"><i class="fa fa-check"></i><b>9.4.3</b> Exercise 3</a></li>
</ul></li>
<li class="chapter" data-level="9.5" data-path="r-exercises.html"><a href="r-exercises.html#loops-and-functions"><i class="fa fa-check"></i><b>9.5</b> Loops and functions</a><ul>
<li class="chapter" data-level="9.5.1" data-path="r-exercises.html"><a href="r-exercises.html#exercise-1-1"><i class="fa fa-check"></i><b>9.5.1</b> Exercise 1</a></li>
<li class="chapter" data-level="9.5.2" data-path="r-exercises.html"><a href="r-exercises.html#exercise-2-1"><i class="fa fa-check"></i><b>9.5.2</b> Exercise 2</a></li>
<li class="chapter" data-level="9.5.3" data-path="r-exercises.html"><a href="r-exercises.html#exercise-3-1"><i class="fa fa-check"></i><b>9.5.3</b> Exercise 3</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="10" data-path="problem-set-1.html"><a href="problem-set-1.html"><i class="fa fa-check"></i><b>10</b> Problem Set 1</a><ul>
<li class="chapter" data-level="10.1" data-path="problem-set-1.html"><a href="problem-set-1.html#estimating-model-parameters"><i class="fa fa-check"></i><b>10.1</b> Estimating model parameters</a></li>
<li class="chapter" data-level="10.2" data-path="problem-set-1.html"><a href="problem-set-1.html#estimate-r2-and-barr2"><i class="fa fa-check"></i><b>10.2</b> Estimate <span class="math inline">\(R^2\)</span> and <span class="math inline">\(\bar{R^2}\)</span></a></li>
<li class="chapter" data-level="10.3" data-path="problem-set-1.html"><a href="problem-set-1.html#simulation-study"><i class="fa fa-check"></i><b>10.3</b> Simulation study</a></li>
</ul></li>
<li class="chapter" data-level="11" data-path="problem-set-2.html"><a href="problem-set-2.html"><i class="fa fa-check"></i><b>11</b> Problem Set 2</a><ul>
<li class="chapter" data-level="11.1" data-path="problem-set-2.html"><a href="problem-set-2.html#implementing-the-f-statistic"><i class="fa fa-check"></i><b>11.1</b> Implementing the F-statistic</a></li>
<li class="chapter" data-level="11.2" data-path="problem-set-2.html"><a href="problem-set-2.html#simulation-study-1"><i class="fa fa-check"></i><b>11.2</b> Simulation Study</a></li>
<li class="chapter" data-level="11.3" data-path="problem-set-2.html"><a href="problem-set-2.html#simulation-study-violating-model-assumptions"><i class="fa fa-check"></i><b>11.3</b> Simulation study: Violating model assumptions</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="problem-set-3-power-and-size.html"><a href="problem-set-3-power-and-size.html"><i class="fa fa-check"></i><b>12</b> Problem Set 3: Power and Size</a><ul>
<li class="chapter" data-level="12.1" data-path="problem-set-3-power-and-size.html"><a href="problem-set-3-power-and-size.html#size"><i class="fa fa-check"></i><b>12.1</b> Size</a></li>
<li class="chapter" data-level="12.2" data-path="problem-set-3-power-and-size.html"><a href="problem-set-3-power-and-size.html#simulation-study-size"><i class="fa fa-check"></i><b>12.2</b> Simulation Study: Size</a></li>
<li class="chapter" data-level="12.3" data-path="problem-set-3-power-and-size.html"><a href="problem-set-3-power-and-size.html#simulation-study-power"><i class="fa fa-check"></i><b>12.3</b> Simulation study: Power</a></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Script Introduction to Data Analysis</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="simple-programming" class="section level1">
<h1><span class="header-section-number">Chapter 7</span> Simple programming</h1>
<p>Very often we want to repeat a particular operation many times: either over different elements of a given data structure (columns of a matrix, individual elements of a vector), or perform an operation until a satisfactory result is reached (run an algorithm until the the value converges to a prespecified level). As well, we sometimes have to check whether a condition about the data is fulfilled, e.g. are elements of a vector equal to 0 and perform different operations depending on the result.</p>
<div id="using-for-loops" class="section level2">
<h2><span class="header-section-number">7.1</span> Using for-loops</h2>
<p>A for-loop is a procedure in which a variable repeats a particular routine, changing its value according to the values of a given data set (usually numeric).</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb1-1" data-line-number="1">x=<span class="dv">0</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2"><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="op">:</span><span class="dv">3</span>) { </a>
<a class="sourceLine" id="cb1-3" data-line-number="3"> x</a>
<a class="sourceLine" id="cb1-4" data-line-number="4"> <span class="kw">print</span>(i)</a>
<a class="sourceLine" id="cb1-5" data-line-number="5"> x=x<span class="op">+</span>i}</a></code></pre></div>
<pre><code>## [1] 1
## [1] 2
## [1] 3</code></pre>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb3-1" data-line-number="1">x</a></code></pre></div>
<pre><code>## [1] 6</code></pre>
<p>We create a scalar x with the value 0 and then begin a for loop where the variable i passes through the values 1, 2 and 3. In the run of the first loop, i has the value 1, i. x is overwritten and is given the value 1 = 0 + 1. In the run of the second loop, i the value 2, i. x is overwritten and has the value 3 = 1 + 2. In the run of the third loop, i the value 3, i. x is overwritten and has the value 6 = 3 + 3.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb5-1" data-line-number="1">x=<span class="dv">0</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2"><span class="cf">for</span> (i <span class="cf">in</span> <span class="dv">1</span><span class="op">:</span><span class="dv">3</span>)</a>
<a class="sourceLine" id="cb5-3" data-line-number="3"> {</a>
<a class="sourceLine" id="cb5-4" data-line-number="4"> x=x<span class="op">+</span>i</a>
<a class="sourceLine" id="cb5-5" data-line-number="5"> z=x<span class="op">*</span>i</a>
<a class="sourceLine" id="cb5-6" data-line-number="6"> }</a>
<a class="sourceLine" id="cb5-7" data-line-number="7">z</a></code></pre></div>
<pre><code>## [1] 18</code></pre>
<p>In the next example, we carry out a simulation study on the law of large numbers. This means that “large” deviations of the sample mean from the expectation for increasing sample size become increasingly improbable. We are now repeating samples from a Bernoulli distribution, that is, from <span class="math inline">\(\mathcal{B}(1.0.5)\)</span>-distribution that simulates the tossing of a fair coin. The sample mean, which results from zeros and ones, corresponds in this case to the relative proportion of the ones. This relative fraction would converge stochastically to 0.5, the expectation value of a <span class="math inline">\(\mathcal{B}(1.0.5)\)</span> distribution for larger samples.</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb7-1" data-line-number="1"><span class="co">#Simulationsstudy: law of large numbers</span></a>
<a class="sourceLine" id="cb7-2" data-line-number="2"></a>
<a class="sourceLine" id="cb7-3" data-line-number="3"><span class="co"># Example: Law of large numbers</span></a>
<a class="sourceLine" id="cb7-4" data-line-number="4"> RelAnteil=<span class="kw">c</span>() <span class="co">###we initialize an "empty vector"</span></a>
<a class="sourceLine" id="cb7-5" data-line-number="5"> N=<span class="dv">500</span><span class="co">#Number of draws</span></a>
<a class="sourceLine" id="cb7-6" data-line-number="6"> <span class="cf">for</span> (n <span class="cf">in</span> <span class="dv">1</span><span class="op">:</span>N)<span class="co">#initialize the loop</span></a>
<a class="sourceLine" id="cb7-7" data-line-number="7"> {</a>
<a class="sourceLine" id="cb7-8" data-line-number="8"> Stichprobe=<span class="kw">rbinom</span>(n,<span class="dv">1</span>,<span class="fl">0.5</span>)<span class="co">#draw a vector with sample size n</span></a>
<a class="sourceLine" id="cb7-9" data-line-number="9"> RelAnteil[n]=<span class="kw">mean</span>(Stichprobe)<span class="co">#calculate the proportion of the ones</span></a>
<a class="sourceLine" id="cb7-10" data-line-number="10"> }</a>
<a class="sourceLine" id="cb7-11" data-line-number="11"><span class="kw">plot</span>(<span class="dv">1</span><span class="op">:</span>N,RelAnteil,<span class="co">#plot the results of all samples</span></a>
<a class="sourceLine" id="cb7-12" data-line-number="12"> <span class="dt">main=</span><span class="st">"B(1,0.5)-Distribution: Law of the large numbers"</span>,</a>
<a class="sourceLine" id="cb7-13" data-line-number="13"> <span class="dt">xlab=</span><span class="st">"n"</span>,<span class="dt">ylab=</span><span class="st">"Relative proportion"</span>,<span class="dt">pch=</span><span class="dv">16</span>,</a>
<a class="sourceLine" id="cb7-14" data-line-number="14"> <span class="dt">cex.axis=</span><span class="fl">1.5</span>,<span class="dt">cex.lab=</span><span class="fl">1.5</span>,<span class="dt">cex.main=</span><span class="fl">1.5</span>)</a>
<a class="sourceLine" id="cb7-15" data-line-number="15"> <span class="kw">abline</span>(<span class="dt">h=</span><span class="fl">0.5</span>,<span class="dt">lwd=</span><span class="dv">2</span>,<span class="dt">col=</span><span class="dv">2</span>)<span class="co">#We add a line for the expected value</span></a></code></pre></div>
</div>
<div id="the-if-condition" class="section level2">
<h2><span class="header-section-number">7.2</span> The if-Condition</h2>
<p>Within a for-loop, it may be useful to perform arithmetic operations only under certain conditions. The basic syntax for this is</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb8-1" data-line-number="1"> <span class="cf">if</span> (condition){</a>
<a class="sourceLine" id="cb8-2" data-line-number="2"> statement </a>
<a class="sourceLine" id="cb8-3" data-line-number="3"> } </a>
<a class="sourceLine" id="cb8-4" data-line-number="4"> <span class="cf">else</span>{</a>
<a class="sourceLine" id="cb8-5" data-line-number="5"> alternative</a>
<a class="sourceLine" id="cb8-6" data-line-number="6"> }</a></code></pre></div>
<p>where else can remain unspecified.</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb9-1" data-line-number="1"><span class="co">###If-condition###</span></a>
<a class="sourceLine" id="cb9-2" data-line-number="2">y<-<span class="kw">c</span>(<span class="dv">1</span>,<span class="dv">4</span>,<span class="dv">5</span>,<span class="dv">2</span>,<span class="dv">7</span>,<span class="dv">8</span>,<span class="dv">2</span>,<span class="dv">4</span>)</a>
<a class="sourceLine" id="cb9-3" data-line-number="3">N <-<span class="st"> </span><span class="kw">length</span>(y)</a>
<a class="sourceLine" id="cb9-4" data-line-number="4"><span class="dv">1</span><span class="op">:</span>N</a>
<a class="sourceLine" id="cb9-5" data-line-number="5"><span class="co">## </span></a>
<a class="sourceLine" id="cb9-6" data-line-number="6">y.sq <-<span class="st"> </span><span class="kw">numeric</span>(N)<span class="co">#creates an empty numeric vector of length N</span></a>
<a class="sourceLine" id="cb9-7" data-line-number="7"></a>
<a class="sourceLine" id="cb9-8" data-line-number="8"><span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="op">:</span>N){ <span class="co">#initializing the for-loop</span></a>
<a class="sourceLine" id="cb9-9" data-line-number="9"> y.sq[i] <-<span class="st"> </span>y[i]<span class="op">^</span><span class="dv">2</span><span class="co">#every element of y.sq is replaced by the respective squared value.</span></a>
<a class="sourceLine" id="cb9-10" data-line-number="10"> <span class="co">#if(i == N){#only when this condition is met, i.e. in the last iteration of the loop </span></a>
<a class="sourceLine" id="cb9-11" data-line-number="11"> <span class="kw">print</span>(y.sq)<span class="co">#is the vector printed</span></a>
<a class="sourceLine" id="cb9-12" data-line-number="12"> <span class="co">#}</span></a>
<a class="sourceLine" id="cb9-13" data-line-number="13">}</a></code></pre></div>
<p>In shorter form the ifelse() command is an alternative.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb10-1" data-line-number="1"><span class="kw">ifelse</span>(test, yes, no)</a></code></pre></div>
</div>
<div id="the-while-loop" class="section level2">
<h2><span class="header-section-number">7.3</span> The while-loop</h2>
<p>The while-loop performs a program (“statement”) as long as certain data conditions (“test expression”) are met. The basic syntax is</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb11-1" data-line-number="1"> <span class="cf">while</span> (testexpression)</a>
<a class="sourceLine" id="cb11-2" data-line-number="2">{</a>
<a class="sourceLine" id="cb11-3" data-line-number="3"> <span class="co">#increment (i+1);</span></a>
<a class="sourceLine" id="cb11-4" data-line-number="4"> statement</a>
<a class="sourceLine" id="cb11-5" data-line-number="5">}</a></code></pre></div>
<p>Here, test_expression is evaluated and the body of the loop is entered if the result is TRUE.</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb12-1" data-line-number="1">i <-<span class="st"> </span><span class="dv">2</span></a>
<a class="sourceLine" id="cb12-2" data-line-number="2">results<-<span class="kw">c</span>()</a>
<a class="sourceLine" id="cb12-3" data-line-number="3"><span class="cf">while</span> (i <span class="op"><</span><span class="st"> </span><span class="dv">6</span>) {</a>
<a class="sourceLine" id="cb12-4" data-line-number="4"> <span class="kw">print</span>(i)</a>
<a class="sourceLine" id="cb12-5" data-line-number="5"> x=i</a>
<a class="sourceLine" id="cb12-6" data-line-number="6"> results[i]<-i</a>
<a class="sourceLine" id="cb12-7" data-line-number="7"> i =<span class="st"> </span>i<span class="op">+</span><span class="dv">1</span></a>
<a class="sourceLine" id="cb12-8" data-line-number="8">}</a></code></pre></div>
<pre><code>## [1] 2
## [1] 3
## [1] 4
## [1] 5</code></pre>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb14-1" data-line-number="1">x</a></code></pre></div>
<pre><code>## [1] 5</code></pre>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb16-1" data-line-number="1">results</a></code></pre></div>
<pre><code>## [1] NA 2 3 4 5</code></pre>
<p>In the above example, i is initially initialized to 1.</p>
<p>Here, the test_expression is i < 6 which evaluates to TRUE since 1 is less than 6. So, the body of the loop is entered and i is printed and incremented.</p>
<p>Incrementing i is important as this will eventually meet the exit condition. Failing to do so will result into an infinite loop.</p>
<p>In the next iteration, the value of i is 2 and the loop continues.</p>
<p>This will continue until i takes the value 6. The condition 6 < 6 will give FALSE and the while loop finally exits.</p>
<p>There is one other loop that can be used in R: repeat.</p>
</div>
</div>
</section>
</div>
</div>
</div>
<a href="statistical-functions.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="construction-of-functions.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"google": false,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["facebook", "google", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/rstudio/bookdown-demo/edit/master/07-programming.Rmd",
"text": "Edit"
},
"history": {
"link": null,
"text": null
},
"download": ["bookdown-demo.pdf", "bookdown-demo.epub"],
"toc": {
"collapse": "subsection"
}
});
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
var src = "true";
if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
src = src.replace(/^https?:/, '');
script.src = src;
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>