\documentclass[11pt]{article}
% \VignetteIndexEntry{GRTS survey designs for a linear resource}
\author{Thomas Kincaid}
\title{GRTS Survey Designs for a Linear Resource}
\usepackage{Sweave}
\textwidth=6.5in
\textheight=9.0in
\oddsidemargin=.1in
\evensidemargin=.1in
\headheight=-.5in

\begin{document}
\maketitle
\tableofcontents
\setkeys{Gin}{width=1.0\textwidth}

This document presents example GRTS survey designs for a linear resource.  The linear resource used in the designs is streams that comprise the Luckiamute watershed in Oregon.  Four survey designs will be presented: (1) an unstratified, equal probability design; (2) a stratified, equal probability design with an oversample; (3) a stratified, unequal probability design with an oversample; and (4) a stratified, unequal probability design with an oversample and a panel structure for survey over time.  The sampling frame used for the survey designs is contained in either an ESRI shapefile or an sp package object.  The frame contains the coordinates for a set of line segments that define the linear resource in addition to attribute data associated with the line segments.  The coordinate system for the set of points in the sampling frame is an equal area projection rather than latitude and longitude.  An equal area projection is used so that calculation of distance between points is valid.

\section{Preliminaries}

The initial step is to use the library function to load the spsurvey package.  After the package is loaded, a message is printed to the R console indicating that the spsurvey package was loaded successfully.

Load the spsurvey package

<<preliminaries>>=
# Load the spsurvey package
library(spsurvey)

@
\begin{verbatim}
Version 2.1 of the spsurvey package was loaded successfully.
\end{verbatim}

\section{Shapefile attribute data}

The next step is to read the attribute data from the shapefile.  The read.dbf function in the spsurvey package is used to read the attribute (dbf) file in the shapefile and assign it to a data frame named att.  The initial six lines in the att data frame are printed using the head function

Next, two attributes, stream type and Strahler stream order, that will be used to define, respectively, stratum codes and unequal selection probability (multidensity) categories for the survey designs are examined.  Stream type is contained in a variable named "perint", and Strahler stream order is contained in a variable named "strahcat".  For stream type, streams are classified as either perennial or intermittent.  For Strahler stream order, streams are classified as either first order ("1st"), second order ("2nd"), or third order and higher ("3rd+").  The table and addmargin functions are used to produce a table displaying number of stream segments for each combination of values for the strata and multidensity category variables.

Finally, frame stream length is summarized for the strata and multidensity category attributes.  Note that stream length measured in kilometers is contained in the variable named "length\_km".  The tapply function is used to calculate total stream length for each combination of stream type and Strahler stream order.  The addmargins function is applied to the output from tapply to calculate stream length for each category of stream type and Strahler stream order, and the round function is used to round value to two decimal places.  Finally, the resulting cross-tabulation of sum of stream length in kilometers for Strahler stream order and stream type is displayed.

Read the attribute table from the shapefile

<<att>>=
# Read the attribute table from the shapefile
att <- read.dbf("luck_ash")

@
Display the initial six lines in the attribute data frame

<<att>>=
# Display the initial six lines in the attribute data frame
head(att)

@
Display number of stream segments cross-classified by the  strata and multidensity category variables

<<att>>=
# Display number of stream segments cross-classified by the  strata and
# multidensity category variables

addmargins(table("Stream Type"=att$perint, "Strahler Order"=att$strahcat))

@
Summarize frame stream length by stratum and multidensity category

<<att>>=
# Summarize frame stream length by stratum and multidensity category
temp <- tapply(att$length_km, list(att$perint, att$strahcat), sum)
temp <- round(addmargins(temp), 2)
names(dimnames(temp)) <- list("Stream Type", "Strahler Order")
temp

