**From the bottom of the heap » R**, and kindly contributed to R-bloggers)

Yesterday (June 11, 2011) the BBC reported on comments by Prof. Phil Jones, of the Climatic Research Unit (CRU), University of East Anglia (UEA), that the warming experienced by the planet since 1995 was statistically significant. That the trend in the recent data was now significant when last year it was not, was attributed to the fact that one more data point (year) was now available for the analysis and Jones highlighted the utility of long-term monitoring data as more data is collected in detecting pattern in noisy data.

In this post I wanted to take a closer look at these data and illustrate how we can use R to fit regression models to time series data.

We will look at the HadCRUT3v data set, using the global means version of the data. I don’t know specifically that Jones was commenting on these particular data as I have been unable to identify a source for his comments other than the information in the BBC article, but it seems plausible that Jones was talking about these data. To load these data into R we use

URL <- url("http://www.cru.uea.ac.uk/cru/data/temperature/hadcrut3vgl.txt") gtemp <- read.table(URL, fill = TRUE)

The data need a little post processing to clean them up as the provided format also has, in every other row, the number of stations used to form each temperature data point. We won’t consider those data in this post, but as they provide information as to the likely uncertainty in each observation, we could use those data as case weights in the analysis. The data come as monthly means, with an annual mean column at the end. The data are anomalies from the 1961-1990 mean.

## Don't need the even rows --- perhaps do as case weights? gtemp <- gtemp[-seq(2, nrow(gtemp), by = 2), ] ## set the Year as rownames rownames(gtemp) <- gtemp[,1] ## Add colnames colnames(gtemp) <- c("Year", month.abb, "Annual") ## Data for 2011 incomplete so work only with 1850-2010 data series gtemp <- gtemp[-nrow(gtemp), ]

The first step in any analysis should be to look at the data. Here we produce a simple time series plot of the annual mean temperature using base graphics commands

ylab <- expression(Temperature~Anomaly~(1961-1990)~degree*C) plot(Annual ~ Year, data = gtemp, type = "o", ylab = ylab, main = "Global mean temperature anomaly 1850-2010")

to produce this figure:

The news report was about trends in temperature from 1995 onwards. We subset the data, selecting only the observations from 1995–2010 (and only variables `Year`

and `Annual`

) and plot those data

grecent <- subset(gtemp, subset = Year >= 1995, select = c(Year, Annual)) ## plot plot(Annual ~ Year, data = grecent, type = "o", ylab = ylab, main = "Global mean temperature anomaly 1995-2010")

In R, linear models fitted by least squares are fitted using the `lm()`

function. In the code chunk below, I fit a linear trend to the 1995–2010 data (`gm1`

) and a null model of no change (`gm0`

), and compare the two models with a F-ratio test

## fit a linear model through these recent data gm1 <- lm(Annual ~ Year, data = grecent) gm0 <- lm(Annual ~ 1, data = grecent) anova(gm0, gm1) coef(gm1)

The linear trend fits the data significantly better than the no-trend model. The rate of increase in temperatures over the period is ~0.01 (standard error = 0.005). As these are time series data and probably violate the independence assumption of the model, the standard error is potentially too small. As the data are regularly spaced in time, we can easily employ the autocorrelation function to investigate residuals correlations in the model errors. The `acf()`

function can be used for that, which produces a plot of the correlogram

acf(resid(gm1))

Although within the approximate 95% confidence intervals, there are some large correlations in the residuals, especially at lag 2. This is something we must try to account for in our model and see how that affects the estimates of the model coefficients and ultimately the significance or otherwise of the fitted trend. Other model diagnostics can be produced via the `plot()`

methods for `"lm"`

objects

layout(matrix(1:4, ncol = 2)) plot(gm1) layout(1)

but given the small sample size there is not much to be worried about here, though note the observations with large residuals and their leverage, which could indicate an undue influence on the model parameters.

We can account for the autocorrelation in the data whilst estimating the linear trend by switching to fitting the model using generalized least squares (GLS) via the `gls()`

function available in the **nlme** package. GLS allows a correlation structure for the model residuals to be estimated using a simple time series model such as an first-order auto-regressive process or AR(1). We need to decide what type of process to use for the correlation structure; the corellogram shown above suggests that AR terms, possibly up to order 2, might be appropriate. So we fit a series of models using `gls()`

that refits the null and linear trend models from before (just so we are confident we are comparing like with like), plus models using AR(1) and AR(2) processes for the residuals

require(nlme) gg0 <- gls(Annual ~ 1, data = recent, method = "ML") gg1 <- gls(Annual ~ Year, data = recent, method = "ML") gg2 <- gls(Annual ~ Year, data = grecent, correlation = corARMA(form = ~ Year, p = 1), method = "ML") gg3 <- gls(Annual ~ Year, data = grecent, correlation = corARMA(form = ~ Year, p = 2), method = "ML")

Correlation structures are specified using one of the `"corStruct"`

classes; here I used the `corARMA()`

function to fit an ARMA process, but will only use AR terms. If all you want to fit is an AR(1) process in the residuals, then `corAR1(form ~ Year)`

can be used. All the models were fitted using maximum likelihood estimation. The `anova()`

method can be used to compare the sequence of nested models. By nested, I mean that you can go from the most complex model (`gg3`

) to the simplest model (`gg0`

) by setting successive parameters to zero. The output from this process is shown below

R> anova(gg0, gg1, gg2, gg3) Model df AIC BIC logLik Test L.Ratio p-value gg0 1 2 -17.36029 -15.81511 10.68014 gg1 2 3 -24.00304 -21.68527 15.00152 1 vs 2 8.642752 0.0033 gg2 3 4 -26.49875 -23.40839 17.24937 2 vs 3 4.495708 0.0340 gg3 4 5 -30.77369 -26.91075 20.38685 3 vs 4 6.274945 0.0122

