File: test_read.R

package info (click to toggle)
r-cran-readstata13 0.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 804 kB
  • sloc: cpp: 1,770; ansic: 278; makefile: 2
file content (355 lines) | stat: -rw-r--r-- 9,450 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
library(readstata13)

context("Reading datasets")


datacompare <- function(x, y) {
  res <- unlist(Map(all.equal, x, y))

  # with all(unlist(res)) if not TRUE, a warning is thrown
  res <- all(unlist(lapply(res, isTRUE)))

  res
}



#### missings ####
# missings.do creates missings.dta
# missings.dta contains variable missings containing ., .a, .b, ..., .z
#
# Note: prior Stata 8 there was only a single missing value

dd <- data.frame(missings = as.numeric(rep(NA, 27)))

missings <- system.file("extdata", "missings.dta", package="readstata13")



dd118 <- read.dta13(missings, missing.type = FALSE)
dd118_m <- read.dta13(missings, missing.type = TRUE)

mvals <- attr(dd118_m, "missing")$missings

test_that("missings", {
  expect_true(datacompare(dd, dd118))
  expect_true(datacompare(dd, dd118_m))
  expect_identical(mvals, as.numeric(0:26))
})


# rm(list = files)

#### missings msf/lsf ####
dd <- data.frame(b = as.logical(c(1,NA)),
                 i=as.integer(c(1,NA)),
                 n=as.numeric(c(1,NA)),
                 s=c("1", ""),
                 stringsAsFactors = FALSE)

dd$b <- as.integer(dd$b)

missings_msf <- system.file("extdata", "missings_msf.dta", package="readstata13")
missings_lsf <- system.file("extdata", "missings_lsf.dta", package="readstata13")

dd_msf <- read.dta13(missings_msf)
dd_lsf <- read.dta13(missings_lsf)



test_that("missings msf/lsf", {
  expect_true(datacompare(dd, dd_msf))
  expect_true(datacompare(dd, dd_lsf))
})

#### generate factors TRUE ####

dd <- data.frame(v1 = as.numeric(1:2))
dd$v1 <- factor(x = dd$v1, levels = 1:2, labels = c("one", "2"))

gen_fac <- system.file("extdata", "gen_fac.dta", package="readstata13")



dd118 <- read.dta13(gen_fac, convert.factors = TRUE, generate.factors = TRUE)

test_that("generate.factors TRUE", {
  expect_true(datacompare(dd, dd118))
})

# rm(list = files)

#### generate factors FALSE ####

dd <- data.frame(v1 = as.numeric(1:2))

gen_fac <- system.file("extdata", "gen_fac.dta", package="readstata13")

suppressWarnings(dd118 <- read.dta13(gen_fac, convert.factors = TRUE, generate.factors = FALSE))

test_that("generate.factors TRUE", {
  expect_true(datacompare(dd, dd118))
})


#### convert.underscore = TRUE ####

dd <- data.frame(v.1 = as.numeric(1:2),
                 v.2  = as.numeric(1:2),
                 long.name.multiple.underscores = as.numeric(1:2))

underscore <- system.file("extdata", "underscore.dta", package="readstata13")

dd118 <- read.dta13(underscore, convert.underscore = T)

test_that("generate.factors TRUE", {
  expect_true(datacompare(dd, dd118))
})

#### convert.underscore = FALSE ####

dd <- data.frame(v.1 = as.numeric(1:2),
                 v.2  = as.numeric(1:2),
                 long_name_multiple_underscores = as.numeric(1:2))

underscore <- system.file("extdata", "underscore.dta", package="readstata13")

dd118 <- read.dta13(underscore, convert.underscore = F)

test_that("generate.factors TRUE", {
  expect_true(datacompare(dd, dd118))
})


#### noint.factors TRUE ####

dd <- data.frame(v1 = as.numeric(1:2))
dd$v1 <- factor(x = dd$v1, levels = 1:2, labels = c("one", "1.2"))

nonint <- system.file("extdata", "nonint.dta", package="readstata13")



dd118 <- read.dta13(nonint, convert.factors = TRUE, generate.factors = TRUE,
                    nonint.factors = TRUE)

test_that("nonint.factors TRUE", {
  expect_true(datacompare(dd, dd118))
})


# rm(list = files)

#### encoding TRUE ####

umlauts <- c("ä","ö","ü","ß","€","Œ")
Encoding(umlauts) <- "UTF-8"

ddcp <- dd <- data.frame(num = factor(1:6, levels = 1:6, labels = umlauts),
                 chr = umlauts, stringsAsFactors = FALSE)

# Dataset in CP1252
levels(ddcp$num)[5:6] <- c("EUR","OE")
ddcp$chr[5:6] <- c("EUR","OE")


# Stata 14
encode <- system.file("extdata", "encode.dta", package="readstata13")
# Stata 12
encodecp <- system.file("extdata", "encodecp.dta", package="readstata13")

ddutf_aE <- read.dta13(encode, convert.factors = TRUE, generate.factors = TRUE,
                       encoding="UTF-8")

# On windows the last two characters will fail on default (not in latin1)
dd_aE <- read.dta13(encode, convert.factors = TRUE, generate.factors = TRUE)

ddcp_aE <- read.dta13(encodecp, convert.factors = TRUE, generate.factors = TRUE)

test_that("encoding CP1252", {
  expect_true(datacompare(ddcp, ddcp_aE))
})

test_that("encoding UTF-8 (Stata 14)", {
  expect_true(datacompare(dd$chr[1:4], dd_aE$chr[1:4]))
  expect_true(datacompare(dd, ddutf_aE))
})

