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