---
title: "Global Warming Acceleration"
output: html_document
---

```{r setup, message=FALSE}
library(ggplot2)
library(Sleuth3)
knitr::opts_chunk$set(echo = TRUE)
```

## Global Warming -- the Data

```{r}
temps <- case1502
```

First, let's plot the temperatures

```{r}
ggplot(temps, aes(Year, Temperature)) + geom_line() + ylab("Deviation from mean temp")
```

Note that the 161-year average temperature has been substructed from the temperatures -- we are plotting the deviations from the mean.


## Global warming -- is there an acceleration?

Let's now transform the data

```{r}
y <- temps$Temperature
n <- length(y)
t <- (temps$Year-1850)/200 #linear transformation to make things more readable



res <- residuals(lm(y~t+I(t^2)))
res.lag <- c(NA, res[1:(n-2)])
c1 <- sum(res*res.lag, na.rm=T)
c0 <- sum(res^2)
r1 <- c1/c0

r1
```

Now:
```{r}
y.lag <- c(NA, y[1:(n-1)])
y_transf <- y.lag-0.6*y

t.lag <- c(NA, t[1:(n-1)])
t_transf <- t.lag-0.6*t

t2 <- t^2
t2.lag <- c(NA, t2[1:(n-1)])
t2_transf <- t2-0.6*t2.lag

fit <- lm(y_transf~t_transf+t2_transf)
summary(fit)
```


It does seem like the rend is accelerating.

Let's look at the plot:
```{r warning=F}
qplot(t_transf, y_transf)
```

As you can see, there is *still* a bump around 0.04, indicating that the transformation didn't really get rid of the correlation between the residuals.
