il linguaggio R per i dati di qualità dell'aria

una demo e alcune spiegazioni

Giovanni Bonafè
Arpa ER

demo

cosa c'è dietro?

  • R
  • il pacchetto openair per elaborazione/visualizzazione dei dati di qualità dell'aria
  • il pacchetto shiny per creare una web app
  • RStudio, un IDE (ambiente di sviluppo integrato) per R
  • i dati 2001-2012 di PM10, organizzati in un oggetto di R di tipo matrix

codice: openair

## carico il pacchetto
library(openair)

## carico i dati
load("~/ShinyApps/openair3/pm10.RData")

## seleziono solo due anni...
Dat <- selectByDate(Dat, year = 2011:2012)

## ...e due stazioni
Dat <- subset(Dat, Name %in% c("Cittadella", "Risorgimento"))

prendere i dati da web

library(RCurl)
library(foreign)
url <- "ftp://ftp.smr.arpa.emr.it/ambiente/giorni-critici/ngg_crit.bo1.csv"
dum <- getURL(url, ssl.verifypeer = FALSE)
data <- read.csv(textConnection(dum))

grafici

myPlot <- timeVariation(Dat, pollutant = "pm10", group = "Province")

I tre grafici sono fatti con la funzione timeVariation(); qui prendo solo il pannello con i mesi

plot(myPlot, subset = "month")

plot of chunk unnamed-chunk-4

aqStats(): cosa fa

Per generare la tabella delle statistiche uso la funzione aqStats()

colnames(Dat)[3] <- "site"  #i nomi dei siti stanno nella terza colonna della mia matrice
aqStats(Dat, pollutant = "pm10", transpose = TRUE)
##    year       variable Cittadella PM10 Risorgimento PM10
## 1  2011   data.capture           98.10             94.80
## 2  2011           mean           36.40             33.81
## 3  2011        minimum            1.00              3.00
## 4  2011        maximum          138.00            148.00
## 5  2011         median           31.00             28.00
## 6  2011      max.daily          138.00            148.00
## 7  2011  max.rolling.8           83.88             88.88
## 8  2011 max.rolling.24           67.59             67.08
## 9  2011  percentile.95           72.15             76.75
## 10 2011  percentile.99          108.58            103.75
## 11 2011     days.gt.50           61.00             62.00
## 12 2012   data.capture           98.40              0.00
## 13 2012           mean           36.08                NA
## 14 2012        minimum            4.00                NA
## 15 2012        maximum          218.00                NA
## 16 2012         median           31.00                NA
## 17 2012      max.daily          218.00                NA
## 18 2012  max.rolling.8          111.67                NA
## 19 2012 max.rolling.24           72.73                NA
## 20 2012  percentile.95           73.05                NA
## 21 2012  percentile.99          110.28                NA
## 22 2012     days.gt.50           70.00                NA

aqStats(): help

per guardare il codice che sta dietro una funzione è sufficiente scrivere il nome della funzione

aqStats

per vedere l'help invece

help(aqStats)

in alcuni casi è utile anche

example(aqStats)
demo(aqStats)

aqStats(): guardiamoci dentro

Vediamo un pezzo di aqStats

if (length(grep("no2", pollutant, ignore.case = TRUE)) == 1) {
    hours <- ddply(mydata[, c("year", pollutant)], .(year), hoursMoreThan, threshold = 200)
}

Nell'help di ddply() scopriamo che in questo caso applica la funzione hoursMoreThan() alla matrice di dati mydata[, c("year", pollutant)] dividendola in sottoinsiemi in base alla variabile year. In più, alla funzione hoursMoreThan() passa il parametro threshold = 200.

Più sopra ecco hoursMoreThan():

hoursMoreThan <- function(mydata, threshold) {
    hours <- length(which(mydata[, pollutant] > threshold))
    hours
}

e poi...

  • per trasformare il tutto in una web app interattiva ho usato shiny
  • questa presentazione HTML5 è stata creata con i pacchetti R knitr e slidify

riferimenti

  • Carslaw, D.C. and K. Ropkins, (2012) openair --- an R package for air quality data analysis. Environmental Modelling & Software. Volume 27-28, 52-61.
  • David Carslaw and Karl Ropkins (2012). openair: Open-source tools for the analysis of air pollution data. R package version 0.5-21.
  • R Core Team (2012). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. ISBN 3-900051-07-0, URL http://www.R-project.org/.
  • Ramnath Vaidyanathan (2012). slidify: Generate reproducible html5 slides from R markdown. R package version 0.3.3. http://ramnathv.github.com/slidify/
  • RStudio Team (2012). RStudio: Integrated Development for R. RStudio, Inc., Boston, MA URL http://www.rstudio.com/.
  • Yihui Xie (2013). knitr: A general-purpose package for dynamic report generation in R. R package version 1.4.1.