library(ggplot2)
library(Sleuth3)
knitr::opts_chunk$set(echo = TRUE)
temps <- case1502
First, let’s plot the temperatures
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.
Let’s now transform the data
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)
## Warning in res * res.lag: longer object length is not a multiple of shorter
## object length
c0 <- sum(res^2)
r1 <- c1/c0
r1
## [1] 0.5899396
Now:
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)
##
## Call:
## lm(formula = y_transf ~ t_transf + t2_transf)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.35762 -0.07713 -0.00133 0.07669 0.40419
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.10525 0.02723 -3.865 0.000162 ***
## t_transf -0.81661 0.42378 -1.927 0.055788 .
## t2_transf 2.04499 0.50115 4.081 7.13e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.1209 on 157 degrees of freedom
## (1 observation deleted due to missingness)
## Multiple R-squared: 0.3532, Adjusted R-squared: 0.3449
## F-statistic: 42.86 on 2 and 157 DF, p-value: 1.401e-15
It does seem like the rend is accelerating.
Let’s look at the plot:
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.