]> git.donarmstrong.com Git - xtable.git/blob - pkg/vignettes/OtherPackagesGallery.Rnw
145982fe6af3e64a36d760816ef28b5f6b38dd9e
[xtable.git] / pkg / vignettes / OtherPackagesGallery.Rnw
1 %\VignetteIndexEntry{xtable List of Tables Gallery}\r
2 %\VignetteDepends{xtable, spdep, splm, sphet}\r
3 %\VignetteKeywords{LaTeX, HTML, table}\r
4 %\VignettePackage{xtable}\r
5 % !Rnw weave = knitr\r
6 % \VignetteEngine{knitr::knitr}\r
7 %**************************************************************************\r
8 \documentclass{article}\r
9 \usepackage[a4paper, height=24cm]{geometry} % geometry first\r
10 \usepackage{array}\r
11 \usepackage{booktabs}\r
12 \usepackage{longtable}\r
13 \usepackage{parskip}\r
14 \usepackage{rotating}\r
15 \usepackage{tabularx}\r
16 \usepackage{titlesec}\r
17 \usepackage{hyperref} % hyperref last\r
18 \titleformat\subsubsection{\bfseries\itshape}{}{0pt}{}\r
19 \newcommand\p{\vspace{2ex}}\r
20 \newcommand\code[1]{\texttt{#1}}\r
21 \newcommand\pkg[1]{\textbf{#1}}\r
22 \setcounter{tocdepth}{2}\r
23 \begin{document}\r
24 \r
25 \title{\bfseries\Large The Other Packages Gallery}\r
26 \author{\bfseries David J. Scott}\r
27 \maketitle\r
28 \r
29 \tableofcontents\r
30 \r
31 \newpage\r
32 \r
33 \section{Introduction}\r
34 This document represents a test of the functions in \pkg{xtable} which\r
35 deal with other packages.\r
36 \r
37 <<set, include=FALSE>>=\r
38 library(knitr)\r
39 opts_chunk$set(fig.path = 'Figures/other', debug = TRUE, echo = TRUE)\r
40 opts_chunk$set(out.width = '0.9\\textwidth')\r
41 @\r
42 \r
43 The first step is to load the package and set some options for this document.\r
44 <<package, results='asis'>>=\r
45 library(xtable)\r
46 options(xtable.floating = FALSE)\r
47 options(xtable.timestamp = "")\r
48 options(width = 60)\r
49 @\r
50 \r
51 \section{The packages \pkg{spdep}, \pkg{splm}, and \pkg{sphet}}\r
52 \r
53 Code for supporting these packages and most of the examples used in\r
54 this section was originally provided by Martin Gubri\r
55 (\url{martin.gubri@framasoft.org}).\r
56 \r
57 \subsection{The package \pkg{spdep}}\r
58 \label{sec:package-pkgspdep}\r
59 \r
60 First load the package and create some objects.\r
61 <<dataspdep>>=\r
62 library(spdep)\r
63 data("oldcol", package = "spdep")\r
64 COL.lag.eig <- lagsarlm(CRIME ~ INC + HOVAL, data = COL.OLD[],\r
65                         nb2listw(COL.nb))\r
66 class(COL.lag.eig)\r
67 COL.errW.GM <- GMerrorsar(CRIME ~ INC + HOVAL, data = COL.OLD,\r
68                           nb2listw(COL.nb, style = "W"),\r
69                           returnHcov = TRUE)\r
70 class(COL.errW.GM)\r
71 COL.lag.stsls <- stsls(CRIME ~ INC + HOVAL, data = COL.OLD,\r
72                        nb2listw(COL.nb))\r
73 class(COL.lag.stsls)\r
74 \r
75 p1 <- predict(COL.lag.eig, newdata = COL.OLD[45:49,],\r
76               listw = nb2listw(COL.nb))\r
77 class(p1)\r
78 p2 <- predict(COL.lag.eig, newdata = COL.OLD[45:49,],\r
79               pred.type = "trend", type = "trend")\r
80 #type option for retrocompatibility with spdep 0.5-92\r
81 class(p2)\r
82 \r
83 imp.exact <- impacts(COL.lag.eig, listw = nb2listw(COL.nb))\r
84 class(imp.exact)\r
85 imp.sim <- impacts(COL.lag.eig, listw = nb2listw(COL.nb), R = 200)\r
86 class(imp.sim)\r
87 @ %def\r
88 \r
89 \r
90 \subsubsection{\code{sarlm} objects}\r
91 \label{sec:codesarlm-objects}\r
92 \r
93 There is an \code{xtable} method for objects of this type.\r
94 <<xtablesarlm, results = 'asis'>>=\r
95 xtable(COL.lag.eig)\r
96 @ %def\r
97 \r
98 The method for \code{xtable} actually uses the summary of the object,\r
99 and an identical result is obtained when using the summary of the\r
100 object, even if the summary contains more additional information.\r
101 \r
102 <<xtablesarlmsumm, results = 'asis'>>=\r
103 xtable(summary(COL.lag.eig, correlation = TRUE))\r
104 @ %def\r
105 \r
106 This same pattern applies to the other objects from this group of packages.\r
107 \r
108 Note that additional prettying of the resulting table is possible, as\r
109 for any table produced using \code{xtable}. For example using the\r
110 \pkg{booktabs} package we get:\r
111 \r
112 <<xtablesarlmbooktabs, results = 'asis'>>=\r
113 print(xtable(COL.lag.eig), booktabs = TRUE)\r
114 @ %def\r
115 \r
116 \subsubsection{\code{gmsar} objects}\r
117 \label{sec:codegmsar-objects}\r
118 \r
119 \r
120 <<xtablegmsar, results = 'asis'>>=\r
121 xtable(COL.errW.GM)\r
122 @ %def\r
123 \r
124 \subsubsection{\code{stsls} objects}\r
125 \label{sec:codestsls-objects}\r
126 \r
127 \r
128 <<xtablestsls, results = 'asis'>>=\r
129 xtable(COL.lag.stsls)\r
130 @ %def\r
131 \r
132 \subsubsection{\code{sarlm.pred} objects}\r
133 \label{sec:codesarlmpred-objects}\r
134 \r
135 \code{xtable} has a method for predictions of \code{sarlm} models.\r
136 \r
137 <<xtablesarlmpred, results = 'asis'>>=\r
138 xtable(p1)\r
139 @ %def\r
140 \r
141 This method transforms the \code{sarlm.pred} objects into data frames, allowing any number of attributes vectors which may vary according to predictor types.\r
142 \r
143 <<xtablesarlmpred2, results = 'asis'>>=\r
144 xtable(p2)\r
145 @ %def\r
146 \r
147 \subsubsection{\code{lagImpact} objects}\r
148 \label{sec:codelagimpact-objects}\r
149 \r
150 The \code{xtable} method returns the values of direct, indirect and total impacts for all the variables in the model. The class \code{lagImpact} have two different sets of attributes according to if simulations are used. But the \code{xtable} method always returns the three components of the non-simulation case.\r
151 \r
152 <<xtablelagimpactexact, results = 'asis'>>=\r
153 xtable(imp.exact)\r
154 @ %def\r
155 \r
156 \p\r
157 <<xtablelagimpactmcmc, results = 'asis'>>=\r
158 xtable(imp.sim)\r
159 @ %def\r
160 \r
161 \r
162 \subsubsection{\code{spautolm} objects}\r
163 \label{sec:codespautolm-objects}\r
164 \r
165 The need for an \code{xtable} method for \code{spautolm} was expressed\r
166 by Guido Schulz (\url{schulzgu@student.hu-berlin.de}), who also\r
167 provided an example of an object of this type. The required code was\r
168 implemented by David Scott (\url{d.scott@auckland.ac.nz}).\r
169 \r
170 First create an object of the required type.\r
171 \r
172 <<minimalexample, results = 'hide'>>=\r
173 library(spdep)\r
174 example(NY_data)\r
175 spautolmOBJECT <- spautolm(Z ~ PEXPOSURE + PCTAGE65P,data = nydata,\r
176                            listw = listw_NY, family = "SAR",\r
177                            method = "eigen", verbose = TRUE)\r
178 summary(spautolmOBJECT, Nagelkerke = TRUE)\r
179 @ %def\r
180 \r
181 \p\r
182 <<spautolmclass>>=\r
183 class(spautolmOBJECT)\r
184 @ %def\r
185 \r
186 \r
187 <<xtablespautolm, results = 'asis'>>=\r
188 xtable(spautolmOBJECT,\r
189        display = c("s",rep("f", 3), "e"), digits = 4)\r
190 @ %def\r
191 \r
192 \r
193 \r
194 \subsection{The package \pkg{splm}}\r
195 \label{sec:package-pkgsplm}\r
196 \r
197 First load the package and create some objects.\r
198 <<datasplm>>=\r
199 library(splm)\r
200 data("Produc", package = "plm")\r
201 data("usaww",  package = "splm")\r
202 fm <- log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp\r
203 respatlag <- spml(fm, data = Produc, listw = mat2listw(usaww),\r
204                    model="random", spatial.error="none", lag=TRUE)\r
205 class(respatlag)\r
206 GM <- spgm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp, data = Produc,\r
207            listw = usaww, moments = "fullweights", spatial.error = TRUE)\r
208 class(GM)\r
209 \r
210 imp.spml <- impacts(respatlag, listw = mat2listw(usaww, style = "W"), time = 17)\r
211 class(imp.spml)\r
212 @ %def\r
213 \r
214 \r
215 \subsubsection{\code{splm} objects}\r
216 \label{sec:codesplm-objects}\r
217 \r
218 <<xtablesplm, results = 'asis'>>=\r
219 xtable(respatlag)\r
220 @ %def\r
221 \r
222 \r
223 \p\r
224 <<xtablesplm1, results = 'asis'>>=\r
225 xtable(GM)\r
226 @ %def\r
227 \r
228 The \code{xtable} method works the same on impacts of \code{splm} models.\r
229 \r
230 <<xtablesplmimpacts, results = 'asis'>>=\r
231 xtable(imp.spml)\r
232 @ %def\r
233 \r
234 \subsection{The package \pkg{sphet}}\r
235 \label{sec:package-pkgsphet}\r
236 \r
237 First load the package and create some objects.\r
238 <<datasphet>>=\r
239 library(sphet)\r
240 data("columbus", package = "spdep")\r
241 listw <- nb2listw(col.gal.nb)\r
242 data("coldis", package = "sphet")\r
243 res.stsls <- stslshac(CRIME ~ HOVAL + INC, data = columbus, listw = listw,\r
244                       distance = coldis, type = 'Triangular')\r
245 class(res.stsls)\r
246 \r
247 res.gstsls <- gstslshet(CRIME ~ HOVAL + INC, data = columbus, listw = listw)\r
248 class(res.gstsls)\r
249 \r
250 imp.gstsls <- impacts(res.gstsls, listw = listw)\r
251 class(imp.gstsls)\r
252 @ %def\r
253 \r
254 \r
255 \subsubsection{\code{sphet} objects}\r
256 \label{sec:codesphet-objects}\r
257 \r
258 <<xtablesphet, results = 'asis'>>=\r
259 xtable(res.stsls)\r
260 @ %def\r
261 \r
262 \p\r
263 <<xtablesphet1, results = 'asis'>>=\r
264 xtable(res.gstsls)\r
265 @ %def\r
266 \r
267 \code{sphet} also provides a method for computing impacts.\r
268 \r
269 <<xtablesphetimpacts, results = 'asis'>>=\r
270 xtable(imp.gstsls)\r
271 @ %def\r
272 \r
273 \end{document}\r