Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Problem Set #6

2022

Front Matter

This assignment covers the estimation of a difference-in-differences specification using panel data. To start:

●  Download a fresh copy of the EC420 Assignment Template from D2L. Save it in a folder you created just for EC420 Problem Set 4 and give it a real name.

●  For each of the numbered main headers (Part 0, Part 1, Part 2) in this document, create a main header in your markdown document using a double ##, as shown in the template.

● When there are tasks (which require only coding) and questions (which are to be answered) (e.g. “Task 1.A” and Question 1.B”), then use triple ###’s, as shown in the template.

●  Tasks only require coding. Make sure your code is echo”ed into your .pdf. Each Task can be done in one code chunk.  Questions should not require any additional coding (though you can add to the corresponding code chunk if you want).

Part 1:  Setup

Task 1.1 (2 points)

Load the lmtest,  sandwich, zoo and lubridate packages.   zoo will help us handle panel data that is measured quarterly. You may have to install it first (remember, never ever use install.packages in your code.  Install it once direcly in the console and use library(...)  to load the package).  For questions in which you are asked to make a plot, feel free to use ggplot if you prefer. Either ggplot or base-R is fine.

You will also need the following code to load the data. Make sure you load all your packages rst, though:

hw6  =  read.csv("https://raw.githubusercontent.com/ajkirkpatrick/EC420online/Spring22/PubData/hw6.csv ", stringsAsFactors  =  F)

Questions 1.1 (4 points)

(a)  (1 point) What is the variable name for the time dimension of this panel?

(b)  (1 point) What is the variable name for the unit of this panel?

(c)  (2 points) In what data type is the yq variable stored?


Part 2: Dierence-in-Dierences

Here we will employ our Difference-in-Differences estimator and compare it to some “naive” estimates. We will first do a little data manipulation and get some practice in R with merging and creating variables.

The data loaded in Task  1.1 is solar installation data from California.   Each observation is aggregated

publicly-available data about the total quarterly solar watts installed in a city, (cecptrating), the sum total cost of all installations in that city-quarter totalcost, the average incentive received (incentiverate), the average base cost of electricity in that city and quarter (PWRPRICE), the location (city), and our outcome variable of interest: watts installed per owner-occupied household WPOOH. You will be doing a similar analysis

as the Kirkpatrick and Bennear paper from our syllabus, but will not be using the exact same data nor get the exact same answer.

Task 2.1 (16 points)

●   (a)  (6 points) We will use the as.yearqtr(...) function from zoo to manage the time variable, yq. This is quite easy. Just use hw6$yq  =  as.yearqtr(hw6$yq). The column will now be recognizable to R as a time series.

●   (b)  (2 points) Try this: Look at head(hw6$yq) and head(hw6$yq  +  .75). Do you see what this does? Don’t overwrite your yq column with this, just take a look at how R manages adding time periods.

●    (c)  (2 points) Use table(...) on the new column called yq to see how many installations we observed in each quarter.

●   (d)  (4 points) Choose one city in the data and make a new data.frame with just that city by subsetting. Use plot(...) to plot WPOOH on the Y-axis and yq on the X-axis.

●    (e)  (2 points) We often want to see a simple line of best fit. This is easy to add after we have plotted our points. On the next line immediately after your plot(...) command, add an a-b line with a lm(...) call like this: abline(lm(WPOOH  ~  yq, mySubsetData),  col="gray50")

Question 2.1 (8 points)

   (a)  (1 point) What is the mean value for total watts installed, cecptrating, in the data?

●   (b)  (2 points) What is T, the number of time periods (hint: unique(...) returns a vector of all of the unique values of a column)

●    (c)  (2 points) What is N, the number of cities (hint: unique(...) returns a vector of all of the unique values of a column)

●   (d)  (1 points) We call a panel “balanced” if we have one observation in every city for every time period. Given the outcome of Task 2.1.a and Question 2.1.b and 2.1.c, do we have a “balanced” panel?

●    (e)  (2 points) What city did you choose for your plot?  Does there seem to be a time trend in the outcome variable, WPOOH?

Task 2.2 (11 points)

