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 Canvas. Submit both the .rmd file, and the HTML file it creates. This assignment is due by 11:59 PM on Wednesday January 24, 2024. Late problem sets will receive a penalty for each day of delay. 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] "/home/jovyan/pset 1 "
setwd("/home/jovyan/pset 1 ")
What command getwd does in the code is that it allows you to see and display the current working directory and return it to its original form.
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.
Q1. A researcher observes that countries with parliamentary systems have higher levels of income. She decides to publish a research article that says having this form of government causes countries to become richer. Would you like to be a co-author on this paper? Why or why not? (100 words max)
I wouldn’t like to be considered a co-author on this paper because although the researcher makes a good hypothesis question that can be tested via quasi-experimental research, her paper has no statistical backing which is based on observation alone which is a logical fallacy, “correlation doesn’t equal causation.”
Q2. Explain what this sentence means: “Experiments create exogeneity via randomization.” (75 words max)
since exogeneity is the independence of explanatory variables from the error term in the model, it’s due to this phenomenon that randomization in experiments helps to ensure that the assignment of subjects to different treatment groups is independent of any other factors. Creating exogeneity and reducing the risk for potential bias or confounding variables to influence the result.
Q3. What do we refer when we talk about the “internal validity” and “external validity” of experiments? (100 words max)
when we refer to the internal validity of an experiment we’re looking at the experiment itself to find out if it is free from confounders, bias, and other problems whereas external validity is looking to see if the finding is generalizable in the grander scheme such as populations, situations, and other case studies.
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. 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?
the groups that were allowed to work within the study groups on the problem sets can be the “treated” group whereas the ones that didn’t can be the “controlled” group.
4b. What is your independent variable and what is your dependent variable?
the Independent variable in the experiment is the “grouping method” used for the treated group whereas the dependent variable is the problem set performance of the whole student population in the study.
4c. Given this set up, list some factors you are controlling for.
some of the factors in the experiment that I would be controlling for if I were to be leading the case would be assuring all of the problem sets in the experiment were evenly difficult, making sure that each group of students in the study received the exact same teaching materials and resources, and making sure both groups have the same deadlines for the assignment.
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.
While the study design suggests a relationship between group work and improved test scores the actual results of the experiment can’t be confirmed without considering potential confounding variables. Factors such as group dynamics, individual student abilities, motivation, and school breaks could have influenced the results. However, the improvement observed could possibly be linked to the interaction between the students in the group.
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.
The finding that students in study groups performed better may not necessarily apply to other types of courses because the effectiveness of group work can be influenced by the nature of the subject matter, the specific learning objectives, and the individual skills of the students. Factors such as the need for creativity, originality, and individual expression in a writing-intensive class may make the dynamics of group work less effective in such contexts which illustrates the importance of considering the context-specificity of research findings in educational settings.
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)
some of the factors that can lead to endogeneity can be issues with reverse causation where higher income influences the ability to pursue further education and omitted variable bias such as unmeasured traits affecting both income and education level.
Part 2. Data Analysis (1/2 point each; 5 points total)
The decrease in infant mortality in the second half of the 20th century is one of the most relevant global development trends. What is the relationship between income and health outcomes?
We will use a dataset from “Our World in Data” (Saloni Dattani and Fiona Spooner and Hannah Ritchie and Max Roser, 2023) to explore this question.
Download the dataset, hdi_health.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 hdi_health.RData, the result will be
available in the data hdi_health. To get at the variable
Entity, for example, you would use
hdi_health$Entity. 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("hdi_health.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?
summary(hdi_health)
## Entity avg_hdi avg_infant_mortality
## Length:175 Min. :0.3097 Min. : 0.5179
## Class :character 1st Qu.:0.5192 1st Qu.: 2.2448
## Mode :character Median :0.6967 Median : 4.4149
## Mean :0.6644 Mean : 5.0629
## 3rd Qu.:0.7935 3rd Qu.: 7.6625
## Max. :0.9214 Max. :14.1199
dim.data.frame(hdi_health)
## [1] 175 3
Q3. How many countries and territories are covered in this data set?
about 175 countries/territories are covered in this data set over three columns.
Q4. Calculate the average of avg_hdi, which is mean
Human Development Index 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 avg_hdi?
summary(hdi_health)
## Entity avg_hdi avg_infant_mortality
## Length:175 Min. :0.3097 Min. : 0.5179
## Class :character 1st Qu.:0.5192 1st Qu.: 2.2448
## Mode :character Median :0.6967 Median : 4.4149
## Mean :0.6644 Mean : 5.0629
## 3rd Qu.:0.7935 3rd Qu.: 7.6625
## Max. :0.9214 Max. :14.1199
Q5. Calculate the average infant mortality rate across all points in the sample. What does this tell you about the prevalence of infant mortality?
mean(hdi_health$avg_infant_mortality)
## [1] 5.062867
what an average of 5.06 suggests that in the countries being analyzed thre is an overwhelming mount of infant mortality in a select few nations suggesting intervention is needed.
Q6. Produce a simple scatterplot with Human Development Index on the horizontal axis and the average Infant Mortality Rate on the vertical axis.
plot(hdi_health$avg_infant_mortality, hdi_health$hdi,
ylab = "infant mortality rate",
xlab = "human development index",
main = "hdi_health")
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(hdi_health$avg_infant_mortality, hdi_health$hdi,
ylab = "infant mortality rate",
xlab = "human development index",
main = "hdi_health")
model <- lm(hdi_health$avg_infant_mortality ~ hdi_health$avg_hdi,
data = hdi_health)
abline(model, col = "red")
Q8. What does this line tell you about development and health outcomes?
what this line on the graph tells us is that decreased infant mortality is strongly correlated with improved human development in most nations around the world.
Q9. What could you call this relationship?
this kind of relationship can be seen as a “positive correlation/inverse relationship” between both sets of data which suggests that better quality of life means less infants will die as a result.