STATA 2

profiledlwsdu
hw2.pdf

Lab 3: Set Working Directory, Scatterplots and Introduction to

Linear Regression

Chao-yo Cheng

[email protected]

Zsuzsanna Magyar

[email protected]

January 16, 2016

1 Section objectives

In this section we will use the HW2.dta. This dataset is a small set of variables from the larger

“Maddison dataset” 1 By the end, you should be comfortable using commands to import

your .dta file, making (somewhat) fancy scatterplots, and running () regressions.

2 Commands

In this lab, you should become familiar with the following commands.

cd

use

regress

and

twoway scatter

lfit

scheme ()

3 Set working directory and quickly running a .do file

• Log in and open Stata.

• Log in the class website. Save the Homework 2 data in “My Documents” (or any folder that works for you).

• Open a .do file. First set the working directory on Stata. Type: cd "insert path address"

To get the address of your path, right click on “My documents” and press “Copy address”

so you can copy this inside your “” after cd.

1See here for more information: http://www.ggdc.net/maddison/maddison-project/home.htm.

1

• To import the data type the command use. Type: use HW2.dta

• Check whether or not the data has been imported properly.

• Now you know how to open your data using code. This means you can quickly run your .do file on a clean dataset using the clear all command at the top of the .do file.

This will save you the trouble of opening a fresh dataset once your do file is finished.

• To sum up, at the top of your .do file, type clear all

cd "path address"

use HW2.dta

Question 1. How many variables are in the dataset, and how many observations are there?

4 Scatterplots

• Everything after the “,” in a graphical command is an option. The variables being graphed come before the comma.

• Sometimes it is nice to use a scheme for your scatterplot so it looks simpler. Here we use the scheme (s1mono). Schemes determine the overall look of a graph.

• To draw scatterplots with observation labels and titles for the y and x axis. Type twoway (scatter gdppc_2000 gdppc_1500, mlabel(country)), ///

scheme (s1mono) ///

ytitle("GDP per capita 2000") ///

xtitle("GDP per capita 1500") ///

title("Scatterplot of GDP per capita 1500 versus 2000")

• Add a line of best fit using lfit command. Type: twoway (scatter gdppc_2000 gdppc_1500, mlabel(country)) (lfit gdppc_2000 gdppc_

1500, color(blue)), ///

scheme (s1mono) ///

ytitle("GDP per capita 2000") ///

xtitle("GDP per capita 1500") ///

title("Scatterplot of GDP per capita 1500 versus 2000")

Question 2. What relationship does the slope of the fitted line indicate?

5 Linear regression

• The dependent variable (or outcome variable) is what we are trying to explain. It is also called the “outcome” or Y .

2

• The explanatory (or independent variables) are what we use to do the explaining. These variables are also called predictors, as we think they are trying to predict the dependent

variable.

• The command for a regression is regress.

• Now let’s run a regression. Note that your dependent variable always comes before the independent variable. You should remember what units you are working

in in order to help you interpret your results. Type:

regress gdppc_2000 gdppc_1500

Question 3. What is the outcome in this regression? What variable are we using to predict

the outcome? How do we interpret the coefficient?

6 Summary statistics by continent

• In Homework 1, you calculated the mean for each region by typing three lines of code. A quicker way to do this is to use the tabstat command followed by by.

• Generate a single variable for continent. The straight vertical line means “or.” Type gen continent = .

replace continent = 1 if africa == 1

replace continent = 2 if east_asia == 1 | west_asia ==1 | cent_asia ==1

replace continent = 3 if latin_america ==1

replace continent = 4 if west_europe ==1 | east_europe ==1

replace continent = 5 if country == "United States" | country == "Canada"

replace continent = 6 if country == "Australia" | country == "New Zealand"

• Lets see what we have. tab continent

• Now we label the new variable. label define contname 1 "Africa" 2 "Asia" 3 "Latin America" 4 "Europe" 5 "North

America" 6 "Australia"

label values continent contname

• Suppose we want the mean population for each continent. tabstat pop_1820, statistics(mean) by(continent)

3

  • Section objectives
  • Commands
  • Set working directory and quickly running a .do file
  • Scatterplots
  • Linear regression
  • Summary statistics by continent