Submit your HTML output and .Rmd file to Canvas by the deadline.

Introduction: Reminders about R and Rmarkdown

It is best to work will small amounts of code at a time: get some code working, copy it into the rmd as a code chunk, write your text answer (outside the code chunk) if needed, and check that the file will still knit properly. Do not proceed to answer more questions until you get the first bit working. If you knit everytime you try to write some new code, you’ll know where the error is (in the last thing you did!) This will save you huge headaches.

Although the questions break up each task for you into parts, remember that you might need to put a bunch of code together into a single chunk to make it work. For example, if you create a density plot in one part of a question, and want to add the mean value to it as a line in another part, you need these two commands to follow one another in the same chunk of code.

Some tips: Start early, work with friends in the class, use the discussion forum, watch section videos, go to office hours, read the textbook – do all these things and you’ll succeed! Good luck.


Question 1. Income and Carbon Emissions (50 points total)

For this problem set, we will further explore a relationship we discussed in lecture: income and carbon pollution. To do so, we will use data from “Our World in Data”

1.1 Set your working directory and load the data.

getwd()
## [1] "/home/jovyan/Section 1"
setwd("/home/jovyan/Section 1")
load("co2_gdppc.RData")

We will deal with two variables: co2percapita and gdppc. The co2percapita measures the emissions of carbon dioxide per person from fossil fuels and industry, measured in tons of carbon. The variable gdppc measures of the GDP per capita of each country-year in 1000 US Dollars to facilitate cross-country comparisons.

1.2 Produce a scatter plot with gdppc on the horizontal access and co2percapita on the vertical axis and use the abline command to put the linear regression line on the plot. Given how you have set up your plot, which is your independent variable and which is your dependent variable? 5 points

df1 <- co2_gdppc
model <- lm(df1$gdppc ~ df1$co2percapita,
            data = df1)
plot(df1$co2percapita, df1$gdppc, 
     xlab = "gdppc",
     ylab = "co2percapita", 
     main = "Relation")

abline(model, col="red") 

dependent variable: co2percapita

independent variable: gdppc

Bonus question: explain why the plot above might not be informative to describe the relationship between these two variables. Up to 10 additional points

This might not be informative, as correlation does not capture all meaningful relationships between X and Y, just linear regressions leaving other type of relationships without being taken into account.

1.3 Estimate and report both the covariance and the correlation of gdppc and co2percapita. Write the meaning of what these results tell you, using the definitions for these two variables stated above. 5 points.

cov(df1$gdppc, df1$co2percapita)
## [1] 77.305
cor(df1$gdppc, df1$co2percapita)
## [1] 0.5588464

0,56 of correlation, shows that the slope is positive and the magnitude states the type of scatter plot that should appear

1.4 The model below describes a linearregression of co2percapita on gdppc. Explain what each of the terms below mean using the terms we studied in lecture. 5 points.

\[co2percapita_i = \beta_{0} + \beta_1 gdppc_i + \epsilon_i\] \[co2percapita_i\]

is the dependent variable: measures the emissions of carbon dioxide per person from fossil fuels and industry, measured in tons of carbon

\[\beta_{0}\]

is the “intercept”: the value of Y(co2percapita) we will guess at X(gdppc) = 0, when there is no gdp.\[\beta_{1}\]

Is the “slope”: a one-unit change in X (gdppc) is associated with a change of the slope in Y(co2percapita)

\[gdppc_i\]is the independen, t variable or predictor or covariate

\[\epsilon_i\]

is the residual, distance between the line and any given point

You do not have to estimate the model yet so this is not in a code chunk.

1.5 Explain how we will estimate the best values of \(\beta_0\) and \(\beta_1\). In what sense is the line that we choose (by choosing \(\beta_0\) and \(\beta_1\)) the “best-fitting” line? 5 points.

The best way to estimate this relationship is to choose our slope and intercepts for X (β0 and β1) to minimize the value or in other words the sum of squared errors (SSE). The SSE takes those residuals or errors (the distance of any point with the linear regression) and squares them, and adds them up. The minimum SSE would be the best fitting.

1.6 Now use linear regression to regress co2percapita on gdppc using the lm() function in R - make sure you save the model as an object. Show the result using the summary() command. Interpret the meaning of the coefficient estimates (both the intercept and the coefficients on gdppc). 15 points.

