Skip to content

Commit

Permalink
version 0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
wamartinez authored and cran-robot committed Mar 8, 2017
1 parent df532bb commit 6efc5e7
Show file tree
Hide file tree
Showing 29 changed files with 600 additions and 250 deletions.
21 changes: 12 additions & 9 deletions DESCRIPTION
@@ -1,16 +1,19 @@
Package: STMedianPolish
Type: Package
Title: Spatio-Temporal Median Polish
Version: 0.1
Date: 2015-06-21
Author: William Martinez, Carlos Melo <cmelo@udistrital.edu.co>
Maintainer: William Martinez <willimarti2008@gmail.com>
Depends: R (>= 2.15.0), maptools, reshape2, sp, spacetime, zoo
Description: Analysis by spatio-temporal data using the decomposition in n-dimensional arrays and using median polish technique.
Version: 0.2
Date: 2017-03-07
Author: William Martínez [aut, cre], Melo Carlos [aut], Melo Oscar [aut].
Maintainer: William Martínez <willimarti2008@gmail.com>
Depends: R (>= 2.15)
Imports: maptools, reshape2, sp, spacetime, zoo, nabor,gstat
URL: https://github.com/WilliamAMartinez/STMedianPolish
Description: Analyses spatio-temporal data, decomposing data in n-dimensional arrays and using the median polish technique.
License: GPL (>= 2)
LazyData: true
Encoding: latin1
Encoding: UTF-8
RoxygenNote: 6.0.1
NeedsCompilation: no
Packaged: 2015-06-21 17:51:19 UTC; willimarti2008
Packaged: 2017-03-08 02:51:54 UTC; willimarti2008
Repository: CRAN
Date/Publication: 2015-06-22 01:52:24
Date/Publication: 2017-03-08 08:22:14
52 changes: 28 additions & 24 deletions MD5
@@ -1,26 +1,30 @@
ea2b8b34d7e43332fca7cf01c280dc17 *DESCRIPTION
feea5ea60346688cb9a97d6b159ee441 *NAMESPACE
b149e55f25ab6e86b77e49bec78802fd *R/ConstructMPst.R
84e7d72b2c47884548e08caef4df4e35 *R/MedianPolishM.ConstructMPst.R
7b8861ddef04715fd8f97f4e1aa584ed *R/MedianPolishM.R
b73918397e43dcf6e1a1faab773abbfc *R/MedianPolishM.default.R
86172daba83d16a44b4cdeeb43ce6e61 *R/Mpplot.R
f1d3c590e5d59ad0c336106d5bac6ea7 *R/plot.MedianPolishM.R
2de03555bbc4927fa65639c99d35e791 *DESCRIPTION
90a307eaf87b5b4dc83adc62f5e0f4c5 *NAMESPACE
0e300c5c48cfe683af5a57550a7485b6 *R/ConstructMPst.R
e18a6f665295db32f30eebe10e862872 *R/MedianPolishM.ConstructMPst.R
139b530662e42484541da97216e1b0f8 *R/MedianPolishM.R
339d6b137c7a52b54ff7ca909d337929 *R/MedianPolishM.default.R
64a18b8854202a91fd22999438208890 *R/Mpplot.R
4c5b3e07e611de84bba2f3b98c81fedb *R/krige0STlocalMP.R
bece3bdf36e0bdd20d7752f2d82624f9 *R/plot.MedianPolishM.R
d10867fe2b260b874ac82271d1941577 *R/print.MedianPolishM.R
0346bbd2561e5bf5df8685b19bcdc683 *R/print.plot.MedianPolishM.R
a4b2a126683e070a2962a444a58655d4 *R/removetrendMPst.R
a364b625115ed75f3d9fb0e89918f017 *R/splineMPST.R
a79897d0df23d110f1bfa36ad721e139 *R/print.plot.MedianPolishM.R
9483aa6a0b487c980e1949500a473b7d *R/removetrendMPst.R
4326380369925408edcf7f315b13b2f2 *R/splineMPST.R
cfdd782969f55e0e69d5dbd588db4177 *data/DemMeta.rda
31682bec6c3a9b3623c754829e06107a *data/HZRMeta.rda
80b0bd2306b3d57f5f1e293e0bc7f05e *data/Metadb.rda
a9a78e1059bd1619a56299907dfd804e *man/ConstructMPst.Rd
5b063cf63b67ccf87f32fe68aef892fd *man/DemMeta.Rd
c0c1888f5d11a773ac55ae4c4076c668 *man/HZRMeta.Rd
e2b97cd99c287c11da6e1fa857807baf *man/MedianPolishM.ConstructMPst.Rd
bddc08a8d69caaba890d4a3d9f051677 *man/MedianPolishM.Rd
23e8b8d5f1a3bb87870f1fb553130430 *man/MedianPolishM.default.Rd
6296cf97001ded8abb6b663d78e19f44 *man/Metabd.Rd
7ae2044edecc12251bd9b726f4d6165d *man/Mpplot.Rd
9f0f00532e872f68a378f9d0c9e9ab0e *man/plot.MedianPolishM.Rd
5898044e2e45ce41b5081d55dfb17759 *man/removetrendMPst.Rd
3b6f0253f1928d6bf97d11fdec0203f2 *man/splineMPST.Rd
996309cbc0b71cc6612a7399c256ff1b *data/HZRMeta.rda
8f56a60adbe5a3dbeefcf588abdd2be5 *data/Metadb.rda
6e50723b26f0496da2180bebaaa7cc4e *data/VRes.rda
d656d643c31a7b3d9351865d9407c4b8 *man/ConstructMPst.Rd
1510cea736b06789e1b2c8d092f77c09 *man/DemMeta.Rd
4ad829f31a908c7e7f5e22a30feb56b1 *man/HZRMeta.Rd
4210cc4f987ab181976b50332299d1c5 *man/MedianPolishM.ConstructMPst.Rd
abff1b2a05ec995271545f21627f75b3 *man/MedianPolishM.Rd
b0079ebf4fe1ce48a855405a9c2fa1a9 *man/MedianPolishM.default.Rd
8aa85452c8e9f616a11e8432077af78f *man/Metabd.Rd
03ccb00158771ae001fcf1d9492e8a39 *man/Mpplot.Rd
703f748205c7d1dcb295d82cbf409808 *man/VRes.Rd
b78962fb4b122ff167a369f5276e50d6 *man/krige0STlocalMP.Rd
e5acef7085b763642da32edf6437b296 *man/plot.MedianPolishM.Rd
76899fbd07afc7841b9e3d812585c753 *man/removetrendMPst.Rd
1268a4eb0ea456bc2da1eaecc0c6f0f8 *man/splineMPST.Rd
22 changes: 20 additions & 2 deletions NAMESPACE
@@ -1,4 +1,4 @@
# Generated by roxygen2 (4.1.1): do not edit by hand
# Generated by roxygen2: do not edit by hand

S3method(MedianPolishM,ConstructMPst)
S3method(MedianPolishM,default)
Expand All @@ -8,17 +8,35 @@ S3method(print,plot.MedianPolishM)
export(ConstructMPst)
export(MedianPolishM)
export(Mpplot)
export(krige0STlocalMP)
export(removetrendMPst)
export(splineMPST)
importFrom(graphics,axis)
importFrom(graphics,layout)
importFrom(graphics,legend)
importFrom(graphics,mtext)
importFrom(graphics,points)
importFrom(graphics,stripchart)
importFrom(graphics,text)
importFrom(gstat,estiStAni)
importFrom(maptools,readShapePoly)
importFrom(nabor,knn)
importFrom(reshape2,melt)
importFrom(sp,Polygon)
importFrom(sp,Polygons)
importFrom(sp,SpatialPoints)
importFrom(sp,SpatialPolygons)
importFrom(sp,over)
importFrom(sp,plot)
importFrom(sp,proj4string)
importFrom(sp,spDists)
importFrom(sp,spsample)
importFrom(spacetime,STFDF)
importFrom(spacetime,stplot)
importFrom(zoo,as.Date)
importFrom(stats,dist)
importFrom(stats,median)
importFrom(stats,na.omit)
importFrom(stats,ts.plot)
importFrom(utils,setTxtProgressBar)
importFrom(utils,txtProgressBar)
importFrom(zoo,as.yearmon)
89 changes: 47 additions & 42 deletions R/ConstructMPst.R
@@ -1,53 +1,58 @@

