Debian Codegraph @

vignettes/import.Rmd

raw
1 ---
2 title: Importing data from SPSS and Stata
3 output: rmarkdown::html_vignette
4 vignette: >
5 % \VignetteIndexEntry{Importing data from SPSS and Stata}
6 % \VignetteEngine{knitr::rmarkdown}
7 %\VignetteEncoding{UTF-8}
8 ---
9
10 ```{r,echo=FALSE,message=FALSE}
11 knitr::opts_chunk$set(comment=NA,eval=FALSE)
12 ```
13
14 # Motivation
15
16 The \"foreign\" package for *R* already provides facilities to import
17 data from other statistical software packages such as SPSS or Stata, but
18 they are limited by the way survey data are generally represented in
19 *R*. That is, since variables in an R data frame can only be numerical
20 vectors or factors, any direct translation of SPSS or Stata data sets
21 into data frames will lead to the loss of information of information,
22 such as variable labels, variable labels, or user-specified missing
23 values. (Value labels can be preserved by translating them into factor
24 levels, but this means losing information about the original codes. It
25 will also lead to undesired missing values, if variables in the original
26 data sets are only partially labelled.) The \"memisc\" package for this
27 reason provides functions that allow to import SPSS or Stata data sets
28 into objects of the class `"data.set"` defined in it.
29
30 # The role of \"importer\" objects
31
32 Importing data using the facilities provided by the \"memisc\" package
33 consists of two steps. In the first step, a description of the data in
34 the file is collected in an object of class \"importer\". In the second
35 step, data are imported into \"data.set\" objects with the help of these
36 \"importer\" objects. These \"importer\" objects contain only meta-data
37 e.g. about variable labels, value labels, and user-defined missing
38 values. This allows to get an overview of the structure of the file
39 without the need of loading the complete data, which is advantageous
40 esp. if the data set is large. For example, with the help of an
41 \"importer object\" it is possible to see what the labels of the
42 variables are so that one can select those variables from the data file
43 that are actually needed. The data set object in R memory can then
44 created by \-- if `imprtr` is an importer object \-- by calls like
45 `subset(imprtr,...)`, `imprtr[...]` or `as.data.set(imprtr)`. Some
46 examples are given in the following.
47
48 Note that these examples require data not included in the package
49 (you need to register to [GESIS](https://www.gesis.org) to download the data).
50 The vignette code cannot be run without this additional data.
51
52 # Importing data from SPSS files
53
54 ## Importing from a SPSS \"system\" file
55
56 In order to import data from an SPSS \"system\" file, the usual binary
57 format in which SPSS data now is usually saved and often distributed,
58 one needs to first make the file that contains the data known to R, as
59 in the following example:
60
61 ``` r
62 library(memisc)
63 ZA5702 <- spss.system.file("Data/ZA5702_v2-0-0.sav")
64 ZA5702
65 ```
66
67 :
68
69 SPSS system file 'Data/ZA5702_v2-0-0.sav'
70 with 979 variables and 3911 observations
71
72 Once the \"system file\" is declared using the function
73 `spss.system.file()`, metadata becomes available, such as the number of
74 cases and variables (as just seen), the names and labels of the
75 variables (as seen below):
76
77 ``` r
78 description(ZA5702)
79 ```
80
81 :
82
83 study 'Studiennummer'
84 version 'GESIS Archiv Version'
85 year 'Erhebungsjahr'
86 field 'Erhebungszeitraum'
87 glescomp 'GLES-Komponente'
88 survey 'Erhebung/Welle'
89 lfdn 'Laufende Nummer (Kumulation)'
90 vlfdn 'Laufende Nummer (Vorwahl)'
91 nlfdn 'Laufende Nummer (Nachwahl)'
92 datum 'Datum der Befragung (Monat/Tag/Jahr)'
93
94 (Here only an extract of the full output was shown, since the data set
95 contains as many as 979 variables.)
96
97 An \"importer\" object, such as `ZA5702` in this example, would also
98 allow to obtain a full codebook with
99
100 ``` r
101 codebook(ZA5702)
102 ```
103
104 but we refrain from showing such a codebook for the obvious reason of
105 not creating too much output. As the inspection of the data in the file
106 shows, most variable names have a standardised, yet non-mnemonic
107 structure. Variables referring to questions asked in the pre-election
108 wave of the GLES 2013 study have names starting with \"`v`\", those
109 referring to questions asked in the post-election wave have names
110 starting with \"`v`\", while those referring to question asked in both
111 waves have names starting \"`nv`\". For a specific analysis, such
112 variable names are not very useful. For this reason we want to rename
113 them. We could do this after loading the data, but it is more convenient
114 to do the data import and the renaming in one step as in the example
115 below:
116
117 ``` r
118 gles2013work <- subset(ZA5702,
119 select=c(
120 wave = survey,
121 intent.turnout = v10,
122 turnout = n10,
123 voteint.candidate = v11aa,
124 voteint.list = v11ba,
125 postal.vote.candidate = v12aa,
126 postal.vote.list = v12ba,
127 vote.candidate = n11aa,
128 vote.list = n11ba,
129 bula = bl
130 ))
131 ```
132
133 The variable names to the left of the equality sign are the variable
134 names as they will appear in the data set after import, while the
135 variable names to the right of the equality aign are the variable names
136 as they exist in the data file.
137
138 As a demonstration of what information can be extracted from the data
139 file, we create a codebook for one of the items in the data set:
140
141 ``` r
142 codebook(gles2013work$turnout)
143 ```
144
145 ```
146 ================================================================================
147
148 gles2013work$turnout 'Wahlbeteiligung'
149
150 --------------------------------------------------------------------------------
151
152 Storage mode: double
153 Measurement: interval
154 Missing values: -Inf - -1
155
156 Values and labels N Valid Total
157
158 -99 M 'keine Angabe' 3 0.1
159 -97 M 'trifft nicht zu' 20 0.5
160 -94 M 'nicht in Auswahlgesamtheit' 2003 51.2
161 1 'ja, habe gewaehlt' 1596 84.7 40.8
162 2 'nein, habe nicht gewaehlt' 289 15.3 7.4
163
164 Min: 1.000
165 Max: 2.000
166 Mean: 1.153
167 Std.Dev.: 0.360
168 ```
169 ## Import from a SPSS \"portable\" file
170
171 Data from SPSS \"portable\" files are imported in essentially the same
172 way as data from SPSS \"system\" files: The first step again is to make
173 the data set known to *R*:
174
175 ``` r
176 ZA3861 <- spss.portable.file("Data/ZA3861.por",iconv=FALSE)
177 ZA3861
178 ```
179
180 :
181
182 SPSS portable file 'Data/ZA3861.por'
183 with 331 variables and 3263 observations
184
185 Since this file contains German umlauts (in contrast to the previous
186 example), we need to convert the character coding of the value labels
187 etc. from \"Latin-1\" (the original coding of the data) into the native
188 encoding of the system (unless the computer is using natively
189 \"Latin-1\" encoding and not - as must Mac and most Linux System - a
190 variant of UTF8).
191
192 ``` r
193 ZA3861 <- Iconv(ZA3861,from="latin1")
194 ```
195
196 Importer objects created from \"portable\" files can be examined in the
197 same way as importer objects created from \"system\" files. For example,
198 we get a description of the variables in the data set (the variable
199 labels) and a codebook.
200
201 ``` r
202 description(ZA3861)
203 ```
204
205 :
206
207 vvpnid 'Fallnummer'
208 vsplitwo 'West-Ost-Kennung'
209 vvornach 'Vor-/Nachwahl'
210 vland 'Bundesland'
211 v10 'Wirtschaftl. Lage allgemein'
212 v20 'Wirtschaftl. Lage retrospektiv'
213 v30 'Wirtschaftl. Lage prospektiv'
214 v31 'Wichtigkeit Erst/Zweitstimme BTW (nicht 94)'
215 v40 'Demokratiezufriedenheit'
216 v50 'Staerke Politikinteresse'
217
218 To actually import the data and make them accessible for analysis we can
219 (as above), use `as.data.set()`, or `subset()` as in this example:
220
221 ``` r
222 work2002 <- subset(ZA3861,
223 select=c(
224 respid = VVPNID,
225 split.wo = VSPLITWO,
226 split.vor.nach = VVORNACH,
227 Bundesland = VLAND,
228 Erststimme = V69,
229 Zweitstimme = V70,
230 Geschlecht = VSEX,
231 GebMonat = VMONAT,
232 GebJahr = VJAHR,
233 Konfession = VRELIG,
234 Kirchgang = VKIRCHG,
235 Erwerbst = VBERUFTG,
236 FrErwerbst = VFRBERTG,
237 Beruf = VBERUF,
238 Famstand = VFAMSTDN,
239 Partner = VPARTNER,
240 BildungP = VPBILDGA,
241 BerufstP = VPBERUFT,
242 FrBerufstP = VPFBERTG,
243 BerufP = VPBERUF,
244 ReprGewicht = VGVWNW
245 )
246 )
247 ```
248
249 ## Import from a fixed-width file accompanied by SPSS syntax
250
251 Data from more recent study components of the American National Elecion
252 Study comes in fixed-width format, with some additional SPSS syntax
253 files that define columns, variable labels, value labels, and missing
254 values. `memisc` also provides an importer function such data. Naturally
255 this requires a little bit more information. In addition to the actual
256 data file, we also need a file with SPSS syntax specifying the data
257 columns. Optionally, Syntax files that define variable labels, value
258 lables, and missing values can also be specified.
259
260 ``` r
261 anes2008TS <- spss.fixed.file("Data/anes2008/anes2008TS_dat.txt",
262 columns.file="Data/anes2008/anes2008TS_col.sps",
263 varlab.file="Data/anes2008/anes2008TS_lab.sps",
264 codes.file="Data/anes2008/anes2008TS_cod.sps",
265 missval.file="Data/anes2008/anes2008TS_md.sps")
266 anes2008TS
267 ```
268
269 :
270
271 SPSS fixed column file 'issues/anes2008/anes2008TS_dat.txt'
272 with 1954 variables and 2322 observations
273 with variable labels from file 'issues/anes2008/anes2008TS_lab.sps'
274 with value labels from file 'issues/anes2008/anes2008TS_cod.sps'
275 with missing value definitions from file 'issues/anes2008/anes2008TS_md.sps'
276
277 Further information about the data can now be obtained from the returned
278 importer object in the same way as from importer objects that describe
279 SPSS \"system\" or SPSS \"portable\" files. That is, we can use
280 `names()`, `description()`, and `codebook()`. To get the data in to the
281 memory of *R* we can use (as above) the functions `as.data.set()` and
282 `subset()`.
283
284 ## Importing data from a Stata file
285
286 Data from Stata files (up to Stata Version 12) can be imported in the
287 same way as data from SPSS files. The main difference is the function
288 used for it, and the fact that user-defined missing values do not exists
289 in Stata. For this, see the following example:
290
291 ``` r
292 library(memisc)
293 ZA5702.dta <- Stata.file("Data/ZA5702_v2-0-0.dta")
294 ZA5702.dta
295 ```
296
297 :
298
299 Stata file 'Data/ZA5702_v2-0-0.dta'
300 with 874 variables and 3911 observations
301
302 ``` r
303 gles2013work.dta <- subset(ZA5702.dta,
304 select=c(
305 wave = survey,
306 intent.turnout = v10,
307 turnout = n10,
308 voteint.candidate = v11aa,
309 voteint.list = v11ba,
310 postal.vote.candidate = v12aa,
311 postal.vote.list = v12ba,
312 vote.candidate = n11aa,
313 vote.list = n11ba,
314 bula = bl
315 ))
316 codebook(gles2013work.dta$turnout)
317 ```
318
319 ```
320 ================================================================================
321
322 gles2013work.dta$turnout 'Wahlbeteiligung'
323
324 --------------------------------------------------------------------------------
325
326 Storage mode: integer
327 Measurement: nominal
328 Missing values: 100 - 127
329
330 Values and labels N Percent
331
332 -99 'keine Angabe' 3 0.1
333 -98 'weiss nicht' 0 0.0
334 -97 'trifft nicht zu' 20 0.5
335 -96 'Split' 0 0.0
336 -95 'nicht teilgenommen' 0 0.0
337 -94 'nicht in Auswahlgesamtheit' 2003 51.2
338 -93 'Interview abgebrochen' 0 0.0
339 -92 'Fehler in Daten' 0 0.0
340 -86 'nicht wahlberechtigt' 0 0.0
341 -85 'nicht waehlen' 0 0.0
342 -84 'keine Erst-/Zweitstimme abgegeben' 0 0.0
343 -83 'ungueltig waehlen' 0 0.0
344 -82 'keine andere Partei waehlen' 0 0.0
345 -81 'noch nicht entschieden' 0 0.0
346 -72 'nicht einzuschaetzen' 0 0.0
347 -71 'nicht bekannt' 0 0.0
348 1 'ja, habe gewaehlt' 1596 40.8
349 2 'nein, habe nicht gewaehlt' 289 7.4
350 ```