WEBVTT

00:00.140 --> 00:06.550
Today, we are going to understand the collapse command.
And this Collapse command is used to aggregate variable

00:06.550 --> 00:13.040
So let's say we want to find a mean of certain variable
or median or strength division of certain variable

00:13.510 --> 00:18.660
And this is the command that we are
going to use. So let's load the data

00:18.660 --> 00:26.420
And this is basically the N L S 88 data set, but
I have just removed a few variables from this to

00:26.660 --> 00:30.570
to be easy to understand this
data and to work with it

00:30.570 --> 00:39.110
So, so how do we aggregate a variable?
Firstly, if we were going to use the

00:39.110 --> 00:43.590
the menu, then we would click
on data, create or change data

00:43.960 --> 00:53.210
other variable transformation command. And we
have this make data sets of main median etcetera

00:53.210 --> 00:56.890
This is the command that we are looking
at. This is the collapse command

00:56.890 --> 01:01.280
but we are going to work with
the, the, the command window

01:01.280 --> 01:08.710
Okay. So to collapse variable,
we write the name of the command

01:10.600 --> 01:17.560
then we write the thing that we want to perform
with the variable and then the variable name

01:17.560 --> 01:23.470
So if I was just going to
show you the, the help window

01:23.470 --> 01:29.710
we write the command name, then C
list, C list can contain these worlds

01:29.710 --> 01:37.240
So we can use mean median P one P two or any
value in between P 5 50 would stand four million

01:37.560 --> 01:45.610
We can use maximum percentage, etcetera. So these are the, this is
the list of the things that can go into these practices over here

01:45.610 --> 01:49.620
that you want to perform. This is
the parameter that you get from

01:49.620 --> 01:54.840
from the, from the variable, from the
performing the collapse command. Okay

01:54.840 --> 02:00.190
And then we can also use it in and then we can use
weights and then we have a bunch of options that we are

02:00.190 --> 02:06.510
going to discuss. Okay. So let's perform this
rather, let me show you the number of observations

02:06.510 --> 02:12.790
We have 20-46 observations, right?
And if we do collapse mean wage

02:12.790 --> 02:19.690
we would be left with only one observation
and that would be the mean of the wage

02:20.370 --> 02:29.580
So you have probably seen that when we collapsed the
data, we are just left with the specific variable

02:29.580 --> 02:34.230
and the specific values. So it's
literally collapsed the data

02:35.320 --> 02:37.080
So if, if that makes sense

02:38.020 --> 02:38.890
okay

02:40.730 --> 02:46.270
But the default is mean, right?
So whenever we collapse the data

02:46.270 --> 02:54.710
remember to save the data before collapsing. So in this case,
I have already saved the data into the collapsed or data file

02:54.710 --> 02:59.390
So I would just re import
the data. So we have the data

02:59.390 --> 03:05.990
the original dataset. And if I was to
use this command just as collapsed wage

03:06.390 --> 03:12.110
And instead of writing mean in parenthesis,
it will also give me the same result

03:12.110 --> 03:20.410
And the reason is that the default of the, which
this collapse command is this mean in parenthesis

03:20.410 --> 03:28.680
right? So it would work both ways, but it's usually a
good idea to write the parameters within the parentheses

03:28.880 --> 03:36.440
So that way the person reading the code would quickly understand
what you are going to perform what this code is going to execute

03:37.160 --> 03:45.940
Okay. So let's aggregate. Now, in this case,
finding a mean of wage wasn't somewhat meaningful

03:45.940 --> 03:50.760
right? But before moving forward,
let me show you the value that we got

03:50.760 --> 03:59.210
it was 7.76694. And if we were
to perform the summarize command

03:59.610 --> 04:07.950
we would get the same value for wage variable. So just to
make sure that you understand what this collapse commanded

04:07.950 --> 04:14.730
it just gave us the mean. But in
this specific circumstance, it was

04:14.730 --> 04:23.250
it wasn't much of a benefit to get mean. We could have
got it from summarize command but wouldn't be nice to have

04:23.570 --> 04:30.930
you know, mean, or some of the wage over certain
group. So we can aggregate variable over the group

04:30.930 --> 04:38.010
So let's collapse and find
the sum of wage for each race

04:38.110 --> 04:43.140
Rather I should, I should use mean
because men would be meaningful

04:43.140 --> 04:52.120
Um The idea was for writing some to show you that
there can be other parameters that we can calculate

04:52.120 --> 05:01.090
But anyhow, so if I collapse the data, we get
to see that we have the main wage for each race

05:01.650 --> 05:07.270
Now, it isn't limited just to one
category, we can write multiple categories

05:07.270 --> 05:13.150
So each time I have to import the data back
to get the original data and I collapse