@
Streams in the Luckiamute watershed are displayed in Figure~\ref{fig:Linear1} classified by stream type and in Figure~\ref{fig:Linear2} classified by Strahler stream order category.  To produce the figure, first the read.shape function in the spsurvey package is used to read the shapefile and assign it to an object named shp.  The shp object takes the form of a spatial data object defined in the sp package.  Specifically, shp belongs to class "SpatialLinesDataFrame".  For further information about spatial data objects, see documentation for the sp package.  The spplot function in the sp package is used to create the figures.

Read the shapefile
\begin{verbatim}
> shp <- read.shape("luck_ash")
\end{verbatim}

Plot streams in the Luckiamute watershed classified by stream type
\begin{verbatim}
> spplot(shp, zcol="perint", col.regions=c("red", "blue"))
\end{verbatim}

Plot streams in the Luckiamute watershed classified by Strahler stream order
\begin{verbatim}
> spplot(shp, zcol="strahcat", col.regions=c("red", "green", "blue"))
\end{verbatim}

\begin{figure}
\centering

<<figure1, fig=TRUE, echo=FALSE>>=
# Read the shapefile
shp <- read.shape("luck_ash")
# Plot streams in the Luckiamute watershed classified by stream type
print(spplot(shp, zcol="perint", col.regions=c("red", "blue")))

@
\caption{Streams in the Luckiamute Watershed Classified by Stream Type.}
\label{fig:Linear1}
\end{figure}

\begin{figure}
\centering

<<figure2, fig=TRUE, echo=FALSE>>=
# Plot streams in the Luckiamute watershed classified by Strahler stream order
# category
print(spplot(shp, zcol="strahcat", col.regions=c("red", "green", "blue")))

@
\caption{Streams in the Luckiamute Watershed Classified by Strahler Stream Order Category.}
\label{fig:Linear2}
\end{figure}

\section{Unstratified, equal probability, GRTS survey design}

The first survey design is an unstratified, equal probability design.  The set.seed function is called so that, if necessary, the designs can be replicated.

The initial step is to create a list named Equaldsgn that contains information for specifying the survey design.  Since the survey design is unstratified, the list contains a single item named "None" that also is a list.  The "None" list includes two items: panel, which is used to specify the sample size for each panel, and seltype, which is used to input the type of random selection for the design.  For this example, panel is assigned a single value named "PanelOne" that is set equal to 50, and seltype is assigned the value "Equal", which indicates equal probability selection.

The grts function in the spsurvey package is called to select the survey design.  The following arguments are included in the call to grts: (1) design: the named list of stratum design specifications, which is assigned the Equaldsgn list; (2) DesignID: name for the design, which is used to create a site ID for each site and is assigned the value "EQUAL"; (3) type.frame: the type of frame, which is assigned the value "linear" to indicate a linear resource; (4) src.frame: source of the frame, which is assigned the value "shapefile" to indicate a shapefile frame; (5) in.shape: name of the input shapefile, which is assigned the value "luck\_ash"; (6) att.frame: the data frame of attributes associated with elements in the frame, which is assigned the att data frame; and (7) shapefile: option to create a shapefile containing the survey design information, which is assigned FALSE.

During execution of the grts function, messages are printed that indicate the initial number of hierarchical levels used for the GRTS grid, the current number of levels, and the final number of levels.  The set of messages is printed for each stratum, and is labeled with the stratum name.  For this example, the set of messages is labeled "None", i.e., the name used in the Equaldsgn list.  Upon completion of the call to grts, the initial six sites for the survey design and a design summary are printed.

Call the set.seed function so that the design can be replicated

<<Equalsites>>=
# Call the set.seed function so that the survey designs can be replicate
set.seed(19742003)

@
Create the design list

<<Equalsites>>=
# Create the design list
Equaldsgn <- list(None=list(panel=c(PanelOne=50), seltype="Equal"))

