Guest User

Untitled

a guest
May 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. # Change-point analysis of HadCRUT3 annual time series data
  2.  
  3. require(strucchange)
  4.  
  5. # Data import
  6. dat.raw <- read.table("~/Downloads/annual")
  7. dat <- data.frame(temp=ts(dat.raw[,2], start=1850))
  8. dat <- window(cbind(temp=dat$temp, temp_lag1=lag(dat$temp, k = -1)), start=1851, end=2010)
  9.  
  10. # Structural change model
  11.  
  12. # Fit model
  13. # Minimal segment size set to 5 years; overfit will be handled later
  14. sc.alan <- breakpoints(temp ~ 1, h = 10, data=dat)
  15. sc <- breakpoints(temp ~ time(temp), h = 5, data=dat)
  16. sc.lag <- breakpoints(temp ~ time(temp) + temp_lag1 * time(temp), h = 5, data=dat)
  17. plot(summary(sc.alan))
  18. plot(summary(sc))
  19. plot(summary(sc.lag))
  20.  
  21. # Alan's model
  22. # By inspecting the BIC, we select a model with 7 breaks
  23. print(breakpoints(sc.alan, breaks=7))
  24. sc.alan <- lm(temp ~ breakfactor(sc.alan, breaks = 7), data=dat)
  25. plot(dat[,1], ylab="temperature", main="Structural change model: no lag, 7 breakpoints")
  26. lines(ts(fitted(sc.alan), start=1850), col=4)
  27. lines(confint(sc.alan))
  28.  
  29. # No red noise correction
  30. # By inspecting the BIC, we select a model with 4 breaks
  31. print(breakpoints(sc, breaks = 4))
  32. sc.4 <- lm(temp ~ time(temp) + breakfactor(sc, breaks = 4) * time(temp), data=dat)
  33. plot(dat[,1],ylab="temperature",main="Structural change model: no lag, 4 breakpoints")
  34. lines(ts(fitted(sc.4), start=1850), col=4)
  35. lines(confint(sc))
  36.  
  37. # Red noise correction
  38. # By inspecting the BIC, we select a model with 1 break
  39. print(breakpoints(sc.lag, breaks=1))
  40. sc.lag.1 <- lm(temp ~ time(temp) + breakfactor(sc.lag, breaks = 1) * time(temp), data=dat)
  41. plot(dat[,1],ylab="temperature",main="Structural change model: 1 year lag, 1 breakpoint")
  42. lines(ts(fitted(sc.lag.1), start=1851), col=4)
  43. lines(confint(sc.lag))
  44.  
  45. # Compare models
  46. print(anova(sc.alan, sc.4, sc.lag.1))
Add Comment
Please, Sign In to add comment