Debian Codegraph @

inst/doc/items.R

raw
1 ## ----echo=FALSE,message=FALSE-------------------------------------------------
2 knitr::opts_chunk$set(comment=NA,
3 fig.align="center",
4 results="markup")
5
6 ## ----echo=FALSE---------------------------------------------------------------
7 set.seed(20)
8 vote.probs <- c(.361,.29,.23,1-.361-.29-.23)
9 decis.probs <- c(vote.probs*.651,1-.651)
10 all.probs <- c(.9*decis.probs,.1*rep(1/3,3))
11 voteint <- sample(c(1:4,9,97,98,99),
12 prob=all.probs,
13 replace=TRUE,
14 size=200)
15 library(memisc)
16
17 ## -----------------------------------------------------------------------------
18 voteint
19
20 ## -----------------------------------------------------------------------------
21 # This is to be run *after* memisc has been loaded.
22 labels(voteint) <- c(Conservative = 1,
23 Labour = 2,
24 "Liberal Democrat" = 3, # We have whitespace in the label,
25 "Other Party" = 4, # so we need quotation marks
26 "Will not vote" = 9,
27 "Don't know" = 97,
28 "Answer refused" = 98,
29 "Not applicable" = 99)
30
31 ## -----------------------------------------------------------------------------
32 class(voteint)
33 str(voteint)
34 voteint
35
36 ## -----------------------------------------------------------------------------
37 labels(voteint)
38
39 ## -----------------------------------------------------------------------------
40 voteint <- relabel(voteint,
41 "Conservative" = "Cons",
42 "Labour" = "Lab",
43 "Liberal Democrat" = "LibDem",
44 "Other Party" = "Other",
45 "Will not vote" = "NoVote",
46 "Don't know" = "DK",
47 "Answer refused" = "Refused",
48 "Not applicable" = "N.a.")
49
50 ## -----------------------------------------------------------------------------
51 labels(voteint)
52 voteint
53 str(voteint)
54
55 ## -----------------------------------------------------------------------------
56 missing.values(voteint) <- c(97,98,99)
57
58 ## -----------------------------------------------------------------------------
59 voteint
60
61 ## -----------------------------------------------------------------------------
62 missing.values(voteint) <- missing.values(voteint) + 9
63
64 ## -----------------------------------------------------------------------------
65 missing.values(voteint)
66
67 ## -----------------------------------------------------------------------------
68 as.numeric(voteint)[1:30]
69 as.factor(voteint)[1:30]
70
71 ## -----------------------------------------------------------------------------
72 missing.values(voteint) <- NULL
73 missing.values(voteint)
74 as.numeric(voteint)[1:30]
75
76 ## -----------------------------------------------------------------------------
77 valid.values(voteint) <- 1:4
78 valid.values(voteint)
79 missing.values(voteint)
80
81 ## -----------------------------------------------------------------------------
82 valid.range(voteint) <- c(1,9)
83 missing.values(voteint)
84
85 ## -----------------------------------------------------------------------------
86 description(voteint) <- "Vote intention"
87 description(voteint)
88
89 ## -----------------------------------------------------------------------------
90 wording(voteint) <- "Which party are you going to vote for in the general election next Tuesday?"
91 wording(voteint)
92 annotation(voteint)
93 annotation(voteint)["wording"]
94
95 ## -----------------------------------------------------------------------------
96 codebook(voteint)
97
98 ## -----------------------------------------------------------------------------
99 voteint1 <- voteint
100 voteint1[sample(length(voteint),size=20)] <- c(rep(5,13),rep(7,7))
101
102 ## -----------------------------------------------------------------------------
103 codebook(voteint1)
104
105 ## -----------------------------------------------------------------------------
106 wild.codes(voteint1)
107
108 ## -----------------------------------------------------------------------------
109 voteint2 <- voteint
110 labels(voteint2) <- NULL # This deletes all value labels
111 codebook(voteint2)
112
113 ## -----------------------------------------------------------------------------
114 measurement(voteint2) <- "interval"
115 codebook(voteint2)
116
117 ## ----results='asis'-----------------------------------------------------------
118 show_html(codebook(voteint))
119
120 ## -----------------------------------------------------------------------------
121 Data <- data.set(
122 vote = sample(c(1,2,3,4,8,9,97,99),
123 size=300,replace=TRUE),
124 region = sample(c(rep(1,3),rep(2,2),3,99),
125 size=300,replace=TRUE),
126 income = round(exp(rnorm(300,sd=.7))*2000)
127 )
128
129 ## -----------------------------------------------------------------------------
130 Data
131
132 ## -----------------------------------------------------------------------------
133 options(show.max.obs=5)
134 Data
135 # Back to the default
136 options(show.max.obs=25)
137
138 ## ----eval=FALSE---------------------------------------------------------------
139 # print(Data)
140
141 ## -----------------------------------------------------------------------------
142 Data <- within(Data,{
143 description(vote) <- "Vote intention"
144 description(region) <- "Region of residence"
145 description(income) <- "Household income"
146 wording(vote) <- "If a general election would take place next Tuesday,
147 the candidate of which party would you vote for?"
148 wording(income) <- "All things taken into account, how much do all
149 household members earn in sum?"
150 foreach(x=c(vote,region),{
151 measurement(x) <- "nominal"
152 })
153 measurement(income) <- "ratio"
154 labels(vote) <- c(
155 Conservatives = 1,
156 Labour = 2,
157 "Liberal Democrats" = 3,
158 "Other" = 4,
159 "Don't know" = 8,
160 "Answer refused" = 9,
161 "Not applicable" = 97,
162 "Not asked in survey" = 99)
163 labels(region) <- c(
164 England = 1,
165 Scotland = 2,
166 Wales = 3,
167 "Not applicable" = 97,
168 "Not asked in survey" = 99)
169 foreach(x=c(vote,region,income),{
170 annotation(x)["Remark"] <- "This is not a real survey item, of course ..."
171 })
172 missing.values(vote) <- c(8,9,97,99)
173 missing.values(region) <- c(97,99)
174
175 # These to variables do not appear in the
176 # the resulting data set, since they have the wrong length.
177 junk1 <- 1:5
178 junk2 <- matrix(5,4,4)
179
180 })
181
182 ## -----------------------------------------------------------------------------
183 Data
184
185 ## -----------------------------------------------------------------------------
186 EnglandData <- subset(Data,region == "England")
187 EnglandData
188
189 ## -----------------------------------------------------------------------------
190 codebook(Data)
191
192 ## ----results='asis'-----------------------------------------------------------
193 show_html(codebook(Data))
194
195 ## -----------------------------------------------------------------------------
196 DataFr <- as.data.frame(Data)
197 ## Looking a the data frame structure
198 str(DataFr)
199 ## Looking at the first 25 observations
200 DataFr[1:25,]
201
202 ## -----------------------------------------------------------------------------
203 xtabs(~vote+region,data=DataFr)
204
205 ## -----------------------------------------------------------------------------
206 xtabs(~vote+region,data=Data)
207
208 ## -----------------------------------------------------------------------------
209 xtabs(~vote+region,data=within(Data,
210 vote <- include.missings(vote)))
211
212 ## ----results='asis'-----------------------------------------------------------
213 show_html(codebook(DataFr))
214
215 ## -----------------------------------------------------------------------------
216 load(system.file("gles/gles2013work.RData",package="memisc"))
217
218 ## ----results='asis'-----------------------------------------------------------
219 with(gles2013work,
220 show_html(codebook(bula)))
221
222 ## -----------------------------------------------------------------------------
223 gles2013work <- within(gles2013work,
224 east.west <- recode(bula,
225 East = 1 <- c(3,4,8,13,14,16),
226 West = 2 <- c(1,2,5:7,9:12,15)
227 ))
228
229 ## -----------------------------------------------------------------------------
230 xtabs(~bula+east.west,data=gles2013work)
231
232 ## -----------------------------------------------------------------------------
233 x <- 1:10
234 xc <- cases(x <= 3,
235 x > 3 & x <= 7,
236 x > 7)
237 data.frame(x,xc)
238
239 ## -----------------------------------------------------------------------------
240 xn <- cases(1 <- x <= 3,
241 2 <- x > 3 & x <= 7,
242 3 <- x > 7)
243 data.frame(x,xn)
244
245 ## -----------------------------------------------------------------------------
246 gles2013work <- within(gles2013work,{
247
248 candidate.vote <- cases(
249 wave == 1 & intent.turnout == 6 -> postal.vote.candidate,
250 wave == 1 & intent.turnout %in% 4:5 -> 900,
251 wave == 1 & intent.turnout %in% 1:3 -> voteint.candidate,
252 wave == 2 & turnout == 1 -> vote.candidate,
253 wave == 2 & turnout == 2 -> 900
254 )
255
256 list.vote <- cases(
257 wave == 1 & intent.turnout == 6 -> postal.vote.list,
258 wave == 1 & intent.turnout %in% 4:5 -> 900,
259 wave == 1 & intent.turnout %in% 1:3 -> voteint.list,
260 wave == 2 & turnout ==1 -> vote.list,
261 wave == 2 & turnout ==2 -> 900
262 )
263 })
264
265 ## -----------------------------------------------------------------------------
266 gles2013work <- within(gles2013work,{
267 candidate.vote <- recode(as.item(candidate.vote),
268 "CDU/CSU" = 1 <- 1,
269 "SPD" = 2 <- 4,
270 "FDP" = 3 <- 5,
271 "Grüne" = 4 <- 6,
272 "Linke" = 5 <- 7,
273 "NPD" = 6 <- 206,
274 "Piraten" = 7 <- 215,
275 "AfD" = 8 <- 322,
276 "Other" = 10 <- 801,
277 "No Vote" = 90 <- 900,
278 "WN" = 98 <- -98,
279 "KA" = 99 <- -99
280 )
281 list.vote <- recode(as.item(list.vote),
282 "CDU/CSU" = 1 <- 1,
283 "SPD" = 2 <- 4,
284 "FDP" = 3 <- 5,
285 "Grüne" = 4 <- 6,
286 "Linke" = 5 <- 7,
287 "NPD" = 6 <- 206,
288 "Piraten" = 7 <- 215,
289 "AfD" = 8 <- 322,
290 "Other" = 10 <- 801,
291 "No Vote" = 90 <- 900,
292 "WN" = 98 <- -98,
293 "KA" = 99 <- -99
294 )
295
296 missing.values(candidate.vote) <- 98:99
297 missing.values(list.vote) <- 98:99
298 measurement(candidate.vote) <- "nominal"
299 measurement(list.vote) <- "nominal"
300 })
301
302 ## ----width=120----------------------------------------------------------------
303 xtabs(~list.vote+east.west,data=gles2013work)
304 xtabs(~list.vote+candidate.vote,data=gles2013work)
305