05:13.470 --> 05:18.410
find the mean of wage by race and
married. And then in this case

05:18.410 --> 05:23.080
we would have a combination of all
the categories of race and married

05:23.080 --> 05:31.050
So in married, we have two categories. So in total,
we would get three into two number of observations

05:32.900 --> 05:42.530
Ah so we get, you know, the main wage
of white, single and white married

05:42.530 --> 05:48.560
person or population or sample and then
black, single, black, married, etcetera

05:48.560 --> 05:53.960
so on and so forth. So that
way we get the main wage for

05:53.960 --> 05:55.640
you know, both the groups

05:57.000 --> 06:04.700
So that was aggregation of variable on the group. What if we
wanted to perform a single parameter for multiple variables

06:04.700 --> 06:10.160
We wanted to calculate some in these all
cases, we were working with a single variable

06:10.160 --> 06:14.270
But what if we wanted to have
multiple variables? So in this case

06:14.270 --> 06:23.300
we have this hour's variable which shows the
usual hours worked and we want to calculate

06:23.300 --> 06:32.750
the sum of wages and hours by race. So we just tried
the command as it is and add as many number of variables

06:32.750 --> 06:39.850
that we want to add. Remember it is not
just a single variable that we can add

06:39.850 --> 06:46.050
we can add VR list, which stands for variable
lists. So we can have multiple variables

06:49.490 --> 06:54.770
Okay. So this is how it
would look like for each race

06:54.770 --> 07:03.890
we get the sum of wages and hours. So white
people performed uh 60,000 something number of

07:03.890 --> 07:04.980
hours of work

07:07.400 --> 07:14.560
This was for single parameter but multiple variables. But
what if we had multiple parameters and multiple variables

07:14.560 --> 07:20.000
So let's just say we wanted
some of wage but mean of ours

07:20.400 --> 07:26.930
So that is the way we write the command for we ride
the parameter and then the name of the variable

07:26.930 --> 07:30.070
then again the parameter
and the name of the variable

07:31.950 --> 07:34.280
So we get

07:35.300 --> 07:43.270
the wages is the sum which you can
clearly see and ours are in mean form

07:46.820 --> 07:55.850
And what if we wanted multiple parameters for
same variable? And this is when most of you would

07:55.850 --> 07:57.700
get an error

08:03.320 --> 08:04.490
Okay

08:05.350 --> 08:10.990
So we collapse, we want to find the
sum of ours and also the mean of ours

08:10.990 --> 08:18.870
right? And if we run this command, we would get an error
because what it is trying to do is it would create this some

08:18.870 --> 08:25.090
of ours. And then when it is going to create the mean of
ours, the variable name that it would create would be same

08:25.090 --> 08:31.830
right? So if you could have seen it from
here, what name of the variable would it give

08:32.060 --> 08:36.660
So if it created ours, then again,
it cannot create ours, you know

08:36.660 --> 08:43.280
give the same name to the variable again.
So what we need is a way around and again

08:43.280 --> 08:47.850
from the area, you can see that it is a name
conflict. So R is equal to some of ours

08:47.850 --> 08:52.250
And again, the this variable
name R is equal to mean of ours

08:52.250 --> 08:59.510
So that is the name conflict. And the way we resolve
the name conflict is if we look at this health money

08:59.510 --> 09:01.060
we get to see that

09:01.820 --> 09:04.120
whatever the new name you want to give

09:04.920 --> 09:09.520
that is the new name or target variable
name is equal to the variable name

09:09.520 --> 09:19.200
the current variable name that you have, right? So uh
so we write the collapse some and the new variable that

09:19.200 --> 09:22.510
you know the new name that we are
going to give this new variable

09:23.480 --> 09:30.100
is equal to the current variable, then mean new
variable name is equal to the current variable

09:30.100 --> 09:37.260
And this is how we get the some of ours and men of
ours remember this is the name that we provided to

09:37.260 --> 09:39.550
You can give any name that you want

09:41.280 --> 09:46.860
Okay. So we will move to the case
wise or wise deletion option

09:46.860 --> 09:48.720
which is C W option

09:49.570 --> 09:58.810
Now, let's summarize this wage and hours. You
get to see that we have 20-46 observations

09:58.810 --> 10:03.580
in wage and 20 to 42 observations in hours

10:05.260 --> 10:10.760
Now, if we were to collapse, create mean
or some whichever parameter you want

10:12.260 --> 10:18.480
What collapse would do is it
would create a mean for all

10:18.480 --> 10:25.220
you know, by taking into account all these 20-46
observations. But in case of a number of hours

10:25.220 --> 10:33.500
it would only take these 20-42
observations. What if you wanted to

10:33.500 --> 10:41.370
you know, create They mean you wanted to
calculate the mean only for rose that are