y <- df1$co2percapita
x <- df1$gdppc
model1 <- lm(y ~ x)
summary(model1)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -17.92  -1.45  -0.98   0.41 365.27 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.758149   0.140617   5.392 7.33e-08 ***
## x           0.301741   0.006607  45.671  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 7.168 on 4593 degrees of freedom
## Multiple R-squared:  0.3123, Adjusted R-squared:  0.3122 
## F-statistic:  2086 on 1 and 4593 DF,  p-value: < 2.2e-16

βˆ0: When gdppc is zero, we expect Y to equal 0,758

βˆ1: For each one-unit change in gdppc, we expect a 0,302 unit change in co2percapita.

Bonus question: Consider the p-values reported on the table in your interpretation, if you want to read ahead and figure out what these mean. Up to 10 additional points.

A p-value of < 2.2e-16 (a very low p-value) in this case indicates that there is an actual relationship between the independent and dependent variables. In other words, the independent variable has an impact on the dependent variable, having a robust relatinoship that is not possibly due to random chances.

1.7 Sometimes we need to transform a variable to make it more suitable to analysis by regression. For example, with income-related variables like gdppc, we usually need to take their log first before using regression. Create two new variables that are equal to: (1) the log of gdppc and (2) the log of co2percapita. 5 points

logx <- log(x)
logy <- log(y)

1.8 Now remake a scatter plot like before but with the log of gdppc on the horizontal access and the log of co2percapita on the vertical axis. Add the regression line using abline(). 5 points

model2 <- lm(logy ~ logx,
            data = df1)
plot(logy, logx, 
     xlab = "loggdppc",
     ylab = "logco2percapita", 
     main = "Relation")

abline(model2, col="red") 

1.9 Regardless of what you actually got in the above analyses, suppose that we find a positive and statistically significant coefficient in these regressions. Can we causally say that “being wealthier (higher GDP per capita) leads to having more carbon pollution per capita?” based on the data that we analyzed? Why or why not? Follow the instructions in class for how to address such causal questions, including pointing out potential confounders, non-comparability, and proposing the ideal research design. 5 points


Question 2. Climate change (20 points total)

Begin by loading a new dataset (climate_change_year.Rdata) into R. This dataset shows the average sea and surface temperature anomaly for the entire world for every year since 1851.

load("climate_change_year.RData")

2.1 Make a scatterplot of temperature over time. Add a trend line. What does it tell us about how the climate is changing over time? 10 points

df2 <- climate_change_year
model3 <- lm(df2$mean_anomaly ~ df2$year,
            data = df2)
plot(df2$year, df2$mean_anomaly, 
     xlab = "year",
     ylab = "temperature", 
     main = "Relation")

abline(model3, col="red")

2.2 Next, subset the data into groups by decade. Start with the seventies, and then create subsets for the eighties, nineties, noughts, teeens, and twenties (up to 2022). What is the mean temperature for each decade? What is the trend over time in the mean? 10 points

subset_1970s <- df2$year[1970:1979]

Question 3. Theoretical Section, 30 points

3.1 How correlation is different from covariance. Provide a formula that can turn \(cov(x,y)\) into \(cor(x,y)\). Explain how correlation relates to covariance. Also explain what the correlation means and the possible values it can take. 10 points

Taking into account the formula of the correlation, we can see that it would be the covariance of both variables divided into the standard deviation of both. Meaning that if you divide the covariance with the standard deviation of each of the variables you get the correlation. The correlation resolves the problem of the covariance, whose scale is not very natural, making the scale go between -1 and 1being those perfect negative and positive relations and if the answer was 0 they would be two perfectly unrelated variables. This way the nearer the correlation is to 1 the more perfect their relationship is going to be and the nearer to 0 they would be more unrelated.

3.2 What is a random variable? In your own words, provide a definition. What are its key components? 10 points

A random variable would be a variable which numeric outcome comes by chance, for example when you roll dice.

3.3 Explain succinctly what the regression is doing to estimate a relationship between two variables. Add a specific political science example. 10 points

What regression does is to mathematize the data into allowing us to predict how changes in a variable might affect other variables. We could see this in different political science scenarios, for example we could see how gdp per capita can affect the educational level of the population, and this could help develop different policies for a better access of education in areas where the gdp per capita is low.