Beruflich Dokumente
Kultur Dokumente
Chapter 1
First things first. Before you can use Stata, you have to get access to it.
How do you get it? Your college or university may provide Stata in
official computer labs. If it doesnt (or if you want a personal copy), you
can buy and download Stata directly (www.stata.com). Fortunately,
reasonable student pricing is available.
With access to Stata, you open it as you would any program on your
computer (like Word, Excel, etc.). When you open Stata on a PC, you
should see something like Figure 1.
FIGURE 1.1
Stata also runs on Macs and, while it looks slightly different, the
commands and functionality are nearly identical on a PC. Figure 2
shows Stata on a Mac.
Lets talk about what you see. There are five panels or windows in
Stata. The biggest one, squarely in the middle of the screen, is the
Results window. Nicely, it shows you the results of what you tell Stata
to do.
At the top left is the Review window. This area provides a history of
all the commands you have given Stata. The top right is where the
variables in your dataset will show up and the bottom right is where
youll see properties of the variables.
Stata Format
FIGURE 1.3
FIGURE 1.4
Also notice that opening the command was recorded in the Review
window.
Of course, to open a Stata dataset you can could also find the file on
your computer and double-click on it, just like you open files with
most other common software.
Lets say you have the following data that you need to get into Stata.
As before, open Stata as you would any other program. At the very top,
you will see an icon that looks like a spreadsheet with a pencil. Figure
1.5 shows this:
If you click on this icon, it will open a Data Editor window. Figure 1.6
shows the Data Editor. As the name suggests, this is where you can
edit data.
Start in the top-left cell (indicated by blue arrow) and type 35000 and
hit enter. Figure 1.7 shows what you should see:
FIGURE 1.7
FIGURE 1.8
Name the variable Income. Figure 1.9 shows what you see after
doing this.
The next step should be to enter experience and the name of the first
person (Bruce). You would enter 8 in the first row, second column
and then Bruce in the first row, third column. Figure 1.10 shows this.
At this point we have all the information for Bruce in the data set. The
first row in the data set contains all of Bruces information. It is worth
repeating that a row in Stata is an observation.
FIGURE 1.11
You have now worked through getting data into Stata the hard way. I
would suggest at this point you save your data set. Save early and
save often is a VERY good rule to live by! The easiest way to do this is
the click on file>save as as you would with any other software (such
as Word) as shown in FIGURE 1.12. Naturally, after you have saved and
named the file the first time, to save you just click on save.
After you have saved your data set, you can now close your data editor
window. Youll probably notice that there are many lines in the Results
and Review windows. This is shown in Figure 1.13 (blue and red arrows,
respectively).
FIGURE 1.13
What you see is Stata making a record of everything you did as you
entered the data in the form of Stata commands. As before, this is a
helpful (and sensible) feature of Stata and something we will explore
more formally later.
We will use the same data we manually imputed. I have recorded the
data in an Excel file, shown in Figure 1.14.
FIGURE 1.14
To import this into Stata, click file>import in Stata and select Excel
spreadsheet (*.xls; *xlsx) This is shown in Figure 1.15 (blue arrow).
FIGURE 1.15
From here, click on Browse (blue arrow) which will allow you to
select the file you want to import. My file is named
FIGURE 1.17
After clicking the first box and NOT the second box, hit OK. This will
automatically pull the Excel data into Stata. You should then see
something like Figure 1.18.
FIGURE 1.18
Once you have data in Stata, you can actually do interesting things.
You will use some commands frequently in Stata and here well work
through some of the common ones. Well use the income and
experience data introduced above and pick up right after the Excel
data import.
Summary Statistics
One question that might come up is, what is the average income for
our data set? Put another way, what is the sample mean of income?
summarize variablename
Taking the above syntax and applying it to our income and experience
data, you would type the following in the Command window
summarize Income
FIGURE 1.19
If you wanted even more information on income, you could ask for
detailed summary statistics. To do this, you would add ,detail to
the end of the command.
Figure 1.20 provides a picture of what you should see after this
command, zooming in to only see what would is displayed in the
Results window.
Adding detail to the command gives you much more information. Our
data set only has 5 observations so this is not as interesting as if we
had thousands of observations. Still, the point is that you can easily get
quite a bit of information about a variable in Statawhether it has 5
observations or 5 million.
Its easy to get summary statistics for more than one variable at the
same time. The general syntax in Stata is:
You can add as many variables to the statement was you want. Or, if
you are lazy (no comment), you can just type:
summarize
That will give summary statistics on every variable in the data set.
Doing that for our data set generates something along the lines of
Figure 1.21.
FIGURE 1.21
Creating Variables
Where newvariable is the name you give to the variable you are
creating.
To create IncPerYrExp as defined above for our data set, Id give the
following command:
Figure 1.22 shows what you should see in Stata after this command.
Not much exciting happens. But, notice that in the Variables window
you have one more variable than before: IncPerYrExp (indicated by
blue arrow).
You can click on the icon to see it. If you do that, you will see
something along the lines of Figure 1.23.
FIGURE 1.23
Hopefully, you are starting to feel a bit more comfortable with Stata
and working with data in Stata. There is much more to learn and do
(Stata is pretty amazing!) and we are on our way.
The regress command tells Stata to take the specified variables and
perform a regression. Lets work through the Magic Hill example on
page 17 in Using Econometrics.
The Magic Hill data can be downloaded from the Using Econometrics
Student Companion website. The name of the data set is HTWT1.dta
and it has two variables:
Y i= 0 + 1 X i+ i
After loading the data into Stata, type the following command in the
Command window and hit enter.
regress Y X
Figure 1.24 indicates what you will see right before hitting enter. Figure
1.25 shows what you should see right after hitting enter.
FIGURE 1.24
The green arrow points to the Coef. column, which reports the
estimated coefficients. The first number in the Coef. column is
6.377093. That is the estimate for 1, the parameter for X. It matches
the 6.38 (rounded) of Equation 1.19 in Using Econometrics. Just below
that is _cons, the estimate of 0, the intercept. It is 103.3971, which
rounds to 103.40.