Welcome to the first problem set. There is not a great deal of material here, but since this may be your first time using R and R Markdown, there are many potential pitfalls, so leave yourself plenty of time to complete it. The idea here is not that you can sit down and answer these questions straight away, but that you have a week to figure it out, and this is a key part of the learning process.
If you are looking at the HTML version of the problem set (pset1.html) that may have opened in your web browser, you are seeing the ouput produced by running the “script”" or code called in the file pset1.rmd, also available on the course website. Go ahead and open the file called pset1.rmd. If it does not open automatically within R Studio, you can open R Studio first and then use the File menu to open up pset1.rmd. Once you open pset1.rmd, you can continue reading the text easily in that file.
It will be easiest for you to open the .rmd file posted for each pset, and start writing your solutions in by learning from the code you see in the questions.
The text, output and graphics in this section are provided as an example whenever you create a new R markdown (.rmd) file in R Studio. It’s a good quick introduction so I replicate it here with minor modification. At this point, you may not understand all of the R code being used here, but the goal is to understand how the .rmd file works and how it relates to the .html file that gets outputted.
Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com. (Here we have set the code to produce an HTML output, which is what you need to upload for this class).
When you click the Knit button a document will be generated that includes both the content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot. In general, you will not include the echo = FALSE, because we will want to see your code.
Please submit your problem set via Gauchospace. Submit both the .rmd file, and the HTML file it creates. This assignment is due by 11:55 PM on Thursday January 24th. No late problem sets accepted. Please list any students you collaborated with.
Here is an example problem, with an example solution.
Question 0. In this question, we’ll provide the answer for you, as an example. You need to be looking at the .rmd file right now for this to make much sense.
Showing your code and the result, execute the code getwd(). Describe what this command does. You may want to execute the command directly in the console first (ask your TA if you dont know how to run a command–this is essential) to see what it does, but be sure to write it into your .rmd file so that it runs when you click knit.
Solution: Question 0
getwd()
## [1] "/Users/cbrehaut/Documents"
This command, when executed (either in the console or through the .rmd file once you click ``knit’’), tells the user what directory is set as the working directory. This is the directory where output will be saved, or where R will look first when searching for a file, for example a dataset. You need to always set your working directory first so R knows where to pull the data from.
Make sure to try executing your .rmd file now by clicking knit. Then take a look at the HTML that it created and see what you get.
Okay, your turn to answer the remaining questions!
Part 1. Short answer questions
Start by reading Chapter 1 of the textbook, Real Stats. You can also review your lecture notes.
Q1. A researcher observes that more educated people vote at a higher rate. He decides to publish a research article that says completing a bachelor’s degree causes people to participate in elections at a higher rate. Would you like to be a co-author on this paper? Why or why not? (100 words max)
I would not like to be a co-author, as making an assumption that a higher degree equates to more political involvement results in over claiming. There’s also an issue of Selection Bias where the author only observes educated people, but doesn’t take locations into account, such as places that are more involved with political campaigns.
Q2. Explain what this sentence means: “Experiments create exogeneity via randomization.” (75 words max)
This means that, with the inclusion of experiments, which, when done well, vastly limits the chance of any confounders, resulting in exogeneity.
Q3. What are some problems with experiments, particularly in a social science discipline such as political science? (100 words max)
Problems with experiments include: -Not everything can be randomized in social sciences (i.e gender, political governments)
-Not everything should be randomized (i.e wars, birth defects, medical aid)
-Can bring up potential ethical dilemmas
-Experiments are expensive
Q4. You decide to run an experiment to see whether going to lectures helps students learn. You randomly assign half of the class to go to the lecture and section for the course, and to read the textbook. For the other half of the class, you just assign the students to read the textbook. At the end of the semester, you give the entire class a test. You find that the students in the first group did much better than those in the second group, who only read the textbook. (120 words max)
4a. What could you call each group?
The first group could be called the uncontrolled or untreated group, while the latter could be the controlled or treated group.
4b. What is your independent variable and what is your dependent variable?
The independent variable, otherwise known as the variable in which you control, include the material of the entire course (attending lectures, sections, and reading textbooks). The dependent variable, the variable that follows an action, would be the test scores.
4c. Given this set up, list some factors you are controlling for. Some factors we’re controlling for are grades/test scores, randomizing between students for each group, and who is or is not attending their sections/lecture.
4d. Can you say that attending lectures caused the students to do better on the test? Why or why not? Explain using the technical terms in the textbook.
We can’t say for sure that attending lectures caused the students to do better on the test, but we could say that test scores are significantly higher among those who attend lecture, section, and read the textbook than those who do not do all three. The reasoning behind this is that the given situation doesn’t take into account other students’ obligations, such as the need to divide their time to work.
4e. Can you say that this finding would also apply in courses with online lectures? Why or why not? Explain using the technical terms in the textbook.
With online courses, there will be different distractions present, such as a potential increase to cheating. In regards to the other issues present with the previous questions, those concerns will still be present (need to work, take care of families, etc.)
Q5. Imagine you are looking at the relationship between income and level of education. List some of the factors that could lead to endogeneity. (50 words max)
Factors that could lead to endogeneity between income and level of education include:
-Connections/networking
-Inheritances
-Paying off loans and bills -Supporting families or other obligations -Competition for jobs between equally qualified candidates, even for those who have completed elevated forms of education (grad school)
Part 2.
When James Carville was crafting a simple catchphrase to summarize then-presidential candidate Bill Clinton’s electoral message, he hung a sign up at campaign headquarters that read ‘The Economy, stupid.’ The phrase has since morphed into ‘It’s the economy, stupid’ and it still reflects the core message that the economy decides elections. When times are good, voters want more of the same; when times are bad, they want a fresh face. If we look at presidential elections from the last 70 years, do the data support this claim?
The Presidential Voteshare database from 1948–2012 offers a chance to evaluate this hypothesis.
Download the dataset, presvote.Rdata, which you’ll find on the course website. You may want to put it in your working directory to make it easy to find (use getwd() to see what your current working directory is; you can use the Session menu in Rstudio or the setwd() command to change your working directory.)
Here is a brief description of the variables:
As will often be the case when using R, you will need to use the $ operator to access these variables within the object. Specifically, once you have loaded presvote.RData, the result will be available in the data presdata. To get at the variable vote, for example, you would use presdata$vote. Remember, the end of each chapter in the textbook includes R code that can be helpful. We also posted R resources on Gauchospace.
Q1. Load the data into R. The data are stored as an Rdata file, so you can use the load() function to load it. load(presdata)
Q2. Check the dimensions of the data (i.e. the number of rows and columns). How many observations are there? What are the dimensions of the data? What is the range of years covered in this data set?
7 Columns, 17 Rows, total number of years covered in the data set = 64 (1948-2012)
Q3. Calculate the average change in real disposable income across all points in the sample. Do you think this is a large or a small average? What is the minimum and the maximum change in real disposable income?
The average is 2.66629929. This is a fairly small average. The minimum change is -0.596950, while the maximum change is 6.035290.
Q4. Calculate the average vote share across all points in the sample. What does this tell you about the power of incumbency?
The average vote share across all points in the sample is 52.04586. This tells us that incumbents have an incredibly large advantage in their votes, with massive support due to already being in office.
Q5. Produce a simple scatterplot with change in income on the horizontal axis, and points showing the incumbent party’s vote share in each year.
plot.default(presdata\(rdi4,presdata\)reelection)
plot(presdata\(rdi4, presdata\)vote, xlab = “Change in Income”, ylab = “Vote Share”, main = “Vote Share based on Change in Income”)
Q6. Add a line to that plot (so you see a jagged line going through all the points). You want it to look like a line graph.
plotline(presdata\(rdi4, presdata\)vote, xlab = “Change in Income”, ylab = “Vote Share”, main = “Vote Share based on Change in Income”)
abline(lm(presdata\(rdi4, presdata\)vote, xlab = “Change in Income”, ylab = “Vote Share”, main = “Vote Share based on Change in Income”))
lines(presdata\(rdi4, presdata\)vote, xlab = “Change in Income”, ylab = “Vote Share”, main = “Vote Share based on Change in Income”)
Q7a. Make the plot again, but this time add a trend line (also known as a line of best fit or a regression line).
abline(presdata\(rdi4, presdata\)vote, xlab = “Change in Income”, ylab = “Vote Share”, main = “Vote Share based on Change in Income”) Q7b. What does this line tell you about elections and the economy?
Q7c. What could you call this relationship?
It’s a positive relationship…