imlab1.pdf
📚 imlab1.pdf
Brief overview
This document introduces fundamental R programming concepts essential for financial analysis. It covers the basics of compound returns, control flow structures, and specialized functions for calculating financial rates. Understanding these elements is crucial for automating and performing complex investment calculations.
Key points
-
Introduction to compound returns and R basics, including vector manipulation.
-
Control flow mechanisms such as for loops and if/else statements in R.
-
How to define and utilize user-defined functions for reusable code.
-
Application of the uniroot function for calculating compound rates.
1. Introduction to compound returns and R basics
1.1 Compound returns
-
Compound return: The growth of an investment over time, where earnings are reinvested. This leads to exponential growth.
-
Example calculation: Investing EUR 11,000 annually at a 10% return for four years.
-
Year 1 investment grows for 4 years: EUR 11,000 * (1.1)^4 = EUR 16,105.10
-
Year 2 investment grows for 3 years: EUR 11,000 * (1.1)^3 = EUR 14,641.00
-
Year 3 investment grows for 2 years: EUR 11,000 * (1.1)^2 = EUR 13,310.00
-
Year 4 investment grows for 1 year: EUR 11,000 * (1.1)^1 = EUR 12,100.00
-
Total at graduation: EUR 56,156.10
-
🔑 Important: Compound returns are essential for understanding long-term investment growth.
1.2 Introduction to R basics
-
R is a programming language and software environment for statistical computing and graphics.
-
It allows for the calculation and analysis of financial data, including compound returns.
1.2.1 Variables and vectors in R
-
Variable: A name used to store data. Variable names are case-sensitive.
-
Vector: A one-dimensional data container that holds a sequence of values.
-
A vector can store numbers, text, or other data types.
-
When you run a line with just a variable name, R displays its contents.
-
📝 Example:
x <- 0.1 # Assigning 0.1 to the variable 'x' x # Displaying the value of 'x' # Output: 0.1.
- Vectors are fundamental for storing and manipulating data for calculations.
1.2.2 Accessing and changing vectors
-
Vector elements are accessed using square brackets
[]and their index (position). -
The first element is at index
1.
📝 Example:
x <- 0.1 x # Accessing the first element of vector 'x'. # Output: 0.1. x <- 5 # Appending the value 5 to the second position [2](#page=2). x <- 6 # Appending the value 6 to the third position [3](#page=3). x <- 7 # Appending the value 7 to the fourth position [4](#page=4). x # Output: 0.1 5.0 6.0 7.0. x <- 22 # Changing the value at the first position. x # Output: 22.0 5.0 6.0 7.0 [1](#page=1).
1.2.3 Creating vectors
R provides several functions to create vectors.
Function | Description | Example | Output |
|---|---|---|---|
| Combines values into a vector. |
|
|
| Creates a sequence of integers. |
|
|
| Repeats a value a specified number of times. |
|
|
|