#### Introduction

The change in temperature over the past 150 years is used by climate scientists as evidence to show that humans might have an increasingly devastating effect on the Earth’s environment. Advances in computer modeling are allowing for climatologists to tease out both the causes and impacts of increasing Earth temperatures. In this recent Star Tribune article, researchers ran computer simulations that compared natural weather fluctuations for today’s CO2 and greenhouse gas levels to pre-Industrial Revolution levels.

Although thinking about climate change can sometimes be daunting, it is an issue that is increasingly important to understand culturally and scientifically. You have probably seen many graphs showing the temperature over x number of years, but have you ever had the chance to examine the data yourself? In this activity, you are provided with a data set of monthly average global temperatures worldwide from January 1881 to December 2012. The goals of this project are to allow you to work with meaningful climate change data so as to draw your own conclusions and become better acquainted with R vectors and some useful functions. Learning these skills to analyze the change in temperature over the past 131 years are extremely useful, and temperature is just the “tip of the iceberg” when it comes to computer modeling of weather-related data.

Deliverables

Your team will turn in two files. One file will be this input document with the .rmd extension. Another will be a copy of your output file with the .html extension.

Data

Run this chunk to load the temperature data into R:

r load(url('http://www.stolaf.edu/people/olaf/cs125/project1.RData'))

When you do so, you will have a vector called tData available in your R session. The data originally came from http://data.giss.nasa.gov/gistemp/tabledata_v3/GLB.Ts+dSST.txt. That file has a brief description of the data at the bottom of the page. Each element in the vector is the scaled deviation from a baseline mean global temperature in Celsius. The baseline mean global temperature is the global temperature from 1951-1980, which is 14.0 C (57.2 Fahrenheit).

The first element of the vector corresponds to the average temperature in January of 1881 (note that this is the second year in the original data file). The second element is February 1881, etc. The last element is December 2012.

Before doing any kind of analysis, you should first get to know your data, so let’s make some plots:

hist(tData)

plot(1:length(tData),tData,xlab="Month (begins in 1881)",ylab="Scaled deviation in temperature",main="Global Temperatures over Time")

mean(tData)
## [1] -1.549874

From the ‘histogram’ above you can see the distribution of the ‘scaled temperature deviations’. Most of the scaled deviations fall between -50 and 50, but the highest and lowest values are around -100 and 100. From the ‘scatterplot’ we can look at the relationship between month and global temperature. We can also take the mean of tData to find the mean of these deviations, -1.5498737.

Exercises

Please complete the following exercises, using R vectors (it is not necessary to use R matrices):

Exercise 1

Convert the data from scaled change in Celsius to absolute temperature in Fahrenheit. What is the mean temperature in Fahrenheit over the entire data set? Confirm that your result is reasonable.

f<-((tData/100)*1.8)+57.2
mean(f)
## [1] 57.1721

Use your new Fahrenheit vector for the rest of the exercises.

Exercise 2

Find the mean average temperature of each month, as well as the lowest and highest average temperatures for each month. This is easiest if you create new vectors that have all January temperatures, all February temperatures, and so on. When did the lowest and highest occur for each month? Are the highest temperatures generally more recent than the lowest temperatures?

mat<-matrix(f,ncol=12,byrow=TRUE)
colnames(mat)<-c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
rownames(mat)<-c(1881:2012)
Jan<-f[seq(from=1, to=1584, by=12)]
Feb<-f[seq(from=2,to=1584,by=12)]
Mar<-f[seq(from=3,to=1584,by=12)]
Apr<-f[seq(from=4,to=1584,by=12)]
May<-f[seq(from=5,to=1584,by=12)]
Jun<-f[seq(from=6,to=1584,by=12)]
Jul<-f[seq(from=7,to=1584,by=12)]
Aug<-f[seq(from=8,to=1584,by=12)]
Sep<-f[seq(from=9,to=1584,by=12)]
Oct<-f[seq(from=10,to=1584,by=12)]
Nov<-f[seq(from=11,to=1584,by=12)]
Dec<-f[seq(from=12,to=1584,by=12)]

mean(Jan)
## [1] 57.15814
mean(Feb)
## [1] 57.18268
mean(Mar)
## [1] 57.20409
mean(Apr)
## [1] 57.16618
mean(May)
## [1] 57.14805
mean(Jun)
## [1] 57.12732
mean(Jul)
## [1] 57.17845
mean(Aug)
## [1] 57.17245
mean(Sep)
## [1] 57.185
mean(Oct)
## [1] 57.20436
mean(Nov)
## [1] 57.191
mean(Dec)
## [1] 57.1475
maxmin<-function(x) c(1880+which.max(x), max(x), 1880+which.min(x), min(x))