@
Select the sample
\begin{verbatim}
> Equalsites <- grts(design=Equaldsgn,
+                    DesignID="EQUAL",
+                    type.frame="linear",
+                    src.frame="shapefile",
+                    in.shape="luck_ash",
+                    att.frame=att,
+                    shapefile=FALSE)
\end{verbatim}

<<Equalsites, echo=FALSE>>=
# Select the sample
Equalsites <- grts(design=Equaldsgn,
                   DesignID="EQUAL",
                   type.frame="linear",
                   src.frame="shapefile",
                   in.shape="luck_ash",
                   att.frame=att,
                   shapefile=FALSE)

@
Print the initial six lines of the survey design

<<Equalsites>>=
# Print the initial six lines of the survey design
head(Equalsites@data)

@
Print the survey design summary

<<Equalsites>>=
# Print the survey design summary
dsgnsum(Equalsites)

@
\section{Stratified, equal probability, GRTS survey design with an oversample}

The second survey design is a stratified, equal probability design with an oversample.  The stream type attribute is used to identify strata.  List Stratdsgn is assigned design specifications.  Since the survey design is stratified, Stratdsgn includes two lists named "Perennial" and "Intermittent" that contains three items: panel, seltype, and over.  Note that the names for the two lists match the levels of the stratum variable.  For both lists, the values for panel and seltype are the same as the ones used for the equal probability design.  The third item, over, assigns the value 50 for size of the oversample.  An oversample provides additional sample sites to replace sites that cannot be used, e.g., to replace sites in the sample that are not accessible.

For this survey design, a shapefile will be used as the sampling frame.  The following arguments are included in the call to grts: (1) design: assigned the Stratdsgn list; (2) DesignID: assigned the value "STRATIFIED"; (3) type.frame: assigned the value "linear"; (4) src.frame: assigned the value "shapefile"; (5) in.shape: assigned the value "luck\_ash"; (6) att.frame: assigned the att data frame; (7) stratum: name of the column in the attributes data frame that identifies the stratum code for each element in the frame, which is assigned the value "perint"; and (8) shapefile: assigned the value FALSE.  Upon completion of the call to grts, the initial six sites for the survey design and a design summary are printed.

Create the design list
\begin{verbatim}
> Stratdsgn <- list(Perennial=list(panel=c(PanelOne=50),
+                                  seltype="Equal",
+                                  over=50),
+                   Intermittent=list(panel=c(PanelOne=50),
+                                     seltype="Equal",
+                                     over=50))
\end{verbatim}

<<Stratsites, echo=FALSE>>=
# Create the design list
Stratdsgn <- list(Perennial=list(panel=c(PanelOne=50),
                                 seltype="Equal",
                                 over=50),
                  Intermittent=list(panel=c(PanelOne=50),
                                    seltype="Equal",
                                    over=50))

@
Select the sample
\begin{verbatim}
> Stratsites <- grts(design=Stratdsgn,
+                    DesignID="STRATIFIED",
+                    type.frame="linear",
+                    src.frame="shapefile",
+                    in.shape="luck_ash",
+                    att.frame=att,
+                    stratum="perint",
+                    shapefile=FALSE)
\end{verbatim}

<<Stratsites, echo=FALSE>>=
# Select the sample
Stratsites <- grts(design=Stratdsgn,
                   DesignID="STRATIFIED",
                   type.frame="linear",
                   src.frame="shapefile",
                   in.shape="luck_ash",
                   att.frame=att,
                   stratum="perint",
                   shapefile=FALSE)

@
Print the initial six lines of the survey design

<<Stratsites>>=
# Print the initial six lines of the survey design
head(Stratsites@data)

@
Print the survey design summary

<<Stratsites>>=
# Print the survey design summary
dsgnsum(Stratsites)

@
\section{Stratified, unequal probability, GRTS survey design with an oversample}

The third survey design is a stratified, unequal probability design with an oversample.  As for the second survey design, the stream type attribute is used to identify strata.  Strahler order categories are used to identify multidensity categories.  List Unequaldsgn is assigned design specifications.  Unequaldsgn includes the same two lists with three items (panel, seltype, and over) as used for the stratified, equal probability design plus a value for caty.n.  For both lists, panel specifies a single panel, and seltype is assigned "Unequal" to indicate unequal probability sampling.  Note that the value 0 is assigned to over for the "Intermittent" stratum, i.e., no oversample.  The over item could have been omitted from the list for "Intermittent".  The vector assigned to caty.n specifies sample sizes for each of the three multidensity categories.  Note that the sum of values provided in caty.n must equal the value in panel.

For this survey design, an sp package object will be used as the sampling frame.  Recall that the read.shape function was used to read the shapefile and assign its output to an sp object named shp.  The following arguments are included in the call to grts: (1) design: assigned the Unequaldsgn list; (2) DesignID: assigned the value "UNEQUAL"; (3) type.frame: assigned the value "linear"; (4) src.frame: assigned the value "sp.object" to indicate that the sampling frame is provided by an sp object; (5) sp.object: name of the sp object, which is assigned the shp object; (6) att.frame: assigned the att data frame; (7) stratum: assigned the value "perint"; (8) mdcaty: name of the column in the attributes data frame that identifies the unequal probability category for each element in the frame, which is assigned the value "strahcat"; (9) shapefile: assigned the value FALSE.  Upon completion of the call to grts, the initial six sites for the survey design and a design summary are printed.

Create the design list
\begin{verbatim}
> Unequaldsgn <- list(Perennial=list(panel=c(PanelOne=75),
+                                    seltype="Unequal",
+                                    caty.n=c("1st"=25, "2nd"=25, "3rd+"=25),
+                                    over=36),
+                     Intermittent=list(panel=c(PanelOne=32),
+                                       seltype="Unequal",
+                                       caty.n=c("1st"=25, "2nd"=5, "3rd+"=2),
+                                       over=0))
\end{verbatim}

<<Unequalsites, echo=FALSE>>=
# Create the design list
Unequaldsgn <- list(Perennial=list(panel=c(PanelOne=75),
                                   seltype="Unequal",
                                   caty.n=c("1st"=25, "2nd"=25, "3rd+"=25),
                                   over=36),
                    Intermittent=list(panel=c(PanelOne=32),
                                      seltype="Unequal",
                                      caty.n=c("1st"=25, "2nd"=5, "3rd+"=2),
                                      over=0))

@
Select the sample
\begin{verbatim}
> Unequalsites <- grts(design=Unequaldsgn,
+                      DesignID="UNEQUAL",
+                      type.frame="linear",
+                      src.frame="sp.object",
+                      sp.object=shp,
+                      att.frame=att,
+                      stratum="perint",
+                      mdcaty="strahcat",
+                      shapefile=FALSE)
\end{verbatim}

<<Unequalsites, echo=FALSE>>=
# Select the sample
Unequalsites <- grts(design=Unequaldsgn,
                     DesignID="UNEQUAL",
                     type.frame="linear",
                     src.frame="sp.object",
                     sp.object=shp,
                     att.frame=att,
                     stratum="perint",
                     mdcaty="strahcat",
                     shapefile=FALSE)

@
Print the initial six lines of the survey design

<<Unequalsites>>=
# Print the initial six lines of the survey design
head(Unequalsites@data)

@
Print the survey design summary

<<Unequalsites>>=
# Print the survey design summary
dsgnsum(Unequalsites)

@
\section{Stratified, unequal probability, GRTS survey design with an oversample and a panel structure for survey over time}