10:41.370 --> 10:49.000
that have complete data that do have a data of
wage and hours rather than missing variable

10:49.060 --> 10:54.880
missing values in a variable. So
let's for the purpose of demonstration

10:54.880 --> 11:03.190
I would create some missing values in the wage
variables to replace wage with missing in row 12

11:04.900 --> 11:14.770
So now if again, I were to show you this, then you
would see that wage have 1746 observations and ours have

11:14.770 --> 11:18.880
20-42. And if we were to use collapse
and in this case, we were to use

11:19.430 --> 11:24.190
we are to count the values rather than

11:25.210 --> 11:29.610
calculate mean or some more, some
other thing of the age and ours

11:29.610 --> 11:31.020
let's see what we get

11:32.400 --> 11:35.570
we get exactly the same number of observations, right

11:36.440 --> 11:41.540
So what this means is that if
instead of count I was to use mean

11:41.810 --> 11:50.840
it would calculate the mean of these 70, observations
and the mean of these 20-42 observations for hours

11:52.120 --> 12:00.210
But this is not what I want I want is that we
should have a revised relation or case of isolation

12:00.210 --> 12:05.380
And what it means is that only
calculate the, the mean of wages

12:05.380 --> 12:14.480
where the wages and ours data, both the data is
available only for those roles and calculate the mean

12:14.480 --> 12:21.680
of ours where only the data of both these variables,
the variable and our variable is available

12:21.680 --> 12:26.030
So where the case is complete,
right? Where the road is complete

12:26.120 --> 12:35.710
just keep those, those and calculate the mean for
those roles. So we use C W which times for case wiser

12:36.140 --> 12:40.800
And when we do that, let me
must bring in the data and

12:42.340 --> 12:51.990
perform this, right? And now if we do that,
you see that we have 17 42 because this is the

12:51.990 --> 12:57.010
these are the observations, we have
the data for both wages and ours

12:58.770 --> 13:05.040
we can also use the F option, the
conditions treatment in the collapse. So

13:06.600 --> 13:12.870
If you collapse, find count of wages
if wage is greater than 30 by race

13:13.240 --> 13:17.570
So we get to see that there
are 35 white people who are

13:17.570 --> 13:24.380
whose wages are greater than 30, but there are only
48 black people whose wages are greater than 30

13:26.490 --> 13:29.970
We can also use weighted average, right

13:31.320 --> 13:39.220
And let me bring the data back. So if
you collapse, we find the mean of wage

13:39.220 --> 13:47.180
but now we use in these brackets, we use
this FW which stands for frequency weight

13:47.370 --> 13:54.380
which will be equal to arts. So it would
calculate the weighted average of means

13:54.380 --> 14:05.430
And weight would be the number of arts. So if you have different stocks
and different industries and you want to calculate debated average

14:05.430 --> 14:14.540
returns of industries, then you are going to use
the mean of returns F W would be equal to the market

14:14.540 --> 14:19.910
capitalization of the, you know, the the firms

14:21.950 --> 14:26.990
and this by variable would contain the
by industry, right? But in this case

14:26.990 --> 14:31.580
we are going to do that without data.
Probably I would make another video for that

14:33.250 --> 14:42.370
So we now have a weighted average of wages, right? But just
to demonstrate and give you a clear picture of what it

14:42.370 --> 14:46.560
is doing behind the scene, I would just take

14:47.650 --> 14:56.770
a screenshot of these, right? And I have it over here and
I would move step by step and just to demonstrate what

14:56.770 --> 15:05.840
would just happen. So first we create by race, we generate
a total variable that would contain the number of hours

15:05.840 --> 15:06.870
for each race

15:08.350 --> 15:15.240
Then we can calculate the percentage for each individual
that would be percentage is equal to ours divided by total

15:17.610 --> 15:26.820
and then multiply the percentage with wages
and then we take the total of the some of these

15:26.820 --> 15:31.770
over the race. So that would be the evaluated

15:34.220 --> 15:35.180
weighted averages

15:36.020 --> 15:40.400
So we get exactly the same values 8.39. uh

15:42.090 --> 15:45.450
and then for the

15:46.490 --> 15:56.490
7.08 For, for black people,
37.008. And the last one is a 9.21

15:56.490 --> 16:05.180
So we get exactly the same, same values just to
get you the idea what have happened behind the same

16:05.360 --> 16:09.510
But in this case, we get to keep
all the data. But in collapse

16:09.510 --> 16:16.280
we just get the, you know, the collapsed
datasets. Thanks for watching the video

16:16.280 --> 16:25.120
Do hit the like button, pelican and subscribe to
this channel. Stay tuned to more updated videos

16:25.120 --> 16:29.190
And if you want to download,
they do file for this video

16:29.190 --> 16:32.490
just click on the link in given description
