Lab 3 - Climate Change

Introduction

There have been many studies documenting that the average global temperature has been increasing over the last century. The consequences of a continued rise in global temperature will be dire. Rising sea levels and an increased frequency of extreme weather events will affect billions of people.

In this problem, we will attempt to study the relationship between average global temperature and several other factors. The file climate_change.csv contains climate data from May 1983 to December 2008.

The available variables include:

Problem 1.1 - Creating Our First Model

We are interested in how changes in these variables affect future temperatures, as well as how well these variables explain temperature changes so far. To do this, first read the dataset climate_change.csv into R.

DFClimate <- read.csv("./climate_change.csv")
cat('STR\n')
## STR
str(DFClimate)
## 'data.frame':    308 obs. of  11 variables:
##  $ Year    : int  1983 1983 1983 1983 1983 1983 1983 1983 1984 1984 ...
##  $ Month   : int  5 6 7 8 9 10 11 12 1 2 ...
##  $ MEI     : num  2.556 2.167 1.741 1.13 0.428 ...
##  $ CO2     : num  346 346 344 342 340 ...
##  $ CH4     : num  1639 1634 1633 1631 1648 ...
##  $ N2O     : num  304 304 304 304 304 ...
##  $ CFC.11  : num  191 192 193 194 194 ...
##  $ CFC.12  : num  350 352 354 356 357 ...
##  $ TSI     : num  1366 1366 1366 1366 1366 ...
##  $ Aerosols: num  0.0863 0.0794 0.0731 0.0673 0.0619 0.0569 0.0524 0.0486 0.0451 0.0416 ...
##  $ Temp    : num  0.109 0.118 0.137 0.176 0.149 0.093 0.232 0.078 0.089 0.013 ...

Then, split the data into a training set, consisting of all the observations up to and including 2006, and a testing set consisting of the remaining years (hint: use subset). A training set refers to the data that will be used to build the model (this is the data we give to the lm() function), and a testing set refers to the data we will use to test our predictive ability.

DFClimate_train = subset(DFClimate, Year<=2006)
DFClimate_test = subset(DFClimate, Year>2006)
cat('Training set has',nrow(DFClimate_train),'elements.\nTest set has',nrow(DFClimate_test),'elements')
## Training set has 284 elements.
## Test set has 24 elements

Next, build a linear regression model to predict the dependent variable Temp, using MEI, CO2, CH4, N2O, CFC.11, CFC.12, TSI, and Aerosols as independent variables (Year and Month should NOT be used in the model). Use the training set to build the model.

Model = lm(Temp ~MEI + CO2 + CH4 + N2O + CFC.11 + CFC.12 + TSI + Aerosols, data=DFClimate_train)

summary(Model)
## 
## Call:
## lm(formula = Temp ~ MEI + CO2 + CH4 + N2O + CFC.11 + CFC.12 + 
##     TSI + Aerosols, data = DFClimate_train)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.25888 -0.05913 -0.00082  0.05649  0.32433 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1.246e+02  1.989e+01  -6.265 1.43e-09 ***
## MEI          6.421e-02  6.470e-03   9.923  < 2e-16 ***
## CO2          6.457e-03  2.285e-03   2.826  0.00505 ** 
## CH4          1.240e-04  5.158e-04   0.240  0.81015    
## N2O         -1.653e-02  8.565e-03  -1.930  0.05467 .  
## CFC.11      -6.631e-03  1.626e-03  -4.078 5.96e-05 ***
## CFC.12       3.808e-03  1.014e-03   3.757  0.00021 ***
## TSI          9.314e-02  1.475e-02   6.313 1.10e-09 ***
## Aerosols    -1.538e+00  2.133e-01  -7.210 5.41e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.09171 on 275 degrees of freedom
## Multiple R-squared:  0.7509, Adjusted R-squared:  0.7436 
## F-statistic: 103.6 on 8 and 275 DF,  p-value: < 2.2e-16
cat('\nModel Multiple R.square:',summary(Model)$r.square,'\n')
## 
## Model Multiple R.square: 0.7508933

Problem 1.2 - Creating Our First Model

Which variables are significant in the model? We will consider a variable significant only if the p-value is below 0.05. (Select all that apply.)

significant = summary(Model)$coeff[-1,4] < 0.05
list = names(significant[significant])
cat('Significant Variables:', list,"\n",sep=" ")
## Significant Variables: MEI CO2 CFC.11 CFC.12 TSI Aerosols

Problem 2.1 - Understanding the Model

Current scientific opinion is that nitrous oxide and CFC-11 are greenhouse gases: gases that are able to trap heat from the sun and contribute to the heating of the Earth.

However, the regression coefficients of both the N2O and CFC-11 variables are negative, indicating that increasing atmospheric concentrations of either of these two compounds is associated with lower global temperatures.

Which of the following is the simplest correct explanation for this contradiction?

Answer: Alternative C. All of the gas concentration variables reflect human development - N2O and CFC.11 are correlated with other variables in the data set.

Problem 2.2 - Understanding the Model

Compute the correlations between all the variables in the training set. Which of the following independent variables is N2O highly correlated with (absolute correlation greater than 0.7)? Select all that apply.

correlation=cor(DFClimate_train$N2O,DFClimate_train)>0.7
correlation
##      Year Month   MEI  CO2  CH4  N2O CFC.11 CFC.12   TSI Aerosols Temp
## [1,] TRUE FALSE FALSE TRUE TRUE TRUE  FALSE   TRUE FALSE    FALSE TRUE