The fourth survey design is a stratified, unequal probability design with an oversample and a panel structure for survey over time.  List Paneldsgn is assigned design specifications.  Analogous to the stratified, unequal probability design, Paneldsgn includes two lists named "Perennial" and "Intermittent".  For the "Perennial" stratum, a vector identifying sample sizes for three panels is assigned to panel.  For the "Intermittent" stratum, the sample size for a single panel named "YearOnce" is assigned to panel.  The value "Unequal" is assigned to seltype for both lists, which indicates unequal selection probabilities.  For both lists, the third item, caty.n, assigns sample sizes for each of the three multidensity categories.  Again, note that the sum of sample sizes provided in caty.n must equal the sum of sample sizes in panel.  For the "Perennial" stratum, the value 50 is assigned to over, which specifies an oversample of 50 sites.  No oversample is specified for the "Intermittent" stratum, and so over is not included in the list.  The grts function attempts to distribute the oversample proportionately among sample sizes for the multidensity categories.  If the oversample proportion for one or more categories is not a whole number, a warning message is printed and the proportion is rounded to the next higher integer.  For the "Perennial" stratum, the oversample is not proportionate to the multidensity category sample sizes, and the warning message is printed by calling the warnings function.

For this survey design, a shapefile will be used as the sampling frame.  The following arguments are included in the call to grts: (1) design: assigned the Paneldsgn list; (2) DesignID: assigned the value "UNEQUAL"; (3) type.frame: assigned the value "linear"; (4) src.frame: assigned the value "shapefile"; (5) in.shape: assigned the value "luck\_ash"; (6) att.frame: assigned the att data frame; 7) stratum: assigned the value "perint"; (8) mdcaty: assigned the value "strahcat"; (9) shapefile: assigned the value FALSE.  Upon completion of the call to grts, the initial six sites for the survey design and a design summary are printed.

Create the design list
\begin{verbatim}
> Paneldsgn <- list(Perennial=list(panel=c(Year1=17, Year2=17, YearAll=16),
+                                  seltype="Unequal",
+                                  caty.n=c("1st"=15, "2nd"=15, "3rd+"=20),
+                                  over=50),
+                   Intermittent=list(panel=c(YearOnce=27),
+                                     seltype="Unequal",
+                                     caty.n=c("1st"=20, "2nd"=5, "3rd+"=2)))
\end{verbatim}

<<Panelsites, echo=FALSE>>=
# Create the design list
Paneldsgn <- list(Perennial=list(panel=c(Year1=17, Year2=17, YearAll=16),
                                 seltype="Unequal",
                                 caty.n=c("1st"=15, "2nd"=15, "3rd+"=20),
                                 over=50),
                  Intermittent=list(panel=c(YearOnce=27),
                                    seltype="Unequal",
                                    caty.n=c("1st"=20, "2nd"=5, "3rd+"=2)))

@
Select the sample
\begin{verbatim}
> Panelsites <- grts(design=Paneldsgn,
+                    DesignID="UNEQUAL",
+                    type.frame="linear",
+                    src.frame="shapefile",
+                    in.shape="luck_ash",
+                    att.frame=att,
+                    stratum="perint",
+                    mdcaty="strahcat",
+                    shapefile=FALSE)
\end{verbatim}

<<Panelsites, echo=FALSE>>=
# Select the sample
Panelsites <- grts(design=Paneldsgn,
                   DesignID="UNEQUAL",
                   type.frame="linear",
                   src.frame="shapefile",
                   in.shape="luck_ash",
                   att.frame=att,
                   stratum="perint",
                   mdcaty="strahcat",
                   shapefile=FALSE)

@
Print the warning message

<<Panelsites, eval=FALSE>>=
# Print the warning message
warnings()

@
\begin{verbatim}
Warning message:
In grts(design = Paneldsgn, DesignID = "UNEQUAL", type.frame = "linear",  :
  
Oversample size is not proportional to category sample sizes for stratum
"Perennial".
\end{verbatim}

Print the initial six lines of the survey design

<<Panelsites>>=
# Print the initial six lines of the survey design
head(Panelsites@data)

@
Print the survey design summary

<<Panelsites>>=
# Print the survey design summary
dsgnsum(Panelsites)

@
\end{document}