As we already saw, the linear trend model fits the data significantly (*p*-value = 0.0033) better than the null model of no trend. The models with AR(1) and AR(2) processes provide significantly better fits than the linear model, with the AR(2) model, `gg3`

, being a significant improvement over the AR(1) model (`gg2`

). A direct comparison between the linear trend model and the linear trend plus AR(2) model indicates the substantially better fit of the latter, despite requiring the estimation of two additional parameters

R> anova(gg1, gg3) Model df AIC BIC logLik Test L.Ratio p-value gg1 1 3 -24.00304 -21.68527 15.00152 gg3 2 5 -30.77369 -26.91075 20.38685 1 vs 2 10.77065 0.0046

The residuals from the best model (`gg3`

) have much-reduced auto-correlations

acf(resid(gg3, type = "normalized"))

Note that we need to use the normalized residuals to get residuals that take account of the fitted correlation structure. We are now in a position to assess the significance of the fitted trend within this model and investigate the slope of trend, which we can do using the `summary()`

method:

R> summary(gg3) Generalized least squares fit by maximum likelihood Model: Annual ~ Year Data: grecent AIC BIC logLik -30.77369 -26.91075 20.38685 Correlation Structure: ARMA(2,0) Formula: ~Year Parameter estimate(s): Phi1 Phi2 0.2412298 -0.6527874 Coefficients: Value Std.Error t-value p-value (Intercept) -16.163962 6.232562 -2.593470 0.0212 Year 0.008268 0.003112 2.656464 0.0188 Correlation: (Intr) Year -1 Standardized residuals: Min Q1 Med Q3 Max -2.29284316 -0.68980863 0.03087847 0.51005562 1.99216289 Residual standard error: 0.08715603 Degrees of freedom: 16 total; 14 residual

The linear trend is still significant at the 95% level, although the estimated rate of change in temperature is a little lower then in the least squares model (0.008°C year^{-1}; standard error = 0.003). The estimates of the AR(2) parameters are also shown (0.24 and -0.65). Approximate confidence intervals on the estimated parameters can be produced using the `intervals()`

function:

R> intervals(gg3) Approximate 95% confidence intervals Coefficients: lower est. upper (Intercept) -29.531478957 -16.163962275 -2.79644559 Year 0.001592535 0.008267935 0.01494333 attr(,"label") [1] "Coefficients:" Correlation structure: lower est. upper Phi1 -0.1675377 0.2412298 0.38958957 Phi2 -0.9036540 -0.6527874 -0.06838202 attr(,"label") [1] "Correlation structure:" Residual standard error: lower est. upper 0.05109686 0.08715603 0.14866224

At this point, it would be useful to visualise the fitted trend on the observed data. To do this, we need to predict values from the fitted model for the trend over the period of interest. The `predict()`

method is used to derive predictions for new observations. First we build a data frame containing the values of `Year`

we want to predict at (1995–2010), and then we add a `yhat`

variable to the data frame, which contains the predicted values. The predicted values are obtained using `predict(gg3, newdata = pred)`

. The `transform()`

function is used to add the `yhat`

component to our data frame `pred`

:

R> pred <- data.frame(Year = 1995:2010) R> pred <- transform(pred, yhat = predict(gg3, newdata = pred)) R> with(pred, yhat) [1] 0.3305672 0.3388351 0.3471030 0.3553710 0.3636389 0.3719069 0.3801748 [8] 0.3884427 0.3967107 0.4049786 0.4132465 0.4215145 0.4297824 0.4380503 [15] 0.4463183 0.4545862 attr(,"label") [1] "Predicted values"

A final step, having produced the predicted values is to plot the trend on the original data series, which we do using the code below, first with the full data series and then with the 1995–2010 subset

layout(matrix(1:2, ncol = 2)) ## plot full data plot(Annual ~ Year, data = gtemp, type = "o", ylab = ylab, main = "Global mean temperature anomaly 1850-2010") lines(yhat ~ Year, data = pred, lwd = 2, col = "red") ## plot the 1995-2010 close-up plot(Annual ~ Year, data = grecent, type = "o", ylab = ylab, main = "Global mean temperature anomaly 1995-2010") lines(yhat ~ Year, data = pred, lwd = 2, col = "red") layout(1)

to produce this figure

What does this tell us about underlying mean temperature of the recent few years? The linear trend doesn’t fit the data all that well; in fact, one could argue for a flat trend the latter half of the 1995–2010 period, and when viewed in the context of the data leading up to 1995, the fitted trend appears to substantially *underestimate* the increase in global mean temperature. A non-linear trend migth do a better job.

By focussing on only the very recent observational period, we have neglected to consider the constantly evolving mean temperature over the past 160 years. It has always seemed to me somewhat perverse that climatologists and climate sceptics alike ignored the decades of data before the recent period and then argued the toss about the sign and strength of a linear trend in the recent period, often claiming that there is insufficient data to ascribe a statistically significant pattern in defence of their particular point of view.

To me, it seems far more sensible to fit a model to the entire series of data, but to use a regression technique that can model local features of the series rather than the global trend over the entire data. That model could then be used to ask questions about whether temperatures have increased in a statistically significant manner for whatever period one liked. In a follow-up posting, I’ll demonstrate one such modelling technique that builds upon the concepts introduced here of using regression models with auto-correlation structures for the residuals.

**leave a comment**for the author, please follow the link and comment on their blog:

**From the bottom of the heap » R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...