Advertisement
Guest User

R script - linear interp w/ 15 minute rolling average

a guest
Feb 27th, 2012
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.89 KB | None | 0 0
  1. #linear.r takes raw data time series index associated with some variable 'y' and uses 1 minute interpolation and 15 minute sliding average to format data. User can comment/uncomment code blocks to disable/enable features respectively. Raw data must be located on desktop as cooling.txt, IT.txt, lighting.txt, or losses.txt
  2.  
  3. #load packages zoo and chron
  4.    require(zoo)
  5.    require(chron)
  6.  
  7. #set working directory
  8.    setwd("/home/knavero/Desktop")
  9.  
  10. #read in data text file to create zoo series object
  11.    rawCool = read.zoo("cooling.txt", FUN = as.chron, format = "%m/%d/%Y %H:%M", sep = "\t", aggregate = function(x) tail(x, 1))
  12.  
  13.    #rawIT = read.zoo("IT.txt", FUN = as.chron, format = "%m/%d/%Y %H:%M", sep = "\t", aggregate = function(x) tail(x, 1))
  14.  
  15.    #rawLight = read.zoo("lighting.txt", FUN = as.chron, format = "%m/%d/%Y %H:%M", sep = "\t", aggregate = function(x) tail(x, 1))
  16.  
  17.    #rawLoss = read.zoo("losses.txt", FUN = as.chron, format = "%m/%d/%Y %H:%M", sep = "\t", aggregate = function(x) tail(x, 1))
  18.  
  19. #graphical parameters for page layout (nr, nc)
  20.    #par(mfrow = c(4, 1))
  21.  
  22. #raw data plots
  23.    #par(mar = c(2, 4, 1, 1))
  24.    #plot(rawCool, ylim = c(0, 2500), type = "o", xlab = "", ylab = "cooling", cex.lab = 1)
  25.    #grid(col = "darkgrey")
  26.  
  27.    #par(mar = c(2, 4, 1, 1))
  28.    #plot(rawIT, ylim = c(0, 500), type = "o", xlab = "", ylab = "IT", cex.lab = 1)
  29.    #grid(col = "darkgrey")
  30.  
  31.    #par(mar = c(2, 4, 1, 1))
  32.    #plot(rawLight, ylim = c(0, 11), type = "o", xlab = "", ylab = "lighting", cex.lab = 1)
  33.    #grid(col = "darkgrey")
  34.  
  35.    #par(mar = c(2, 4, 1, 1))
  36.    #plot(rawLoss, ylim = c(0, 90), type = "o", xlab = "", ylab = "losses", cex.lab = 1)
  37.    #grid(col = "darkgrey")
  38.  
  39. #1 minute increment linear interpolation
  40.    oneMin = seq(start(rawCool), end(rawCool), by = times("00:01:00"))
  41.    intCool = na.approx(rawCool, xout = oneMin)
  42.  
  43.    #oneMin = seq(start(rawIT), end(rawIT), by = times("00:01:00"))
  44.    #intIT = na.approx(rawIT, xout = oneMin)
  45.  
  46.    #oneMin = seq(start(rawLight), end(rawLight), by = times("00:01:00"))
  47.    #intLight = na.approx(rawLight, xout = oneMin)
  48.  
  49.    #oneMin = seq(start(rawLoss), end(rawLoss), by = times("00:01:00"))
  50.    #intLoss = na.approx(rawLoss, xout = oneMin)
  51.  
  52.    #combine = merge(intCool, intIT, intLight, intLoss)
  53.    #intSum = zoo(rowSums(combine), time(combine))
  54.  
  55. #1 minute increment linear interpolation plots
  56.    #par(mar = c(2, 4, 1, 1))
  57.    #plot(intCool, ylim = c(0, 2500), type ="o", xlab = "", ylab ="cooling int.", cex.lab = 1)
  58.    #grid(col ="darkgrey")
  59.  
  60.    #par(mar = c(2, 4, 1, 1))
  61.    #plot(intIT, ylim = c(0, 500), type ="o", xlab = "", ylab ="IT int.", cex.lab = 1)
  62.    #grid(col ="darkgrey")
  63.  
  64.    #par(mar = c(2, 4, 1, 1))
  65.    #plot(intLight, ylim = c(0, 11), type ="o", xlab = "", ylab ="lighting int.", cex.lab = 1)
  66.    #grid(col ="darkgrey")
  67.  
  68.    #par(mar = c(2, 4, 1, 1))
  69.    #plot(intLoss, ylim = c(0, 90), type ="o", xlab = "", ylab ="losses int.", cex.lab = 1)
  70.    #grid(col ="darkgrey")
  71.  
  72.    #par(mar = c(2, 4, 1, 1))
  73.    #plot(intSum, ylim = c(0, 3000), type ="o", xlab = "", ylab ="sum int.", cex.lab = 1)
  74.    #grid(col ="darkgrey")
  75.  
  76. #write linear interpolated data to file
  77.    #write.zoo(intCool, file = "intOutputCooling.txt")
  78.    #write.zoo(intIT, file = "intOutputIT.txt")
  79.    #write.zoo(intLight, file = "intOutputLighting.txt")
  80.    #write.zoo(intLoss, file = "intOutputLosses.txt")
  81.    #write.zoo(intSum, file = "intOutputSum.txt")
  82.  
  83. #15 minute rolling average/mean
  84.    avgCool = aggregate(intCool, trunc(time(intCool), times("00:15:00")), mean)
  85.    #avgIT = aggregate(intIT, trunc(time(intIT), times("00:15:00")), mean)
  86.    #avgLight = aggregate(intLight, trunc(time(intLight), times("00:15:00")), mean)
  87.    #avgLoss = aggregate(intLoss, trunc(time(intLoss), times("00:15:00")), mean)
  88.    #avgSum = aggregate(intSum, trunc(time(intSum), times("00:15:00")), mean)
  89.  
  90. #15 minute rolling average/mean plot
  91.    #par(mar = c(2, 4, 1, 1))
  92.    #plot(avgCool, ylim =c(0, 2500), type ="o", xlab = "", ylab ="avg cooling", cex.lab =1)
  93.    #grid(col ="darkgrey")
  94.  
  95.    #par(mar = c(2, 4, 1, 1))
  96.    #plot(avgIT, ylim =c(0, 500), type ="o", xlab = "", ylab ="avg IT", cex.lab =1)
  97.    #grid(col ="darkgrey")
  98.  
  99.    #par(mar = c(2, 4, 1, 1))
  100.    #plot(avgLight, ylim =c(0, 11), type ="o", xlab = "", ylab ="avg lighting", cex.lab =1)
  101.    #grid(col ="darkgrey")
  102.  
  103.    #par(mar = c(2, 4, 1, 1))
  104.    #plot(avgLoss, ylim =c(0, 90), type ="o", xlab = "", ylab ="avg losses", cex.lab =1)
  105.    #grid(col ="darkgrey")
  106.  
  107.    #par(mar = c(2, 4, 1, 1))
  108.    #plot(avgSum, ylim =c(0, 3000), type ="o", xlab = "", ylab ="avg sum", cex.lab =1)
  109.    #grid(col ="darkgrey")
  110.  
  111. #write linear interpolated, 15 minute sliding mean to file
  112.    #write.zoo(avgCool, file = "finalOutputCool.txt")
  113.    #write.zoo(avgIT, file = "finalOutputIT.txt")
  114.    #write.zoo(avgLight, file = "finalOutputLight.txt")
  115.    #write.zoo(avgLoss, file = "finalOutputLoss.txt")
  116.    #write.zoo(avgSum, file = "finalOutputSum.txt")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement