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(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.

Submission Instructions

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.

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] "/home/jovyan"

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: Although this conclusion at face value appears to make logical sense, a key aspect of statistical research is causation does not equal correlation. In order for the researcher to make a more compelling argument she would have to prove that her independent variable, democracy, is the reason for this change by providing clear evidence ruling out other confounding variables.

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

ANSWER: Exogeneity refers to the ideal standard in statistical research, where the independent variable is not correlated with the error term. When there is no/lower correlation between the independent variable (x) and the error term then it is more likely for X to be the cause of Y. Randomization can help because it ensures the application of the X is random and not linked with other confounders, thus making it easier to establish causal conclusions.

Q3. What do we refer when we talk about the “internal validity” and “external validity” of experiments? (100 words max)

ANSWER: Internal validity refers to how well the experiment is designed. For instance, does this experiment ensure that it is free from bias and other confounders. When conducting an experiment you want to ensure the X variable is causing the Y variable and not other factors. Alternatively, external validity focuses more on the application of such experiment. In order for a test to have external validity, the test should apply to other populations, situations, or cases sometimes outside of the context in which the finding was generated.

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 students attending office hours and studying in groups would be labeled the treated group, while the students working individually are the control group.

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

ANSWER: The independent variable would be students working in groups. The dependent variable would be the rest score.

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

ANSWER: Prior motivation, previous experience, past 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: No you cannot because the test introduces a new confounder. Since students working in groups must also attend office hours, we cannot definitively say it was the studying in groups and not the office hour meetings.

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: It does not necessarily have external validation because the findings would not automatically apply due to the potential variations such as assessment styles and different skills needed for writing assignments.

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 include prior generational wealth, social connections, and more opportunities. People who receive forms of higher education could come from wealthier families that can afford the cost of college. Such individuals tend to also have more networking connections and social status that can all influence income levels.


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:

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 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?

ANSWER: We have 160 observations and 3 variables

nrow(owid)
## [1] 160
ncol(owid)
## [1] 3

Q3. How many countries are covered in this data set?

ANSWER: 160 countries included 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"

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.

ANSWER: The average is relatively small in comparison to the maximum value, highlighting discrepancy between the wealthiest countries when compared to the average of other countries. The minimum is 596.393 dollars and the maximum is 88366.22 dollars. The average of gdppc is 19112.69 dollars.

mean(owid$gdppc)
## [1] 19112.69
max(owid$gdppc)
## [1] 88366.22
min(owid$gdppc)
## [1] 596.393

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.

ANSWER: The Human rights index measures the existence of human rights in a given country on a scale from 0-1, with 1 being the most human rights protections in a country and 0 being the least. Given that the average standard is well below the ideal level, it highlights that, on average, there is a clear underdevelopment of human rights in most countries

mean(owid$humanrights)
## [1] 0.6737188

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 based on GDPPC"
    
  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 based on GDPPC"
    
  model <- lm(owid$humanrights ~ owid$gdppc, data = owid)
  
  abline(model, col = "red")

Q8. What does this line tell you about development and human rights?

ANSWER: Although we cannot make causal claims since the data does not rule out any other confounders, it highlights that there is a positive correlation between development of a country (gdppc), and human rights.

Q9. What could you call this relationship? Why?

ANSWER: We would not label this relationship causal since there is no data proving the external influence of confounders. Instead we can say that there is a positive correlation between the two factors. This highlights that there is a correlation in which when X rises so does Y.

Q10. Did you collaborate with anyone on this problem set? If so, list them here.

ANSWER: Jai Grewal

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 I did not