noc21-ma36-lec18

IIT Kanpur July 201
20 followers
2 Video Views·Dec 2, 2023
In this lecture, simple random sampling is discussed using R software with the packages "sampling" and "sample".
[Music]
[Music]
hello
welcome to the course essentials of data
science with r
software 2 where we are trying to
understand the basic concept of
sampling theory and linear regression
analysis in this module we are going to
continue
with the topics of simple random
sampling with r software
you will recall that in the earlier
lecture we consider
the package r sample
to draw the samples
using simple random sampling under
without replacement and with replacement
cases
in r usually
a job can also be done by some other
package
why because different people who are
working independently they can
contribute different packages
those packages may not be identical but
possibly they might be
doing a similar job or even the same job
but their outcomes the pattern of
outcome
the syntax commands
that may differ
so thinking on the same lines ah today i
am going to use
another package whose name is sampling
to draw simple random sample
and you will see that there is a
difference
between the two and whatever is the
difference
i will show you that i am going to
utilize it
whatever is the outcome that will be
coming from here
from sample and sampling that i will try
to compare and i will
try to show you that how the output
differs and how they can be used in the
best possible way
so let us try to start our
discussion with the package sampling
so in order to use the package
sampling first you need to install it so
in order to install it you have to
simply use the command
install dot packages and inside the
double quotes
within the parenthesis just write
sampling
and then you have to load it library
sampling
and this software provides the simple
random samples using vidhan
without replacement right
okay so let me first try to show you
what are the
usage so the syntax or the command to
draw
a simple random sample
in case of without replacement is
s r s w o r all in small
letters and inside the parenthesis you
have to write
small n and capital n the small n and
capital n are the same what we have
assumed small n is the
sample size and capital n is the
population size so essentially this
srswor command is
is asking that write the command srs wor
and inside the parenthesis first write
the sample size which we want and
then write the population size
okay so this command is going to give us
a
simple random sample which has equal
probabilities of selection
for every unit and this returns a
vector this vector will have two
quantities 0
and 1 right and there is an
indication the element k of this vector
indicates the status of
unit k that means in case if
k is equal to 1 this means
the unit is selected
and if k is equal to 0 then the unit is
not selected meaning of this that is k
equal to 0 and k
equal to 1 i will try to illustrate with
some examples
but before going to the example google
let us also see how one can draw the
simple random sampling with replacement
so
in order to draw a sample by srswr
the command is srswr and
inside the parenthesis you have to give
small n
and capital n
so small n is the sample size and
capital n is the population size
and once we try to execute it it will
return a vector of size
capital n each element of this vector
will have some number of values
and that will indicate that each element
k of this vector
is the number of replicate for unit k in
the sample
what does this mean i will try to show
you with some examples now
first let me try to show you the
examples on the slides itself
you please try to understand and later
on i will try to execute these things
on the r console i am not doing it
right now because in case if i try to
use this command
in the r console the outcome is going to
be different
what we are looking on the screen
and in further slides we are going to
use this outcome
so that is why i will try to explain you
keeping this outcome fixed and later on
i can explain you on the r console
that if the outcome changes means
everything changes accordingly
right ok so now if you try to see here i
am trying to
take here a population of size 10 so n
is equal to 10
and small n is equal to here 3 that
means if i would like to have a sample
of size 3.
so i try to comma use the command here s
r s w
o r and with sample size 3 and
population size capital 10
and whatever is the outcome of this
vector i will try to store
in a vector s w o r so the name
indicates that
this is a sample by without replacement
so in case if you try to execute you get
here a
outcome like this one now this is
what i was saying that these are the
values which contains only 0
and ones and 0 and 1 they have an
interpretation
that this is trying to
give me the position of values in the
population
say y 1 y 2 y 3 y 4
y 5 y 6 y 7 y 8
y 9 and y 10 so those places where you
have here one
corresponding to these ones the the
corresponding units are selected in the
sample
so here you can see corresponding to
this one
the unit is y5 corresponding to
this one the unit is y8 and
corresponding to this
one the unit is y10 so this y 5 y
8 and y 10 they are selected in my
sample
and all those places where we have got
zero
these zeros are indicating that the
that these units are not selected in the
sample
but that is obvious we wanted to have a
sample of size 3
and now here we have a sample of size 3
1 unit 2 unit
and 3 units right so that's what i am
writing here that the interpretation is
that number corresponding to one are
selected in the sample
so this is what i was trying to
communicate with you
convey with you convey to you that
different
packages may have different
interpretations and different
styles but the outcome is similar
so now if you remember in the
earlier lecture what i had done i had
taken an example
and then i was trying to to
choose the values of students or their
heights
based on those variable so i would
try to do the same thing
with the package sampling right
so if you remember
but before that let me try to show you
the
the outcome of srsw
r also
so the command here is to draw a sample
of size
3 from a population of size 10
by srs w r is srswr wr and inside the
parenthesis 3 comma 10.
so now this will give you an outcome
like
this one here right so you can see here
this is here
the screen shot so here also
the dot com is the the interpretation is
the same that the number corresponding
to one are
selected in the sample
right okay so that means
y one y three and y ten they are
selected in the
sample and i am trying to store these
these values as
swr vector okay
and so now let me try to
show you that this data what we
saw in the example of srswr
that has been chosen according to
variable
1 to 10 but now we would like to execute
it on the example that we considered
earlier and
then we would try to choose the sample
according
to the variable name so if you remember
we had considered this example where we
had a
data set in the form of a table where
there are
10 students whose names are a b c d up
to j
and their heights are denoted by y 1 y 2
y 10 and their values are given here in
the
table and using this
data set we had created a data frame
so so i have entered the variables
here height and the variable name
for the name of the students exactly in
the same way what is given in the data
and then using the command data dot
frame i have constructed the
data frame of name and height so that is
exactly the same thing that we did in
the
last lecture and this is my here the
data frame
and from this data frame if you want to
extract the data on individual variables
for example if you want to extract the
data on the
name which is here then i had given it
the name
names n-a-m-e-s and if you want to
extract the
data on the height that name i had given
earlier as a heights
and the command to to extract this data
is
that first you try to write down the
name of the data frame
followed by the variable name which is
joined by a
dollar sign and the same thing is here
also
if you want to extract the data on
height write down that get a frame name
height data
then variable name height and then join
them by
dollar sign so you can see here that
this is giving you the
same data here whatever is given in the
data frame
ok so that is the same thing what we had
done earlier and this is the screen shot
so you can believe that
whatever i am showing you here that that
will hold correct
now now i try to compare
now if you try to recall earlier
when we use the command sample then in
order to
to get the sample
on the basis of the variable names
we had used the command sample the
variable name names
and the size of the sample which was 5
and then because we wanted to use srswor
so we use
replace equal to fall and this had given
us a
simple random sample without a
replacement
now we would like to use this command
srs wor and if you try to
see here i am trying to just use this
data set what we have obtained earlier
if you try to see here that i means in
this example also i had considered
n equal to 10 and small n is equal to 3
so my this s w o r values are something
like this
and here if you try to see this is my
fifth value this is my
eighth value and this is my head tenth
value
now my objective is this that i have got
here
a sample in terms of zeros and ones
using the command srs wor
and based on that i would like to see
that what are these ones which
are corresponding to a given variable
name
for example i have considered in that
example
two variables names and heights
so now i am choosing here 5th unit
8th unit and 10th unit so i would like
to
find out here that in the names vector
what
are my 5th 8th
and 10th units or what are the
fifth eighth and tenth names similarly
when i try to use the variable here
heights then i would like to know what
are the values corresponding to
fifth eighth and tenth value in the
variable
heights so that is what i want to do
so if you remember when we considered
the introduction to our software means i
had
quickly given you some commands
related to logical operators at that
time
we had considered an example like
something
x x double equal to 1 or 0 something
like
this one so i had explain you that when
i try to write x
double equal to sign equal to 1 then
this double equal to sign is the logical
equal to sign and we are trying to see
here that
in the data vector x what are the values
which are equal to 1. so this will give
us
an outcome in terms of true and false
and then
once i use here this command here x
with this bracket sign then this entire
command is going to give us
the values in the data vector
x for which x is equal to 1. so if i
write it here
neatly when i am trying to write down
here like this
so this command is going to tell us
that what are the values in the vector x
corresponding to which x is equal to
1 or x is equal to right so
this logic can be used over here and now
i have here
a data vector s w o r where i have zeros
and ones
so when i say s w o r
double equal to sign that is the logical
equality is equal to 1
that means i am trying to find out all
those values
in the data vector s w o r corresponding
to which the values are
1 so what are the values in the data
vector s w
o r which are 1
and now i am asking that
please tell me the corresponding names
so in case if i try to combine both the
sentence
i can say very simply that mr
r please tell me the
values in the name vector
corresponding to the data vector s w o
r where the s w o r values are
1 ok so now
what it will do here that it will
give us it will give us the values
of names corresponding to
the places where s w o r was
1 s w o r was 1 where
that you can see here y 5 y 8 and
y 10 and you can see here in this table
that y 5 name is e y
8 name is h and y 10 name is j
so that means i am looking for these
values
e h and j and now
if you try to see in this outcome here
what you are getting
here this is here e h and
j one thing you have to just keep in
mind that the command which i have used
here
names s w o r double equal to 1
this will give you the same outcome e h
and j just like
it here this one but still i am using
here as
dot vector because i just want to save
the outcome of this command in the form
of a
data vector so that it is compatible
with the mathematical manipulations at a
later stage otherwise only this command
is sufficient
to give this solution an answer
so now in this example i have taken the
variable names now i try to
take another variable heights and then
corresponding to the
same sample data that we have selected
and that was stored under
swor where we had three units y3 y5 and
y8
they were selected so now i would
try to find out the values of y 3 y 5
y 8 under heights earlier
we had found it under names
so now what i use the command i try to
use here the command here
s w o r is equal to double equal to
equal to one
so that means please try to identify the
values
in the data vector s w o r which are
equal to one
and corresponding to them please come to
the variable
heights and try to identify the
corresponding sampling units so in this
case
there are three values at third fifth
and eighth position which are equal to
one
here so now
the control will come to heights and it
will find the heights of the third fifth
and
eighth observation
so this command will give us the same
output but
as i said earlier we would like to store
this outcome in the form of a vector so
i am just
using here as dot vector to store the
observation in a data vector and you can
see here
this is going to give us the outcome 155
158 and 160 and if you try to look into
your example
the fifth unit here is 155
8th unit here is 158 and 10th unit here
is 160.
so now we had chosen that 5th unit
8th unit and 10th unit and corresponding
to which we have found the
names or their
heights so this is how we can proceed
further and you can see here this is
the screenshot this is the screenshot
when i
conducted this experiment on my computer
but now once i try to do it on the
r console before you then surely
this swor will change and then
the corresponding values of here esj
will also change
and this will also be changed the
concept will remain the same
now i try to take the example of a
simple random sampling
with replacement using the same example
so
for example i can show you here
i am taking here as population of size
10
and a sample of size 8. so i want to
draw a sample of size 8 from a
population of size 10
well i have taken it to be pretty large
because i wanted to show you something
so now i use the command here srs wr
8 and 10. remember earlier when i used
the the command here srswr here with 10
comma 3 you can see here
the values were only here 1 and 0's
but now i would like to show you
something more so when i try to take
here srswr810 that means i want to
choose
8 values out of 10 values by simple
random sampling with replacement
then my outcome comes out to be like
this so you can see here that this time
it is not actually only 0
and 1 but there are some other values 2
and 3 also
ok so what does this mean the
interpretation goes like this that
let me try to write down the
corresponding
units
for these numbers so
now you can see here when i am getting
here 3 this is indicating that there are
3 values of y 1 are selected so the
sample will look like
y 1 y 1 y 1.
now it is saying that there is one value
corresponding to y4
so that means another value selected is
y4
and similarly here it is 2 corresponding
to y7
so that mean there are two values
corresponding to
y seven and there are two values here
corresponding to eight
so y eight and y eight are occurring two
times so you can see here
now my sample consists of three values
of y one
one value of y four two values of y
seven and two values of y
eight amounting to n is equal to eight
that means sample of size
eight so this is the interpretation when
i say
we are using the command srswr so
the rule is the numbers which are not
equal to 0 are selected in the
sample and these numbers are
representing the frequency of the
numbers in the
sample here you can see this is
different
than the case of srsw or because in the
case of srswor
these values can be can only be either 0
or 1
because either the value is selected or
not selected
a value cannot be selected more than one
so in this case suppose i have got this
sample
now my next objective is this i want to
know
that what are the units which are
selected
remember i am asking only the unit which
are selected
i am not asking for the frequency so in
case if i want to know the selected unit
then i can use here the command
1 to 10 which was my this population
size because here the population is
given as integer and then i am trying to
find out
the values of s w r which are not equal
to zero
if you remember negation sign is given
by symmetry sign
and equal to sign that means not equal
to zero
right so now from this swr vector
it is trying to say that this value 3
1 2 and 2 they are not equal to 0
and that is what it is giving me here
that the corresponding units are
1 fourth seventh and eight they are
selected
now in case if i want to find out what
is the frequency of these selected
values
then i can use here the command on the
same lines that
find out the values of swr which are not
equal to 0 from the data vector swr
using this command right now you are
familiar with this command
and it will give me a value here
3 1 2 2. so you can see here this three
one two two is the same thing which i
have
obtained here that means the
value here y one this is repeated three
times the
fourth unit this is occurring one time
seventh unit this is occurring
two times and eighth unit which is
occurring two times
so you can see here i have tried to
illustrate it in a more
simple way but just by looking at the
outcome
you can see what is really happening
okay now the same thing i try to
do that i would like to consider the
same example and i would
try to find out the values
corresponding to the variable name so is
so this is simply a repetition of
whatever i have done
the only thing is this i simply have to
need i simply have to change here the
data vector
so i am trying to find out here the
ah the the values of the
name corresponding to which the data in
the
swr vector is not equal to 0
from here and these values are a d
g and h right and similarly
if you want to find out the
corresponding value of height so i can
take here the values of height
corresponding to which the swr
the vector has nonzero values right
and you can see here this will come out
to be 151 154 157
and 1 58. so this is how you can do
different type
of manipulations and you can see here
this is the screenshot
which was coming when i was trying to
conduct
this example on my computer now i will
try to
show you this entire analysis on the arc
console and
based on that you can see that these
things are
working okay so let us try to come to
our console right let me first
load the library
library sampling and yeah actually i
already have
installed this library in my computer
so let me increase the font also so that
you can see more clearly
now i try to come to the beginning
so you see now i have installed the
packages
sampling and i have uploaded it now
the first example which i took in which
i just ah
drawn a simple random sampling with and
without replacement
of size 3 from the population of size
capital 10.
so you can see here if i try to do the
same command over here
you can see here this is here like this
but definitely
as i said now this sample will be
differing from the sample which i
reported in my
slides because you can see here this
will
as soon as i repeat it it will miss
every time it will give me a
different sample so
and similarly after this i had obtained
the
the simple random sample with the
replacement
so if i try to take here the same
command here
so you can see here swr
this is coming out to be say
a sample of size 3 you can see here now
this time it is coming out to be here 1
and here 2
right and if you try to repeat it here
you can see here now this time it is
only 0 and 1.
so yeah so this is going to increase for
example if i
try to take here a sample of size 8 then
you will see here that the things are
changing
right so so now
we should be confident that all these
things are working
okay now i try to create this
data set on my r console
so i try to copy and paste and you can
see here this is my
data frame okay and from this
data frame if i want to abstract the
information on
this here names and height i have to use
the command here
name sequel to the data frame and then
the dollar sign followed by the
name of the vector you can see here this
is here names
and similarly if you want to extract the
information
on your heights so you have to use the
same command here
i am just copying and pasting before you
so that you can also do the
same exercise without any problem you
can see here now
this is giving me the data which is
on heights okay now after this i will
try to use the
same command here okay
let me try to show you because because i
said that
this command is sufficient to give us
the
informations on the names
but still i have used here in this case
you can see i've used
it here as a vector so first you can
see here what is my let me clear my
screen and you can see here
the the data which i had for the simple
random sampling without replacement it
was like this
that means here in this case the
fifth unit eighth unit and ninth unit
they are selected in my sample so you
can see here now i try to obtain here
the value of the name corresponding to
this fifth
eighth and ninth units so you can say
see here that the
fifth letter is a b c
d e that is and then h i
right and
similarly if you want to find out here
heights
so you can see here these values are
155 58 158 and 1
159 and these are very easy to verify
that was the reason i had taken
it was my hair fifth value that means
155
this is eighth value that means 158 and
this is my hair ninth value which is
159 right so this
gives you this thing and now the only
thing is this if you
want to execute this command over here
so that will only do one thing now
this will save the same outcome in the
form
of a data vector so that will be
convenient for you
to use some mathematical operation
r functions over this thing that is the
only difference
right so similarly is the case of here
height which i have shown you
and similarly if i try to do it for the
srs wr unit that i have selected
so if you want to see here what are the
units which are selected here
so i can use here the command here for
example if you remember
the swr data here is given like this so
if you try to find out here
1 to 10 where swr is not equal to 0
so you can see here what are the values
where
swr is not equal to zero these are one
two
three fourth unit then fifth
unit then six has zero then seventh
eighth nine tenths
if this is exactly what is given here
that these are the places
where the units are not equal to 0
and similarly if you want to find out
here that what are the values
corresponding to under do this swr which
are
not 0 and their frequency so i can use
here this thing you can see here
this is telling you that unit number 4
is occurring 1 time unit number five
is occurring three times and this three
is the same as
these three and unit number seven eight
nine
ten each of them is occurring one time
right
so i hope i have made it uh clear right
and if you want to find out the names
corresponding to
the units which you have selected in the
srswr
so you can see here the names
corresponding
to the values where we have non zero
values in the data vector swr they are d
e g h i j and
in case if you want to find out the
values of the height variable
corresponding to these non zero
values this is here like this now so
you can see here i have completed all
the commands which i have shown you in
this
lecture ok so now i think
this is enough for you to learn and
understand that how
the samples can be drawn using srs wr or
wor in an automated way
inside the r software so we have two
commands
we have two packages which we have
utilized here
there may be more and in the forthcoming
days
who knows there can be some more
development but i have shown you that
how you can learn a package also
simply try to take the help from the
from the built in software and try to
spend some time in
studying reading that material and i am
sure that you will be
successful in using any type of package
so i stop here and in the next turn i
will try to now concentrate on some
some more fundamental properties like as
estimation
of mean variance
and relate and confidence interval and
then i will try to show you how you can
do it on the
r console so you practice and i will see
you in the
next lecture again see you good bye
[Music]
so
[Music]
[Music]
you
