Homework: Coding questions #2
From the "Learn R. By coding." (Kurnicki, 2018) textbook:
Chapter 3.4: Exercises: 5, 6, 9**
**ex. 9 is very advanced - expert level coding - do not worry if you cannot solve - fixed income securities are very tricky
Submit your answers with all your R code in a .pdf format (limit your file to 6 pages). Write commentary/conclusions for every exercise.
Complete the exercises using the following "answer framework" for each question:
- Exercise/question number
- R Code / libraries should be included (important chunks are highlighted)
- Code output (data frame, vector, chart)
- A chart/visual, if needed
- Your comment, conclusions and/or insight into the results.
If your code produces errors and you cannot find a fix, try to explain what this error means. Do not ignore errors!
Microsoft Word - Learn R.By coding.docx LearnR.Bycoding. Copyright Titlebook:LearningR.Bycoding. Authorbook:ThomasKurnicki ©2019,ThomasKurnicki Selfpublishing ISBN978-83-952046-1-6 ALLRIGHTSRESERVED.Thisbookcontainsmaterialprotected under International and Federal Copyright Laws and Treaties. Anyunauthorizedreprintoruseofthismaterialisprohibited.No partofthisbookmaybereproducedortransmittedinanyform or by any means, electronic or mechanical, including photocopying, recording, or by any information storage and retrieval systemwithout expresswritten permission from the author/publisher Dedicatedto… Students… Andanyoneelsewho understandstheimportanceof data. Table of Contents Preface .......................................................................................... 1 1. R intro: the environment ....................................................... 3 1.1 Short passage about R ................................................ 3 1.2 Installation.................................................................. 4 1.2.1 R Studio installation................................................. 5 1.2.2 Installation verification ............................................ 6 1.2.2 Workflow in the R Studio environment. .................. 6 1.3 Exercises ..................................................................... 9 2. Data objects ............................................................................ 12 2.1 Vectors, matrices, and data frames .......................... 12 2.2 Testing and changing the type of objects.................. 16 2.3 Writing comments in your script .............................. 17 2.4 Sub-setting objects using names and indexes ........... 19 2.5 Importing and exporting data sets ............................ 21 2.6 Cleaning and sub-setting your data .......................... 23 2.7 Exercises ................................................................... 24 3. Functions ................................................................................. 28 3.1 Packages / libraries – installation and usage ............ 28 3.2 Functions from libraries ............................................ 30 3.3 User defined functions ............................................. 35 3.4 Exercises ................................................................... 37 4. Loops ....................................................................................... 40 4.1 The “FOR” loop ......................................................... 42 4.2 The “WHILE” loop ..................................................... 45 4.3 Apply family function ............................................... 47 4.4 Exercises ................................................................... 49 5. If statements ........................................................................... 53 5.1 One possibility if statement ...................................... 53 5.2 Nested if statement .................................................. 54 5.3 Combining an if statement with a for loop ............... 56 5.4 Exercises ................................................................... 57 6. Estimation and optmization .................................................... 60 6.1 Linear regression ..............................................