Learning Objectives

Following this assignment students should be able to:

  • Load data from various locations
  • Create a reproducicble development environment
  • Construct relative paths
  • Apply house style
  • Employ Stata’s order of operations
  • Revise and reformat code

Reading

Topics

  • Project directory
  • Relative paths
  • Format
  • Style
  • Readable code

Readings

Lecture Notes

  1. Pathways
  2. Setting up your Development Environment
  3. House Style and Formatting
  4. Code Execution

Exercises

  1. Load or Download File (10 pts)

    There are a number of different ways to get data into Stata. And Stata can handle a number of different file formats. The way to load data into Stata requires answering several different questions.

    • Is the data remote (in the cloud) or local (on your machine)?
    • If the data is local, what is its location relative to your working directory?
    • What is the file format of the data?

    Knowing the answer to these questions will tell you which of the many ways you will want to load data into Stata.

    1. You can load data into Stata directly from a website. If the data is in Stata’s .dta format, you can use the standard Stata commands for loading data, just give it the absolute pathway to the location of the data on the internet. Install data from https://haghish.github.io/github/langlist.dta and then describe the data.

    2. You can also download data to your machine, placing it in a specific folder, and then use the absolute path to load the data useing use. Download the file tenuredata.dta. Take care to note where you save this file. Then load the data into stata and describe the data.

    3. Stata has a number of different example data sets that get download to your machine when you install Stata. Stata knows where these files are so one does not need to provide an aboslute or relative path when loading them. Load cancer.dta using the sysuse command. Then describe the data.

    4. Stata can also input almost any type of data file and convert it to a .dta format. To do this, you use a compound command that starts with import and is followed by the format of the file. Type in help import to get a list of all the different types of files Stata can import. Download the file district_size.csv. Again, take care to note where you saved the file. Then use help to find the Stata command to import a comma-separated (the cs in .csv) file. Describe the data.


  2. Format the Code (10 pts)

    Programming computers to do cool science is a major advantage for modern scientists. But, developing research programs that are transparent, collaborative and reproducible is a major advantage for science. All data scientists can contribute to this goal by writing their code in easily readable, well described, and well commented scripts.

    This script, which evaluates the number of diary entries made over time by treatment and control households in a solar stove field experiment that I ran, doesn’t follow good style. It works, but it’s difficult to understand exactly what it is doing.

    • Download the script
    • Download the data
    • Take a minute to try understand what is going on (you won’t perfectly because you are unfamiliar with many of the commands)
    • Modify the code so that it still does the same thing, but is easier to read because it has better style

  3. Basic If Statements (15 pts)

    Load auto.dta using the sysuse command.

    1. Create a variable y that equals “1” if price is less than or equal to “4195”. How many cars have a price less than “4195”?

    2. Replace the value of y with a “3” if price is greater than or equal to “6342”. How many cars have a price greater than “6342”?

    3. Replace the value of y with a “2” if price is greater than “4195” and less than “6342”. How many cars have a price between “4195” and “6342”?

    4. The code below creates a global called pack that equals “1”. Complete the code so that if $pack == 1 the code prints “Setup mode: running installation/setup steps…” and if $pack doesn’t equal “1” then the code prints “Run mode: skipping setup and continuing with the analysis.” Set $pack to something other than “1” and run the code again.

    	global   pack  1
    
    	if XXX == y {
    		
    	}
    	else {
    
    	}
    

    5. Update your code above by adding an else if statement that prints “Update mode: updating ado files…” if $pack == 2


  4. Code Shuffle (15 pts)

    We are interested in understanding the monthly variation in precipitation near Gainesville, FL, so that we can understand the correlation between rainfall and citrus yield. We’ll use some data from the NOAA National Climatic Data Center. Each row of the data is a year (from 1961-2013) and each column is a month (January - December).

    Rearrange the following program so that it:

    • Imports the data from the web into a data frame
    • Creates a variable called year which tracks how many years it has been since 1960
    • Calculates the mean precipitation in each month across years
    • Plots the monthly averages as a simple line plot

    Finally, add comments above each code bloack that describes what it does.

    It’s OK if you don’t know exactly how the details of the program work at this point, you just need to figure out the right order of the lines based on when variables are defined and when they are used.

        egen		mean_rain = rowmean(v*)
    	twoway		(line mean_rain year)
    	gen			year = _n
        import		delimited "https://datacarpentry.org/semester-biology/data/gainesville-precip.csv", clear
    

  5. Create Project Do (40 pts)

    For this exercise we are going to create a project.do file that we will use on this and all subsequent assignments.

    1. Open your Stata Project and in the blank .do file editor start by typing in our standard preamble (using either 497 or 597 for course).

    * course: AAE 597A
    * created on: dec 25
    * created by: jdm
    * edited on: 16 dec 25
    * edited by: jdm
    * Stata v.19.5
    

    2. Then type in a a bulletted list of what this file does and what it assumes that a user has on their computer. This part of the preamble isn’t required for assignments, since what an assignment .do file does and assumes is pretty self explanatory. But when you start doing research, having a list of what a file does, assumes, and if the file is complete (TO DO) is very useful. It allows you (or your advisor) to quickly see what a file contains without reading through the code.

    * does
    	* establishes identical development environment for users
    	* sets globals that define absolute paths
    	* loads any user written packages needed for analysis
    	* runs all assignment do-files
    
    * assumes
    	* access to all data and code
    
    * TO DO:
    	* done
    

    3. Next, create the 0 - setup section that comes after the preamble in every file that you write. Typically this is where we set our relative paths. But for the project.do file we are going to start by creating a global called pack (short for package) and we will set the value of pack to 0. We will call this global later in the project.do file. After we create pack we want to specify which version of Stata the code runs on.

    ******************************************************************
    **# 0 - setup
    ******************************************************************
    
    * set $pack to 0 to skip package installation
    	global 			pack 	0
    		
    * Specify Stata version in use
        global          stataVersion 19.5
        version         $stataVersion
    

    4. Now we will use a combination of local and global macros, along with a conditional if statement, to dynamically set the working directory. * The if statement will define the global code and data as absolute paths, depending on which computer the code runs on * The c(username) local checks to see what the computer’s username is (c(username)) * The global code is assigned a value that equals the absolute path on the machine jdmichler to the git repo where the code lives * The global data is assigned a value that equals the absolute path on the machine jdmichler to the cloud-based storage system where the data lives * Since git syncs repos with the cloud, it is redundant to store code on a cloud-based storage system * This is not true for data, so make sure you save data to Dropbox, OneDrive, Google Drive, Box, or some other storage system that syncs or backs up to the cloud * In addition to defining the globals code and data for your own machine, make sure you also define them for my office desktop (jdmichler) and my laptop (jdmic) as written below. * This will allow me to run your code on my machine without changing any of your code.

    ******************************************************************
    **## 0.1 - Create user specific paths
    ******************************************************************
    
    * Define root folder globals
    	if `"`c(username)'"' == "jdmichler" {
    		global	code	"C:/Users/jdmichler/git/semester26"
    		global	data	"C:/Users/jdmichler/dropbox/semester26/data"
    	}
    
    	if `"`c(username)'"' == "jdmic" {
    		global	code	"C:/Users/jdmic/git/semester26"
    		global	data	"C:/Users/jdmic/dropbox/semester26/data"
    	}
    

    5. Add an additional section (call it **# 1 - run assignment files) to the project.do file that runs or executes assignment 2 so that when I check your work I only have to run the project.do file and it sets the development environment and runs all the code you wrote for assignment 2. This is what is known as “push button replicability.” To do this, you need to use the do command and write out the relative path for the location of assignment 2. The path will be “relative” to the absolute path defined in code so it should start with the global $code.


  6. Check That Your Code Runs (10 pts)

    Sometimes you think you’re code runs, but it only actually works because of something else you did previously. To make sure it actually runs you should save your work and then run it in a clean environment.

    Follow these steps in to make sure your code really runs:

    1. Restart Stata by closing the instance of Stata that you have open.

    2. When you reopen Stata, type in the command line clear all. Or, better yet, put clear all at the top of your .do file, after the preamble but before any commands.

    3. Rerun your entire homework assignment by clicking the Execute button to make sure it runs from start to finish and produces the expected results.

    4. Make sure that you saved your code with the name of the assignment number (i.e., assignment_01). You should see the file in your git repo on your machine.

    5. Make sure that your code will run on other computers (relevant for all assignments after the first assignment)

      • No cd. Use a project.do file instead to set directories
      • Use only relative paths in files other than the project.do file
      • Use / not \ for paths

  7. Challenge 2 (Challenge - 20 pts)

    For this challange, you will need to use if and else statements along with the global $pack to set up a loop that installs user written packages that we will need throughout the length of the course.

    1. Add an additional subsection (call it **## 0.2 - Check if any required packages are installed) to the project.do file that will contain the code to install packages.

    2. Next, write an if statement that will execute a code block if the global $pack is equal to 1.

    3. After the if statement, add braces ({}). The code that we write within these braces will be what gets executed if $pack equals 1.

    4. Within the braces, define a local (loc) called userpack. The contents of the local will be the following user written packages. When using a local in this way you need to use = in order to set userpack equal to the list of user written packages. You will also need to inclose the list of packages in " ". Packages should be listed with just a space in between each package name and they must all be on the same line (no breaking or wrapping the line like we do in the style guide. This is the only exception to that rule). * blindschemes * estout * palettes * distinct * catplot * colrspace * coefplot

    5. After you define the local userpack then paste in the following code block. For each user written package in the local userpack (foreach package in userpack) the code checks to see if the package is already installed (capture : which package, all). If it isn’t (if (_rc)), it creates a pop up window (capture window stopbox) called rusure that tells the user that the package is missing (You are missing some packages) and asks if the user wants to install the package (Do you want to install package?). If the user selectes “yes” than the code installs the package from the “ssc” repository (capture ssc install package, replace). If the package is not on “ssc” (if (_rc)), then the code creates another pop up window (window stopbox rusure) that tells the user the package is missing (This package is not on SSC. ) and asks the user if they want to proceed without the package (Do you want to proceed without it?). Alternatively, if the package is already installed (else) then the code simply exists the loop (exit 199).

    	loc userpack = "blindschemes estout palettes distinct catplot colrspace coefplot"
    	
    * install packages that are on ssc	
    	foreach package in `userpack' {
    		capture : which `package', all
    		if (_rc) {
    			capture window stopbox rusure "You are missing some packages." "Do you want to install `package'?"
    			if _rc == 0 {
    				capture ssc install `package', replace
    				if (_rc) {
    					window stopbox rusure `"This package is not on SSC. Do you want to proceed without it?"'
    				}
    			}
    			else {
    				exit 199
    			}
    		}
    	}
    

    6. Finally, after the above code block, but still within the {} of the original if statment, tell Stata to update all user written packages (ado files). And then permanently change the graphic scheme to one we just downloaded (set scheme plotplain, perm). Lastly, Stata’s default is to freeze the output it displays until you tell it to move on. We will turn this feature off (set more off).

    	* update all ado files
    		ado update, update
    
    	* set graph and Stata preferences
    		set scheme plotplain, perm
    		set more off
    

Assignment submission checklist

Solutions

  1. Load or Download File
  2. Format the Code
  3. Basic If Statements
  4. Code Shuffle
  5. Create Project Do
  6. Check That Your Code Runs
  7. Challenge 2