Professional Documents
Culture Documents
Introducción A R para Finanzas
Introducción A R para Finanzas
Finance
▪ With the coding skills you'll gain in this introduction, you'll be ready to
undertake your own data analysis for finance in R.
What do we need?:
▪ R and R Studio.
Installation of R
https://posit.co/download/rstudio-desktop/
Source of Information
Package selected –
library( )
Instructive - Script
Artboard – R Console
o Addition: +
o Subtraction: -
o Multiplication: *
o Division: /
o Exponentiation: ^
o Modulo: %%
Introduction to R for Finance
Intro to basics: Variable assignment
▪ You can then later use this variable's name to easily access the value or the
object that is stored within this variable.
Introduction to R for Finance
Intro to basics: Assignment 1
▪ Suppose you have a fruit basket with five apples. As a data analyst in
training, you want to store the number of apples in a variable.
▪ Every tasty fruit basket needs oranges, so you decide to add six oranges.
As a data analyst. Next, you want to calculate how many pieces of fruit you
have in total.
▪ Common knowledge tells you not to add apples and oranges. If you really
tried to add "apples" and "oranges" and assigned a text value (“six”) to the
variable oranges, you would be trying to assign the addition of a numeric
and a character variable.
Introduction to R for Finance
Intro to basics: Basic data types in R
▪ R works with numerous data types. Some of the most basic types to get
started are:
o Decimal values like 4.5 are called numerics.
o Whole numbers like 4 are called integers. Integers are also numerics.
o Text (or string) values are called characters.
▪ You can do this with the class() function, as the code in the editor shows.
Introduction to R for Finance
A little more advance: Interest Rate
Define: Suppose we have a nominal interest rate of 2% per month that is compounded monthly. In this case,
the frequency of compounding is n=12 (12 months in a year). Then, the above formula can be used to calculate R:
Create a function to
switch from annual
effective rates to
nominal or periodic
rates
Introduction to R for Finance
Vectors
▪ Vectors are one-dimension arrays that can hold numeric data, character
data, or logical data.
▪ In R, you create a vector with the combine function c(). You place the vector
elements separated by a comma between the parentheses.
Introduction to R for Finance
Vectors: Arithmetic calculations on vectors.
▪ It is important to know that if you sum two vectors in R, it takes the element-
wise sum. For example, the following three statements are completely
equivalent:
▪ You can also do the calculations with variables that represent vectors:
o For poker: On Monday you won $140, Tuesday you lost $50, Wednesday you won $20, Thursday
you lost $120, and Friday you won $240
o For roulette: On Monday you lost $24, Tuesday you lost $50, Wednesday you won $100,
Thursday you lost $350, Friday you won $10
▪ Give a name to the elements of each vector with the names() function.
▪ How much did you win or lose each day (poker and roulette combined)?
▪ Calculate the total amount of money that you have won/lost with roulette and poker and the totals for
both.
▪ To select elements of a vector, you can use square brackets. Between the
square brackets, you indicate what elements to select.
▪ To select multiple elements from a vector, you can add parentheses in the
middle of the square brackets. You must indicate between the parentheses
what elements should be selected.
Introduction to R for Finance
Vectors - Select specific elements of the vector
▪ Another way to tackle the previous exercise is by using the names of the
vector elements (Monday, Tuesday, …) instead of their numeric positions.
▪ You can calculate the mean of the vector or a selection using the function
mean().
Introduction to R for Finance
Vectors - Selection by comparison
▪ You can use these comparison operators also on vectors. This command
tests for every element of the vector if the condition stated by the
comparison operator is TRUE or FALSE.
Introduction to R for Finance
Vectors - Selection by comparison
▪ Additionally, you can simply ask R to return only those days where you
realized an effective comparison.
▪ You can select the desired elements, by putting a selection vector between
the square brackets that follow interest vector.
▪ Check which elements in poker and roulette vector are positive and assign
this to a selection vector.
▪ Use a selection vector in square brackets to assign the amounts that you
won on the profitable days to a variable that indicate the poker and roulette
winning days.
Introduction to R for Finance
Matrix
▪ Since you are only working with rows and columns, a matrix is called two-
dimensional.
o The first argument is the collection of elements that R will arrange into the rows and
columns of the matrix. Here, we use 1:9 which is a shortcut for c(1, 2, 3, 4, 5, 6, 7, 8, 9).
o The argument byrow indicates that the matrix is filled by the rows. If we want the matrix
to be filled by the columns, we just place byrow = FALSE.
o The third argument nrow indicates that the matrix should have three rows.
Introduction to R for Finance
Matrix – naming, sum rows and column.
▪ Similar to vectors, you can add names for the rows and the columns of a matrix:
▪ In R, the function rowSums() conveniently calculates the totals for each row of a matrix.
This function creates a new vector:
▪ In the same way, the function colSums() calculates the totals for each column of a matrix.
This function creates a new matrix of one row and n columns.
Introduction to R for Finance
Matrix – Adding rows and Selection of matrix elements
▪ You can add a column or multiple columns to a matrix with the cbind() function, which merges matrices
and/or vectors together by column. For example:
▪ You can add a row or multiple rows to a matrix with the rbind() function, which merges matrices and/or
rows together.
▪ You can use the square brackets [ ] to select one or multiple elements from a matrix. Whereas vectors
have one dimension, matrices have two dimensions. You should therefore use a comma to separate the
rows you want to select from the columns. For example:
o My_matrix[1,2] selects the element at the first row and second column.
o my_matrix[1:3,2:4] results in a matrix with the data on the rows 1, 2, 3 and columns 2, 3, 4.
▪ If you want to select all elements of a row or a column, no number is needed before or after the comma,
respectively:
o Those who are familiar with matrices should note that this is not the standard matrix
multiplication for which you should use %*% in R.
Introduction to R for Finance
Matrix - Assignment 4
▪ Consider the box office numbers from the first three Star Wars movies. New Hope (460.9, 314.4),
Empire Strikes (290.4, 247.9), and Return Jedi (309.3, 165.8).
▪ The first element indicates the US box office revenue, the second element refers to the Non-US
box office (source: Wikipedia).
▪ Calculate the worldwide box office figures for the three movies and put these in a vector.
▪ Add the worldwide box office as a new column to the star wars matrix and assign the result a new
name.
Introduction to R for Finance
Matrix - Assignment 4
▪ Now, Consider the box office with similar data for the prequel's trilogy. The Phantom Menace (474.5,
552.5), Attack of the Clones (310.7 , 338.7), and Revenge of the Sith (380.3 , 468.5).
▪ The first element indicates the US box office revenue, the second element refers to the Non-US
box office (source: Wikipedia).
▪ Calculate the total revenue for the US and the non-US region.
▪ Select the non-US revenue for all movies and calculate its average.
▪ Select the non-US revenue for the first two movies and calculate its average.
Introduction to R for Finance
Matrix - Assignment 4
▪ How many visitors went to each movie for each geographical area. Assume that the price of a ticket
was 5 dollars.
▪ Prices were not the same for each movie and region. Prices for each movie and region were: