File: reg-tests-3.R

package info (click to toggle)
r-base 4.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 112,924 kB
  • sloc: ansic: 291,338; fortran: 111,889; javascript: 14,798; yacc: 6,154; sh: 5,689; makefile: 5,239; tcl: 4,562; perl: 963; objc: 791; f90: 758; asm: 258; java: 31; sed: 1
file content (286 lines) | stat: -rw-r--r-- 8,159 bytes parent folder | download | duplicates (2)
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
### Regression tests for which the printed output is the issue
### May fail.
### Skipped on a Unix-alike without Recommended packages

pdf("reg-tests-3.pdf", encoding = "ISOLatin1.enc")

## str() for character & factors with NA (levels), and for Surv objects:
ff <- factor(c(2:1,  NA),  exclude = NULL)
str(levels(ff))
str(ff)
str(ordered(ff, exclude=NULL))
if(require(survival)) {
    (sa <- Surv(aml$time, aml$status))
    str(sa)
    detach("package:survival", unload = TRUE)
}
## were different, the last one failed in 1.6.2 (at least)


## lm.influence where hat[1] == 1
if(require(MASS)) {
    fit <- lm(formula = 1000/MPG.city ~ Weight + Cylinders + Type + EngineSize + DriveTrain, data = Cars93)
    print(lm.influence(fit))
    ## row 57 should have hat = 1 and resid=0.
    summary(influence.measures(fit))
}
## only last two cols in row 57 should be influential


## PR#6640  Zero weights in plot.lm
if(require(MASS)) {
    fm1 <- lm(time~dist, data=hills, weights=c(0,0,rep(1,33)))
    plot(fm1)
}
## gave warnings in 1.8.1


## PR#7829 model.tables & replications
if(require(MASS)) {
    oats.aov <- aov(Y ~ B + V + N + V:N, data=oats[-1,])
    model.tables(oats.aov, "means", cterms=c("N", "V:N"))
}
## wrong printed output in 2.1.0


## drop1 on weighted lm() fits
if(require(MASS)) {
    hills.lm <- lm(time ~ 0 + dist + climb, data=hills, weights=1/dist^2)
    print(drop1(hills.lm))
    print(stats:::drop1.default(hills.lm))
    hills.lm2 <- lm(time/dist ~ 1 + I(climb/dist), data=hills)
    drop1(hills.lm2)
}
## quoted unweighted RSS etc in 2.2.1


## tests of ISO C99 compliance (Windows fails without a workaround)
sprintf("%g", 123456789)
sprintf("%8g", 123456789)
sprintf("%9.7g", 123456789)
sprintf("%10.9g", 123456789)
sprintf("%g", 12345.6789)
sprintf("%10.9g", 12345.6789)
sprintf("%10.7g", 12345.6789)
sprintf("%.7g", 12345.6789)
sprintf("%.5g", 12345.6789)
sprintf("%.4g", 12345.6789)
sprintf("%9.4g", 12345.6789)
sprintf("%10.4g", 12345.6789)
## Windows used e+008 etc prior to 2.3.0


## weighted glm() fits
if(require(MASS)) {
    hills.glm <- glm(time ~ 0 + dist + climb, data=hills, weights=1/dist^2)
    print(AIC(hills.glm))
    print(extractAIC(hills.glm))
    print(drop1(hills.glm))
    stats:::drop1.default(hills.glm)
}
## wrong AIC() and drop1 prior to 2.3.0.

## calculating no of signif digits
print(1.001, digits=16)
## 2.4.1 gave  1.001000000000000
## 2.5.0 errs on the side of caution.


## as.matrix.data.frame with coercion
if(require("survival")) {
    soa <- Surv(1:5, c(0, 0, 1, 0, 1))
    df.soa <- data.frame(soa)
    print(as.matrix(df.soa)) # numeric result
    df.soac <- data.frame(soa, letters[1:5])
    print(as.matrix(df.soac)) # character result
    detach("package:survival", unload = TRUE)
}
## failed in 2.8.1

## wish of PR#13505
npk.aov <- aov(yield ~ block + N * P + K, npk)
foo <- proj(npk.aov)
cbind(npk, foo)
## failed in R < 2.10.0


if(suppressMessages(require("Matrix", .Library))) {
  print(cS. <- contr.SAS(5, sparse = TRUE))
  stopifnot(all(contr.SAS(5) == cS.),
	    all(contr.helmert(5, sparse = TRUE) == contr.helmert(5)))

  x1 <- x2 <- c('a','b','a','b','c')
  x3 <- x2; x3[4:5] <- x2[5:4]
  print(xtabs(~ x1 + x2, sparse= TRUE, exclude = 'c'))
  print(xtabs(~ x1 + x3, sparse= TRUE, exclude = 'c'))
  detach("package:Matrix")
  ## failed in R <= 2.13.1
}

## regression tests for dimnames (broken on 2009-07-31)
contr.sum(4)
contr.helmert(4)
contr.sum(2) # needed drop=FALSE at one point.

## xtabs did not exclude levels from factors
x1 <- c('a','b','a','b','c', NA)
x2 <- factor(x1, exclude=NULL)
print(xtabs(~ x1 + x2, na.action = na.pass))
print(xtabs(~ x1 + x2, exclude = 'c', na.action = na.pass))


## median should work by default for a suitable S4 class.
## adapted from adaptsmoFMRI
if(suppressMessages(require("Matrix", .Library))) {
    x <- matrix(c(1,2,3,4))
    print(m <- median(x))
    stopifnot(all.equal(m, median(as(x, "denseMatrix"))))
    detach("package:Matrix")
}

