Remind students to install Stata
Have students open Github Desktop
Have students open Stata.
Stata
- Programming language
- IDE - Integrated Development Environment
- A statistics and data analysis environment
- Now can run simulations, do ML, produce markdown files, interact with Python
- R is what runs all of the code we will write this semester
- Separate from RStudio
Stata Graphic User Interface (GUI)
- Three components for interfacing with Stata
- Command line - contains several windows
- Command window
- Results window
- History window
- Variable window
- Results window
- Project Manager - contains several windows
- Project window
- Text Editor window
- Navigator window
- Properties window
- Data Editor/Browser
- Viewer - how one reads help files and logs
- Graph Editor
- Command line - contains several windows
- Command window is where Stata is actually running
- Can work in here “interactively”
- Run a single command and see the result
- This is also where Stata will run code written in the text editor
- Text editor (
.do-files)- Where we write code we want to keep and potentially reuse later
- Creates a plain text file that stores the code we’ve written
- This is where you will write all your code and is what you will submit for assignments
- Can also run commands using Stata’s drop-down menu
- Never do this to actually execute commands!
- can be useful to learn syntax of new commands or infrequently used commands
Basic expressions
- Write code directly in the text editor
display 2+2
display 2 * ttail(20, 2.1)
- This is called an expression
-
A set of commands that returns a value
- Run line
-
Run selection
- Save as
stata-intro.do - Need to add file or directory to see it in the Project window
- We can also use this tab to create, delete, and rename files & folders
Loading data
- Load sample data file
sysuse auto.dta, clear
describe
- Stata files end in
.dta - Stata can input data in almost any format
-
You can look directly at the data using Stata’s Data Editor/Browser
- Look at the data
sort price
- Take a look at the data again
Variables
- Data sets will already contain variables
-
A variable is a name that has a value associated with it
- We can manipulate an existing variable
sum weight
replace weight = weight * 2.205
sum weight
- We can also create new variables
gen kmpl = mpg / 2.3520
sum mpg kmpl
- Often more useful than looking at the data in the data frame is visualizing the data
- Stata has numerous different graphics commands
- However, most start with
graph twoway
graph twoway (scatter price mpg) (lfit price mpg)
Basic Stata syntax
prefix: command [varlist] [= exp] [if exp] [in range] [weight] [, options]
prefix: A prefix to a Stata command that modifies how or on what the command is carried out, such asbyorquietly.command: The specific Stata command you want to run, such assummarize,list, orgraph. Most commands can be shortened (e.g.,sumforsummarize).[varlist]: A list of one or more variables to be used by the command.[=exp]: An optional expression to create a new variable.[if exp]: A conditional statement that restricts the command to only certain observations. The expression is evaluated as true or false. For example, ifrep78 > 3.[in range]: A qualifier that selects a specific range of observations, such asin 1/100for the first 100 observations.[weight]: Specifies a weighting variable.[, options]: A comma followed by options that modify the command’s behavior. Options are separated by spaces. For example,summarize, detailprovides more detailed statistics.
Comments
- Remember what code is doing
- For humans, not computers
- Use the
*
* Convert imperial to metric
Assignments format
- SEPARATE FILES FOR IN-CLASS CODING AND HOMEWORK
- Comment before each problem and each sub-problem
**# Problem 1
**## 1.1 - convert length to metric
replace length = length * 2.54
**## 1.2 - compare mpg across car type
sum mpg, by(foreign)
**# Problem 2
* regress price on mpg and repair record
reg price mpg rep78
Create assignment script, put in new folder
- Now we’re going to work on some exercises to get a feel for this
- In class we will often only do part of an exercise and save the rest for later
Summary
- Stata is both a programming language and an Integrated Development Environment (IDE).
- Stata allows us to manage data, run statistical analyses, and produce deliverables.
- The GUI includes specialized windows for commands, results, variables, and history.