SHARE
TWEET

Untitled

user1205901 Nov 30th, 2015 (edited) 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # The code below compares three approaches to autocorrelated data
  2. # 1. Just take the mean of the raw data
  3. # 2. Adjust the raw data to remove autocorrelation
  4. # 3. Trim the raw data to take only every second data point
  5.  
  6. constant <- 2 # Set constant to whatever
  7.  
  8. # Make the values below divisible by 2 or it won't work
  9. max_iterations <- 5000
  10. data_set_size <- 100
  11.  
  12. rawmeanz <- vector(length=max_iterations)
  13. rawsdz <- vector(length=max_iterations)
  14. adjmeanz <- vector(length=max_iterations)
  15. adjsdz <- vector(length=max_iterations)
  16. trimmeanz <- vector(length=max_iterations/2)
  17. trimsdz <- vector(length=max_iterations/2)
  18.  
  19. for(i in 1:max_iterations)
  20. {
  21.   x <- filter(rnorm(data_set_size), filter=rep(1,3), circular=TRUE)+constant
  22.    
  23.   rawmeanz[i] <- mean(x)
  24.   rawsdz[i] <- sd(x)
  25.   trimmeanz[i] <- mean(x[seq(2, data_set_size, 2)])
  26.   trimsdz[i] <- sd(x[seq(2, data_set_size, 2)])
  27.  
  28.   model <- arima(x,order=c(1,0,0))
  29.  
  30.   adjres <- residuals(model) + coef(model)[2]
  31.  
  32.   # coef(model)[2] is the unconditional mean, although
  33.   # inexplicably the output says 'intercept'.
  34.   # See http://www.stat.pitt.edu/stoffer/tsa3/Rissues.htm Issue 1
  35.   # for a partial explanation of how this happened.
  36.  
  37.   adjmeanz[i] <- mean(adjres)
  38.   adjsdz[i] <- sd(adjres)
  39.  
  40. }
  41.  
  42. sprintf("Mean error of raw data is %f",mean(abs(constant-rawmeanz)))
  43. sprintf("Mean error of adjusted data is %f",mean(abs(constant-adjmeanz)))
  44. sprintf("Mean error of trimmed data is %f",mean(abs(constant-trimmeanz)))
  45.  
  46. sprintf("Mean SD of raw data is %f",mean(rawsdz))
  47. sprintf("Mean SD of adjusted data is %f",mean(adjsdz))
  48. sprintf("Mean SD of trimmed data is %f",mean(trimsdz))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top