## Various arguments were not duplicated:  PR#15352 to 15354
x <- 5
y <- 2
f <- function (y) x
numericDeriv(f(y),"y")
x

a<-list(1,2)
b<-rep.int(a,c(2,2))
b[[1]][1]<-9
a[[1]]

a <- numeric(1)
x <- mget("a",as.environment(1))
x
a[1] <- 9
x


## needs MASS installed
## PR#2586 labelling in alias()
if(require("MASS")) {
    Y <- c(0,1,2)
    X1 <- c(0,1,0)
    X2 <- c(0,1,0)
    X3 <- c(0,0,1)
    print(res <- alias(lm(Y ~ X1 + X2 + X3)))
    stopifnot(identical(rownames(res[[2]]), "X2"))
}
## the error was in lm.(w)fit

if(require("Matrix", .Library)) {
 m1 <- m2 <- m <- matrix(1:12, 3,4)
 dimnames(m2) <- list(LETTERS[1:3],
                      letters[1:4])
 dimnames(m1) <- list(NULL,letters[1:4])
 M  <- Matrix(m)
 M1 <- Matrix(m1)
 M2 <- Matrix(m2)
 ## Now, with a new ideal cbind(), rbind():
 print(cbind(M, M1))
 stopifnot(identical(cbind (M, M1),
                     cbind2(M, M1)))
 rm(M,M1,M2)
 detach("package:Matrix", unload=TRUE)
}##{Matrix}


## Invalid UTF-8 strings
x <- c("Jetz", "no", "chli", "z\xc3\xbcrit\xc3\xbc\xc3\xbctsch:",
       "(noch", "ein", "bi\xc3\x9fchen", "Z\xc3\xbc", "deutsch)",
       "\xfa\xb4\xbf\xbf\x9f")
lapply(x, utf8ToInt)
Encoding(x) <- "UTF-8"
nchar(x, "b")
try(nchar(x, "c"))
try(nchar(x, "w"))
nchar(x, "c", allowNA = TRUE)
nchar(x, "w", allowNA = TRUE)
## Results differed by platform, but some gave incorrect results on string 10.


## str() on large strings
nchar(L <- strrep(paste(LETTERS, collapse="."), 100000), type="b") # 5.1 M
str(L)


if(require("Matrix", .Library)) {
    M <- Matrix(diag(1:10), sparse=TRUE) # a "ddiMatrix"
    setClass("TestM", slots = c(M='numeric'))
    setMethod("+", c("TestM","TestM"), function(e1,e2) {
        e1@M + e2@M
    })
    M+M # works the first time
    M+M # was error   "object '.Generic' not found"
    ##
    as.Matrix <- if(packageVersion("Matrix", .Library) >= "1.4.2") {
                     as.matrix
                 } else function(x) `dimnames<-`(as.matrix(x), list(NULL,NULL))
    stopifnot(exprs = {
        identical(pmin(2,M), pmin(2, as.matrix(M)))
        identical(as.matrix(pmax(M, 7)),
                  pmax(as.Matrix(M), 7))
    })
    rm(M)

    ## show(<S4 generic>) from base
    show(chol2inv) # last line now has .. showMethods(chol2inv) ..

    detach("package:Matrix", unload=TRUE)
}##{Matrix}

## citation() / bibentry
options(width=88) # format.bibentry() using strwrap()
c1 <- citation()
c1$year <- "9999" # avoid tedious updates of reference output
print(c1)
fc1B <- format(c1)
fc1N <- format(c1, bibtex=FALSE)
stopifnot(exprs = {
    identical(fc1B[-2], fc1N[-2])
    (b2 <- fc1B[2]) != (n2 <- fc1N[2]) # bibtex left away (at end of line 2)
    startsWith(b2, n2)
})

pkg <- "nlme"
(hasME <- requireNamespace(pkg, quietly=TRUE, lib.loc = .Library))
if(hasME) withAutoprint({
    c2 <- citation(pkg, .Library)
    ## avoid spurious diffs:
    c2$author[[1]]$given[[1]] <- "J."
    c2$year[[1]] <- "9999"
    c2$note[[1]] <- sub("3.1-[0-9]*$", "3.1-999", c2$note[[1]])
    print(c2)
    print(c2, bibtex=FALSE) # no final message
    print(c2, bibtex=TRUE)  # w/ two bibTeX
    stopifnot(length(c2) >= 2)
    f2N <- format(c2) # -> format.bibentry(*, style="citation")
    f2B <- format(c2, bibtex=TRUE)
    stopifnot(exprs = {
        print(n <- length(f2N)) == length(f2B)
        identical(f2N[1], f2B[1])
        grepl("see these entries in BibTeX",       f2N[n], fixed=TRUE)
        grepl("'format(<citation>, bibtex=TRUE)'", f2N[n], fixed=TRUE) # "format(..)" was "print(..)"
        f2B[n] == ""
        (ie <- -c(1,n)) < 0
        grepl("A BibTeX entry ", f2B[ie], fixed=TRUE)
        grepl(" @[A-Z]", f2B[ie]) # @Book etc
       !grepl(" @[A-Z]", f2N[ie]) # *not* there
        nchar(f2N[ie]) < nchar(f2B[ie])
        startsWith(f2B[ie], f2N[ie])
      })

    desc <- packageDescription(pkg, .Library)
    desc$URL <- paste(URL1 <- "https://example.org",
                      "https://example.com", sep = "\n") # via continuation line
    desc$Repository <- NULL
    c3 <- citation(auto = desc)
    stopifnot(identical(print(c3$url), URL1)) # R <= 4.4.0 gave both URLs

    unloadNamespace(pkg)
})



cat('Time elapsed: ', proc.time(),'\n')