Querying portia - Data analysis with R

Reading last package


In [138]:
headers <- httr::add_headers(Authorization = "Bearer bdb6e780b43011e7af0b67cba486057b", Accept = "text/csv")
params  <- list(order = "-1")

response <- httr::GET("http://io.portia.supe.solutions/api/v1/device/HytTDwUp-j8yrsh8e/port/1/sensor/1/last", headers, query = params)


content               = httr::content(response, "text", encoding = "UTF-8")
readings.temperature  = read.csv(text=content, sep=";")

summary(readings.temperature$dimension_value)


   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   24.1    24.1    24.1    24.1    24.1    24.1 

In [139]:
params  <- list(order = "-1", limit = 100)

response <- httr::GET("http://io.portia.supe.solutions/api/v1/device/HytTDwUp-j8yrsh8e/port/1/sensor/1/last", headers, query = params)


content               = httr::content(response, "text", encoding = "UTF-8")
readings.temperature  = read.csv(text=content, sep=";")

summary(readings.temperature$dimension_value)


   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  22.20   22.67   23.50   23.36   24.10   24.20 

Specific time frame data analysis - Last 24 hours

We are authenticating sucessfully, so let's dive into the data...


In [140]:
headers <- httr::add_headers(Authorization = "Bearer bdb6e780b43011e7af0b67cba486057b", Accept = "text/csv")
# params  <- list(order = "-1", from_timestamp ="1483228800000", to_timestamp="1510074561000")

response <- httr::GET("http://io.portia.supe.solutions/api/v1/device/HytTDwUp-j8yrsh8e/port/1/sensor/1", headers, query = params)
content               = httr::content(response, "text", encoding = "UTF-8")
readings.temperature  = read.csv(text=content, sep=";")

summary(readings.temperature$dimension_value)


   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  15.80   16.40   17.40   18.94   21.60   24.50 

In [141]:
response <- httr::GET("http://io.portia.supe.solutions/api/v1/device/HytTDwUp-j8yrsh8e/port/1/sensor/2", headers, query = params)
content           = httr::content(response, "text", encoding = "UTF-8")
readings.umidity  = read.csv(text=content, sep=";")

summary(readings.umidity$dimension_value)


   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  68.80   81.85   86.30   85.14   89.70   92.30 

In [142]:
response <- httr::GET("http://io.portia.supe.solutions/api/v1/device/HytTDwUp-j8yrsh8e/port/3/sensor/1", headers, query = params)
content           = httr::content(response, "text", encoding = "UTF-8")
readings.toxicgases  = read.csv(text=content, sep=";")

summary(readings.toxicgases$dimension_value)


   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  74.00   78.00   79.00   83.08   83.00  168.00 

In [143]:
readings.temperature <- transform(readings.temperature, ts_server     = server_timestamp / 1000, ts_local = package_local_timestamp / 1000, ts_server_text = as.POSIXct(server_timestamp / 1000, origin="1970-01-01" ), ts_local_text = as.POSIXct(package_local_timestamp / 1000, origin="1970-01-01" ) )
readings.temperature <- subset(   readings.temperature, select = -c( X, dimension_code, dimension_unity_code, dimension_thing_code, package_device_hash, dimension_port_id, dimension_sensor_id, package_local_timestamp, package_local_timestamp, server_timestamp))

readings.umidity     <- transform(readings.umidity, ts_server         = server_timestamp / 1000, ts_local = package_local_timestamp / 1000, ts_server_text = as.POSIXct(server_timestamp / 1000, origin="1970-01-01" ), ts_local_text = as.POSIXct(package_local_timestamp / 1000, origin="1970-01-01" )  )
readings.umidity     <- subset(   readings.umidity, select     = -c( X, dimension_code, dimension_unity_code, dimension_thing_code, package_device_hash, dimension_port_id, dimension_sensor_id, package_local_timestamp, package_local_timestamp, server_timestamp))

readings.toxicgases     <- transform(readings.toxicgases, ts_server         = server_timestamp / 1000, ts_local = package_local_timestamp / 1000, ts_server_text = as.POSIXct(server_timestamp / 1000, origin="1970-01-01" ),ts_local_text = as.POSIXct(package_local_timestamp / 1000, origin="1970-01-01" )  )
readings.toxicgases     <- subset(   readings.toxicgases, select     = -c( X, dimension_code, dimension_unity_code, dimension_thing_code, package_device_hash, dimension_port_id, dimension_sensor_id, package_local_timestamp, package_local_timestamp, server_timestamp))

In [144]:
paste("Número de pacotes:", nrow(readings.temperature))
head(readings.temperature, n=3)


'Número de pacotes: 1398'
dimension_valuets_serverts_localts_server_textts_local_text
24.1 1510076095 1510076091 2017-11-07 15:34:542017-11-07 15:34:51
24.1 1510076035 1510076031 2017-11-07 15:33:542017-11-07 15:33:51
24.1 1510075974 1510075971 2017-11-07 15:32:532017-11-07 15:32:51

In [145]:
paste("Número de pacotes:", nrow(readings.umidity))
head(readings.umidity, n=3)


'Número de pacotes: 1398'
dimension_valuets_serverts_localts_server_textts_local_text
92.3 1510076096 1510076092 2017-11-07 15:34:562017-11-07 15:34:52
92.0 1510076036 1510076031 2017-11-07 15:33:562017-11-07 15:33:51
91.8 1510075975 1510075971 2017-11-07 15:32:542017-11-07 15:32:51

