Introduction

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.

Before you start: The R Markdown Introduction

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.

Submission Instructions

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.

Example Problem

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/matthewross/Downloads"
setwd("/Users/matthewross/Downloads")

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)

No, I would not want to be a co-author on his paper. By stating that completing a bachelor’s degree causes that population to participate in elections at a higher rate, the researcher directly insinuates a causative relationship between the two trends. Assuming that the researcher has gathered the data from an observational study with no guarantee of randomization, there are likely to be confounders that confuse the relationship between the observed IV and DV in this experiment. This increases the overall endogeneity of the relationship, as we can not determine that our DV is independent of an unobserved error term.

Q2. Explain what this sentence means: “Experiments create exogeneity via randomization.” (75 words max)

Exogeneity is the opposite of endogeneity. It is a state in which all confounders or unintended error terms (Stochastic elements) are eliminated through randomization. A correlative relationship between an observed independent variable acting upon the dependent variable can be deemed causitive. An experimental study would allow for this randomization to occur in the data, therefore fostering exongeneity in the data relationship.

Q3. What are some problems with experiments, particularly in a social science discipline such as political science? (100 words max)

The largest issue with performing experimental studies in social science disciplines is that, often times, it is difficult to replicate conditions necessary to collect randomized data so as to exclude stochastic elements. For example, one would find it nearly impossible to replicate a number of statistically random nations that differ in government type in order to measure economic conditions as correlates to system of government. As questions like this constitute the majority of inquiries in the social sciences–specifically political science–observational studies are typically more practical for observing relationships.

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 group that does not attend lectures would be the control group while the group that does attend would be the treatment group.

4b. What is your independent variable and what is your dependent variable?

IV: Lecture Attendance

DV: Test Score

4c. Given this set up, list some factors you are controlling for.

-Through randomization, you control propensity for studying within the class. -Through randomization, you control prior experience with course material. -By providing both groups a textbook, you control for information provided.

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.

You could potentially make the claim, depending on the degree of correlation, that there is a causative relationship between the IV and the DV. This is because the study performed was experimental, non-stochastic and eliminated the potential for a confounder. This means that it would be internally valid to claim that attending lectures caused the students to do better on the test.

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.

No, you could not that this finding would also apply in courses with online lectures. Although internally valid, the findings of the experimental study are not necessarily generalizable as online lectures would likely cause the treatment group to perform in a different manner on the final test.

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)

-The average level of education required for each profession (Legal/Medical Professions). -A stable, nuclear family in childhood (financial security while growing up). -Area of home (impacts level of educational opportunity and income). -Work ethic (Willingness to pursue risk).


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.RData")

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?

range(presdata$year)
## [1] 1948 2012
max(presdata$year) - min(presdata$year)
## [1] 64
dim(presdata)
## [1] 17  7

Based upon the dimension of the data, which is 17x7, the total number of individual observations 119. The data set covers years between 1948 and 2012 making the range total 64 years (though not continuous).

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?

var(presdata$rdi4)
## [1] 3.103718
max(presdata$rdi4)
## [1] 6.03529
min(presdata$rdi4)
## [1] -0.59695

The average change in real disposable income across all points is 3.103717%.This is a fairly large average as 3% of an individuals total income can constitute a signifigant dollar amount. The maximum change in real disposable income is 6.03529%. The minimum change in real disposable income is 0.05506%.

Q4. Calculate the average vote share across all points in the sample. What does this tell you about the power of incumbency?

mean(presdata$vote)
## [1] 52.04586

The average vote share across all points in the sample is 52.04586. This means that, on average, an incumbent will win more than half of the popular vote (at 52.04586% of the vote), which typically indicates a victory in the election. This means that incumbents have a better chance at winning the presidential election after having previously served a term 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(presdata$rdi4, presdata$vote, main="% Change in Income v. % Votes for Incumbent", xlab = "% Change in Income", ylab = "% Vote for Incumbent President")

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.

newdata <- presdata[order(presdata$rdi4, presdata$vote),]
plot(newdata$rdi4, newdata$vote, main="% Change in Income v. % Votes for Incumbent", xlab = "% Change in Income", ylab = "% Vote for Incumbent President")
lines(newdata$rdi4, newdata$vote, type="l")

Q7a. Make the plot again, but this time add a trend line (also known as a line of best fit or a regression line).

model1 <- lm(presdata$vote ~ presdata$rdi4, data=presdata)
plot(presdata$rdi4, presdata$vote, main="% Change in Income v. % Votes for Incumbent", xlab = "% Change in Income", ylab = "% Vote for Incumbent President")
abline(model1 ,col="red")

summary(model1)
## 
## Call:
## lm(formula = presdata$vote ~ presdata$rdi4, data = presdata)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.6842 -3.7406 -0.2731  2.6357  7.5002 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    45.9385     1.6919  27.152 3.62e-14 ***
## presdata$rdi4   2.2906     0.5342   4.288 0.000648 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.765 on 15 degrees of freedom
## Multiple R-squared:  0.5507, Adjusted R-squared:  0.5207 
## F-statistic: 18.38 on 1 and 15 DF,  p-value: 0.0006477

Q7b. What does this line tell you about elections and the economy?

This regression tells us that there is a strong, positive correlation between change in income and percentage vote for an incumbent president.

Q7c. What could you call this relationship?

You can call this relationship a strong, positive correlative relationship.