test_that("Reading of strls", {
  strl <- system.file("extdata", "statacar.dta", package="readstata13")

  ddstrlf <- read.dta13(strl, replace.strl = F)
  ddstrlfref <- paste0("11_", 1:8)
  expect_equal(ddstrlf$modelStrL, ddstrlfref)

  ddstrl <- read.dta13(strl, replace.strl = T)
  expect_equal(ddstrl$model, ddstrl$modelStrL)
})

test_that("reading of many strls", {

  # slow test
  N = 1e4
  big_strl <- data.frame(
    x = 1:N,
    y = sample(LETTERS, N, replace = TRUE),
    z = c(paste(rep("a", 3000), collapse=""), sample(LETTERS, N-1, replace=TRUE))
  )

  # writing the file is slow
  if (!file.exists("big_strl.dta"))
    readstata13::save.dta13(big_strl, "big_strl.dta")

  expect_silent(x <- readstata13::read.dta13("big_strl.dta", select.rows = 1))
  unlink("big_strl.dta")

})

test_that("various datetime conversions", {
  datetime <- system.file("extdata", "datetime.dta", package="readstata13")

  td       <- c("2001-05-15",
                "1999-04-01",
                "1975-11-15",
                "1960-08-26",
                "1987-12-16")
  tc       <- c("2011-06-25 05:15:06",
                "2011-03-13 08:30:45",
                "2011-04-09 10:17:08",
                "2012-02-11 10:30:12",
                "2012-08-01 06:45:59")
  tc_hh_mm <- c("2011-06-29 10:27:00",
                "2011-03-26 02:15:00",
                "2011-04-09 19:35:00",
                "2012-02-16 02:15:00",
                "2012-08-02 11:59:00")
  ty       <- c("2011-01-01",
                "2011-01-01",
                "2011-01-01",
                "2012-01-01",
                "2012-01-01")
  tm       <- c("2011-06-01",
                "2011-03-01",
                "2011-04-01",
                "2012-02-01",
                "2012-08-01")
  tq       <- c("2011-04-01",
                "2011-01-01",
                "2011-04-01",
                "2012-01-01",
                "2012-07-01")

  dd <- data.frame(td = as.Date(td),
                   tc = as.POSIXct(tc, tz = "GMT"),
                   tc_hh_mm = as.POSIXct(tc_hh_mm, tz = "GMT"),
                   ty = as.Date(ty),
                   tm = as.Date(tm),
                   tq = as.Date(tq))
  dddates <- read.dta13(datetime, convert.dates = TRUE)
  expect_true(all.equal(dd, dddates, check.attributes = FALSE))
})

test_that("reading file format 120 works", {

  fl <- system.file("extdata", "myproject2.dtas", package="readstata13")

  tmp <- tempdir()

  fls <- unzip(fl, exdir = tmp)

  # data name, dta file name, dta version
  data_fram <- strsplit(readLines(fls[1])[-c(1:2)], " ")
  data_fram <- as.data.frame(do.call("rbind", data_fram))

  expect_equal(data_fram$V1, c("persons", "counties"))

  # read dtas
  dtas <- fls[tools::file_ext(fls) == "dta"]
  expect_equal(basename(dtas), paste0(data_fram$V2, ".dta"))

  expect_warning(
    df1 <- read.dta13(dtas[1]),
    "File contains unhandled alias variable in column: 5"
  )
  df2 <- read.dta13(dtas[2], convert.factors = FALSE)

  expect_equal(attr(df1, "version"), as.integer(data_fram$V3[1]))
  expect_equal(attr(df2, "version"), as.integer(data_fram$V3[2]))

  # backup order
  nams <- names(df1)

  # merge: fralias_from in attr(df1, "expansion.fields") tells what to merge
  df <- merge(
    df1[-which(names(df1) == "median")],
    df2,
    by = "countyid",
    all.x = TRUE
  )

  # update names
  as_name <- attr(df1, "expansion.fields")[[16]]
  nams2 <- names(df)
  nams2[nams2 == as_name[3]] <- as_name[1]
  names(df) <- nams2

  # resore expected order
  df <- df[nams]

  # restore order
  df <- df[order(df$personid), ]

  expect_equal(
    df$personid, 1:20
  )

  expect_equal(
    c("personid", "countyid", "income", "counties", "median", "ratio"),
    names(df)
  )

  
  # read all frames in myproject2.dtas
  expect_warning(
    dtas1 <- read.dtas(fl),
    "File contains unhandled alias variable in column: 5")
  
  expect_equal(
    c("persons", "counties"),
    names(dtas1)
  )
  
  # read selected frames
  expect_warning(
    dtas2 <- read.dtas(fl, select.frames = c("persons", "counties")),
    "File contains unhandled alias variable in column: 5")
  
  expect_equal(
    c("persons", "counties"),
    names(dtas2)
  )
  
  # read only frame counties
  dtas3 <- read.dtas(fl, select.frames = c("counties"))

  expect_equal(
    "counties",
    names(dtas3)
  )
  
  # read frames with different arguments
  dtas4 <- read.dtas(fl, 
            read.dta13.options = list(counties = list(select.cols = "median_income"),
                                      persons = list(select.cols = "income")))
  
  expect_equal(names(dtas4$persons), "income")  
  expect_equal(names(dtas4$counties), "median_income")

  # read frames with different arguments
  dtas5 <- read.dtas(fl, 
                     read.dta13.options = list(persons = list(select.cols = c("income", "countyid"))))
  
  expect_equal(ncol(dtas5$persons), 2)  
  expect_equal(names(dtas5$persons), c("countyid", "income"))
  
})