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(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
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 Canvas. Submit both the .rmd file, and the HTML file it creates. This assignment is due by 11:55am next Wednesday (October 15). Late problem sets will receive a penalty for each day of delay. Please list any students you collaborated with.
Please disclose whether you employed ChatGPT to complete this assignment, and how you used it in the space at the end of the problem set.
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
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. Theory. Short answer questions (1 point each; 5 points total)
Start by reading Chapter 1 of the textbook, Real Stats. You can also review your lecture notes. You may NOT use generative AI to answer these questions.
Q1. A researcher observes that countries with democratic systems have weaker climate policies, compared to countries with non-democratic systems. She decides to publish a research article that says having this form of government causes countries to become less climate friendly. Would you like to be a co-author on this paper? Why or why not? (100 words max)
ANSWER: I would not co-author this paper, as the author has failed to find the direct causation for democratic systems having weaker climate policies. She has failed to account for other potential confounders in her analysis, for example democratic countries being home to big corporations with massive factories which are necessary for their production. This reflects a common theme of the course that correlation does not equal causation.
Q2. Explain what this sentence means: “Experiments create exogeneity via randomization.” (75 words max)
ANSWER: This sentence means that when experimenting it is ideal to randomize the sample, because it creates exogenity, which reflects a good experiment. It ensures that the only thing that differs within the control groups is the treatment itself rather than any confounding factors.
Q3. What do we refer when we talk about the “internal validity” and “external validity” of experiments? (100 words max)
ANSWER: Internal and External validity refer to how well an experiment is designed and how generalizable it is. Internal validity means that the X variable is causing the Y variable based on the sample you are testing and not due to other confounders. A good experiment has to be Internally valid. External validity, however, means the experiment is valid in other contexts. For example, in Brazil, babies who can walk by one years old can jump really high, but that does not mean that is true for babies in India. This would be internally valid, but not externally valid.
Q4. You decide to run an experiment to see whether working in groups helps students learn. You randomly assign half of the class to form study groups to work on their problem sets, and to visit office hours every week. For the other half of the class, you assign them to work individually. 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 worked individually. (120 words max)
4a. What could you call each group?
ANSWER: The group that worked in groups and went to office hours would be the treated group, while the group that worked independantly would be the control group.
4b. What is your independent variable and what is your dependent variable?
ANSWER: Independent variable is the students who work in groups. Dependent variable is their grades.
4c. Given this set up, list some factors you are controlling for.
ANSWER: Students who already show prior academic ability, motivated students, and student participation.
4d. Can you say that working in groups caused the students to do better on the test? Why or why not? Explain using the technical terms in the textbook.
ANSWER: Not directly, because the mandatory office hours (which does not apply to group work) can be causing the grade increase in the treated group (not completely internally valid).
4e. Can you say that this finding would also apply in other types of courses (for example, writing-intensive classes)? Why or why not? Explain using the technical terms in the textbook.
ANSWER: Not necessarily, this experiment did not gain any external validity because it was tested for student in a Poli Sci 15 course. For a more writing intensive class, it may be better for students to work by themselves.
Q5. Imagine you are looking at whether people with higher levels of education have higher incomes. List some of the factors that could lead to endogeneity. (50 words max)
ANSWER: Some factors that can lead to endogeneity are family wealth, innate ability and social networks. People who come from money are more likely to have better resources and networks, while also having the money to afford school, making it harder to analyze if education directly causes higher income.
Part 2. Data Analysis (1/2 point each; 5 points total)
The improvement in human rights in the second half of the 20th century is one of the most relevant global development trends. What is the relationship between economic development and human rights?
We will use a dataset from “Our World in Data (OWID)” (Saloni Dattani and Fiona Spooner and Hannah Ritchie and Max Roser, 2023) to explore this question.
Download the dataset, humanrights.RData, which you’ll
find on the course website and also on the online RStudio platform. 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:
GDP per capitaAs 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 humanrights.RData, the result will be
available in the data owid. To get at the variable
country, for example, you would use
owid$country. Remember, the end of each chapter in the
textbook includes R code that can be helpful. We also posted R resources
on Canvas.
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("humanrights.RData")
Q2. Check the dimensions of the data (i.e. the number of rows and columns). How many observations are there? How many variables are there?
nrow(owid)
## [1] 160
ncol(owid)
## [1] 3
ANSWER: There are 160 rows and 3 columns. This means there are 160 observations and 3 variables.
Q3. How many countries are covered in this data set?
list(owid$country)
## [[1]]
## [1] "Afghanistan" "Albania"
## [3] "Algeria" "Angola"
## [5] "Argentina" "Armenia"
## [7] "Australia" "Austria"
## [9] "Azerbaijan" "Bahrain"
## [11] "Bangladesh" "Barbados"
## [13] "Belarus" "Belgium"
## [15] "Benin" "Bolivia"
## [17] "Bosnia and Herzegovina" "Botswana"
## [19] "Brazil" "Bulgaria"
## [21] "Burkina Faso" "Burundi"
## [23] "Cambodia" "Cameroon"
## [25] "Canada" "Cape Verde"
## [27] "Central African Republic" "Chad"
## [29] "Chile" "China"
## [31] "Colombia" "Comoros"
## [33] "Congo" "Costa Rica"
## [35] "Cote d'Ivoire" "Croatia"
## [37] "Cuba" "Cyprus"
## [39] "Czechia" "Democratic Republic of Congo"
## [41] "Denmark" "Djibouti"
## [43] "Dominican Republic" "Ecuador"
## [45] "Egypt" "El Salvador"
## [47] "Equatorial Guinea" "Estonia"
## [49] "Eswatini" "Ethiopia"
## [51] "Finland" "France"
## [53] "Gabon" "Gambia"
## [55] "Georgia" "Germany"
## [57] "Ghana" "Greece"
## [59] "Guatemala" "Guinea"
## [61] "Guinea-Bissau" "Haiti"
## [63] "Honduras" "Hong Kong"
## [65] "Hungary" "Iceland"
## [67] "India" "Indonesia"
## [69] "Iran" "Iraq"
## [71] "Ireland" "Israel"
## [73] "Italy" "Jamaica"
## [75] "Japan" "Jordan"
## [77] "Kazakhstan" "Kenya"
## [79] "Kuwait" "Kyrgyzstan"
## [81] "Laos" "Latvia"
## [83] "Lebanon" "Lesotho"
## [85] "Liberia" "Libya"
## [87] "Lithuania" "Luxembourg"
## [89] "Madagascar" "Malawi"
## [91] "Malaysia" "Mali"
## [93] "Malta" "Mauritania"
## [95] "Mauritius" "Mexico"
## [97] "Moldova" "Mongolia"
## [99] "Montenegro" "Morocco"
## [101] "Mozambique" "Myanmar"
## [103] "Namibia" "Nepal"
## [105] "Netherlands" "New Zealand"
## [107] "Nicaragua" "Niger"
## [109] "Nigeria" "North Korea"
## [111] "North Macedonia" "Norway"
## [113] "Oman" "Pakistan"
## [115] "Panama" "Paraguay"
## [117] "Peru" "Philippines"
## [119] "Poland" "Portugal"
## [121] "Romania" "Russia"
## [123] "Rwanda" "Sao Tome and Principe"
## [125] "Saudi Arabia" "Senegal"
## [127] "Serbia" "Seychelles"
## [129] "Sierra Leone" "Singapore"
## [131] "Slovakia" "Slovenia"
## [133] "South Africa" "South Korea"
## [135] "Spain" "Sri Lanka"
## [137] "Sweden" "Switzerland"
## [139] "Syria" "Taiwan"
## [141] "Tajikistan" "Tanzania"
## [143] "Thailand" "Togo"
## [145] "Trinidad and Tobago" "Tunisia"
## [147] "Turkey" "Turkmenistan"
## [149] "Uganda" "Ukraine"
## [151] "United Arab Emirates" "United Kingdom"
## [153] "United States" "Uruguay"
## [155] "Uzbekistan" "Venezuela"
## [157] "Vietnam" "Yemen"
## [159] "Zambia" "Zimbabwe"
ANSWER: 160
Q4. Calculate the average of gdppc, which is average
income per capita for each country, across all points in the sample. Do
you think this average is large or small? What is the minimum and the
maximum gdppc? Make sure to explain what each of these
values are communicating.
mean(owid$gdppc)
## [1] 19112.69
max(owid$gdppc)
## [1] 88366.22
min(owid$gdppc)
## [1] 596.393
ANSWER: The average income per capita for each country is relatively small. The average (19112.69 dollars) is much closer to the min(596.39 dollars) rather than the max(88366.33). This highlights the difference between the wealthiest of countries compared to the average of others.
Q5. Calculate the average of the human rights index across all points in the sample. What does this tell you about the prevalence of human rights protections? Make sure to explain what this value is communicating.
mean(owid$humanrights)
## [1] 0.6737188
ANSWER: According to the Human Rights Index, human rights are measured on a scale of 0-1, and with the average being at a .6737188, this value is communicating that is under the ideal amount.
Q6. Produce a simple scatterplot with average income (GDP per capita) on the horizontal axis and the human rights protections (Human Rights Index) on the vertical axis.
plot(owid$gdppc, owid$humanrights,
ylab = "humanrights",
xlab = "gdppc",
main = "Human Rights Protections Based on Income")
model <- lm(owid$humanrights ~ owid$gdppc,
data = owid)
Q7. Make the plot again, but this time add a trend line (also known
as a line of best fit or a regression line) using the
abline() command.
plot(owid$gdppc, owid$humanrights,
ylab = "humanrights",
xlab = "gdppc",
main = "Human Rights Protections Based on Income")
abline(model, col = "red")
Q8. What does this line tell you about development and human rights?
ANSWER: This line highlights that there is a positive correlation between the development of a country (gdppc) and the amount of human rights. Given the information we have, we can not definitively say that their is a causal relationship between these two but they are definitely related.
Q9. What could you call this relationship? Why?
ANSWER: This is a positive relationship because when the X variable rises so does the Y variable. We cannot definitively call this relationship causal as there may be other factors at play.
Q10. Did you collaborate with anyone on this problem set? If so, list them here.
ANSWER: Elias Bernard
Q11. Did you use generative AI on any part of this problem set? If so, identify which model you used and how you used it – be specific!
ANSWER: No