Querying portia - Data analysis with R

Specific time frame data analysis - Last 24 hours

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


In [1]:
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/2/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. 
  16.60   20.10   23.40   23.39   26.10   28.80 

In [2]:
response <- httr::GET("http://io.portia.supe.solutions/api/v1/device/HytTDwUp-j8yrsh8e/port/2/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. 
  65.50   77.20   90.30   87.56   99.90   99.90 

In [ ]:


In [3]:
readings.temperature <- transform(readings.temperature, ts     = server_timestamp / 1000, ts_local = package_local_timestamp / 1000, ts_text = as.POSIXct(server_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.temperature <- subset(   readings.temperature, ts     > 1508536800)

readings.umidity     <- transform(readings.umidity, ts         = server_timestamp / 1000, ts_local = package_local_timestamp / 1000, ts_text = as.POSIXct(server_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.umidity     <- subset(   readings.umidity, ts         > 1508536800)

In [4]:
head(readings.temperature, n=5)


dimension_valuetsts_localts_text
21.3 1509829311 252 2017-11-04 19:01:50
21.3 1509829254 192 2017-11-04 19:00:53
21.4 1509829252 132 2017-11-04 19:00:51
21.4 1509829235 132 2017-11-04 19:00:35
20.0 1509829235 1509771506 2017-11-04 19:00:35

In [5]:
head(readings.umidity, n=5)


dimension_valuetsts_localts_text
90.1 1509829311 252 2017-11-04 19:01:50
91.9 1509829254 192 2017-11-04 19:00:53
89.7 1509829252 132 2017-11-04 19:00:51
89.7 1509829235 132 2017-11-04 19:00:35
99.9 1509829235 1509771506 2017-11-04 19:00:35

In [ ]:


In [6]:
diff_ts_temperature = diff(readings.temperature$ts)
summary(diff_ts_temperature)
avgdiff = mean(diff_ts_temperature)
paste("Temperatura - Diferença média entre um pacote e outro: ", avgdiff, " segundos no servidor")
paste("Número de pacotes:", nrow(readings.temperature))

diff_ts_umidity = diff(readings.umidity$ts)
summary(diff_ts_umidity)
avgdiff = mean(diff_ts_umidity)
paste("Umidade - Diferença média entre um pacote e outro: ", avgdiff, " segundos no servidor")
paste("Número de pacotes:", nrow(readings.umidity))

diff_ts_local_temperature = diff(readings.temperature$ts_local)
summary(diff_ts_local_temperature)
avgdiff = mean(diff_ts_local_temperature)
paste("Temperatura - Diferença média entre um pacote e outro: ", avgdiff, " segundos locais")

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


plot(diff_ts_temperature,type = "l",col = "red", xlab = "intervalo")
lines(diff_ts_umidity, type = "l", col = "blue")
lines(diff_ts_local_temperature, type = "l", col = "black")
lines(diff_ts_local_umidity, type = "l", col = "green")
# legend(2000,9.5,c("Health","Defense"),lwd=c(2.5,2.5),col=c("blue","red"))


     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
-1112.000   -60.830   -60.140   -56.860   -59.280    -0.001 
'Temperatura - Diferença média entre um pacote e outro: -56.8599283765153 segundos no servidor'
'Número de pacotes: 1467'
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
-1113.000   -61.040   -60.100   -56.820   -58.850    -0.001 
'Umidade - Diferença média entre um pacote e outro: -56.8211690524686 segundos no servidor'
'Número de pacotes: 1468'
      Min.    1st Qu.     Median       Mean    3rd Qu.       Max. 
-1.509e+09 -6.100e+01 -6.000e+01  1.030e+06 -6.000e+01  1.510e+09 
'Temperatura - Diferença média entre um pacote e outro: 1029840.17667121 segundos locais'
      Min.    1st Qu.     Median       Mean    3rd Qu.       Max. 
-1.509e+09 -6.100e+01 -6.000e+01  1.029e+06 -6.000e+01  1.510e+09 
'Umidade - Diferença média entre um pacote e outro: 1029138.1724608 segundos locais'

In [ ]:


In [7]:
summary(readings.temperature$dimension_value)
plot(readings.temperature$ts_text,readings.temperature$dimension_value, "s", col="red")
readings.temperature$index <- seq.int(nrow(readings.temperature))
linear.model = lm(readings.temperature$dimension_value ~ readings.temperature$ts)
abline(linear.model)
# plot(readings.temperature$ts_text,readings.temperature$dimension_value, "o",  col="red")


   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  16.60   20.10   23.40   23.39   26.10   28.80 

In [8]:
summary(readings.umidity$dimension_value)
plot(readings.umidity$ts_text,readings.umidity$dimension_value, "s",  col="blue")
# plot(readings.umidity$ts_text,readings.umidity$dimension_value, "o",  col="blue")


   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  65.50   77.20   90.30   87.56   99.90   99.90 

In [9]:
par(mfrow=c(2,2))
plot(readings.temperature$ts_text,readings.temperature$dimension_value, "s", col="red")
plot(readings.umidity$ts_text,readings.umidity$dimension_value, "s",  col="blue")



In [10]:
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)



In [ ]:


In [11]:
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()



In [ ]:


In [12]:
#readings <- merge(readings.temperature,readings.umidity, by="ts")
# readings

# Define 2 vectors
readings.temperature$dimension_value <- readings.temperature$dimension_value
readings.umidity$dimension_value <- readings.umidity$dimension_value

# 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)

# 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$dimension_value, type="s", col="red", ylim=g_range, 
 ann=TRUE)

# axis(2, las=1, at=5*0:g_range[2])


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

# 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);