From 9a8ee0a153dfceda897bba47faa0643899b74e19 Mon Sep 17 00:00:00 2001 From: Olav Peeters Date: Mon, 9 Mar 2015 11:59:15 +0100 Subject: [PATCH] add demo correlation between two time series --- demo/00Index | 1 + demo/correlation.R | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 demo/correlation.R diff --git a/demo/00Index b/demo/00Index index 3f4b946..9da8cbe 100644 --- a/demo/00Index +++ b/demo/00Index @@ -5,3 +5,4 @@ openair2 Openair pollution rose demo cache How to manipulate the request cache spatial-coercion Coercing selected classes to spatial classes distance-matrix Find the five nearest stations +correlation Correlation between two timeseries diff --git a/demo/correlation.R b/demo/correlation.R new file mode 100644 index 0000000..abc09a9 --- /dev/null +++ b/demo/correlation.R @@ -0,0 +1,72 @@ +library(sensorweb4R) +library(sp) +library(lubridate) +library(zoo) +library(openair) + +# endpoint is rest-api of IRCEL +endpoint <- example.endpoints()[2] + +# available services on this endpoint: only one +srv <- services(endpoint) +label(srv) + +## get the names of the phenonoma = pollutants +phe <- phenomena(endpoint) +head(label(phe)) + +##get the names of the stations +sta <- stations(srv) +head(label(sta)) + +# filter by category: e.g. only Black Carbon stations +cat <- categories(srv) +head(label(cat)) +sta <- stations(srv,category=cat[1]) +head(label(sta)) + +# conversion info to dataframe, e.g.: as.data.frame() + +# get spatial info on stations +geom <- sp::geometry(sta) +head(geom) + +# get all timeseries in service +# ts <- timeseries(srv) +# or filter by station +# ts <- timeseries(sta[1]) +# filter by category +ts <- timeseries(endpoint, category=cat[1])[1:2] + +## available info on timeseries + +str(ts, max.level=2) +ts <- fetch(ts) # fetch info +label(station(ts)) +label(category(ts)) +label(procedure(ts)) + + +#### download data + +# length of timeseries + +lubridate::duration(lubridate::new_interval(time(firstValue(ts)), time(lastValue(ts)))) + +# filter: only a part of the series requested + +last <- min(time(lastValue(ts))) +time <- lubridate::as.interval(lubridate::weeks(1), last-lubridate::weeks(1)) + +data <- getData(ts,timespan=time) +str(data) + +#### Create a scatterplot of two stations ## +############################################ + +BC <- as.data.frame(data) +head(BC) +class(BC$time2) +names(BC) <- c("time",label(station(ts))[1],"time2",label(station(ts))[2]) + +scatterPlot(BC, x=label(station(ts))[1], y=label(station(ts))[2], linear=TRUE) \ No newline at end of file