Welcome to block 2 linear algebra.
This is the companion video of the first concepts
about set notation and basic operators.
I am Lidia Montero and I will explain some examples.
I will show you some examples related to the basic concepts
that we have explained in the companion video, and I will focus
especially on the application shown, not acceptance in set theory
but in the selection process
and the manipulation of data in RStudio
that in fact is R language and this is
something that you will have to do
in a Master in Urban Mobility.
So practical applications let this start
by a very basic and traditional examples.
So determine the set of real numbers X that satisfy
the equation x squared plus one equals zero.
No real number, only complex numbers.
So in this case is the empty set
Second, a slight modification of the equation.
We will see the the details in one minute.
So the first one,
no real number satisfies the equation.
Now, in the second example we have modified a little bit
the equation is x squared minus one equal zero.
And we have two real numbers, plus one and minus one
that satisfy the equation.
Then, assuming that U
is the set of integer numbers,
then let S be one, two, three.
So find out the complement of S, this is a question of notation.
One, two, three.
They complement is
all the numbers in the set of integers
So all integer numbers
either positive or negative that are not included in S. So
minus infinite to minus two,
minus one, zero and then four, five, six and so on.
Okay, some more examples.
Here I have the union of {one, two, three} and {three, four, five}.
So this will be {one, two, three, four, five}.
Then the union of {Paris,
Barcelona} set and the {zero, nine} set is
{Paris, Barcelona, nine, zero}.
So this is a set just elements
and the union of {three, four} set
with the empty set is {three, four}.
Let's see the Cartesian product if I have A
that this r and s, two elements and B that is {2, 4}.
to write
the Cartesian product of these two sets would be four pairs.
The first element from A and the second element
from B, here you have the four elements.
So let S be {0,1,2},
then determine the power set of S.
So as we have seen, the power set of S will contain
two raised to the third power elements.
So eight elements and here they are...
all the possible subsets of
zero, one and two
then a technicality
determine the power set of the empty set
on this case.
The empty set.
Okay.
But take into account
that the empty set contains zero elements,
but the power of the empty set contains one element
that is the empty set itself.
Okay,
let's A be one, two, three.
And set B be three, four, five.
Okay.
Assume that the universal set is the decimal numbers.
{0,1,2, ...9}.
Use DeMorgan's laws to get the union
of the complements of A and B.
So I write the complements of A and B, union.
I use the property and this would be
as the complement of the intersection.
Okay.
So the intersection between these two sets is element three
and the complement of element three
is just the rest of digits, all minus three.
So you have here
the eight elements of these complement set
Use, DeMorgan's laws to get the intersection
set of the complements of A and B,
then again.
we write
So the intersection of the complements, can be rewritten
as the complement of the union.
So firstly
what we do in this case,
I developed the left part of these of these equalities
and you are you can see that
the outcome is just the solution.
And we have applied DeMorgan's Law.
So how to use these set operations
in a practical field in programming.
We have to write programs, we have to write sentences, selecting data
in all the data softwares based on scripting.
Okay, So in this case, we will be using R
and the R language contains operators
related to this set theory in many cases.
In fact, they are lists, not exactly sets.
So first, let's see how to define a list or a data frame in R
List constructor is just list parentheses.
Okay.
So if you would like to
define a list composed by three elements with no specific name,
then you just write list( x, y, z )
if you're interested in
meaningful names for these three elements, then you write the names
and you use the list constructor.
Data frames are obtained
by the constructor data.frame()
and let's see, for example, I define X as a list
that we have seen before and in this case 1:2.
So two elements.
Then I define the first of the second letter, again
two elements and then z that is 1:3.
So the colon ':' operator means
the sequence from the left number to the right number.
Okay.
So now I can define the data frame
constituted by x and y
or I can try
to define data.frame( x, y, z).
But this doesn't work because the length of x
and y is two, but in z is three.
So this it is not compatible.
Okay, so let's see.
Access to dataframes.
Accessing to a data.frame means.
For example, I define data.frame( x, y) and Y as I did before.
So the object would be
DF, in capital letters.
If I'm interested on x column,
then what I have to write is the name of the object, the data frame
object $ the name of the column that I'm interested in directly.
So without the $, this is not working.
An alternative could be, for example,
just to use the name itself
and but taking into account the bidimensional
structure of the data frame, this would be an alternative
or just take into account the first element
of column X
or the first element in row one,
and second element in column two.
So this is a matrix notation
we are going to use
now an example that is the New York City flights
2013, it is a big database,
but I'm not interested in giving details, and in fact it refers
to departures, arrivals, delays
or air time for flights.
Okay.
So first of all, this data set is included
in the library, New York City Flight 13.
Okay.
So you have to install this library and then the flights,
the data is included as an object that is a tibble.
This object is not a data frame.
And to start with R, we prefer to deal with the original structure,
the traditional structure that is a data frame.
So I use as.data.frame( flights )
and the outcome is the object flights that now is a dataframe
to inspect a dataframe, I use a summary.
This is a summary based on numbers
that is based on the
five numbers summaries, defined by Bell Laboratories
and Tukey many years ago, more than 50 years ago.
And then I will see the list of the columns.
So this is with the names.
Let's see a little bit how this works.
Okay.
So I will be interested
in showing the pairs of relationships between some of the variables
that are in this data set, in particular
a subset of the columns.
Okay.
So a subset of the column names that is composed by the variables,
distance, air time, arrival and departure delays.
Okay.
So I'm using a subset
of the universal set in this case
that these are all the names for the columns of this dataframe.
Okay. So, this is very connected.
Something that is also useful is just to plot
only pairs of relations
that contain data.
So missing data, the lack of data
in any of these four elements that I mentioned before.
So delays, arrival and departure, air time and distance
are not included in the plot.
So in this case, I have to define a subset, again.
Okay.
So what I'm using is the dataframe,
the attach function means that I get access
directly to the names of these flight
columns without the need of
using the name of the object and the name of the variable.
So it's just to write a little bit faster.
In this case, the condition is
I do know first of all, I do not want
is.na(distance), so if possible, if positive.
So this is a boolean number, then
these would be not interesting to me.
Okay.
So I select those that are NA by distance,
those that are NA by arrival delay,
those that are NA by departure delay
and those that are NA, so missing data, in air time.
So satisfying, either of these conditions.
So is the union, then this gives me
the subset of the rows, including at least
one missing value in any of these four variables.
So I'm not interested.
The complement is written
as the sign !, the admiration sign,
and the name of the list, 'not.these' list.
So this is the number of rows.
Okay?
And the columns are the subset and since I have not defined any object
containing the subset, I directly write
the list containing the subset of the columns that I'm interested in.
And finally, I apply the pairs command
and I see the chart that you
that it is present in this slide from the very beginning,
this was the aim of this subsetting
to show these pairs of relations.
Okay, once done this, I detach the flights.
So I'm not interested anymore in
this saving or writing.
If we continue, then
let's see that the summary
of the columns for this data frame
could be defined,
could be included into A set.
Okay, this is a set and we will see that B,
these are the four variables that we are dealing with all the time
is a subset of a containing the distance,
air time, arrival and departure delays.
Okay.
So I define the set C
as the set of row numbers with NAs in
any column of B, Okay.
In more than one. Yeah, no problem.
So if we would like to refer
to E the complement of C,
then I can write just the claim
and the name of the object.
So let's see, A names of flights.
This could be the output.
Okay is a list and this list contains
11 sorry 19 elements
and from year to time hour
then should be distance global time.
The partial delay sorry arrival delayed departure delay another time.
C These are the rows
satisfying the condition of having
at least one missing element
to select those flights?
We south any and a
in the four variables that are included in B,
then I can write as is included here flights
square packets because this indicates that I would like to define the condition
for growth and columns and a condition for the first
they mention is not in C so
claims on the C and then I have
B that the is the subset of the columns that I'm interested.
If you would like to set different then no problem.
We can write say difference from A and B
so this could be A minus B
and you will see the output.
So with this
we have seen important properties and
this important properties are very related to the set definition
and the operators that we have seen in the context of linear algebra,
you could apply this selection
to okay, to many to many situations,
for example, to select the subset of observations
belonging to the first month and the first day in the month.
Okay, so this is a subset.
You can count the cardinality of the subset that in R is the function
length, and we will see that in this example
we have 842 observations.
Okay.
So with this
we can write using the B set that we have defined
and I hope that this is useful
just to understand that selection
and sometimes programming
has a lot of set notation.
Once you have seen the videos
related to probabilities,
then I will leave you in the slides
one exercise that is about a driving test on probabilities.
Okay, but this will not be included so far.
So thank you very much for your attention.