Advertisement
Guest User

linear.r

a guest
Mar 5th, 2012
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 5.02 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(3, 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. #calculate 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. #calculate 15 minute rolling average/mean
  84.    min15 = times("00:15:00")
  85.    offset = min15*14/15
  86.  
  87.    avgCool = suppressWarnings(aggregate(intCool, trunc(time(intCool) + offset, min15), mean))
  88.    avgIT = suppressWarnings(aggregate(intIT, trunc(time(intIT) + offset, min15), mean))
  89.    avgLight = suppressWarnings(aggregate(intLight, trunc(time(intLight) + offset, min15), mean))
  90.    avgLoss = suppressWarnings(aggregate(intLoss, trunc(time(intLoss) + offset, min15), mean))
  91.    avgSum = suppressWarnings(aggregate(intSum, trunc(time(intSum) + offset, min15), mean))
  92.  
  93. #15 minute rolling average/mean plot
  94.    #par(mar = c(2, 4, 1, 1))
  95.    #plot(avgCool, ylim =c(0, 2500), type ="o", xlab = "", ylab ="avg cooling", cex.lab =1)
  96.    #grid(col ="darkgrey")
  97.  
  98.    #par(mar = c(2, 4, 1, 1))
  99.    #plot(avgIT, ylim =c(0, 500), type ="o", xlab = "", ylab ="avg IT", cex.lab =1)
  100.    #grid(col ="darkgrey")
  101.  
  102.    #par(mar = c(2, 4, 1, 1))
  103.    #plot(avgLight, ylim =c(0, 11), type ="o", xlab = "", ylab ="avg lighting", cex.lab =1)
  104.    #grid(col ="darkgrey")
  105.  
  106.    #par(mar = c(2, 4, 1, 1))
  107.    #plot(avgLoss, ylim =c(0, 90), type ="o", xlab = "", ylab ="avg losses", cex.lab =1)
  108.    #grid(col ="darkgrey")
  109.  
  110.    #par(mar = c(2, 4, 1, 1))
  111.    #plot(avgSum, ylim =c(0, 3000), type ="o", xlab = "", ylab ="avg sum", cex.lab =1)
  112.    #grid(col ="darkgrey")
  113.  
  114. #write linear interpolated, 15 minute sliding mean to file
  115.    write.zoo(avgCool, file = "finalOutputCool.txt")
  116.    write.zoo(avgIT, file = "finalOutputIT.txt")
  117.    write.zoo(avgLight, file = "finalOutputLight.txt")
  118.    write.zoo(avgLoss, file = "finalOutputLoss.txt")
  119.    write.zoo(avgSum, file = "finalOutputSum.txt")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement