| Type: | Package |
| Title: | Simulate Auto Regressive Data from Precision Matricies |
| Version: | 0.1.0 |
| Author: | Neal Marquez [aut, cre, cph] |
| Maintainer: | Neal Marquez <nmarquez@uw.edu> |
| Description: | Using sparse precision matricies and Choleski factorization simulates data that is auto-regressive. |
| Depends: | R (≥ 3.3.0) |
| Imports: | MASS, Matrix, sparseMVN, sp |
| Suggests: | ggplot2, leaflet |
| License: | GPL-2 |GPL-3 [expanded from: GPL (≥ 2)] |
| BugReports: | https://github.com/nmmarquez/ar.matrix/issues |
| Encoding: | UTF-8 |
| LazyData: | true |
| RoxygenNote: | 6.1.1 |
| NeedsCompilation: | no |
| Packaged: | 2018-11-20 20:44:39 UTC; nmarquez |
| Repository: | CRAN |
| Date/Publication: | 2018-12-02 17:30:06 UTC |
Precision matrix for an AR1 process
Description
Functions for creating precision matricies and observations ofan AR1 process
Usage
Q.AR1(M, sigma, rho, sparse=FALSE, vcov=FALSE)r.AR1(n, M, sigma, rho)Arguments
M | int > 0, number of elements in the AR1 process. |
sigma | float > 0, pairwise observation standard deviation. |
rho | float >= 0 & < 1, how correlated pairwise observations are. Thefunction will still run with values outside of the range [0,1) however thestability of the simulation results are not gaurunteed. |
sparse | bool Should the matrix be of class 'dsCMatrix' |
vcov | bool If the vcov matrix should be returned instead of theprecision matrix. |
n | int > 0, number of observations to simulate from the GMRF. |
Value
Q.AR1 returns either a precision or variance-covariance functionwith a AR1 structure.
r.AR1 retrurns a matrix with n rows which are the n observations of aGaussian Markov random field AR1 process.
Examples
require("ggplot2")# simulate AR1 GMRFobs <- r.AR1(100, M=30, sigma=1, rho=.98)# resulting matrix is n x Mdim(obs)# subtract off the first time point to more easily observe correlationobs_adj <- obs - obs[,1]# move objects to a data framear1_df <- data.frame(obs=c(t(obs_adj)), realization=rep(1:100, each=30), time=rep(1:30, 100))# plot each realizationggplot(data=ar1_df, aes(time, obs, group=realization, color=realization)) + geom_line()Precision matrix for a IID process
Description
Functions for creating precision matricies and observationsof a independent identically distributed GMRF process.
Usage
Q.iid(M, sigma, sparse=FALSE, vcov=FALSE)r.iid(n, M, sigma)Arguments
M | int > 0, number of elements in the process. |
sigma | float > 0, standard deviat |
sparse | bool Should the matrix be of class 'dsCMatrix' |
vcov | bool If the vcov matrix should be returned instead of theprecision matrix. |
n | int > 0, number of observations to simulate from the GMRF. |
Value
Q.iid returns either a precision or variance-covariance functionwith iid structure.
r.iid retrurns a matrix with n rows which are the n observations of aGaussian Markov random field iid process.
Examples
require("leaflet")require("sp")# simulate iid data and attach to spatial polygons data frameUS.df@data$data <- c(r.iid(1, M=nrow(US.graph), sigma=1))# color palette of datapal <- colorNumeric(palette="YlGnBu", domain=US.df@data$data)# see mapmap1<-leaflet() %>% addProviderTiles("CartoDB.Positron") %>% addPolygons(data=US.df, fillColor=~pal(data), color="#b2aeae", fillOpacity=0.7, weight=0.3, smoothFactor=0.2) %>% addLegend("bottomright", pal=pal, values=US.df$data, title="", opacity=1)map1Precision matrix for a pCAR process
Description
Functions for creating precision matricies and observationsof a Leroux CAR(lCAR) process as defined in MacNab 2011. The matrix definesthe precision of estimates when observations share connections which areconditionally auto-regressive(CAR).
Usage
Q.lCAR(graph, sigma, rho, sparse=FALSE, vcov=FALSE)r.lCAR(n, graph, sigma, rho)Arguments
graph | matrix, square matrix indicating where two observations areconnected (and therefore conditionally auto-regressive). |
sigma | float > 0, process standard derviation see MacNab 2011. |
rho | float >= 0 & < 1, how correlated neighbors are. Thefunction will still run with values outside of the range [0,1) however thestability of the simulation results are not gaurunteed. see MacNab 2011. |
sparse | bool Should the matrix be of class 'dsCMatrix' |
vcov | bool If the vcov matrix should be returned instead of theprecision matrix. |
n | int > 0, number of observations to simulate from the GMRF. |
Value
Q.lCAR returns either a precision or variance-covariance functionwith a lCAR structure.
r.lCAR retrurns a matrix with n rows which are the n observations of aGaussian Markov random field lCAR process.
References
Y.C. MacNab On Gaussian Markov random fields and Bayesiandisease mapping. Statistical Methods in Medical Research. 2011.
Examples
require("leaflet")require("sp")# simulate lCAR data and attach to spatial polygons data frameUS.df@data$data <- c(r.lCAR(1, graph=US.graph, sigma=1, rho=.99))# color palette of datapal <- colorNumeric(palette="YlGnBu", domain=US.df@data$data)# see mapmap1<-leaflet() %>% addProviderTiles("CartoDB.Positron") %>% addPolygons(data=US.df, fillColor=~pal(data), color="#b2aeae", fillOpacity=0.7, weight=0.3, smoothFactor=0.2) %>% addLegend("bottomright", pal=pal, values=US.df$data, title="", opacity=1)map1Modified Precision matrix for a BYM process
Description
EXPIREMENTAL. Functions for creating precision matricies andobservations of a modified BYM process as defined in MacNab 2011. The matrixdefines the precision of estimates when observations share connections whichare conditionally auto-regressive(CAR). Because the precision matrix isnot symetric the process is not a true GMRF.
Usage
Q.mBYM(graph, sigma, rho, vcov=FALSE)r.mBYM(n, graph, sigma, rho)Arguments
graph | matrix, square matrix indicating where two observations areconnected (and therefore conditionally auto-regressive). |
sigma | float > 0, process standard derviation see MacNab 2011. |
rho | float >= 0 & < 1, how correlated neighbors are. Thefunction will still run with values outside of the range [0,1) however thestability of the simulation results are not gaurunteed. see MacNab 2011. |
vcov | bool If the vcov matrix should be returned instead of theprecision matrix. |
n | int > 0, number of observations to simulate from the GMRF. |
Value
Q.mBYM returns either a precision or variance-covariance functionwith a modified BYM structure.
r.mBYM retrurns a matrix with n rows which are the n observations of a pseudoGaussian Markov random field of a modified BYM process.
References
Y.C. MacNab On Gaussian Markov random fields and Bayesiandisease mapping. Statistical Methods in Medical Research. 2011.
Examples
## Not run: require("leaflet")require("sp")# simulate mBYM data and attach to spatial polygons data frameUS.df@data$data <- c(r.mBYM(1, graph=US.graph, sigma=1, rho=.99))# color palette of datapal <- colorNumeric(palette="YlGnBu", domain=US.df@data$data)# see mapmap1<-leaflet() %>% addProviderTiles("CartoDB.Positron") %>% addPolygons(data=US.df, fillColor=~pal(data), color="#b2aeae", fillOpacity=0.7, weight=0.3, smoothFactor=0.2) %>% addLegend("bottomright", pal=pal, values=US.df$data, title="", opacity=1)map1## End(Not run)Precision matrix for a pCAR process
Description
Functions for creating precision matricies and observationsof a proper CAR(pCAR) process as defined in MacNab 2011. The matrix definesthe precision of estimates when observations share connections which areconditionally auto-regressive(CAR).
Usage
Q.pCAR(graph, sigma, rho, sparse=FALSE, vcov=FALSE)r.pCAR(n, graph, sigma, rho)Arguments
graph | matrix, square matrix indicating where two observations areconnected (and therefore conditionally auto-regressive). |
sigma | float > 0, process standard derviation see MacNab 2011. |
rho | float >= 0 & < 1, how correlated neighbors are. Thefunction will still run with values outside of the range [0,1) however thestability of the simulation results are not gaurunteed. see MacNab 2011. |
sparse | bool Should the matrix be of class 'dsCMatrix' |
vcov | bool If the vcov matrix should be returned instead of theprecision matrix. |
n | int > 0, number of observations to simulate from the GMRF. |
Value
Q.pCAR returns either a precision or variance-covariance functionwith a pCAR structure.
r.pCAR retrurns a matrix with n rows which are the n observations of aGaussian Markov random field pCAR process.
References
Y.C. MacNab On Gaussian Markov random fields and Bayesiandisease mapping. Statistical Methods in Medical Research. 2011.
Examples
require("leaflet")require("sp")# simulate pCAR data and attach to spatial polygons data frameUS.df@data$data <- c(r.pCAR(1, graph=US.graph, sigma=1, rho=.99))# color palette of datapal <- colorNumeric(palette="YlGnBu", domain=US.df@data$data)# see mapmap1<-leaflet() %>% addProviderTiles("CartoDB.Positron") %>% addPolygons(data=US.df, fillColor=~pal(data), color="#b2aeae", fillOpacity=0.7, weight=0.3, smoothFactor=0.2) %>% addLegend("bottomright", pal=pal, values=US.df$data, title="", opacity=1)map1Spatial Polygons Data Frame of Counties for Several States
Description
Spatial Polygons data frame with 475 counties from the US statesLouisiana, Texas, Mississippi, & Arkansas. FIPS codes for the state andcounty are provided in the data frame.
Matrix of Shared Boundaries Between US.df Counties
Description
A 475x475 matrix where the index corresponds to a row in theUS.df Spatial Polygons data frame and the index of the matrix at row i columnj is 1 when US.df[i,] and US.df[j,] share a border and 0 when they do not.
Simulate correlated data from a precision matrix.
Description
Takes in a square precision matrix, which ideally should besparse and using Choleski factorization simulates data from a mean 0 processwhere the inverse of the precision matrix represents the variance-covarianceof the points in the process. The resulting simulants represent samples of aGaussian Markov random field (GMRF).
Usage
sim.AR(n, Q)Arguments
n | int > 0, number of observations to simulate from the GMRF. |
Q | matrix, a square precision matrix. |
Value
Matrix object, matrix where each row is a single obsrevation froma GMRF with covariance structure Q^-1.
Examples
require("ggplot2")# simulate 2D ar1 process# pairwise correlationrho <- .95# pairwise variancesigma <- .5# 2 dimensions of simulationsyears <- 20ages <- 10# kronnecker product to get joint covarianceQ2D <- kronecker(Q.AR1(M=years, sigma, rho), Q.AR1(M=ages, sigma, rho))# simulate the data and place it in a data frameQ2D.df <- data.frame(obs=c(sim.AR(1, Q2D)), age=rep(1:ages, years), year=rep(1:years, each=ages))# graph resultsggplot(data=Q2D.df, aes(year, obs, group=age, color=age)) + geom_line()