Answer : Year, CO2, CH4, N2O, CFC.12, Aerosols

Which of the following independent variables is CFC.11 highly correlated with? Select all that apply.

correlation=cor(DFClimate_train$CFC.11,DFClimate_train)>0.7
correlation
##       Year Month   MEI   CO2  CH4   N2O CFC.11 CFC.12   TSI Aerosols  Temp
## [1,] FALSE FALSE FALSE FALSE TRUE FALSE   TRUE   TRUE FALSE    FALSE FALSE

Answer: CH4, CFC.11, CFC.12, Temp

Problem 3 - Simplifying the Model

Given that the correlations are so high, let us focus on the N2O variable and build a model with only MEI, TSI, Aerosols and N2O as independent variables. Remember to use the training set to build the model.

Enter the coefficient of N2O in this reduced model:

Model = lm(Temp ~MEI + N2O  + TSI + Aerosols, data=DFClimate_train)

summary(Model)
## 
## Call:
## lm(formula = Temp ~ MEI + N2O + TSI + Aerosols, data = DFClimate_train)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.27916 -0.05975 -0.00595  0.05672  0.34195 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1.162e+02  2.022e+01  -5.747 2.37e-08 ***
## MEI          6.419e-02  6.652e-03   9.649  < 2e-16 ***
## N2O          2.532e-02  1.311e-03  19.307  < 2e-16 ***
## TSI          7.949e-02  1.487e-02   5.344 1.89e-07 ***
## Aerosols    -1.702e+00  2.180e-01  -7.806 1.19e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.09547 on 279 degrees of freedom
## Multiple R-squared:  0.7261, Adjusted R-squared:  0.7222 
## F-statistic: 184.9 on 4 and 279 DF,  p-value: < 2.2e-16
cat('N2O Coefficient:',summary(Model)$coefficients[3],'\n')
## N2O Coefficient: 0.02531975

How does this compare to the coefficient in the previous model with all of the variables?

Answer : Previous coeficient was -1.653e-02. It changed sign and increased a bit in magnitude

Enter the model R2:

cat('Model Multiple R.square:',summary(Model)$r.square,'\n')
## Model Multiple R.square: 0.7261321

Problem 4 - Automatically Building the Model

We have many variables in this problem, and as we have seen above, dropping some from the model does not decrease model quality. R provides a function, step, that will automate the procedure of trying different combinations of variables to find a good compromise of model simplicity and R2. This trade-off is formalized by the Akaike information criterion (AIC) - it can be informally thought of as the quality of the model with a penalty for the number of variables in the model.

The step function has one argument - the name of the initial model. It returns a simplified model. Use the step function in R to derive a new model, with the full model as the initial model (HINT: If your initial full model was called “climateLM”, you could create a new model with the step function by typing step(climateLM). Be sure to save your new model to a variable name so that you can look at the summary. For more information about the step function, type ?step in your R console.)

Enter the R2 value of the model produced by the step function:

Model = lm(Temp ~MEI + CO2 + CH4 + N2O + CFC.11 + CFC.12 + TSI + Aerosols, data=DFClimate_train)

Model = step(Model)
## Start:  AIC=-1348.16
## Temp ~ MEI + CO2 + CH4 + N2O + CFC.11 + CFC.12 + TSI + Aerosols
## 
##            Df Sum of Sq    RSS     AIC
## - CH4       1   0.00049 2.3135 -1350.1
## <none>                  2.3130 -1348.2
## - N2O       1   0.03132 2.3443 -1346.3
## - CO2       1   0.06719 2.3802 -1342.0
## - CFC.12    1   0.11874 2.4318 -1335.9
## - CFC.11    1   0.13986 2.4529 -1333.5
## - TSI       1   0.33516 2.6482 -1311.7
## - Aerosols  1   0.43727 2.7503 -1301.0
## - MEI       1   0.82823 3.1412 -1263.2
## 
## Step:  AIC=-1350.1
## Temp ~ MEI + CO2 + N2O + CFC.11 + CFC.12 + TSI + Aerosols
## 
##            Df Sum of Sq    RSS     AIC
## <none>                  2.3135 -1350.1
## - N2O       1   0.03133 2.3448 -1348.3
## - CO2       1   0.06672 2.3802 -1344.0
## - CFC.12    1   0.13023 2.4437 -1336.5
## - CFC.11    1   0.13938 2.4529 -1335.5
## - TSI       1   0.33500 2.6485 -1313.7
## - Aerosols  1   0.43987 2.7534 -1302.7
## - MEI       1   0.83118 3.1447 -1264.9
cat('Model Multiple R.square:',summary(Model)$r.square,'\n')
## Model Multiple R.square: 0.7508409

Which of the following variable(s) were eliminated from the full model by the step function? Select all that apply.

Answer : Step function removed the variable CH4.

Problem 5 - Testing on Unseen Data

We have developed an understanding of how well we can fit a linear regression to the training data, but does the model quality hold when applied to unseen data?

Using the model produced from the step function, calculate temperature predictions for the testing data set, using the predict function.

Enter the testing set R2:

Climate_Prediction = predict(Model,newdata = DFClimate_test) 

SSE = sum((DFClimate_test$Temp-Climate_Prediction)^2)
SST = sum((DFClimate_test$Temp-mean(DFClimate_test$Temp))^2)

r.square = 1 - (SSE/SST)

cat('Step Model Multiple R.square:',r.square,'\n')
## Step Model Multiple R.square: 0.1864065