The treatment we are interested in, PACE, is not in our data. We have to merge in information about each city’s treatment status by quarter. Merging data in R is not terribly hard. You just have to have the data

you’d like to merge in another R data.frame and know which field(s) are the key fields. “Key” elds are the fields that will be matched up - here, it will be the city. The TREATMENT data has the city, the county, and the date that treatment (PACE) started, if any.

●   (a)  (2 points) We will merge in some TREATMENT data on each city in the data. It is located on my gitHub as well. Load it using the following code:


TREATMENT  =  read.csv(paste0("https://raw.githubusercontent.com ",

"/ajkirkpatrick/EC420online/Spring22/",

"PubData/TREATMENT.csv"),

stringsAsFactors=F)

●   (b)  (2 points) Since city is the key” field in TREATMENT, we need to check that it is unique. The R function duplicated(...) tells us which values in whatever column we give it are duplicated. If we sum(duplicated(...)) we can see how many duplicated values there are. Use duplicated(...) and sum(...) to see that TREATMENT$city has no duplicates. Duplicate values in the merge will multiply the number of rows in our data, which would be bad!

●    (c)  (5 points) Now, we want to merge the city level data in TREATMENT to the city-quarter data in hw6. We will use merge(...) for this and call the new object PAN.merged (PAN is for PANel). Merge takes the following function inputs:

1. x  =  hw6 (the data you start with)

2. y  =  TREATMENT (the data being merged)

3. by  =  c(city) The "keys" on which we are merging

4.  all  =  F This tells R to do an "inner join" which keeps only those cities that are in both PAN and TREATMENT

Put it all together: PAN.merged  = merge(x  =  hw6,  y  =  TREATMENT,  by  =  c(city),  all=F)

●   (d)  (2 points) Use names(PAN.merged) to show the names of the columns we now have merged and NROW(PAN.merged) to see how many observations we have.

Question 2.2 (4 points)

   (a)  (2 points) How many observations do you have now? Hint: it should be 2,250

●   (b)  (2 point) View() the data to see what we have. Is this panel, time series, or cross-sectional data? Why?

Task 2.3 (16 points)

The last thing we need is to create our time-varying treatment variable from the treatment start date in the data.

●   (a)  (2 points) We are going to set the PACE variable based on whether or not yq>=PACE.start. Currently, PACE.start is NA for all untreated cities, but contains the date of PACE start for the treated cities. First, convert PAN.merged$PACE.start to an R-recognizable date. Do this by using PAN.merged$PACE.startdate  =  ymd(PAN.merged$PACE.start).

●   (b)  (2 points) Next convert PAN.merged$PACE.startdate to a year-quarter using as.yearqtr(...) as before.

●    (c)  (4 points) Create a column in PAN.merged called PACE that is TRUE if both yq>=PACE.startdate and FALSE otherwise.

●   (d)  (2 points) R has trouble comparing anything to a NA. It’s likely that your PAN.merged$PACE column is a lot of TRUE and NA. Using a subset, assign a value of FALSE to any PACE that is na:

  PAN.merged[is.na (PAN.merged$PACE),  "PACE"]  =  FALSE.

  Note that we don’t have to assign this to a new object or column - we are updating the NA ’s in PAN.merged in place”.

 When we compare things using <,>,=, we get a type of data that is called a logical. R recognizes the words FALSE and TRUE as the two values of a logical object.


Now that we have our time-varying treatment variable, PACE, we need a non-time-varying indicator for all the treated cities. Just like in lecture, we will call this TMT and it will be TRUE for all cities who are ever treated. We will use an R shortcut function %in% for this.

The shortcut %in% takes whatever is to the left of it and tells you, item by item, if it is anywhere in the thing on the right. So c(1,2,3)  %in%  c(2,3,4) will return FALSE,TRUE,TRUE because the second two entries in c(1,2,3), 2,3, are in both 1,2,3 and 2,3,4.

●    (e)  (3 points) Make a new object called treated.cities  =  PAN.merged[PAN.merged$PACE==T,"city"]. This will be a vector of all of the treatment cities. Then, make a new column in PAN.merged$TMT     =  PAN.merged$city  %in%  treated.cities.

