| 1 |
\name{Memisc} |
| 2 |
\docType{package} |
| 3 |
\alias{memisc-package} |
| 4 |
\alias{memisc} |
| 5 |
\title{Introduction to the 'memisc' Package} |
| 6 |
\description{This package collects an assortment of tools that are intended to make |
| 7 |
work with \code{R} easier for the author of this package |
| 8 |
and are submitted to the public in the hope that they will be also be useful to others. |
| 9 |
|
| 10 |
The tools in this package can be grouped into four major categories: |
| 11 |
\itemize{ |
| 12 |
\item Data preparation and management |
| 13 |
\item Data analysis |
| 14 |
\item Presentation of analysis results |
| 15 |
\item Programming |
| 16 |
} |
| 17 |
} |
| 18 |
\section{Data preparation and management}{ |
| 19 |
\subsection{Survey Items}{ |
| 20 |
\code{memisc} provides facilities to work with what users from other |
| 21 |
packages like SPSS, SAS, or Stata know as `variable labels', `value labels' |
| 22 |
and `user-defined missing values'. In the context of this package these |
| 23 |
aspects of the data are represented by the \code{"description"}, |
| 24 |
\code{"labels"}, and \code{"missing.values"} attributes of a |
| 25 |
data vector. |
| 26 |
These facilities are useful, for example, if you work with |
| 27 |
survey data that contain coded items like vote intention that |
| 28 |
may have the following structure: |
| 29 |
|
| 30 |
Question: ``If there was a parliamentary election next tuesday, which party would you vote for?'' |
| 31 |
\tabular{rl}{ |
| 32 |
1 \tab Conservative Party \cr |
| 33 |
2 \tab Labour Party \cr |
| 34 |
3 \tab Liberal Democrat Party \cr |
| 35 |
4 \tab Scottish Nation Party \cr |
| 36 |
5 \tab Plaid Cymru \cr |
| 37 |
6 \tab Green Party \cr |
| 38 |
7 \tab British National Party \cr |
| 39 |
8 \tab Other party \cr |
| 40 |
96 \tab Not allowed to vote \cr |
| 41 |
97 \tab Would not vote \cr |
| 42 |
98 \tab Would vote, do not know yet for which party \cr |
| 43 |
99 \tab No answer |
| 44 |
} |
| 45 |
A statistical package like SPSS allows to |
| 46 |
attach labels like `Conservative Party', `Labour Party', etc. |
| 47 |
to the codes 1,2,3, etc. and to mark |
| 48 |
mark the codes 96, 97, 98, 99 |
| 49 |
as `missing' and thus to exclude these variables from statistical |
| 50 |
analyses. \code{memisc} provides similar facilities. |
| 51 |
Labels can be attached to codes by calls like \code{\link{labels}(x) <- something} |
| 52 |
and expendanded by calls like \code{\link{labels}(x) <- \link{labels}(x) + something}, |
| 53 |
codes can be marked as `missing' by |
| 54 |
calls like \code{\link{missing.values}(x) <- something} and |
| 55 |
\code{\link{missing.values}(x) <- \link{missing.values}(x) + something}. |
| 56 |
|
| 57 |
\code{memisc} defines a class called "data.set", which is similar to the class "data.frame". |
| 58 |
The main difference is that it is especially geared toward containing survey item data. |
| 59 |
Transformations of and within "data.set" objects retain the information about |
| 60 |
value labels, missing values etc. Using \code{as.data.frame} sets the data up for |
| 61 |
\emph{R}'s statistical functions, but doing this explicitely is seldom necessary. |
| 62 |
See \code{\link{data.set}}. |
| 63 |
} |
| 64 |
\subsection{More Convenient Import of External Data}{ |
| 65 |
Survey data sets are often relative large and contain up to a few thousand variables. |
| 66 |
For specific analyses one needs however only a relatively small subset of these variables. |
| 67 |
Although modern computers have enough RAM to load such data sets completely into an R session, |
| 68 |
this is not very efficient having to drop most of the variables after loading. Also, loading |
| 69 |
such a large data set completely can be time-consuming, because R has to allocate space for |
| 70 |
each of the many variables. Loading just the subset of variables really needed for an analysis |
| 71 |
is more efficient and convenient - it tends to be much quicker. Thus this package provides |
| 72 |
facilities to load such subsets of variables, without the need to load a complete data set. |
| 73 |
Further, the loading of data from SPSS files is organized in such a way that all informations |
| 74 |
about variable labels, value labels, and user-defined missing values are retained. |
| 75 |
This is made possible by the definition of \code{\link{importer}} objects, for which |
| 76 |
a \code{\link{subset}} method exists. \code{\link{importer}} objects contain only |
| 77 |
the information about the variables in the external data set but not the data. |
| 78 |
The data itself is loaded into memory when the functions \code{subset} or \code{\link{as.data.set}} |
| 79 |
are used. |
| 80 |
} |
| 81 |
\subsection{Recoding}{ |
| 82 |
\code{memisc} also contains facilities for recoding |
| 83 |
survey items. Simple recodings, for example collapsing answer |
| 84 |
categories, can be done using the function \code{\link{recode}}. More |
| 85 |
complex recodings, for example the construction of indices from |
| 86 |
multiple items, and complex case distinctions, can be done |
| 87 |
using the function \code{\link{cases}}. This function may also |
| 88 |
be useful for programming, in so far as it is a generalization of |
| 89 |
\code{\link{ifelse}}. |
| 90 |
} |
| 91 |
\subsection{Code Books}{ |
| 92 |
There is a function \code{\link{codebook}} which produces a code book of an |
| 93 |
external data set or an internal "data.set" object. A codebook contains in a |
| 94 |
conveniently formatted way concise information about every variable in a data set, |
| 95 |
such as which value labels and missing values are defined and some univariate statistics. |
| 96 |
|
| 97 |
An extended example of all these facilities is contained in the vignette "anes48", |
| 98 |
and in \code{demo(anes48)} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
\section{Data Analysis}{ |
| 103 |
\subsection{Tables and Data Frames of Descriptive Statistics}{ |
| 104 |
\code{\link{genTable}} is a generalization of \code{\link{xtabs}}: |
| 105 |
Instead of counts, also descriptive statistics like means or variances |
| 106 |
can be reported conditional on levels of factors. Also conditional |
| 107 |
percentages of a factor can be obtained using this function. |
| 108 |
|
| 109 |
In addition an \code{Aggregate} |
| 110 |
function is provided, which has the same syntax as \code{genTable}, but |
| 111 |
gives a data frame of descriptive statistics instead of a \code{table} |
| 112 |
object. |
| 113 |
} |
| 114 |
\subsection{Per-Subset Analysis}{ |
| 115 |
\code{\link{By}} is a variant of the |
| 116 |
standard function \code{\link[base]{by}}: Conditioning factors |
| 117 |
are specified by a formula and are |
| 118 |
obtained from the data frame the subsets of which are to be analysed. |
| 119 |
Therefore there is no need to \code{\link{attach}} the data frame |
| 120 |
or to use the dollar operator. |
| 121 |
} |
| 122 |
} |
| 123 |
\section{Presentation of Results of Statistical Analysis}{ |
| 124 |
\subsection{Publication-Ready Tables of Coefficients}{ |
| 125 |
|
| 126 |
Journals of the Political and Social Sciences usually require |
| 127 |
that estimates of regression models are presented in the following |
| 128 |
form: |
| 129 |
\preformatted{ |
| 130 |
================================================== |
| 131 |
Model 1 Model 2 Model 3 |
| 132 |
-------------------------------------------------- |
| 133 |
Coefficients |
| 134 |
(Intercept) 30.628*** 6.360*** 28.566*** |
| 135 |
(7.409) (1.252) (7.355) |
| 136 |
pop15 -0.471** -0.461** |
| 137 |
(0.147) (0.145) |
| 138 |
pop75 -1.934 -1.691 |
| 139 |
(1.041) (1.084) |
| 140 |
dpi 0.001 -0.000 |
| 141 |
(0.001) (0.001) |
| 142 |
ddpi 0.529* 0.410* |
| 143 |
(0.210) (0.196) |
| 144 |
-------------------------------------------------- |
| 145 |
Summaries |
| 146 |
R-squared 0.262 0.162 0.338 |
| 147 |
adj. R-squared 0.230 0.126 0.280 |
| 148 |
N 50 50 50 |
| 149 |
================================================== |
| 150 |
} |
| 151 |
|
| 152 |
Such tables of coefficient estimates can be produced |
| 153 |
by \code{\link{mtable}}. To see some of the possibilities of |
| 154 |
this function, use \code{example(mtable)}. |
| 155 |
} |
| 156 |
\subsection{LaTeX Representation of R Objects}{ |
| 157 |
Output produced by \code{\link{mtable}} can be transformed into |
| 158 |
LaTeX tables by an appropriate method of the generic function |
| 159 |
\code{\link[utils]{toLatex}} which is defined in the package |
| 160 |
\code{utils}. In addition, \code{memisc} defines \code{toLatex} methods |
| 161 |
for matrices and \code{\link[stats]{ftable}} objects. Note that |
| 162 |
results produced by \code{\link{genTable}} can be coerced into |
| 163 |
\code{\link[stats]{ftable}} objects. Also, a default method |
| 164 |
for the \code{toLatex} function is defined which coerces its |
| 165 |
argument to a matrix and applies the matrix method of \code{toLatex}. |
| 166 |
} |
| 167 |
} |
| 168 |
\section{Programming}{ |
| 169 |
\subsection{Looping over Variables}{ |
| 170 |
Sometimes users want to contruct loops that run over variables rather than values. |
| 171 |
For example, if one wants to set the missing values of a battery of items. |
| 172 |
For this purpose, the package contains the function \code{\link{foreach}}. |
| 173 |
To set 8 and 9 as missing values for the items \code{knowledge1}, |
| 174 |
\code{knowledge2}, \code{knowledge3}, one can use |
| 175 |
\preformatted{ |
| 176 |
foreach(x=c(knowledge1,knowledge2,knowledge3), |
| 177 |
missing.values(x) <- 8:9) |
| 178 |
} |
| 179 |
} |
| 180 |
\subsection{Changing Names of Objects and Labels of Factors}{ |
| 181 |
\code{R} already makes it possible to change the names of an object. |
| 182 |
Substituting the \code{\link[base]{names}} or \code{\link[base]{dimnames}} |
| 183 |
can be done with some programming tricks. This package defines |
| 184 |
the function \code{\link{rename}}, |
| 185 |
\code{\link{dimrename}}, \code{\link{colrename}}, and \code{\link{rowrename}} |
| 186 |
that implement these tricks in a convenient way, so that programmers |
| 187 |
(like the author of this package) need not reinvent the weel in |
| 188 |
every instance of changing names of an object. |
| 189 |
} |
| 190 |
\subsection{Dimension-Preserving Versions of \code{lapply} and \code{sapply}}{ |
| 191 |
If a function that is involved in a call to |
| 192 |
\code{\link[base:lapply]{sapply}} returns a result an array or a matrix, the |
| 193 |
dimensional information gets lost. Also, if a list object to which |
| 194 |
\code{\link[base]{lapply}} or \code{\link[base:lapply]{sapply}} are applied |
| 195 |
have a dimension attribute, the result looses this information. |
| 196 |
The functions \code{\link{Lapply}} and |
| 197 |
\code{\link{Sapply}} defined in this package preserve such |
| 198 |
dimensional information. |
| 199 |
} |
| 200 |
\subsection{Combining Vectors and Arrays by Names}{ |
| 201 |
The generic function \code{\link{collect}} collects several objects of the |
| 202 |
same mode into one object, using their names, \code{rownames}, |
| 203 |
\code{colnames} and/or \code{dimnames}. There are methods for |
| 204 |
atomic vectors, arrays (including matrices), and data frames. |
| 205 |
For example |
| 206 |
\preformatted{ |
| 207 |
a <- c(a=1,b=2) |
| 208 |
b <- c(a=10,c=30) |
| 209 |
collect(a,b) |
| 210 |
} |
| 211 |
leads to |
| 212 |
\preformatted{ |
| 213 |
x y |
| 214 |
a 1 10 |
| 215 |
b 2 NA |
| 216 |
c NA 30 |
| 217 |
} |
| 218 |
} |
| 219 |
\subsection{Reordering of Matrices and Arrays}{ |
| 220 |
The \code{memisc} package includes a \code{\link{reorder}} |
| 221 |
method for arrays and matrices. For example, the matrix |
| 222 |
method by default reorders the rows of a matrix according the results |
| 223 |
of a function. |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
\keyword{misc} |