Guest User

Untitled

a guest
Jun 21st, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.16 KB | None | 0 0
  1. #IOT Energy Analytics - Review of Submetering
  2. install.packages("dplyr")
  3. library(dplyr)
  4. install.packages(c("tidyr", "devtools"))
  5. library(tidyr)
  6. library(devtools)
  7.  
  8.  
  9. #Data Install
  10. setwd("~/Desktop/IOT Analytics")
  11. library(readr)
  12. household_power_consumption <- read_csv("household_power_consumption.txt")
  13. View(household_power_consumption)
  14. attributes(household_power_consumption)
  15. housevs2 <- read.table("household_power_consumption.txt", header = TRUE, sep = ";", fill = TRUE)
  16. energy_iot <- housevs2
  17. View(energy_iot)
  18. summary(energy_iot)
  19. str(energy_iot)
  20.  
  21. #One column for Date and Time Stamp
  22. energy_combined <-cbind(energy_iot,paste(energy_iot$Date,energy_iot$Time), stringsAsFactors=FALSE)
  23. colnames(energy_combined)[10] <-"DateTime"
  24. View(energy_combined)
  25. energy_combined <- energy_combined[,c(ncol(energy_combined), 1:(ncol(energy_combined)-1))]
  26. head(energy_combined)
  27. View(energy_combined)
  28.  
  29. #Convert Date and Time
  30. energy_combined$DateTime <- strptime(energy_combined$DateTime, "%d/%m/%Y %H:%M:%S")
  31. energy_combined$Date <- as.Date(energy_combined$Date, "%d/%m/%Y")
  32. str(energy_combined)
  33. summary(energy_combined)
  34.  
  35. #Attributes redefined
  36.  
  37. energy_combined$Sub_metering_3 <- as.factor(energy_combined$Sub_metering_3)
  38. str(energy_combined)
  39.  
  40. #Group Data and transform Posix _ dec
  41. library(dplyr)
  42.  
  43. energy_combined$Date <- as.POSIXct(energy_combined$Date)
  44. energy_combined$DateTime <- as.POSIXct(energy_combined$DateTime)
  45.  
  46. #Descriptive Stats
  47. summary(energy_combined)
  48.  
  49. #Feature Engineering
  50. energy_combined$Global_active_power<-NULL
  51. energy_combined$Global_reactive_power<-NULL
  52. energy_combined$Voltage<-NULL
  53. energy_combined$Global_intensity<- NULL
  54.  
  55.  
  56. #Df Quarter1 2007
  57. install.packages("lubridate")
  58. library(lubridate)
  59. dim(energy_combined)
  60. str(energy_combined)
  61.  
  62. #Subset Dataframes 2007 - Submeter 1 - March
  63. DF2007_march <- subset(energy_combined, Date == "2007-03-01"| Date =="2007-03-03"| Date=="2007-03-04"| Date=="2007-03-05"|Date=="2007-03-06"|Date=="2007-03-07"|Date=="2007-03-08"|Date=="2007-03-09"|Date=="2007-03-10")
  64. View(DF2007_march)
  65.  
  66. #Subset Dataframes 2007 - Submeter 2 - August
  67. DF2007_august <- subset(energy_combined, Date == "2007-08-01"| Date =="2007-08-03"| Date=="2007-08-04"| Date=="2007-08-05"|Date=="2007-08-06"|Date=="2007-08-07"|Date=="2007-08-08"|Date=="2007-08-09"|Date=="2007-08-10")
  68. View(DF2007_august)
  69.  
  70. #Subset Dataframes 2007 - Submeter 3 - December
  71. DF2007_dec <- subset(energy_combined, Date == "2007-12-01"| Date =="2007-12-03"| Date=="2007-12-04"| Date=="2007-12-05"|Date=="2007-12-06"|Date=="2007-12-07"|Date=="2007-12-08"|Date=="2007-12-09"|Date=="2007-12-10")
  72. View(DF2007_dec)
  73.  
  74. #Subset Dataframes 2008 - Submeter 1 - march
  75. DF2008_march <- subset(energy_combined, Date == "2008-03-01"| Date =="2008-03-03"| Date=="2008-03-04"| Date=="2008-03-05"|Date=="2008-03-06"|Date=="2008-03-07"|Date=="2008-03-08"|Date=="2008-03-09"|Date=="2008-03-10")
  76. View(DF2008_aug)
  77.  
  78. #Subset Dataframes 2008 - Submeter 2 - august
  79. DF2008_august <- subset(energy_combined, Date == "2008-08-01"| Date =="2008-08-03"| Date=="2008-08-04"| Date=="2008-08-05"|Date=="2008-08-06"|Date=="2008-08-07"|Date=="2008-08-08"|Date=="2008-08-09"|Date=="2008-08-10")
  80. View(DF2008_august)
  81.  
  82. #Subset Dataframes 2008 - Submeter 3 - december
  83. DF2008_dec <- subset(energy_combined, Date == "2008-12-01"| Date =="2008-12-03"| Date=="2008-12-04"| Date=="2008-12-05"|Date=="2008-12-06"|Date=="2008-12-07"|Date=="2008-12-08"|Date=="2008-12-09"|Date=="2008-12-10")
  84. View(DF2008_dec)
  85.  
  86. #Subset Dataframes 2009 - Submeter 1 -march
  87. DF2009_march <- subset(energy_combined, Date == "2009-03-01"| Date =="2009-03-03"| Date=="2009-03-04"| Date=="2009-03-05"|Date=="2009-03-06"|Date=="2009-03-07"|Date=="2009-03-08"|Date=="2009-03-09"|Date=="2009-03-10")
  88. View(DF2009_march)
  89.  
  90. #Subset Dataframes 2009 - Submeter 2 - august
  91. DF2009_august <- subset(energy_combined, Date == "2009-08-01"| Date =="2009-08-03"| Date=="2009-08-04"| Date=="2009-08-05"|Date=="2009-08-06"|Date=="2009-08-07"|Date=="2009-08-08"|Date=="2009-08-09"|Date=="2009-08-10")
  92. View(DF2009_august)
  93.  
  94.  
  95. #Subset Dataframes 2009 - Submeter 3 - december
  96. DF2009_dec <- subset(energy_combined, Date == "2009-12-01"| Date =="2009-12-03"| Date=="2009-12-04"| Date=="2009-12-05"|Date=="2009-12-06"|Date=="2009-12-07"|Date=="2009-12-08"|Date=="2009-12-09"|Date=="2009-12-10")
  97. View(DF2009_dec)
  98.  
  99. #Subset Dataframes 2010 - Submeter 1 - march
  100. DF2010_march <- subset(energy_combined, Date == "2010-03-01"| Date =="2010-03-03"| Date=="2010-03-04"| Date=="2010-03-05"|Date=="2010-03-06"|Date=="2010-03-07"|Date=="2010-03-08"|Date=="2010-03-09"|Date=="2010-03-10")
  101. View(DF2010_march)
  102.  
  103. #Subset Dataframes 2010 - Submeter 2 -august
  104. DF2010_august <- subset(energy_combined, Date == "2010-08-01"| Date =="2010-08-03"| Date=="2010-08-04"| Date=="2010-08-05"|Date=="2010-08-06"|Date=="2010-08-07"|Date=="2010-08-08"|Date=="2010-08-09"|Date=="2010-08-10")
  105. View(DF2010_august)
  106.  
  107. #Subset Dataframes 2010 - Submeter 3 -november
  108. DF2010_nov <- subset(energy_combined, Date == "2010-11-01"| Date =="2010-11-03"| Date=="2010-11-04"| Date=="2010-11-05"|Date=="2010-11-06"|Date=="2010-11-07"|Date=="2010-11-08"|Date=="2010-11-09"|Date=="2010-11-10")
  109. View(DF2010_nov)
  110.  
  111. #Remove variable noise for august
  112. DF2007_august$Sub_metering_2 <- NULL
  113. DF2007_august$Sub_metering_3 <- NULL
  114. DF2007_august$DateTime <- NULL
  115. DF2007_august$Global_intensity <- NULL
  116. DF2007_august$Global_active_power <- NULL
  117. DF2007_august$Global_reactive_power <- NULL
  118. DF2007_august$Date <- NULL
  119. DF2007_august$Time<- NULL
  120. View(DF2007_august)
  121.  
  122. str(DF2007_august)
  123. glimpse(DF2007_august)
  124.  
  125.  
  126. library("")
  127. ?tslm
  128. ?forecast
  129.  
  130.  
  131. #forecast for august 2007_ten days in august_submeter1
  132. Fore2007 <- ts(DF2007_august)
  133. plot.ts(Fore2007)
  134. plot(Fore2007)
  135. plot.forecast(Fore2007)
  136. View (Fore2007)
  137. summary(Fore2007)
  138. Augustfore_2007 <- forecast()
  139. Augustfore_2007
  140. augustoutput <- as.data.frame(Augustfore_2007)
  141. write_excel_csv(augustoutput, "August2007.csv")
  142.  
  143. #forecast focusing on Submeter 1 for August 2007
  144.  
  145. DF2007_august$Sub_metering_2 <- NULL
  146. DF2007_august$Sub_metering_3 <- NULL
  147. View(Fore2007)
  148. plot(Fore2007)
  149. Aug2007_sub1 <- forecast(Fore2007, level = c(80,95), lambda = NULL)
  150. Aug2007_sub1
  151. Augsub1 <- as.data.frame(Aug2007_sub1)
  152. write_excel_csv(Augsub1, "augsub1.csv")
  153.  
  154. #Voltage August 2007
  155. plot(Augustfore_2007)
  156.  
  157.  
  158. #Forecast time series #2 (Dec_2008_winter)
  159. DF2008_dec$DateTime<-NULL
  160. DF2008_dec$Date<-NULL
  161. Fore2008 <- ts(DF2008_dec)
  162. is.na(Fore2008)
  163. plot(Fore2008)
  164. View(Fore2008)
  165. Decfore_2008 <- forecast(Fore2008, level = c(80,95), lambda = NULL)
  166. Decfore_2008
  167. plot(Decfore_2008)
  168. decoutput <- as.data.frame(Decfore_2008)
  169. write_excel_csv(decoutput, "Dec2008.csv")
  170.  
  171. #Dec 2008 focus on submeter 2
  172. DF2008_dec$Sub_metering_3<- NULL
  173. DF2008_dec$Sub_metering_1 <- NULL
  174. dec2008_sub2 <- forecast(Fore2008, level = c(80,95), lambda = NULL)
  175. plot(dec2008_sub2)
  176. dec2008_sub2
  177. decsub2<-as.data.frame(dec2008_sub2)
  178. write_excel_csv(decsub2,"dec2008sub2.csv")
  179.  
  180. #Voltage dec 2008 sub 2
  181. DF2008_dec$Global_active_power <-NULL
  182. DF2008_dec$Global_reactive_power <- NULL
  183. DF2008_dec$Global_intensity <- NULL
  184. DF2008_dec$Time<- NULL
  185. Fore2008 <- ts(DF2008_dec)
  186. plot(Fore2008)
  187.  
  188.  
  189. #Forecast time series #3 (March 2009_spring)
  190. DF2009_march$DateTime <- NULL
  191. DF2009_march$Date<-NULL
  192. DF2009_march$Time<-NULL
  193. Fore2009 <- ts(DF2009_march)
  194. View(Fore2009)
  195. plot(Fore2009)
  196.  
  197.  
  198. Marchfore_2009 <- forecast(Fore2009, level = c(80,95), lambda = NULL)
  199. Marchfore_2009
  200. marchustoutput <- as.data.frame(Marchfore_2009)
  201. write_excel_csv(marchustoutput, "March 2009.csv")
  202.  
  203. #March 2009 focus on submeter 3
  204. DF2009_march$Sub_metering_1<-NULL
  205. DF2009_march$Sub_metering_2<-NULL
  206. MAR2009_sub3 <- forecast(Fore2009, level = c(80,95), lambda = NULL)
  207. plot(MAR2009_sub3)
  208. dec2008_sub2
  209. marsub3<-as.data.frame(MAR2009_sub3)
  210. write_excel_csv(marsub3,"mar2009sub3.csv")
  211.  
  212. #Voltage Macrch 2009 sub3
  213.  
  214. DF2009_march$Global_active_power <-NULL
  215. DF2009_march$Global_reactive_power <- NULL
  216. DF2009_march$Global_intensity <- NULL
  217.  
  218. Fore2009 <- ts(DF2009_march)
  219. plot(Fore2009)
  220.  
  221.  
  222.  
  223.  
  224. #Decomposing Seasonal Trends for Energy data _ August 2007
  225. library("dplyr")
  226.  
  227. householdtimeseries_2007_activepower<- ts(energy_combined$Global_active_power, frequency = 12, start = c(2007,1), end = c(2007,12))
  228. householdtimeseries_2007
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238. plot.ts(householdtimeseries_2007)
  239. season2007<-decompose(householdtimeseries_2007_activepower, "multiplicative")
  240. season2007
  241. plot(as.ts(season2007$seasonal))
  242. plot(as.ts(season2007$random))
  243. plot(as.ts(season2007$trend))
  244. str(season2007)
  245. summary(season2007)
  246. season2007_df <- data.frame(season2007 = c(season2007), time = c(time(season2007)))
  247. write_excel_csv(season2007_df,"season2007.csv")
  248.  
  249.  
  250. #Decomposing Seasonal Trends for Energy data _ Dec 2008
  251.  
  252. library("dplyr")
  253. householdtimeseries_2008 <- ts(energy_combined, frequency = 4, start = c(2008,1), end = c(2008,12))
  254. householdtimeseries_2008
  255.  
  256. plot.ts(householdtimeseries_2008)
  257. season2008<-decompose(householdtimeseries_2008, "multiplicative")
  258. season2008
  259. plot(as.ts(season2008$seasonal))
  260. plot(as.ts(season2008$random))
  261. plot(as.ts(season2008$trend))
  262. season2008_df <- data.frame(season2008 = c(season2008), time = c(time(season2008)))
  263. write_excel_csv(season2008_df,"season2008.csv")
  264.  
  265.  
  266. #Decomposing Seasonal Trends for Energy data _ March 2009
  267.  
  268.  
  269. library("dplyr")
  270. householdtimeseries_2009 <- ts(energy_combined, frequency = 4, start = c(2009,1), end = c(2009,12))
  271. householdtimeseries_2009
  272.  
  273. plot.ts(householdtimeseries_2009)
  274. season2009<-decompose(, "multiplicative")
  275. season2009
  276. plot(as.ts(season2009$seasonal))
  277. plot(as.ts(season2009$random))
  278. plot(as.ts(season2009$trend))
  279. season2009_df <- data.frame(season2009 = c(season2009), time = c(time(season2009)))
  280. write_excel_csv(season2009_df,"season2009.csv")
  281.  
  282.  
  283.  
  284. #holtwinters for 2007
  285. ?HoltWinters
  286. library(ggplot2)
  287. ?ts
  288.  
  289. householdtimeseries_2007 <- ts(energy_combined, frequency = 12, start = c("2007-08-01"), end = c("2007-09-01"))
  290. householdtimeseries_2007
  291. plot(householdtimeseries_2007)
  292.  
  293. august2007 <- ts(energy_combined, frequency = 1, start = c(2007,8), end = c(2007,9))
  294. august2007
  295. energy_combined
  296. View(energy_combined)
  297. View(august2007)
  298.  
  299. smooth2007<-HoltWinters(householdtimeseries_2007, beta = FALSE, gamma = FALSE)
  300. lines(fitted(smooth2007)[,1], col = 3)
  301. plot(smooth2007)
  302. plot(fitted(smooth2007))
  303.  
  304.  
  305. #Holtwinter for 2008
  306. smmoth2008<-HoltWinters(householdtimeseries_2008, beta = FALSE, gamma = FALSE)
  307. plot(smmoth2008)
  308.  
  309. #Holtwinter for 2009
  310. smmoth2009<-HoltWinters(householdtimeseries_2009, beta = FALSE, gamma = FALSE)
  311.  
  312.  
  313. View(Fore2008)
  314. View(DF2008_dec)
  315. View(DF2009_march)
Add Comment
Please, Sign In to add comment