maxmin(Jan)
## [1] 2007.000   58.874 1893.000   55.688
maxmin(Feb)
## [1] 1998.000   58.748 1905.000   56.120
maxmin(Mar)
## [1] 2002.000   58.802 1911.000   56.066
maxmin(Apr)
## [1] 2010.000   58.658 1909.000   56.192
maxmin(May)
## [1] 2010.000   58.460 1917.000   56.156
maxmin(Jun)
## [1] 1998.000   58.514 1909.000   56.282
maxmin(Jul)
## [1] 2011.000   58.442 1912.000   56.354
maxmin(Aug)
## [1] 2011.000   58.442 1912.000   56.138
maxmin(Sep)
## [1] 2005.00   58.46 1912.00   56.21
maxmin(Oct)
## [1] 2005.000   58.550 1912.000   56.066
maxmin(Nov)
## [1] 2010.00   58.55 1910.00   56.21
maxmin(Dec)
## [1] 2006.000   58.532 1916.000   55.868
##Higher temperature are all after 1996

Exercise 3

For the rest of the project, it will be helpful to define two functions. Define one function that takes the month and year as input and outputs the index in the vector for that given month and year. Define another function that takes the index in the vector as the input and outputs the month and year.

outindex <- function(x,y) {
  if(y=="Jan"){
    z<-0
  } else {
    if(y=="Feb"){
      z<-1
    } else{
      if(y=="Mar"){
        z<-2
      } else{
        if(y=="Apr"){
          z<-3
        } else{
          if(y=="May"){
            z<-4
          } else{
            if(y=="Jun"){
              z<-5
            } else{
              if(y=="Jul"){
                z<-6
              } else{
                if(y=="Aug"){
                  z<-7
                } else{
                  if(y=="Sep"){
                    z<-8
                  } else{
                    if(y=="Oct"){
                      z<-9
                    } else{
                      if(y=="Nov"){
                        z<-10
                      } else{
                        if(y=="Dec"){
                          z<-11
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
return((1+12*(x-1881))+z)
}

  
outmoyear<- function(x){
  if(x%%12==1){
    mo<-"Jan"
  } else{
    if(x%%12==2){
      mo<-"Feb"
    } else{
      if(x%%12==3){
        mo<-"Mar"
      } else{
        if(x%%12==4){
          mo<-"Apr"
        } else{
          if(x%%12==5){
            mo<-"May"
          } else{
            if(x%%12==6){
              mo<-"Jun"
            } else{
              if(x%%12==7){
                mo<-"Jul"
              } else{
                if(x%%12==8){
                  mo<-"Aug"
                } else{
                  if(x%%12==9){
                    mo<-"Sep"
                  } else{
                    if(x%%12==10){
                      mo<-"Oct"
                    } else{
                      if(x%%12==11){
                        mo<-"Nov"
                      } else{
                        if(x%%12==0){
                          mo<-"Dec"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  yr<-(as.integer((x/12)-.05)+1881)
return (c(mo, yr))
} 

Exercise 4

Create plots of the monthly average temperature over time for each month: one plot for all January average temperatures, one plot for all February average temperatures, etc. The plots should have Years on the horizontal (x) axis, and Temperature on the vertical (y) axis. The previously defined functions will be helpful here. Your code should plot all months (separately).

pyt<-function(x,y) plot((1+1880):(length(x)+1880),x,xlab="Year",ylab="Temperature (F)", main=y)
pyt(Jan,"Jan")

pyt(Feb,"Feb")

pyt(Mar,"Mar")

pyt(Apr,"Apr")

pyt(May,"May")

pyt(Jun,"Jun")

pyt(Jul,"Jul")

pyt(Aug,"Aug")

pyt(Sep,"Sep")

pyt(Oct,"Oct")

pyt(Nov,"Nov")

pyt(Dec,"Dec")

Does there seem to be a warming trend in your graphs? Type your answer below: Yes, warms over the years

Exercise 5

Create a plot that shows the average annual temperature over all years. (That is, create one plot where each data point corresponds to the average temperature for a year.) (HINT: This is easiest if you create a new vector with elements representing average temperature).

mean(split(f, ceiling(seq_along(f)/12)))
## Warning in mean.default(split(f, ceiling(seq_along(f)/12))): argument is
## not numeric or logical: returning NA
## [1] NA
test<-split(f, ceiling(seq_along(f)/12))
yravg<-sapply(test, mean)


plot((1+1880):(length(yravg)+1880),yravg,xlab="Year",ylab="Average Annual Temperature", main="Average Annual Temperature By Year")

which.max(yravg)
## 130 
## 130
which.min(yravg)
## 29 
## 29

In what years did the highest and lowest average temperatures occur? 1909 for lowest, 2010 for highest

Exercise 6

Create a plot showing the monthly average temperature starting at January 2000 (one plot showing all data points from January 2000 through December 2012). The previously defined functions will be helpful here.

plot((1:156),(f[(outindex(2000,"Jan"):outindex(2012,"Dec"))]),xlab="Month Number, Starting Jan 2000, to Dec 2012", ylab="Average Temperature (F)", main="Monthly Average Tempearture Jan 2000 - Dec 2012")

Do you see any pattern to the data? Are temperatures rising? No obvious pattern

Exercise 7

Plot the annual average temperature from 2000-2012.

plot((120+1880):(length(yravg)+1880),yravg[120:length(yravg)],xlab="Year",ylab="Average Annual Temperature", main="Average Annual Temperature By Year")

mean(yravg[120:length(yravg)])
## [1] 58.21423

Is it easier to see a warming trend? What is the average temperature of these 13 years? Harder to see the warming trend. 58.21423 F

Exercise 8

Create a plot of the monthly average temperatures starting at January 1990 and ending on December 1999.

plot((1:120),(f[(outindex(1990,"Jan"):outindex(1999,"Dec"))]),xlab="Month Number, Starting Jan 1990, to Dec 1999", ylab="Average Temperature (F)", main="Monthly Average Tempearture Jan 1990 - Dec 1999")

which.min(f[(outindex(1990,"Jan"):outindex(1999,"Dec"))])
## [1] 33

Do you see any pattern to the data? Are temperatures rising? Which months had the highest and lowest temperatures? Highest: February 1997 Lowest: September 1992 Very slight decrease then increase in temperatures

Exercise 9

Plot the annual average temperature of every year from 1990 to 1999.

plot((111+1880):(120+1880),yravg[111:120],xlab="Year",ylab="Average Annual Temperature", main="Average Annual Temperature By Year")

mean(yravg[111:120])
## [1] 57.8507

Is it easier to see a warming trend? What is the average temperature of the 1990s? Slightly easier Average temp: 57.8507

Exercise 10

Plot the monthly average temperature of every month in the 1890s.

plot((1:120),(f[(outindex(1890,"Jan"):outindex(1899,"Dec"))]),xlab="Month Number, Starting Jan 1890, to Dec 1899", ylab="Average Temperature (F)", main="Monthly Average Tempearture Jan 1890 - Dec 1899")

mean(f[(outindex(1890,"Jan"):outindex(1899,"Dec"))])
## [1] 56.70485
mean(f[(outindex(1990,"Jan"):outindex(1999,"Dec"))])
## [1] 57.84935

What is the mean temperature of this decade? How does this decade compare to the 1990s? 56.70485 vs 57.84935 in the 1990s (about 1 degree less)

Exercise 10

Create plots of the monthly averages of the first 44 years, the second 44 years, and the last 43 years. Remember to use the previously defined functions!

plot((1:528),(f[(outindex(1881,"Jan"):outindex(1924,"Dec"))]),xlab="Month Number, Starting Jan 1881, to Dec 1924", ylab="Average Temperature (F)", main="Monthly Average Tempearture Jan 1881 - Dec 1924")

plot((1:528),(f[(outindex(1925,"Jan"):outindex(1968,"Dec"))]),xlab="Month Number, Starting Jan 1925, to Dec 1968", ylab="Average Temperature (F)", main="Monthly Average Tempearture Jan 1925 - Dec 1968")

plot((1:528),(f[(outindex(1969,"Jan"):outindex(2012,"Dec"))]),xlab="Month Number, Starting Jan 1969, to Dec 2012", ylab="Average Temperature (F)", main="Monthly Average Tempearture Jan 1969 - Dec 2012")

mean(f[(outindex(1881,"Jan"):outindex(1924,"Dec"))])
## [1] 56.67892
mean(f[(outindex(1925,"Jan"):outindex(1968,"Dec"))])
## [1] 57.09272
mean(f[(outindex(1969,"Jan"):outindex(2012,"Dec"))])
## [1] 57.74467

Do you see any warming trends in any of these plots? What is the average temperature of each of these time periods?

No big changes in first and middle 44, warming trend in last 44 First 44: 56.67892 Second 44: 57.09272 Last 44: 57.74467