#' Construct Spatio - temporal regular data.
#' Construct Spatio - temporal regular data.
#'
#' Create an spatio - temporal object with regular data to order employing median polish technique.
#' Create an spatio - temporal object with regular data, in order to employ median polish technique.
#' @usage ConstructMPst(valuest,time,pts,Delta)
#'
#' @param valuest it's a data.frame in which different columns refer to different locations, and each row reflects a particular observation time.
#'
#' @param valuest data.frame in which different columns refer to different locations, and each row reflects a particular observation time.
#' @param time indicate the time of valuest, the intervals of time must be regular.
#' @param pts it's a data.frame that hold three dimensions spatial coordinates \code{x}, \code{y} and \code{z}.
#' @param pts data.frame that hold three dimensions spatial coordinates \code{x}, \code{y} and \code{z}. Therefore, the coordinates should be projected.
#' @param Delta vector with number of divisions of each spatial direction. c(Delta \code{x}, Delta \code{y}, Delta \code{z}).
#' @return An object of class ConstructMPst with the following list of components:
#' @return \item{results}{average value of the set of stations into unity spatio - temporal defined for delta.}
#' @return \item{Value}{array with the results organized in dimensions defined in Delta.}
#' @return \item{valuest}{valuest}
#' @return \item{pts}{pts}
#' @return \item{time}{time}
#' @return \item{Delta}{Delta}
#' @details Table composed for coordinates of center and average of position of stations for unit tridimensional array in space - time, in which show a average value of site.
#' @return \item{results}{average value on the stations set into unity spatio -- temporal defined for delta.}
#' @return \item{Value}{array with the results organized by cells, the size of the cells is defined in Delta.}
#' @return \item{valuest}{valuest.}
#' @return \item{pts}{pts.}
#' @return \item{time}{time.}
#' @return \item{Delta}{Delta.}
#' @details This function configures an irregular distribution of spatio -- temporal data in four - ways. Therefore, the new data corresponds to the average of values and coordinates of every spatio -temporal cell.
#'
#' @references Martínez, W. A., Melo, C. E., & Melo, O. O. (2017). \emph{Median Polish Kriging for space--time analysis of precipitation} Spatial Statistics, 19, 1-20. \href{http://www.sciencedirect.com/science/article/pii/S2211675316301336}{[link]}
#' @references Berke, O. (2001). \emph{Modified median polish kriging and its application to the wolfcamp - aquifer data.} Environmetrics, 12(8):731-748.\href{http://onlinelibrary.wiley.com/doi/10.1002/env.495/abstract}{[link]}
#' @examples
#' @importFrom reshape2 melt
#' @importFrom zoo as.yearmon
#' @importFrom stats na.omit
#' @importFrom utils txtProgressBar setTxtProgressBar
#' @examples
#' ## Not run:
#' library(zoo)
#' data(Metadb)
#' #records of monthly precipitation from january 2007 to january 2010
#' Metadb<-Metadb[,c(1:4,89:125)]
#' x<-matrix(0,1,37)
#' for(i in 1:37){
#' x[,i] <- 2007 + (seq(0, 36)/12)[i]
#' }
#' x<-as.Date (as.yearmon(x), frac = 1)
#' time = as.POSIXct(x, tz = "GMT")
#'
#' MPST<-ConstructMPst(Metadb[,-c(1:4)],time,pts=Metadb[,2:4],Delta=c(7,6,5))
#' ## End(Not run)
#' @importFrom reshape2 melt
#' @importFrom zoo as.Date
#' @importFrom zoo as.yearmon
#' @export
#'
#'
ConstructMPst <-
function(valuest,time,pts,Delta)
{
if (!is.data.frame(valuest))
if (!is.data.frame(valuest))
stop("data is not a data.frame")
if (length(Delta)!=3)
if (length(Delta)!=3)
stop("Delta must have the division of the space (x,y,z)")
if (dim(pts)[2]!=3)
if (dim(pts)[2]!=3)
stop("The points of the space must have coordinates (x,y,z)")
if (dim(valuest)[1]!=dim(pts)[1])
stop("The valuest must have the same number of rows as coordinates")
if (length(na.omit(pts[,1]))!=length(pts[,1])||length(na.omit(pts[,2]))!=length(pts[,2]) || length(na.omit(pts[,3]))!=length(pts[,3]))
if (length(na.omit(pts[,1]))!=length(pts[,1])||length(na.omit(pts[,2]))!=length(pts[,2]) || length(na.omit(pts[,3]))!=length(pts[,3]))
stop("The are NAs in pts")

datast<-data.frame(time=rep(time,rep(dim(pts)[1],length(time))),pts,values=as.matrix(as.numeric(as.matrix(valuest))))
MpData<-list()
Data<-rep(0,1,(Delta[1]*Delta[2]*Delta[3]))
Expand All @@ -58,15 +63,15 @@ ConstructMPst <-
Mx1<-Mx
My1<-Mx
Mz1<-Mx

VDelta<-list()
Min<-c(1:length(Delta))
Max<-c(1:length(Delta))
for(i in 1:length(Delta)){
Min[i]<-min(pts[,i]) -(max(pts[,i])-min(pts[,i]))/((Delta[i]-1)*2)}
for(i in 1:length(Delta)){
Max[i]<-max(pts[,i]) +(max(pts[,i])-min(pts[,i]))/((Delta[i]-1)*2)}

Mvalue<-matrix(0,length(unique(datast[,1])),length(Data))
MCoorx<-Mvalue
MCoory<-Mvalue
Expand All @@ -75,27 +80,27 @@ ConstructMPst <-
MCoory1<-Mvalue
MCoorz1<-Mvalue
q=0

for(i in 1:length(Delta)){
VDelta[1:length(Delta)][[i]]<-(seq(Min[i],Max[i],abs(Min[i]-Max[i])/(Delta[i])))}

pb <- txtProgressBar(min = 0, max = length(unique(datast[,1])), style = 3)

for(timei in unique(datast[,1]))
{
q=q+1
sub_i <- which(datast[,1] == timei)

for(i in 1:Delta[1]){
for(j in 1:Delta[2]){
for(k in 1:Delta[3]){
sub_indexi <- which(datast[sub_i,2] >= VDelta[][[1]][i] & datast[sub_i,2] <= VDelta[][[1]][i+1])
sub_indexj <- which(datast[sub_i,3] >= VDelta[][[2]][j] & datast[sub_i,3] <= VDelta[][[2]][j+1])
sub_indexk <- which(datast[sub_i,4] >= VDelta[][[3]][k] & datast[sub_i,4] <= VDelta[][[3]][k+1])

inter1<-intersect(sub_indexj,sub_indexi)
inter2<-intersect(inter1,sub_indexk)

if(length(inter2)==0 )
{
Data[i,j,k]<-NA
Expand All @@ -105,7 +110,7 @@ ConstructMPst <-
Mx1[i,j,k]<-Mx[i,j,k]
My1[i,j,k]<-My[i,j,k]
Mz1[i,j,k]<-Mz[i,j,k]

}else if(length(which(datast[sub_i,5][inter2]!='NA'))==0)
{
Data[i,j,k]<-NA
Expand All @@ -115,22 +120,22 @@ ConstructMPst <-
Mx1[i,j,k]<-mean(c(VDelta[][[1]][i],VDelta[][[1]][i+1]))
My1[i,j,k]<-mean(c(VDelta[][[2]][j],VDelta[][[2]][j+1]))
Mz1[i,j,k]<-mean(c(VDelta[][[3]][k],VDelta[][[3]][k+1]))

}else
{

#==========================================================
#===========WID DEM and Values=============================
#==========================================================

MC<-datast[sub_i,5][inter2]!='NA'
Mx[i,j,k]<-sum(datast[sub_i,2][inter2*MC],na.rm = TRUE)/length(which(datast[sub_i,2][inter2*MC]!='NA'))
My[i,j,k]<-sum(datast[sub_i,3][inter2*MC],na.rm = TRUE)/length(which(datast[sub_i,3][inter2*MC]!='NA'))
Mz[i,j,k]<-sum(datast[sub_i,4][inter2*MC],na.rm = TRUE)/length(which(datast[sub_i,4][inter2*MC]!='NA'))
Mx1[i,j,k]<-mean(c(VDelta[][[1]][i],VDelta[][[1]][i+1]))
My1[i,j,k]<-mean(c(VDelta[][[2]][j],VDelta[][[2]][j+1]))
Mz1[i,j,k]<-mean(c(VDelta[][[3]][k],VDelta[][[3]][k+1]))

if(dim((na.omit(datast[sub_i,4:5][inter2,])))[1]!=1 )
{
distsValue = sqrt(apply(( t(as.matrix(na.omit(datast[sub_i,2:4][inter2*MC,])))-c(Mx[i,j,k],My[i,j,k],Mz[i,j,k]))^2, 2,sum))
Expand All @@ -145,7 +150,7 @@ ConstructMPst <-
}
}
}

Mvalue[which(unique(datast[,1])==timei),]<-melt(Data)[,4]
MCoorx[which(unique(datast[,1])==timei),]<-melt(Mx)[,4]
MCoory[which(unique(datast[,1])==timei),]<-melt(My)[,4]
Expand All @@ -156,23 +161,23 @@ ConstructMPst <-
setTxtProgressBar(pb, q)
}
close(pb)

Date<-rep(unique(datast[,1]),rep(prod(Delta),length(unique(datast[,1]))))

MpData$results<-data.frame(Date,
XCenter=as.numeric(t(MCoorx1)),
XCenter=as.numeric(t(MCoorx1)),
YCenter=as.numeric(t(MCoory1)),
ZCenter=as.numeric(t(MCoorz1)),
Value=as.numeric(t(Mvalue)),
Xmean=as.numeric(t(MCoorx)),
Xmean=as.numeric(t(MCoorx)),
Ymean=as.numeric(t(MCoory)),
Zmean=as.numeric(t(MCoorz)))
MpData$Value<-array(MpData$results[,5],c(Delta,length(time)))
MpData$valuest<-valuest
MpData$pts<-pts
MpData$time<-time
MpData$Delta<-Delta

class(MpData) <- "ConstructMPst"
return(MpData)
}
Expand Down
5 changes: 3 additions & 2 deletions R/MedianPolishM.ConstructMPst.R
@@ -1,18 +1,19 @@
#' Median polish multidimensional.
#'
#' Fits an additive model for multidimensional array, using Tukey's median polish procedure.
#' An additive model for multidimensional array is fitted, using Tukey's median polish procedure.
#'
#' @param data class \code{\link{ConstructMPst}}.
#' @param eps real number greater than \code{0}, default 0.01. A tolerance for convergence: see Details
#' @param maxiter the maximum number of iterations. Default 10.
#' @param na.rm logical. If the data contains NA's. Default TRUE.
#' @param \dots ignored.
#' @details The model fitted is additive \eqn{\mu +\alpha _{a} + \beta _{b} + \xi _{c} + \tau _{t}}, where \eqn{\mu} is an overall mean, \eqn{\alpha_{a}} is the \eqn{a}-th row effect, \eqn{\beta_{b}} is the effect \eqn{b}-th column effect, \eqn{\xi_{c}} is the \eqn{c}-th layer effect, \eqn{\tau _{t}} is the \eqn{t}-th time effect. The algorithm works by alternately removing medians of every spatio - temporal dimensions, and continues until the proportional reduction in the sum of absolute residuals is less than eps or until there have been maxiter iterations. If na.rm is FALSE the presence of any NA value in x will cause an error, otherwise NA values are ignored. MedianPolishM returns an object of class MedianPolishM (see below). There is a plotting method for this class, \code{\link{plot.MedianPolishM}}.
#' @details The model fitted is an additive, \eqn{\mu +\alpha _{a} + \beta _{b} + \xi _{c} + \tau _{t}}, where \eqn{\mu} is an overall mean, \eqn{\alpha_{a}} is the \eqn{a}-th row effect, \eqn{\beta_{b}} is the effect \eqn{b}-th column effect, \eqn{\xi_{c}} is the \eqn{c}-th layer effect, \eqn{\tau _{t}} is the \eqn{t}-th time effect. The algorithm works by alternately removing medians of every spatio - temporal dimensions, and continues until the proportional reduction in the sum of absolute residuals is less than eps or until there have been maxiter iterations. If na.rm is FALSE, the presence of any NA value in x will cause an error, otherwise NA values are ignored. MedianPolishM returns an object of class MedianPolishM (see below). There is a plotting method for this class, \code{\link{plot.MedianPolishM}}.
#' @return An object of class medpolish with the following named components in a list:
#' @return \item{residuals}{the residuals.}
#' @return \item{overall}{the fitted constant term.}
#' @return \item{effects}{the fitted every space - time effects.}
#' @return \item{iter}{number of iterations used in the range maxiter.}
#' @references Martínez, W. A., Melo, C. E., & Melo, O. O. (2017). \emph{Median Polish Kriging for space--time analysis of precipitation} Spatial Statistics, 19, 1-20. \href{http://www.sciencedirect.com/science/article/pii/S2211675316301336}{[link]}
#' @references Hoaglin, D. C., Mosteller, F., & Tukey, J. W. (Eds.). (2011). \emph{Exploring data tables, trends, and shapes} (Vol. 101). John Wiley & Sons.\href{http://www.wiley.com/WileyCDA/WileyTitle/productCd-047004005X.html}{[link]}
#' @importFrom reshape2 melt
#' @export
Expand Down
8 changes: 5 additions & 3 deletions R/MedianPolishM.R
@@ -1,14 +1,16 @@
#' Median polish multidimensional.
#'
#' Fits an additive model for multidimensional array, using Tukey's median polish procedure.
#' @param data object of class array, table, or matrix, see details.
#' An additive model for multidimensional array is fitted, using Tukey's median polish procedure.
#' @param data object of class array, table or matrix (see details).
#' @param \dots default arguments, see \code{\link{MedianPolishM.default}}
#' @details The function MedianPolishM is generic. See the documentation for \code{\link{MedianPolishM.default}} for further details.
#' @return An object of class medpolish with the following named components in a list:
#' @return \item{residuals}{the residuals.}
#' @return \item{overall}{the fitted constant term.}
#' @return \item{effects}{the fitted every dimensions effects fo array multidimensional.}
#' @return \item{effects}{the fitted every dimensions effects to array multidimensional.}
#' @return \item{iter}{number of iterations used in the range maxiter.}
#'
#' @references Martínez, W. A., Melo, C. E., & Melo, O. O. (2017). \emph{Median Polish Kriging for space--time analysis of precipitation} Spatial Statistics, 19, 1-20. \href{http://www.sciencedirect.com/science/article/pii/S2211675316301336}{[link]}
#' @references Hoaglin, D. C., Mosteller, F., & Tukey, J. W. (Eds.). (2011). \emph{Exploring data tables, trends, and shapes} (Vol. 101). John Wiley & Sons.\href{http://www.wiley.com/WileyCDA/WileyTitle/productCd-047004005X.html}{[link]}
#' @importFrom reshape2 melt
#'
Expand Down

0 comments on commit 6efc5e7

Please sign in to comment.