Welcome to the Data Hall YouTube channel. In this video we are going to talk about how do you
standardize or normalize variable in Stata. So there are different ways of standardizing or
normalizing variable for example we can use z-score or mean centering or normalization
and in Stata we have the center command that is quite useful and an easy way of doing all this
process. So we will first discuss how do we do this manually and walk you through the process
and that would help us in learning the idea behind these methods and lastly we'll discuss
the center command that is easy and intuitive to use. So let's load this sysuse data and let's
start with how do we standardize a variable. Now standardization means that we detect the mean of
that specific variable from all of its values and divide them by its standard deviation so that way
we would have a variable that would have a mean value of zero and its standard deviation would
be equal to one. Now we do standardization so that we can bring all the different variables
on to a similar scale and that way they are easy for us to compare. So let's summarize and let's
look at this price variable. This is a data set related to auto data set that contains
different variables related to different cars we have their price their mileage their
their weight their length etc etc. Now there is this variable the price of these cars and we can
see that there are different price values right and let's say if we wanted to standardize this
what we would do is we would generate a new variable let's call it price new and that would
be equal to the price variable so what this would do is it would take each value of this price variable
minus the mean value which we we got it from here divided by the standard deviation of the price
variable which we got it from here. So if I execute this and if I can show you this from the data view
we can see that we got this new variable called price new and this is a standardized variable
and we we discussed that it would have a mean of zero and a standard deviation of one so this is
a mean of zero and now this is what we did a manual method just to give you an idea of what
process goes behind the standardization or z-score and let's do this easy method that we can use to
standardize a variable in startup. So we have this command which is called egen and let's create a
new variable let's call it price new one so we have a different variable than this one and let's
use this std function this std function is used to calculate the z-score of a variable so we want
starter to calculate the z-score of this price variable so if I execute this and this variable
would be created and it contains the exact same values as we have when we did the manual method
now there might be small changes because in the manual method we didn't had quite a lot of decimal
points precision but in this case obviously we do have a precision so let's summarize the price the
price new variable and the price new one variable that we created and you can see that all of both
of these variables do have a mean of zero and the standard deviation is one right let's move to how
do we standardize this how do we standardize using manus you can also do this process from manus
you'd have to click on data then create and change data then there is this option which is called
create a new variable extended and if we go over there you just write the name of the new variable
over in this checkbox in this in this text box and scroll way down where you see this standardized
value you can change the mean value or the standard deviation value if you want to but
if you just and you write the name of the variable that you want to change press ok and that would
create this standardized value let's move to the second part which is called mean centering what
in mean centering what we do is simply deduct the mean from the each value of the variable so we have
this price variable and we know its mean value is 2949 let's create a price center which is mean
centered and let's take each value of price variable and deduct the mean so instead of
writing it manually over here this value we are using the scalars remember we can access scalars
using this command return list and that would give us the name of the scalar that
stores the mean value okay so let's execute this and let's look at the mean value now what's mean
centering would do is it would not change the standard deviation but it would simply convert
the mean value to zero right deducting it mean value from each of the values of the price variable
would convert it into a zero mean value let's move to the third part which is which is called
normalization now normalization you know what it does is it it scales the variable to a fixed
range so typically the range is from from zero to one in normalization is typically used when
the scale of a variable is not known and when the variable has a non-uniform distribution
now this helps this variable to bring it to a same scale and it's again easier for us to compare now
what we do is not in normalization is we take the original variable minus the minimum value and
divide it by the range that is the the difference between the maximum and the minimum value so let's
do this process for the mpg variable let's execute summarize we have the mean and the minimum and the
maximum value we do not need mean in this case so let's create mpg1 and that would be our so that
would be mpg minus the minimum value divided by the range that is maximum minus minimum this is
how we can write command but i would rather use both of them which is the scalar method that i
discussed previously both of these would give us the same values that is their mean would be now
zero right last is how do we use the center command now the center command is the easy way to
use to standardize or normalize a variable we just tried the name of the command and name of
the variables that we want to standardize what it would do is it would create these variables
and add a prefix to them so for example by default it would centerize it would do mean
centering of the variable so let's compare this with our the variable that we generated and the
variable that is generated from the from the center command if we compare them you would see that we
get the exact same values we can also do standardization and we can also add prefix to each
of the variable that would be created so by default it added a prefix of c underscore but we can have
a different prefix so let's standardize this price variable and let's compare it
with the variable that we generated std underscore price and the variable that we generated using center command and the variable that we
generated previously using the manual method and you can see that they produces almost the same
result we can also generate change the name of the variable that would be generated so let's say we
want to generate variable but and instead of having this c underscore price let's have a new
name that we want to give it to now the nice thing is that we can use by sort for us of what it would
do is it would do mean centering for this price variable but instead of like doing the mean of
all the price values in the price variable it would do it for each category of the foreign variable
so there are a lot of different options that you can learn from this center command you can look
at the help menu and look at the different options that are there play with them and learn the standard
center command this is quite an easy way to standardize or normalize a variable so i hope
this video was useful do subscribe to this channel and do hit the bell icon thanks for watching this video