The new column in PAN.merged will be TRUE if the city is ever treated, and false otherwise.

●    (f)  (3 points) We should probably compare the before-treatment levels of the outcome variable between the two groups. We can use a boxplot to compare the mean and distribution boxplot(WPOOH  ~ TMT,  data  =  PAN.merged[PAN.merged$PACE==F,])

Question 2.3 (4 points)

   (a)  (2 points) Use table(...) to see how many treated city-quarter observations we have.

●   (b)  (2 points) The boxplot shows the mean (the horizontal bar) and the 25th-75th percentile (the edges of the box) for the variable WPOOH for each group before PACE starts.  The dots are the “outliers”. From this boxplot, does it look like there is a systematic difference before treatment between the treatment and the control?

Task 2.4 (5 points)

Let’s run some regressions. I’m not going to write out each regression. It’s up to you to construct the right regression formula. Make sure you always use HC-robust errors as usual.

First, lets be very naive and just compare the before-after amongst the treated cities only.   Run a re-

gression of WPOOH on PACE, PWRPRICE, and incentiverate on a subset of the data consisting only of the treatment group.  That is, only on the data for the treated.  You can subset in the lm(..., data=PAN.merged[PAN.merged$TMT==T,]) call. Use HC-robust errors.

 PWRPRICE is the average cost per kwh of electricity for the city.

●  incentiverate is the per-watt subsidy given by the state under the California Solar Initiative. It was designed to “step down” over time as more people install solar (it eventually hit zero in early 2014).

● PACE is our treatment variable of interest. It is the presence of a PACE program in that city during that quarter. It varies by city-quarter.

Question 2.4 (11 points)

   (a)  (1 points) By including PWRPRICE and incentiverate, what are we controlling for and why?

●   (b)  (3 points) Can you think of anything that isn’t controlled for that could cause bias? Hint: there are lots of possible answers since we are only looking at the treatment group, but make sure you explain why yours could cause bias!

    (c)  (1 point) What is the coecient on incentiverate and what does it mean?

●   (d)  (2 points) Does this comport with your prior expectation? That is, does it make sense? Why or why not?

●    (e)  (2 point) What is the coefficient on PACE and what does it mean? Make sure you state your answer including units.


●    (f)  (2 points) What is the std.  error on the coefficient for PACE, and is it statistically significantly different from zero?

Task 2.5 (5 points)

Run the regression from 2.4 again, but keep the whole sample. PACE is yq- and city-specific, so you don’t need to interact it with anything.

Question 2.5 (6 points)

●   (a)  (2 points) If we call the cities with PACE programs the treatment”, what do we call the cities without treatment?

   (b)  (1 point) What is the coecient on PACE in this specification?

    (c)  (1 point) Is it significant? Why or why not?

   (d)  (2 points) Is there anything missing that we have not controlled for?

Task 2.6 (5 points)

Finally, let’s do a Difference-in-differences specification.  PACE is already the interaction between TMT and POST (unlike in lecture, we have time-varying treatment times, so we don’t really have a POST), so all we need to do is add time fixed effects yq and city-level fixed effects city. This only works because PACE is already the interaction of TMT and POST. Run the DID specification.

Your output might be really long here (lots of coecients). Thats ok.

Question 2.6 (20 points)

   (a)  (3 points) By adding the fixed effects, what have we controlled for?

   (b)  (5 points) What is the identifying assumption for this regression?

●    (c)  (2 points) What is the coefficient on PACE and what does it mean  (note:  it won’t equal the coefficient in the Kirkpatrick and Bennear paper)? Is this the ATE?

   (d)  (2 points) Is it statistically significant and why?

●    (e)  (2 points) What is the mean of the outcome in the data (use mean(...)) and is the effect on PACE economically meaningful or not. That is, compared to the average value of WPOOH, is the effect big or small?

    (f)  (2 points) Which specification do you feel is the least biased? Why?

●   (g)  (4 points) Scroll down to see the time fixed effects. Do they follow a pattern? Does that pattern match the pattern you saw in the plot in Task 2.1? Note that, depending on what size window you have open, the p-value column might appear down below.

Part 3 (3 points)

How long did you spend working on this problem set (in hours)?