]> git.donarmstrong.com Git - xtable.git/blob - pkg/vignettes/OtherPackagesGallery.Rnw
fff3c4073c2e1869dd96b5750be7f0dd84bff401
[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 set.seed(1234)\r
50 @\r
51 \r
52 \section{The packages \pkg{spdep}, \pkg{splm}, and \pkg{sphet}}\r
53 \r
54 Code for supporting these packages and most of the examples used in\r
55 this section was originally provided by Martin Gubri\r
56 (\url{martin.gubri@framasoft.org}).\r
57 \r
58 \subsection{The package \pkg{spdep}}\r
59 \label{sec:package-pkgspdep}\r
60 \r
61 First load the package and create some objects.\r
62 <<dataspdep>>=\r
63 library(spdep)\r
64 data("oldcol", package = "spdep")\r
65 COL.lag.eig <- lagsarlm(CRIME ~ INC + HOVAL, data = COL.OLD[],\r
66                         nb2listw(COL.nb))\r
67 class(COL.lag.eig)\r
68 COL.errW.GM <- GMerrorsar(CRIME ~ INC + HOVAL, data = COL.OLD,\r
69                           nb2listw(COL.nb, style = "W"),\r
70                           returnHcov = TRUE)\r
71 class(COL.errW.GM)\r
72 COL.lag.stsls <- stsls(CRIME ~ INC + HOVAL, data = COL.OLD,\r
73                        nb2listw(COL.nb))\r
74 class(COL.lag.stsls)\r
75 \r
76 p1 <- predict(COL.lag.eig, newdata = COL.OLD[45:49,],\r
77               listw = nb2listw(COL.nb))\r
78 class(p1)\r
79 p2 <- predict(COL.lag.eig, newdata = COL.OLD[45:49,],\r
80               pred.type = "trend", type = "trend")\r
81 #type option for retrocompatibility with spdep 0.5-92\r
82 class(p2)\r
83 \r
84 imp.exact <- impacts(COL.lag.eig, listw = nb2listw(COL.nb))\r
85 class(imp.exact)\r
86 imp.sim <- impacts(COL.lag.eig, listw = nb2listw(COL.nb), R = 200)\r
87 class(imp.sim)\r
88 @ %def\r
89 \r
90 \r
91 \subsubsection{\code{sarlm} objects}\r
92 \label{sec:codesarlm-objects}\r
93 \r
94 There is an \code{xtable} method for objects of this type.\r
95 <<xtablesarlm, results = 'asis'>>=\r
96 xtable(COL.lag.eig)\r
97 @ %def\r
98 \r
99 The method for \code{xtable} actually uses the summary of the object,\r
100 and an identical result is obtained when using the summary of the\r
101 object, even if the summary contains more additional information.\r
102 \r
103 <<xtablesarlmsumm, results = 'asis'>>=\r
104 xtable(summary(COL.lag.eig, correlation = TRUE))\r
105 @ %def\r
106 \r
107 This same pattern applies to the other objects from this group of packages.\r
108 \r
109 Note that additional prettying of the resulting table is possible, as\r
110 for any table produced using \code{xtable}. For example using the\r
111 \pkg{booktabs} package we get:\r
112 \r
113 <<xtablesarlmbooktabs, results = 'asis'>>=\r
114 print(xtable(COL.lag.eig), booktabs = TRUE)\r
115 @ %def\r
116 \r
117 \subsubsection{\code{gmsar} objects}\r
118 \label{sec:codegmsar-objects}\r
119 \r
120 \r
121 <<xtablegmsar, results = 'asis'>>=\r
122 xtable(COL.errW.GM)\r
123 @ %def\r
124 \r
125 \subsubsection{\code{stsls} objects}\r
126 \label{sec:codestsls-objects}\r
127 \r
128 \r
129 <<xtablestsls, results = 'asis'>>=\r
130 xtable(COL.lag.stsls)\r
131 @ %def\r
132 \r
133 \subsubsection{\code{sarlm.pred} objects}\r
134 \label{sec:codesarlmpred-objects}\r
135 \r
136 \code{xtable} has a method for predictions of \code{sarlm} models.\r
137 \r
138 <<xtablesarlmpred, results = 'asis'>>=\r
139 xtable(p1)\r
140 @ %def\r
141 \r
142 This method transforms the \code{sarlm.pred} objects into data frames,\r
143 allowing any number of attributes vectors which may vary according to\r
144 predictor types.\r
145 \r
146 <<xtablesarlmpred2, results = 'asis'>>=\r
147 xtable(p2)\r
148 @ %def\r
149 \r
150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
151 %% xtable.lagImpactMat removed because of problems with unexported function\r
152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
153 \r
154 %% \subsubsection{\code{lagImpact} objects}\r
155 %% \label{sec:codelagimpact-objects}\r
156 \r
157 %% The \code{xtable} method returns the values of direct, indirect and\r
158 %% total impacts for all the variables in the model. The class\r
159 %% \code{lagImpact} have two different sets of attributes according to if\r
160 %% simulations are used. But the \code{xtable} method always returns the\r
161 %% three components of the non-simulation case.\r
162 \r
163 %% <<xtablelagimpactexact, results = 'asis'>>=\r
164 %% xtable(imp.exact)\r
165 %% @ %def\r
166 \r
167 %% \p\r
168 %% <<xtablelagimpactmcmc, results = 'asis'>>=\r
169 %% xtable(imp.sim)\r
170 %% @ %def\r
171 \r
172 \r
173 \subsubsection{\code{spautolm} objects}\r
174 \label{sec:codespautolm-objects}\r
175 \r
176 The need for an \code{xtable} method for \code{spautolm} was expressed\r
177 by Guido Schulz (\url{schulzgu@student.hu-berlin.de}), who also\r
178 provided an example of an object of this type. The required code was\r
179 implemented by David Scott (\url{d.scott@auckland.ac.nz}).\r
180 \r
181 First create an object of the required type.\r
182 \r
183 <<minimalexample, results = 'hide'>>=\r
184 library(spdep)\r
185 example(NY_data)\r
186 spautolmOBJECT <- spautolm(Z ~ PEXPOSURE + PCTAGE65P,data = nydata,\r
187                            listw = listw_NY, family = "SAR",\r
188                            method = "eigen", verbose = TRUE)\r
189 summary(spautolmOBJECT, Nagelkerke = TRUE)\r
190 @ %def\r
191 \r
192 \p\r
193 <<spautolmclass>>=\r
194 class(spautolmOBJECT)\r
195 @ %def\r
196 \r
197 \r
198 <<xtablespautolm, results = 'asis'>>=\r
199 xtable(spautolmOBJECT,\r
200        display = c("s",rep("f", 3), "e"), digits = 4)\r
201 @ %def\r
202 \r
203 \r
204 \r
205 \subsection{The package \pkg{splm}}\r
206 \label{sec:package-pkgsplm}\r
207 \r
208 First load the package and create some objects.\r
209 <<datasplm>>=\r
210 library(splm)\r
211 data("Produc", package = "plm")\r
212 data("usaww",  package = "splm")\r
213 fm <- log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp\r
214 respatlag <- spml(fm, data = Produc, listw = mat2listw(usaww),\r
215                    model="random", spatial.error="none", lag=TRUE)\r
216 class(respatlag)\r
217 GM <- spgm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp, data = Produc,\r
218            listw = usaww, moments = "fullweights", spatial.error = TRUE)\r
219 class(GM)\r
220 \r
221 imp.spml <- impacts(respatlag, listw = mat2listw(usaww, style = "W"), time = 17)\r
222 class(imp.spml)\r
223 @ %def\r
224 \r
225 \r
226 \subsubsection{\code{splm} objects}\r
227 \label{sec:codesplm-objects}\r
228 \r
229 <<xtablesplm, results = 'asis'>>=\r
230 xtable(respatlag)\r
231 @ %def\r
232 \r
233 \r
234 \p\r
235 <<xtablesplm1, results = 'asis'>>=\r
236 xtable(GM)\r
237 @ %def\r
238 \r
239 \r
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
241 %% xtable.lagImpactMat removed because of problems with unexported function\r
242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
243 \r
244 %% The \code{xtable} method works the same on impacts of \code{splm} models.\r
245 \r
246 %% <<xtablesplmimpacts, results = 'asis'>>=\r
247 %% xtable(imp.spml)\r
248 %% @ %def\r
249 \r
250 \subsection{The package \pkg{sphet}}\r
251 \label{sec:package-pkgsphet}\r
252 \r
253 First load the package and create some objects.\r
254 <<datasphet>>=\r
255 library(sphet)\r
256 data("columbus", package = "spdep")\r
257 listw <- nb2listw(col.gal.nb)\r
258 data("coldis", package = "sphet")\r
259 res.stsls <- stslshac(CRIME ~ HOVAL + INC, data = columbus, listw = listw,\r
260                       distance = coldis, type = 'Triangular')\r
261 class(res.stsls)\r
262 \r
263 res.gstsls <- gstslshet(CRIME ~ HOVAL + INC, data = columbus, listw = listw)\r
264 class(res.gstsls)\r
265 \r
266 imp.gstsls <- impacts(res.gstsls, listw = listw)\r
267 class(imp.gstsls)\r
268 @ %def\r
269 \r
270 \r
271 \subsubsection{\code{sphet} objects}\r
272 \label{sec:codesphet-objects}\r
273 \r
274 <<xtablesphet, results = 'asis'>>=\r
275 xtable(res.stsls)\r
276 @ %def\r
277 \r
278 \p\r
279 <<xtablesphet1, results = 'asis'>>=\r
280 xtable(res.gstsls)\r
281 @ %def\r
282 \r
283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
284 %% xtable.lagImpactMat removed because of problems with unexported function\r
285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
286 %% \code{sphet} also provides a method for computing impacts.\r
287 \r
288 %% <<xtablesphetimpacts, results = 'asis'>>=\r
289 %% xtable(imp.gstsls)\r
290 %% @ %def\r
291 \r
292 \section{The \pkg{zoo} package}\r
293 \label{sec:pkgzoo-package}\r
294 \r
295 \r
296 <<zoo, results = 'asis'>>=\r
297 library(zoo)\r
298 xDate <- as.Date("2003-02-01") + c(1, 3, 7, 9, 14) - 1\r
299 as.ts(xDate)\r
300 x <- zoo(rnorm(5), xDate)\r
301 xtable(x)\r
302 @ %def\r
303 \r
304 \r
305 \p\r
306 \r
307 <<zoots, results = 'asis'>>=\r
308 tempTs <- ts(cumsum(1 + round(rnorm(100), 0)),\r
309               start = c(1954, 7), frequency = 12)\r
310 tempTable <- xtable(tempTs, digits = 0)\r
311 tempTable\r
312 tempZoo <- as.zoo(tempTs)\r
313 xtable(tempZoo, digits = 0)\r
314 @ %def\r
315 \r
316 \r
317 \section{The \pkg{survival} package}\r
318 \label{sec:pkgsurvival-package}\r
319 \r
320 \r
321 <<survival, results = 'asis'>>=\r
322 library(survival)\r
323 test1 <- list(time=c(4,3,1,1,2,2,3),\r
324               status=c(1,1,1,0,1,1,0),\r
325               x=c(0,2,1,1,1,0,0),\r
326               sex=c(0,0,0,0,1,1,1))\r
327 coxFit <- coxph(Surv(time, status) ~ x + strata(sex), test1)\r
328 xtable(coxFit)\r
329 @ %def\r
330 \r
331 \end{document}\r