In [146]:
paste("Número de pacotes:", nrow(readings.toxicgases))
head(readings.toxicgases, n=3)


'Número de pacotes: 1403'
dimension_valuets_serverts_localts_server_textts_local_text
77 1510076066 1510076064 2017-11-07 15:34:262017-11-07 15:34:24
77 1510076006 1510076004 2017-11-07 15:33:262017-11-07 15:33:24
77 1510075946 1510075944 2017-11-07 15:32:252017-11-07 15:32:24

In [147]:
diff_ts_local_temperature = diff(readings.temperature$ts_local)
avg_ts_local_temperature = mean(diff_ts_local_temperature)
paste("Temperatura - Diferença média entre um pacote e outro: ", avg_ts_local_temperature, " segundos locais")
paste("Número de pacotes:", nrow(readings.temperature))

diff_ts_local_umidity = diff(readings.umidity$ts_local)
summary(diff_ts_local_umidity)
avg_ts_local_umidity = mean(diff_ts_local_umidity)
paste("Umidade - Diferença média entre um pacote e outro: ", avg_ts_local_umidity, " segundos locais")

diff_ts_local_gas = diff(readings.toxicgases$ts_local)
summary(diff_ts_local_gas)
avg_ts_local_gas = mean(diff_ts_local_gas)
paste("Gases Nocivos - Diferença média entre um pacote e outro: ", avg_ts_local_gas, " segundos locais")
paste("Número de pacotes:", nrow(readings.toxicgases))


par(mfrow=c(2,2))
plot(diff_ts_local_temperature,type = "l",col = "red")
plot(diff_ts_local_umidity, type = "l", col = "blue")
plot(diff_ts_local_gas, type = "l", col = "green")


'Temperatura - Diferença média entre um pacote e outro: -61.790264853257 segundos locais'
'Número de pacotes: 1398'
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
-666.00  -61.00  -60.00  -61.79  -60.00  -50.00 
'Umidade - Diferença média entre um pacote e outro: -61.7909806728704 segundos locais'
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
-606.00  -61.00  -60.00  -61.57  -60.00  -57.00 
'Gases Nocivos - Diferença média entre um pacote e outro: -61.5670470756063 segundos locais'
'Número de pacotes: 1403'

In [148]:
#summary(readings.temperature$dimension_value)

par(mfrow=c(2,2))

plot(readings.temperature$ts_local_text,readings.temperature$dimension_value, "l", col="red")
linear.model = lm(readings.temperature$dimension_value ~ readings.temperature$ts_local)
abline(linear.model)

#summary(readings.umidity$dimension_value)
plot(readings.umidity$ts_local_text,readings.umidity$dimension_value, "l",  col="blue")
linear.model = lm(readings.umidity$dimension_value ~ readings.umidity$ts_local)
abline(linear.model)

#summary(readings.toxicgases$dimension_value)
plot(readings.toxicgases$ts_local_text,readings.toxicgases$dimension_value, "l",  col="green")
linear.model = lm(readings.toxicgases$dimension_value ~ readings.toxicgases$ts_local)
abline(linear.model)



In [149]:
par(mfrow=c(2,2))

m  <-mean(readings.temperature$dimension_value);
std<-sqrt(var(readings.temperature$dimension_value))
hist(readings.temperature$dimension_value,prob=T,main="Temperature")
curve(dnorm(x, mean=m, sd=std), col="darkblue", lwd=3, add=TRUE)

m<-mean(readings.umidity$dimension_value);std<-sqrt(var(readings.umidity$dimension_value))
hist(readings.umidity$dimension_value,prob=T,main="Umidity")
curve(dnorm(x, mean=m, sd=std), col="red", lwd=2, add=TRUE)
box()


m<-mean(readings.toxicgases$dimension_value);std<-sqrt(var(readings.toxicgases$dimension_value))
hist(readings.toxicgases$dimension_value,prob=T,main="Toxic Gases")
curve(dnorm(x, mean=m, sd=std), col="green", lwd=2, add=TRUE)
box()



In [150]:
# Calculate range from 0 to max value of readings.temperature$dimension_value and readings.umidity$dimension_value
g_range <- range(0, readings.temperature$dimension_value, readings.umidity$dimension_value, readings.toxicgases$dimension_value)

# Graph autos using y axis that ranges from 0 to max 
# value in readings.temperature$dimension_value or readings.umidity$dimension_value vector.  Turn off axes and 
# annotations (axis labels) so we can specify them ourself
plot(readings.temperature$ts_local_text, readings.temperature$dimension_value, type="s", col="red", ylim=g_range, ann=TRUE)


# Graph readings.umidity$dimension_value with red dashed line and square points
lines(readings.umidity$ts_local_text,readings.umidity$dimension_value, type="s", col="blue")
lines(readings.toxicgases$ts_local_text,readings.toxicgases$dimension_value, type="s", col="brown")



# Create a title with a red, bold/italic font
#title(main="Umidade e Temperatura")

# Label the x and y axes with dark green text
# title(xlab="Tempo", col.lab=rgb(0,0.5,0))

# Create a legend at (1, g_range[2]) that is slightly smaller 
# (cex) and uses the same line colors and points used by 
# the actual plots 
#legend(1, g_range[2], c("temperatura","umidade"), cex=0.8,    col=c("red","blue"), pch=21:22, lty=1:2);

lo <- loess(readings.temperature$dimension_value~readings.temperature$ts_local)
# plot(readings.temperature$ts_local_text, readings.temperature$dimension_value)
lines(predict(lo), col='blue', lwd=2)